fsl_mrt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_MRT_H_
  35. #define _FSL_MRT_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup mrt
  39. * @{
  40. */
  41. /*! @file */
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. /*! @name Driver version */
  46. /*@{*/
  47. #define FSL_MRT_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  48. /*@}*/
  49. /*! @brief List of MRT channels */
  50. typedef enum _mrt_chnl
  51. {
  52. kMRT_Channel_0 = 0U, /*!< MRT channel number 0*/
  53. kMRT_Channel_1, /*!< MRT channel number 1 */
  54. kMRT_Channel_2, /*!< MRT channel number 2 */
  55. kMRT_Channel_3 /*!< MRT channel number 3 */
  56. } mrt_chnl_t;
  57. /*! @brief List of MRT timer modes */
  58. typedef enum _mrt_timer_mode
  59. {
  60. kMRT_RepeatMode = (0 << MRT_CHANNEL_CTRL_MODE_SHIFT), /*!< Repeat Interrupt mode */
  61. kMRT_OneShotMode = (1 << MRT_CHANNEL_CTRL_MODE_SHIFT), /*!< One-shot Interrupt mode */
  62. kMRT_OneShotStallMode = (2 << MRT_CHANNEL_CTRL_MODE_SHIFT) /*!< One-shot stall mode */
  63. } mrt_timer_mode_t;
  64. /*! @brief List of MRT interrupts */
  65. typedef enum _mrt_interrupt_enable
  66. {
  67. kMRT_TimerInterruptEnable = MRT_CHANNEL_CTRL_INTEN_MASK /*!< Timer interrupt enable*/
  68. } mrt_interrupt_enable_t;
  69. /*! @brief List of MRT status flags */
  70. typedef enum _mrt_status_flags
  71. {
  72. kMRT_TimerInterruptFlag = MRT_CHANNEL_STAT_INTFLAG_MASK, /*!< Timer interrupt flag */
  73. kMRT_TimerRunFlag = MRT_CHANNEL_STAT_RUN_MASK, /*!< Indicates state of the timer */
  74. } mrt_status_flags_t;
  75. /*!
  76. * @brief MRT configuration structure
  77. *
  78. * This structure holds the configuration settings for the MRT peripheral. To initialize this
  79. * structure to reasonable defaults, call the MRT_GetDefaultConfig() function and pass a
  80. * pointer to your config structure instance.
  81. *
  82. * The config struct can be made const so it resides in flash
  83. */
  84. typedef struct _mrt_config
  85. {
  86. bool enableMultiTask; /*!< true: Timers run in multi-task mode; false: Timers run in hardware status mode */
  87. } mrt_config_t;
  88. /*******************************************************************************
  89. * API
  90. ******************************************************************************/
  91. #if defined(__cplusplus)
  92. extern "C" {
  93. #endif
  94. /*!
  95. * @name Initialization and deinitialization
  96. * @{
  97. */
  98. /*!
  99. * @brief Ungates the MRT clock and configures the peripheral for basic operation.
  100. *
  101. * @note This API should be called at the beginning of the application using the MRT driver.
  102. *
  103. * @param base Multi-Rate timer peripheral base address
  104. * @param config Pointer to user's MRT config structure. If MRT has MULTITASK bit field in
  105. * MODCFG reigster, param config is useless.
  106. */
  107. void MRT_Init(MRT_Type *base, const mrt_config_t *config);
  108. /*!
  109. * @brief Gate the MRT clock
  110. *
  111. * @param base Multi-Rate timer peripheral base address
  112. */
  113. void MRT_Deinit(MRT_Type *base);
  114. /*!
  115. * @brief Fill in the MRT config struct with the default settings
  116. *
  117. * The default values are:
  118. * @code
  119. * config->enableMultiTask = false;
  120. * @endcode
  121. * @param config Pointer to user's MRT config structure.
  122. */
  123. static inline void MRT_GetDefaultConfig(mrt_config_t *config)
  124. {
  125. assert(config);
  126. #if !(defined(FSL_FEATURE_MRT_HAS_NO_MODCFG_MULTITASK) && FSL_FEATURE_MRT_HAS_NO_MODCFG_MULTITASK)
  127. /* Use hardware status operating mode */
  128. config->enableMultiTask = false;
  129. #endif
  130. }
  131. /*!
  132. * @brief Sets up an MRT channel mode.
  133. *
  134. * @param base Multi-Rate timer peripheral base address
  135. * @param channel Channel that is being configured.
  136. * @param mode Timer mode to use for the channel.
  137. */
  138. static inline void MRT_SetupChannelMode(MRT_Type *base, mrt_chnl_t channel, const mrt_timer_mode_t mode)
  139. {
  140. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  141. uint32_t reg = base->CHANNEL[channel].CTRL;
  142. /* Clear old value */
  143. reg &= ~MRT_CHANNEL_CTRL_MODE_MASK;
  144. /* Add the new mode */
  145. reg |= mode;
  146. base->CHANNEL[channel].CTRL = reg;
  147. }
  148. /*! @}*/
  149. /*!
  150. * @name Interrupt Interface
  151. * @{
  152. */
  153. /*!
  154. * @brief Enables the MRT interrupt.
  155. *
  156. * @param base Multi-Rate timer peripheral base address
  157. * @param channel Timer channel number
  158. * @param mask The interrupts to enable. This is a logical OR of members of the
  159. * enumeration ::mrt_interrupt_enable_t
  160. */
  161. static inline void MRT_EnableInterrupts(MRT_Type *base, mrt_chnl_t channel, uint32_t mask)
  162. {
  163. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  164. base->CHANNEL[channel].CTRL |= mask;
  165. }
  166. /*!
  167. * @brief Disables the selected MRT interrupt.
  168. *
  169. * @param base Multi-Rate timer peripheral base address
  170. * @param channel Timer channel number
  171. * @param mask The interrupts to disable. This is a logical OR of members of the
  172. * enumeration ::mrt_interrupt_enable_t
  173. */
  174. static inline void MRT_DisableInterrupts(MRT_Type *base, mrt_chnl_t channel, uint32_t mask)
  175. {
  176. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  177. base->CHANNEL[channel].CTRL &= ~mask;
  178. }
  179. /*!
  180. * @brief Gets the enabled MRT interrupts.
  181. *
  182. * @param base Multi-Rate timer peripheral base address
  183. * @param channel Timer channel number
  184. *
  185. * @return The enabled interrupts. This is the logical OR of members of the
  186. * enumeration ::mrt_interrupt_enable_t
  187. */
  188. static inline uint32_t MRT_GetEnabledInterrupts(MRT_Type *base, mrt_chnl_t channel)
  189. {
  190. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  191. return (base->CHANNEL[channel].CTRL & MRT_CHANNEL_CTRL_INTEN_MASK);
  192. }
  193. /*! @}*/
  194. /*!
  195. * @name Status Interface
  196. * @{
  197. */
  198. /*!
  199. * @brief Gets the MRT status flags
  200. *
  201. * @param base Multi-Rate timer peripheral base address
  202. * @param channel Timer channel number
  203. *
  204. * @return The status flags. This is the logical OR of members of the
  205. * enumeration ::mrt_status_flags_t
  206. */
  207. static inline uint32_t MRT_GetStatusFlags(MRT_Type *base, mrt_chnl_t channel)
  208. {
  209. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  210. return (base->CHANNEL[channel].STAT & (MRT_CHANNEL_STAT_INTFLAG_MASK | MRT_CHANNEL_STAT_RUN_MASK));
  211. }
  212. /*!
  213. * @brief Clears the MRT status flags.
  214. *
  215. * @param base Multi-Rate timer peripheral base address
  216. * @param channel Timer channel number
  217. * @param mask The status flags to clear. This is a logical OR of members of the
  218. * enumeration ::mrt_status_flags_t
  219. */
  220. static inline void MRT_ClearStatusFlags(MRT_Type *base, mrt_chnl_t channel, uint32_t mask)
  221. {
  222. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  223. base->CHANNEL[channel].STAT = (mask & MRT_CHANNEL_STAT_INTFLAG_MASK);
  224. }
  225. /*! @}*/
  226. /*!
  227. * @name Read and Write the timer period
  228. * @{
  229. */
  230. /*!
  231. * @brief Used to update the timer period in units of count.
  232. *
  233. * The new value will be immediately loaded or will be loaded at the end of the current time
  234. * interval. For one-shot interrupt mode the new value will be immediately loaded.
  235. *
  236. * @note User can call the utility macros provided in fsl_common.h to convert to ticks
  237. *
  238. * @param base Multi-Rate timer peripheral base address
  239. * @param channel Timer channel number
  240. * @param count Timer period in units of ticks
  241. * @param immediateLoad true: Load the new value immediately into the TIMER register;
  242. * false: Load the new value at the end of current timer interval
  243. */
  244. void MRT_UpdateTimerPeriod(MRT_Type *base, mrt_chnl_t channel, uint32_t count, bool immediateLoad);
  245. /*!
  246. * @brief Reads the current timer counting value.
  247. *
  248. * This function returns the real-time timer counting value, in a range from 0 to a
  249. * timer period.
  250. *
  251. * @note User can call the utility macros provided in fsl_common.h to convert ticks to usec or msec
  252. *
  253. * @param base Multi-Rate timer peripheral base address
  254. * @param channel Timer channel number
  255. *
  256. * @return Current timer counting value in ticks
  257. */
  258. static inline uint32_t MRT_GetCurrentTimerCount(MRT_Type *base, mrt_chnl_t channel)
  259. {
  260. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  261. return base->CHANNEL[channel].TIMER;
  262. }
  263. /*! @}*/
  264. /*!
  265. * @name Timer Start and Stop
  266. * @{
  267. */
  268. /*!
  269. * @brief Starts the timer counting.
  270. *
  271. * After calling this function, timers load period value, counts down to 0 and
  272. * depending on the timer mode it will either load the respective start value again or stop.
  273. *
  274. * @note User can call the utility macros provided in fsl_common.h to convert to ticks
  275. *
  276. * @param base Multi-Rate timer peripheral base address
  277. * @param channel Timer channel number.
  278. * @param count Timer period in units of ticks
  279. */
  280. static inline void MRT_StartTimer(MRT_Type *base, mrt_chnl_t channel, uint32_t count)
  281. {
  282. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  283. /* Write the timer interval value */
  284. base->CHANNEL[channel].INTVAL = count;
  285. }
  286. /*!
  287. * @brief Stops the timer counting.
  288. *
  289. * This function stops the timer from counting.
  290. *
  291. * @param base Multi-Rate timer peripheral base address
  292. * @param channel Timer channel number.
  293. */
  294. static inline void MRT_StopTimer(MRT_Type *base, mrt_chnl_t channel)
  295. {
  296. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  297. /* Stop the timer immediately */
  298. base->CHANNEL[channel].INTVAL = MRT_CHANNEL_INTVAL_LOAD_MASK;
  299. }
  300. /*! @}*/
  301. /*!
  302. * @name Get & release channel
  303. * @{
  304. */
  305. /*!
  306. * @brief Find the available channel.
  307. *
  308. * This function returns the lowest available channel number.
  309. *
  310. * @param base Multi-Rate timer peripheral base address
  311. */
  312. static inline uint32_t MRT_GetIdleChannel(MRT_Type *base)
  313. {
  314. return base->IDLE_CH;
  315. }
  316. #if !(defined(FSL_FEATURE_MRT_HAS_NO_CHANNEL_STAT_INUSE) && FSL_FEATURE_MRT_HAS_NO_CHANNEL_STAT_INUSE)
  317. /*!
  318. * @brief Release the channel when the timer is using the multi-task mode.
  319. *
  320. * In multi-task mode, the INUSE flags allow more control over when MRT channels are released for
  321. * further use. The user can hold on to a channel acquired by calling MRT_GetIdleChannel() for as
  322. * long as it is needed and release it by calling this function. This removes the need to ask for
  323. * an available channel for every use.
  324. *
  325. * @param base Multi-Rate timer peripheral base address
  326. * @param channel Timer channel number.
  327. */
  328. static inline void MRT_ReleaseChannel(MRT_Type *base, mrt_chnl_t channel)
  329. {
  330. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  331. uint32_t reg = base->CHANNEL[channel].STAT;
  332. /* Clear flag bits to prevent accidentally clearing anything when writing back */
  333. reg = ~MRT_CHANNEL_STAT_INTFLAG_MASK;
  334. reg |= MRT_CHANNEL_STAT_INUSE_MASK;
  335. base->CHANNEL[channel].STAT = reg;
  336. }
  337. #endif
  338. /*! @}*/
  339. #if defined(__cplusplus)
  340. }
  341. #endif
  342. /*! @}*/
  343. #endif /* _FSL_MRT_H_ */