fsl_mrt.h 11 KB

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