fsl_epit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2015-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_EPIT_H_
  31. #define _FSL_EPIT_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup epit
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. #define FSL_EPIT_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  43. /*@}*/
  44. /*!
  45. * @brief List of clock sources
  46. * @note Actual number of clock sources is SoC dependent
  47. */
  48. typedef enum _epit_clock_source
  49. {
  50. kEPIT_ClockSource_Off = 0U, /*!< EPIT Clock Source Off.*/
  51. kEPIT_ClockSource_Periph = 1U, /*!< EPIT Clock Source from Peripheral Clock.*/
  52. kEPIT_ClockSource_HighFreq = 2U, /*!< EPIT Clock Source from High Frequency Reference Clock.*/
  53. kEPIT_ClockSource_LowFreq = 3U, /*!< EPIT Clock Source from Low Frequency Reference Clock.*/
  54. } epit_clock_source_t;
  55. /*!
  56. * @brief List of output compare operation mode
  57. */
  58. typedef enum _epit_output_operation_mode
  59. {
  60. kEPIT_OutputOperation_Disconnected = 0U, /*!< EPIT Output Operation: Disconnected from pad.*/
  61. kEPIT_OutputOperation_Toggle = 1U, /*!< EPIT Output Operation: Toggle output pin.*/
  62. kEPIT_OutputOperation_Clear = 2U, /*!< EPIT Output Operation: Clear output pin.*/
  63. kEPIT_OutputOperation_Set = 3U, /*!< EPIT Output Operation: Set putput pin.*/
  64. } epit_output_operation_mode_t;
  65. /*! @brief List of EPIT interrupts */
  66. typedef enum _epit_interrupt_enable
  67. {
  68. kEPIT_OutputCompareInterruptEnable = EPIT_CR_OCIEN_MASK, /*!< Output Compare interrupt enable*/
  69. } epit_interrupt_enable_t;
  70. /*! @brief List of EPIT status flags */
  71. typedef enum _epit_status_flags
  72. {
  73. kEPIT_OutputCompareFlag = EPIT_SR_OCIF_MASK, /*!< Output Compare flag */
  74. } epit_status_flags_t;
  75. /*! @brief Structure to configure the running mode. */
  76. typedef struct _epit_config
  77. {
  78. epit_clock_source_t clockSource; /*!< clock source for EPIT module. */
  79. uint32_t divider; /*!< clock divider (prescaler+1) from clock source to counter. */
  80. bool enableRunInStop; /*!< EPIT enabled in stop mode. */
  81. bool enableRunInWait; /*!< EPIT enabled in wait mode. */
  82. bool enableRunInDbg; /*!< EPIT enabled in debug mode. */
  83. bool enableCounterOverwrite; /*!< set timer period results in counter value being overwritten. */
  84. bool enableFreeRun; /*!< true: free-running mode, counter will be reset to 0xFFFFFFFF when timer expires;
  85. false: set-and-forget mode, counter will be reloaded from set timer periods. */
  86. bool enableResetMode; /*!< true: counter is reset to timer periods in set-and-forget mode or 0xFFFFFFFF in
  87. free-running mode when enabled;
  88. false: counter restores the value that it was disabled when enabled. */
  89. } epit_config_t;
  90. /*******************************************************************************
  91. * API
  92. ******************************************************************************/
  93. #if defined(__cplusplus)
  94. extern "C" {
  95. #endif
  96. /*!
  97. * @name Software Reset
  98. * @{
  99. */
  100. /*!
  101. * @brief Software reset of EPIT module.
  102. *
  103. * @param base EPIT peripheral base address.
  104. */
  105. static inline void EPIT_SoftwareReset(EPIT_Type *base)
  106. {
  107. base->CR |= EPIT_CR_SWR_MASK;
  108. /* Wait reset finished. */
  109. while ((base->CR & EPIT_CR_SWR_MASK) == EPIT_CR_SWR_MASK)
  110. {
  111. }
  112. }
  113. /* @} */
  114. /*!
  115. * @name Initialization and deinitialization
  116. * @{
  117. */
  118. /*!
  119. * @brief Ungates the EPIT clock and configures the peripheral for a basic operation.
  120. *
  121. * This function issues a software reset to reset all the registers to their reset values,
  122. * except for the EN, ENMOD, STOPEN, WAITEN and DBGEN bits in Control register.
  123. *
  124. * @note This API should be called at the beginning of the application using the EPIT driver.
  125. *
  126. * @param base EPIT peripheral base address.
  127. * @param config Pointer to the user configuration structure.
  128. */
  129. void EPIT_Init(EPIT_Type *base, const epit_config_t *config);
  130. /*!
  131. * @brief Disables the module and gates the EPIT clock.
  132. *
  133. * @param base EPIT peripheral base address.
  134. */
  135. void EPIT_Deinit(EPIT_Type *base);
  136. /*!
  137. * @brief Fills in the EPIT configuration structure with default settings.
  138. *
  139. * The default values are:
  140. * @code
  141. * config->clockSource = kEPIT_ClockSource_Periph;
  142. * config->divider = 1U;
  143. * config->enableRunInStop = true;
  144. * config->enableRunInWait = true;
  145. * config->enableRunInDbg = false;
  146. * config->enableCounterOverwrite = false;
  147. * config->enableFreeRun = false;
  148. * config->enableResetMode = true;
  149. * @endcode
  150. * @param config Pointer to the user configuration structure.
  151. */
  152. void EPIT_GetDefaultConfig(epit_config_t *config);
  153. /*! @}*/
  154. /*!
  155. * @name Clock source and frequency control
  156. * @{
  157. */
  158. /*!
  159. * @brief Set clock source of EPIT.
  160. *
  161. * @param base EPIT peripheral base address.
  162. * @param source clock source to switch to.
  163. */
  164. static inline void EPIT_SetClockSource(EPIT_Type *base, epit_clock_source_t source)
  165. {
  166. base->CR = (base->CR & ~EPIT_CR_CLKSRC_MASK) | EPIT_CR_CLKSRC(source);
  167. }
  168. /*!
  169. * @brief Set clock divider inside EPIT module.
  170. *
  171. * @param base EPIT peripheral base address.
  172. * @param divider Clock divider in EPIT module (1-4096, divider = prescaler + 1).
  173. */
  174. static inline void EPIT_SetClockDivider(EPIT_Type *base, uint32_t divider)
  175. {
  176. assert((divider > 0) && (divider <= (EPIT_CR_PRESCALAR_MASK >> EPIT_CR_PRESCALAR_SHIFT) + 1));
  177. base->CR = (base->CR & ~EPIT_CR_PRESCALAR_MASK) | EPIT_CR_PRESCALAR(divider - 1);
  178. }
  179. /*!
  180. * @brief Get clock divider inside EPIT module.
  181. *
  182. * @param base EPIT base pointer.
  183. * @return clock divider in EPIT module (1-4096).
  184. */
  185. static inline uint32_t EPIT_GetClockDivider(EPIT_Type *base)
  186. {
  187. return ((base->CR & EPIT_CR_PRESCALAR_MASK) >> EPIT_CR_PRESCALAR_SHIFT) + 1;
  188. }
  189. /*! @}*/
  190. /*!
  191. * @name Timer Start and Stop
  192. * @{
  193. */
  194. /*!
  195. * @brief Start EPIT timer.
  196. *
  197. * @param base EPIT peripheral base address.
  198. */
  199. static inline void EPIT_StartTimer(EPIT_Type *base)
  200. {
  201. base->CR |= EPIT_CR_EN_MASK;
  202. }
  203. /*!
  204. * @brief Stop EPIT timer.
  205. *
  206. * @param base EPIT peripheral base address.
  207. */
  208. static inline void EPIT_StopTimer(EPIT_Type *base)
  209. {
  210. base->CR &= ~EPIT_CR_EN_MASK;
  211. }
  212. /*! @}*/
  213. /*!
  214. * @name Read and Write the timer period
  215. * @{
  216. */
  217. /*!
  218. * @brief Sets the timer period in units of count.
  219. *
  220. * Timers begin counting down from the value set by this function until it reaches 0, at which point
  221. * it generates an interrupt and loads this register value again.
  222. * When enableCounterOverwrite is false, writing a new value to this register does not restart the timer,
  223. * and the value is loaded after the timer expires. When enableCounterOverwrite is true, the counter
  224. * will be set immediately and starting counting down from that value.
  225. *
  226. * @note User can call the utility macros provided in fsl_common.h to convert to ticks.
  227. *
  228. * @param base EPIT peripheral base address.
  229. * @param ticks Timer period in units of ticks.
  230. */
  231. static inline void EPIT_SetTimerPeriod(EPIT_Type *base, uint32_t ticks)
  232. {
  233. base->LR = ticks;
  234. }
  235. /*!
  236. * @brief Reads the current timer counting value.
  237. *
  238. * This function returns the real-time timer counting value, in a range from 0 to a
  239. * timer period.
  240. *
  241. * @note User can call the utility macros provided in fsl_common.h to convert ticks to microseconds or milliseconds.
  242. *
  243. * @param base EPIT peripheral base address.
  244. *
  245. * @return Current timer counting value in ticks.
  246. */
  247. static inline uint32_t EPIT_GetCurrentTimerCount(EPIT_Type *base)
  248. {
  249. return base->CNR;
  250. }
  251. /*@}*/
  252. /*!
  253. * @name Output Signal Control
  254. * @{
  255. */
  256. /*!
  257. * @brief Set EPIT output compare operation mode.
  258. *
  259. * @param base EPIT peripheral base address.
  260. * @param mode EPIT output compare operation mode.
  261. */
  262. static inline void EPIT_SetOutputOperationMode(EPIT_Type *base, epit_output_operation_mode_t mode)
  263. {
  264. base->CR = (base->CR & ~EPIT_CR_OM_MASK) | EPIT_CR_OM(mode);
  265. }
  266. /*!
  267. * @brief Set EPIT output compare value.
  268. *
  269. * @param base EPIT peripheral base address.
  270. * @param value EPIT output compare value.
  271. */
  272. static inline void EPIT_SetOutputCompareValue(EPIT_Type *base, uint32_t value)
  273. {
  274. base->CMPR = value;
  275. }
  276. /*@}*/
  277. /*!
  278. * @name Interrupt Interface
  279. * @{
  280. */
  281. /*!
  282. * @brief Enables the selected EPIT interrupts.
  283. *
  284. * @param base EPIT peripheral base address.
  285. * @param mask The interrupts to enable. This is a logical OR of members of the
  286. * enumeration ::epit_interrupt_enable_t
  287. */
  288. static inline void EPIT_EnableInterrupts(EPIT_Type *base, uint32_t mask)
  289. {
  290. base->CR |= mask;
  291. }
  292. /*!
  293. * @brief Disables the selected EPIT interrupts.
  294. *
  295. * @param base EPIT peripheral base address
  296. * @param mask The interrupts to disable. This is a logical OR of members of the
  297. * enumeration ::epit_interrupt_enable_t
  298. */
  299. static inline void EPIT_DisableInterrupts(EPIT_Type *base, uint32_t mask)
  300. {
  301. base->CR &= ~mask;
  302. }
  303. /*!
  304. * @brief Gets the enabled EPIT interrupts.
  305. *
  306. * @param base EPIT peripheral base address
  307. *
  308. * @return The enabled interrupts. This is the logical OR of members of the
  309. * enumeration ::epit_interrupt_enable_t
  310. */
  311. static inline uint32_t EPIT_GetEnabledInterrupts(EPIT_Type *base)
  312. {
  313. return (base->CR & EPIT_CR_OCIEN_MASK);
  314. }
  315. /*! @}*/
  316. /*!
  317. * @name Status Interface
  318. * @{
  319. */
  320. /*!
  321. * @brief Gets the EPIT status flags.
  322. *
  323. * @param base EPIT peripheral base address
  324. *
  325. * @return The status flags. This is the logical OR of members of the
  326. * enumeration ::epit_status_flags_t
  327. */
  328. static inline uint32_t EPIT_GetStatusFlags(EPIT_Type *base)
  329. {
  330. return (base->SR & EPIT_SR_OCIF_MASK);
  331. }
  332. /*!
  333. * @brief Clears the EPIT status flags.
  334. *
  335. * @param base EPIT peripheral base address
  336. * @param mask The status flags to clear. This is a logical OR of members of the
  337. * enumeration ::epit_status_flags_t
  338. */
  339. static inline void EPIT_ClearStatusFlags(EPIT_Type *base, uint32_t mask)
  340. {
  341. base->SR = mask;
  342. }
  343. /*@}*/
  344. #if defined(__cplusplus)
  345. }
  346. #endif
  347. /*! @} */
  348. #endif /*_FSL_EPIT_H_*/