fsl_gpt.h 17 KB

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