fsl_gpt.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_GPT_H_
  9. #define _FSL_GPT_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup gpt
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. #define FSL_GPT_DRIVER_VERSION (MAKE_VERSION(2, 0, 3))
  21. /*@}*/
  22. /*!
  23. * @brief List of clock sources
  24. * @note Actual number of clock sources is SoC dependent
  25. */
  26. typedef enum _gpt_clock_source
  27. {
  28. kGPT_ClockSource_Off = 0U, /*!< GPT Clock Source Off.*/
  29. kGPT_ClockSource_Periph = 1U, /*!< GPT Clock Source from Peripheral Clock.*/
  30. kGPT_ClockSource_HighFreq = 2U, /*!< GPT Clock Source from High Frequency Reference Clock.*/
  31. kGPT_ClockSource_Ext = 3U, /*!< GPT Clock Source from external pin.*/
  32. kGPT_ClockSource_LowFreq = 4U, /*!< GPT Clock Source from Low Frequency Reference Clock.*/
  33. kGPT_ClockSource_Osc = 5U, /*!< GPT Clock Source from Crystal oscillator.*/
  34. } gpt_clock_source_t;
  35. /*! @brief List of input capture channel number. */
  36. typedef enum _gpt_input_capture_channel
  37. {
  38. kGPT_InputCapture_Channel1 = 0U, /*!< GPT Input Capture Channel1.*/
  39. kGPT_InputCapture_Channel2 = 1U, /*!< GPT Input Capture Channel2.*/
  40. } gpt_input_capture_channel_t;
  41. /*! @brief List of input capture operation mode. */
  42. typedef enum _gpt_input_operation_mode
  43. {
  44. kGPT_InputOperation_Disabled = 0U, /*!< Don't capture.*/
  45. kGPT_InputOperation_RiseEdge = 1U, /*!< Capture on rising edge of input pin.*/
  46. kGPT_InputOperation_FallEdge = 2U, /*!< Capture on falling edge of input pin.*/
  47. kGPT_InputOperation_BothEdge = 3U, /*!< Capture on both edges of input pin.*/
  48. } gpt_input_operation_mode_t;
  49. /*! @brief List of output compare channel number. */
  50. typedef enum _gpt_output_compare_channel
  51. {
  52. kGPT_OutputCompare_Channel1 = 0U, /*!< Output Compare Channel1.*/
  53. kGPT_OutputCompare_Channel2 = 1U, /*!< Output Compare Channel2.*/
  54. kGPT_OutputCompare_Channel3 = 2U, /*!< Output Compare Channel3.*/
  55. } gpt_output_compare_channel_t;
  56. /*! @brief List of output compare operation mode. */
  57. typedef enum _gpt_output_operation_mode
  58. {
  59. kGPT_OutputOperation_Disconnected = 0U, /*!< Don't change output pin.*/
  60. kGPT_OutputOperation_Toggle = 1U, /*!< Toggle output pin.*/
  61. kGPT_OutputOperation_Clear = 2U, /*!< Set output pin low.*/
  62. kGPT_OutputOperation_Set = 3U, /*!< Set output pin high.*/
  63. kGPT_OutputOperation_Activelow = 4U, /*!< Generate a active low pulse on output pin.*/
  64. } gpt_output_operation_mode_t;
  65. /*! @brief List of GPT interrupts */
  66. typedef enum _gpt_interrupt_enable
  67. {
  68. kGPT_OutputCompare1InterruptEnable = GPT_IR_OF1IE_MASK, /*!< Output Compare Channel1 interrupt enable*/
  69. kGPT_OutputCompare2InterruptEnable = GPT_IR_OF2IE_MASK, /*!< Output Compare Channel2 interrupt enable*/
  70. kGPT_OutputCompare3InterruptEnable = GPT_IR_OF3IE_MASK, /*!< Output Compare Channel3 interrupt enable*/
  71. kGPT_InputCapture1InterruptEnable = GPT_IR_IF1IE_MASK, /*!< Input Capture Channel1 interrupt enable*/
  72. kGPT_InputCapture2InterruptEnable = GPT_IR_IF2IE_MASK, /*!< Input Capture Channel1 interrupt enable*/
  73. kGPT_RollOverFlagInterruptEnable = GPT_IR_ROVIE_MASK, /*!< Counter rolled over interrupt enable*/
  74. } gpt_interrupt_enable_t;
  75. /*! @brief Status flag. */
  76. typedef enum _gpt_status_flag
  77. {
  78. kGPT_OutputCompare1Flag = GPT_SR_OF1_MASK, /*!< Output compare channel 1 event.*/
  79. kGPT_OutputCompare2Flag = GPT_SR_OF2_MASK, /*!< Output compare channel 2 event.*/
  80. kGPT_OutputCompare3Flag = GPT_SR_OF3_MASK, /*!< Output compare channel 3 event.*/
  81. kGPT_InputCapture1Flag = GPT_SR_IF1_MASK, /*!< Input Capture channel 1 event.*/
  82. kGPT_InputCapture2Flag = GPT_SR_IF2_MASK, /*!< Input Capture channel 2 event.*/
  83. kGPT_RollOverFlag = GPT_SR_ROV_MASK, /*!< Counter reaches maximum value and rolled over to 0 event.*/
  84. } gpt_status_flag_t;
  85. /*! @brief Structure to configure the running mode. */
  86. typedef struct _gpt_init_config
  87. {
  88. gpt_clock_source_t clockSource; /*!< clock source for GPT module. */
  89. uint32_t divider; /*!< clock divider (prescaler+1) from clock source to counter. */
  90. bool enableFreeRun; /*!< true: FreeRun mode, false: Restart mode. */
  91. bool enableRunInWait; /*!< GPT enabled in wait mode. */
  92. bool enableRunInStop; /*!< GPT enabled in stop mode. */
  93. bool enableRunInDoze; /*!< GPT enabled in doze mode. */
  94. bool enableRunInDbg; /*!< GPT enabled in debug mode. */
  95. bool enableMode; /*!< true: counter reset to 0 when enabled;
  96. false: counter retain its value when enabled. */
  97. } gpt_config_t;
  98. /*******************************************************************************
  99. * API
  100. ******************************************************************************/
  101. #if defined(__cplusplus)
  102. extern "C" {
  103. #endif
  104. /*!
  105. * @name Initialization and deinitialization
  106. * @{
  107. */
  108. /*!
  109. * @brief Initialize GPT to reset state and initialize running mode.
  110. *
  111. * @param base GPT peripheral base address.
  112. * @param initConfig GPT mode setting configuration.
  113. */
  114. void GPT_Init(GPT_Type *base, const gpt_config_t *initConfig);
  115. /*!
  116. * @brief Disables the module and gates the GPT clock.
  117. *
  118. * @param base GPT peripheral base address.
  119. */
  120. void GPT_Deinit(GPT_Type *base);
  121. /*!
  122. * @brief Fills in the GPT configuration structure with default settings.
  123. *
  124. * The default values are:
  125. * @code
  126. * config->clockSource = kGPT_ClockSource_Periph;
  127. * config->divider = 1U;
  128. * config->enableRunInStop = true;
  129. * config->enableRunInWait = true;
  130. * config->enableRunInDoze = false;
  131. * config->enableRunInDbg = false;
  132. * config->enableFreeRun = false;
  133. * config->enableMode = true;
  134. * @endcode
  135. * @param config Pointer to the user configuration structure.
  136. */
  137. void GPT_GetDefaultConfig(gpt_config_t *config);
  138. /*!
  139. * @name Software Reset
  140. * @{
  141. */
  142. /*!
  143. * @brief Software reset of GPT module.
  144. *
  145. * @param base GPT peripheral base address.
  146. */
  147. static inline void GPT_SoftwareReset(GPT_Type *base)
  148. {
  149. base->CR |= GPT_CR_SWR_MASK;
  150. /* Wait reset finished. */
  151. while ((base->CR & GPT_CR_SWR_MASK) == GPT_CR_SWR_MASK)
  152. {
  153. }
  154. }
  155. /*!
  156. * @name Clock source and frequency control
  157. * @{
  158. */
  159. /*!
  160. * @brief Set clock source of GPT.
  161. *
  162. * @param base GPT peripheral base address.
  163. * @param gptClkSource Clock source (see @ref gpt_clock_source_t typedef enumeration).
  164. */
  165. static inline void GPT_SetClockSource(GPT_Type *base, gpt_clock_source_t gptClkSource)
  166. {
  167. if (gptClkSource == kGPT_ClockSource_Osc)
  168. {
  169. base->CR = (base->CR & ~GPT_CR_CLKSRC_MASK) | GPT_CR_EN_24M_MASK | GPT_CR_CLKSRC(gptClkSource);
  170. }
  171. else
  172. {
  173. base->CR = (base->CR & ~(GPT_CR_CLKSRC_MASK | GPT_CR_EN_24M_MASK)) | GPT_CR_CLKSRC(gptClkSource);
  174. }
  175. }
  176. /*!
  177. * @brief Get clock source of GPT.
  178. *
  179. * @param base GPT peripheral base address.
  180. * @return clock source (see @ref gpt_clock_source_t typedef enumeration).
  181. */
  182. static inline gpt_clock_source_t GPT_GetClockSource(GPT_Type *base)
  183. {
  184. return (gpt_clock_source_t)(uint8_t)((base->CR & GPT_CR_CLKSRC_MASK) >> GPT_CR_CLKSRC_SHIFT);
  185. }
  186. /*!
  187. * @brief Set pre scaler of GPT.
  188. *
  189. * @param base GPT peripheral base address.
  190. * @param divider Divider of GPT (1-4096).
  191. */
  192. static inline void GPT_SetClockDivider(GPT_Type *base, uint32_t divider)
  193. {
  194. assert(divider - 1U <= GPT_PR_PRESCALER_MASK);
  195. base->PR = (base->PR & ~GPT_PR_PRESCALER_MASK) | GPT_PR_PRESCALER(divider - 1U);
  196. }
  197. /*!
  198. * @brief Get clock divider in GPT module.
  199. *
  200. * @param base GPT peripheral base address.
  201. * @return clock divider in GPT module (1-4096).
  202. */
  203. static inline uint32_t GPT_GetClockDivider(GPT_Type *base)
  204. {
  205. return ((base->PR & GPT_PR_PRESCALER_MASK) >> GPT_PR_PRESCALER_SHIFT) + 1U;
  206. }
  207. /*!
  208. * @brief OSC 24M pre-scaler before selected by clock source.
  209. *
  210. * @param base GPT peripheral base address.
  211. * @param divider OSC Divider(1-16).
  212. */
  213. static inline void GPT_SetOscClockDivider(GPT_Type *base, uint32_t divider)
  214. {
  215. assert(divider - 1U <= (GPT_PR_PRESCALER24M_MASK >> GPT_PR_PRESCALER24M_SHIFT));
  216. base->PR = (base->PR & ~GPT_PR_PRESCALER24M_MASK) | GPT_PR_PRESCALER24M(divider - 1U);
  217. }
  218. /*!
  219. * @brief Get OSC 24M clock divider in GPT module.
  220. *
  221. * @param base GPT peripheral base address.
  222. * @return OSC clock divider in GPT module (1-16).
  223. */
  224. static inline uint32_t GPT_GetOscClockDivider(GPT_Type *base)
  225. {
  226. return ((base->PR & GPT_PR_PRESCALER24M_MASK) >> GPT_PR_PRESCALER24M_SHIFT) + 1U;
  227. }
  228. /*! @}*/
  229. /*!
  230. * @name Timer Start and Stop
  231. * @{
  232. */
  233. /*!
  234. * @brief Start GPT timer.
  235. *
  236. * @param base GPT peripheral base address.
  237. */
  238. static inline void GPT_StartTimer(GPT_Type *base)
  239. {
  240. base->CR |= GPT_CR_EN_MASK;
  241. }
  242. /*!
  243. * @brief Stop GPT timer.
  244. *
  245. * @param base GPT peripheral base address.
  246. */
  247. static inline void GPT_StopTimer(GPT_Type *base)
  248. {
  249. base->CR &= ~GPT_CR_EN_MASK;
  250. }
  251. /*!
  252. * @name Read the timer period
  253. * @{
  254. */
  255. /*!
  256. * @brief Reads the current GPT counting value.
  257. *
  258. * @param base GPT peripheral base address.
  259. * @return Current GPT counter value.
  260. */
  261. static inline uint32_t GPT_GetCurrentTimerCount(GPT_Type *base)
  262. {
  263. return base->CNT;
  264. }
  265. /*@}*/
  266. /*!
  267. * @name GPT Input/Output Signal Control
  268. * @{
  269. */
  270. /*!
  271. * @brief Set GPT operation mode of input capture channel.
  272. *
  273. * @param base GPT peripheral base address.
  274. * @param channel GPT capture channel (see @ref gpt_input_capture_channel_t typedef enumeration).
  275. * @param mode GPT input capture operation mode (see @ref gpt_input_operation_mode_t typedef enumeration).
  276. */
  277. static inline void GPT_SetInputOperationMode(GPT_Type *base,
  278. gpt_input_capture_channel_t channel,
  279. gpt_input_operation_mode_t mode)
  280. {
  281. assert(channel <= kGPT_InputCapture_Channel2);
  282. base->CR =
  283. (base->CR & ~(GPT_CR_IM1_MASK << ((uint32_t)channel * 2UL))) | (GPT_CR_IM1(mode) << ((uint32_t)channel * 2UL));
  284. }
  285. /*!
  286. * @brief Get GPT operation mode of input capture channel.
  287. *
  288. * @param base GPT peripheral base address.
  289. * @param channel GPT capture channel (see @ref gpt_input_capture_channel_t typedef enumeration).
  290. * @return GPT input capture operation mode (see @ref gpt_input_operation_mode_t typedef enumeration).
  291. */
  292. static inline gpt_input_operation_mode_t GPT_GetInputOperationMode(GPT_Type *base, gpt_input_capture_channel_t channel)
  293. {
  294. assert(channel <= kGPT_InputCapture_Channel2);
  295. return (gpt_input_operation_mode_t)(uint8_t)((base->CR >> (GPT_CR_IM1_SHIFT + (uint32_t)channel * 2UL)) &
  296. (GPT_CR_IM1_MASK >> GPT_CR_IM1_SHIFT));
  297. }
  298. /*!
  299. * @brief Get GPT input capture value of certain channel.
  300. *
  301. * @param base GPT peripheral base address.
  302. * @param channel GPT capture channel (see @ref gpt_input_capture_channel_t typedef enumeration).
  303. * @return GPT input capture value.
  304. */
  305. static inline uint32_t GPT_GetInputCaptureValue(GPT_Type *base, gpt_input_capture_channel_t channel)
  306. {
  307. assert(channel <= kGPT_InputCapture_Channel2);
  308. return base->ICR[(uint32_t)channel];
  309. }
  310. /*!
  311. * @brief Set GPT operation mode of output compare channel.
  312. *
  313. * @param base GPT peripheral base address.
  314. * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
  315. * @param mode GPT output operation mode (see @ref gpt_output_operation_mode_t typedef enumeration).
  316. */
  317. static inline void GPT_SetOutputOperationMode(GPT_Type *base,
  318. gpt_output_compare_channel_t channel,
  319. gpt_output_operation_mode_t mode)
  320. {
  321. assert(channel <= kGPT_OutputCompare_Channel3);
  322. base->CR =
  323. (base->CR & ~(GPT_CR_OM1_MASK << ((uint32_t)channel * 3UL))) | (GPT_CR_OM1(mode) << ((uint32_t)channel * 3UL));
  324. }
  325. /*!
  326. * @brief Get GPT operation mode of output compare channel.
  327. *
  328. * @param base GPT peripheral base address.
  329. * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
  330. * @return GPT output operation mode (see @ref gpt_output_operation_mode_t typedef enumeration).
  331. */
  332. static inline gpt_output_operation_mode_t GPT_GetOutputOperationMode(GPT_Type *base,
  333. gpt_output_compare_channel_t channel)
  334. {
  335. assert(channel <= kGPT_OutputCompare_Channel3);
  336. return (gpt_output_operation_mode_t)(uint8_t)((base->CR >> (GPT_CR_OM1_SHIFT + (uint32_t)channel * 3UL)) &
  337. (GPT_CR_OM1_MASK >> GPT_CR_OM1_SHIFT));
  338. }
  339. /*!
  340. * @brief Set GPT output compare value of output compare channel.
  341. *
  342. * @param base GPT peripheral base address.
  343. * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
  344. * @param value GPT output compare value.
  345. */
  346. static inline void GPT_SetOutputCompareValue(GPT_Type *base, gpt_output_compare_channel_t channel, uint32_t value)
  347. {
  348. assert(channel <= kGPT_OutputCompare_Channel3);
  349. base->OCR[(uint32_t)channel] = value;
  350. }
  351. /*!
  352. * @brief Get GPT output compare value of output compare channel.
  353. *
  354. * @param base GPT peripheral base address.
  355. * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
  356. * @return GPT output compare value.
  357. */
  358. static inline uint32_t GPT_GetOutputCompareValue(GPT_Type *base, gpt_output_compare_channel_t channel)
  359. {
  360. assert(channel <= kGPT_OutputCompare_Channel3);
  361. return base->OCR[(uint32_t)channel];
  362. }
  363. /*!
  364. * @brief Force GPT output action on output compare channel, ignoring comparator.
  365. *
  366. * @param base GPT peripheral base address.
  367. * @param channel GPT output compare channel (see @ref gpt_output_compare_channel_t typedef enumeration).
  368. */
  369. static inline void GPT_ForceOutput(GPT_Type *base, gpt_output_compare_channel_t channel)
  370. {
  371. assert(channel <= kGPT_OutputCompare_Channel3);
  372. base->CR |= (GPT_CR_FO1_MASK << (uint32_t)channel);
  373. }
  374. /*@}*/
  375. /*!
  376. * @name GPT Interrupt and Status Interface
  377. * @{
  378. */
  379. /*!
  380. * @brief Enables the selected GPT interrupts.
  381. *
  382. * @param base GPT peripheral base address.
  383. * @param mask The interrupts to enable. This is a logical OR of members of the
  384. * enumeration ::gpt_interrupt_enable_t
  385. */
  386. static inline void GPT_EnableInterrupts(GPT_Type *base, uint32_t mask)
  387. {
  388. base->IR |= mask;
  389. }
  390. /*!
  391. * @brief Disables the selected GPT interrupts.
  392. *
  393. * @param base GPT peripheral base address
  394. * @param mask The interrupts to disable. This is a logical OR of members of the
  395. * enumeration ::gpt_interrupt_enable_t
  396. */
  397. static inline void GPT_DisableInterrupts(GPT_Type *base, uint32_t mask)
  398. {
  399. base->IR &= ~mask;
  400. }
  401. /*!
  402. * @brief Gets the enabled GPT interrupts.
  403. *
  404. * @param base GPT peripheral base address
  405. *
  406. * @return The enabled interrupts. This is the logical OR of members of the
  407. * enumeration ::gpt_interrupt_enable_t
  408. */
  409. static inline uint32_t GPT_GetEnabledInterrupts(GPT_Type *base)
  410. {
  411. return (base->IR & (GPT_IR_OF1IE_MASK | GPT_IR_OF2IE_MASK | GPT_IR_OF3IE_MASK | GPT_IR_IF1IE_MASK |
  412. GPT_IR_IF2IE_MASK | GPT_IR_ROVIE_MASK));
  413. }
  414. /*!
  415. * @name Status Interface
  416. * @{
  417. */
  418. /*!
  419. * @brief Get GPT status flags.
  420. *
  421. * @param base GPT peripheral base address.
  422. * @param flags GPT status flag mask (see @ref gpt_status_flag_t for bit definition).
  423. * @return GPT status, each bit represents one status flag.
  424. */
  425. static inline uint32_t GPT_GetStatusFlags(GPT_Type *base, gpt_status_flag_t flags)
  426. {
  427. return base->SR & (uint32_t)flags;
  428. }
  429. /*!
  430. * @brief Clears the GPT status flags.
  431. *
  432. * @param base GPT peripheral base address.
  433. * @param flags GPT status flag mask (see @ref gpt_status_flag_t for bit definition).
  434. */
  435. static inline void GPT_ClearStatusFlags(GPT_Type *base, gpt_status_flag_t flags)
  436. {
  437. base->SR = (uint32_t)flags;
  438. }
  439. /*@}*/
  440. #if defined(__cplusplus)
  441. }
  442. #endif
  443. /*! @}*/
  444. #endif /* _FSL_GPT_H_ */