fsl_ctimer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. #include "fsl_ctimer.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.ctimer"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Gets the instance from the base address
  44. *
  45. * @param base Ctimer peripheral base address
  46. *
  47. * @return The Timer instance
  48. */
  49. static uint32_t CTIMER_GetInstance(CTIMER_Type *base);
  50. /*******************************************************************************
  51. * Variables
  52. ******************************************************************************/
  53. /*! @brief Pointers to Timer bases for each instance. */
  54. static CTIMER_Type *const s_ctimerBases[] = CTIMER_BASE_PTRS;
  55. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  56. /*! @brief Pointers to Timer clocks for each instance. */
  57. static const clock_ip_name_t s_ctimerClocks[] = CTIMER_CLOCKS;
  58. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  59. #if defined(FSL_FEATURE_CTIMER_WRITE_ZERO_ASSERT_RESET) && FSL_FEATURE_CTIMER_WRITE_ZERO_ASSERT_RESET
  60. /*! @brief Pointers to Timer resets for each instance, writing a zero asserts the reset */
  61. static const reset_ip_name_t s_ctimerResets[] = CTIMER_RSTS_N;
  62. #else
  63. /*! @brief Pointers to Timer resets for each instance, writing a one asserts the reset */
  64. static const reset_ip_name_t s_ctimerResets[] = CTIMER_RSTS;
  65. #endif
  66. /*! @brief Pointers real ISRs installed by drivers for each instance. */
  67. static ctimer_callback_t *s_ctimerCallback[FSL_FEATURE_SOC_CTIMER_COUNT] = {0};
  68. /*! @brief Callback type installed by drivers for each instance. */
  69. static ctimer_callback_type_t ctimerCallbackType[FSL_FEATURE_SOC_CTIMER_COUNT] = {kCTIMER_SingleCallback};
  70. /*! @brief Array to map timer instance to IRQ number. */
  71. static const IRQn_Type s_ctimerIRQ[] = CTIMER_IRQS;
  72. /*******************************************************************************
  73. * Code
  74. ******************************************************************************/
  75. static uint32_t CTIMER_GetInstance(CTIMER_Type *base)
  76. {
  77. uint32_t instance;
  78. uint32_t ctimerArrayCount = (sizeof(s_ctimerBases) / sizeof(s_ctimerBases[0]));
  79. /* Find the instance index from base address mappings. */
  80. for (instance = 0; instance < ctimerArrayCount; instance++)
  81. {
  82. if (s_ctimerBases[instance] == base)
  83. {
  84. break;
  85. }
  86. }
  87. assert(instance < ctimerArrayCount);
  88. return instance;
  89. }
  90. void CTIMER_Init(CTIMER_Type *base, const ctimer_config_t *config)
  91. {
  92. assert(config);
  93. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  94. /* Enable the timer clock*/
  95. CLOCK_EnableClock(s_ctimerClocks[CTIMER_GetInstance(base)]);
  96. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  97. /* Reset the module */
  98. RESET_PeripheralReset(s_ctimerResets[CTIMER_GetInstance(base)]);
  99. /* Setup the cimer mode and count select */
  100. base->CTCR = CTIMER_CTCR_CTMODE(config->mode) | CTIMER_CTCR_CINSEL(config->input);
  101. /* Setup the timer prescale value */
  102. base->PR = CTIMER_PR_PRVAL(config->prescale);
  103. }
  104. void CTIMER_Deinit(CTIMER_Type *base)
  105. {
  106. uint32_t index = CTIMER_GetInstance(base);
  107. /* Stop the timer */
  108. base->TCR &= ~CTIMER_TCR_CEN_MASK;
  109. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  110. /* Disable the timer clock*/
  111. CLOCK_DisableClock(s_ctimerClocks[index]);
  112. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  113. /* Disable IRQ at NVIC Level */
  114. DisableIRQ(s_ctimerIRQ[index]);
  115. }
  116. void CTIMER_GetDefaultConfig(ctimer_config_t *config)
  117. {
  118. assert(config);
  119. /* Run as a timer */
  120. config->mode = kCTIMER_TimerMode;
  121. /* This field is ignored when mode is timer */
  122. config->input = kCTIMER_Capture_0;
  123. /* Timer counter is incremented on every APB bus clock */
  124. config->prescale = 0;
  125. }
  126. status_t CTIMER_SetupPwm(CTIMER_Type *base,
  127. ctimer_match_t matchChannel,
  128. uint8_t dutyCyclePercent,
  129. uint32_t pwmFreq_Hz,
  130. uint32_t srcClock_Hz,
  131. bool enableInt)
  132. {
  133. assert(pwmFreq_Hz > 0);
  134. uint32_t reg;
  135. uint32_t period, pulsePeriod = 0;
  136. uint32_t timerClock = srcClock_Hz / (base->PR + 1);
  137. uint32_t index = CTIMER_GetInstance(base);
  138. if (matchChannel == kCTIMER_Match_3)
  139. {
  140. return kStatus_Fail;
  141. }
  142. /* Enable PWM mode on the channel */
  143. base->PWMC |= (1U << matchChannel);
  144. /* Clear the stop, reset and interrupt bits for this channel */
  145. reg = base->MCR;
  146. reg &= ~((CTIMER_MCR_MR0R_MASK | CTIMER_MCR_MR0S_MASK | CTIMER_MCR_MR0I_MASK) << (matchChannel * 3));
  147. /* If call back function is valid then enable match interrupt for the channel */
  148. if (enableInt)
  149. {
  150. reg |= (CTIMER_MCR_MR0I_MASK << (CTIMER_MCR_MR0I_SHIFT + (matchChannel * 3)));
  151. }
  152. /* Reset the counter when match on channel 3 */
  153. reg |= CTIMER_MCR_MR3R_MASK;
  154. base->MCR = reg;
  155. /* Calculate PWM period match value */
  156. period = (timerClock / pwmFreq_Hz) - 1;
  157. /* Calculate pulse width match value */
  158. if (dutyCyclePercent == 0)
  159. {
  160. pulsePeriod = period + 1;
  161. }
  162. else
  163. {
  164. pulsePeriod = (period * (100 - dutyCyclePercent)) / 100;
  165. }
  166. /* Match on channel 3 will define the PWM period */
  167. base->MR[kCTIMER_Match_3] = period;
  168. /* This will define the PWM pulse period */
  169. base->MR[matchChannel] = pulsePeriod;
  170. /* Clear status flags */
  171. CTIMER_ClearStatusFlags(base, CTIMER_IR_MR0INT_MASK << matchChannel);
  172. /* If call back function is valid then enable interrupt and update the call back function */
  173. if (enableInt)
  174. {
  175. EnableIRQ(s_ctimerIRQ[index]);
  176. }
  177. return kStatus_Success;
  178. }
  179. status_t CTIMER_SetupPwmPeriod(CTIMER_Type *base,
  180. ctimer_match_t matchChannel,
  181. uint32_t pwmPeriod,
  182. uint32_t pulsePeriod,
  183. bool enableInt)
  184. {
  185. uint32_t reg;
  186. uint32_t index = CTIMER_GetInstance(base);
  187. if (matchChannel == kCTIMER_Match_3)
  188. {
  189. return kStatus_Fail;
  190. }
  191. /* Enable PWM mode on the channel */
  192. base->PWMC |= (1U << matchChannel);
  193. /* Clear the stop, reset and interrupt bits for this channel */
  194. reg = base->MCR;
  195. reg &= ~((CTIMER_MCR_MR0R_MASK | CTIMER_MCR_MR0S_MASK | CTIMER_MCR_MR0I_MASK) << (matchChannel * 3));
  196. /* If call back function is valid then enable match interrupt for the channel */
  197. if (enableInt)
  198. {
  199. reg |= (CTIMER_MCR_MR0I_MASK << (CTIMER_MCR_MR0I_SHIFT + (matchChannel * 3)));
  200. }
  201. /* Reset the counter when match on channel 3 */
  202. reg |= CTIMER_MCR_MR3R_MASK;
  203. base->MCR = reg;
  204. /* Match on channel 3 will define the PWM period */
  205. base->MR[kCTIMER_Match_3] = pwmPeriod;
  206. /* This will define the PWM pulse period */
  207. base->MR[matchChannel] = pulsePeriod;
  208. /* Clear status flags */
  209. CTIMER_ClearStatusFlags(base, CTIMER_IR_MR0INT_MASK << matchChannel);
  210. /* If call back function is valid then enable interrupt and update the call back function */
  211. if (enableInt)
  212. {
  213. EnableIRQ(s_ctimerIRQ[index]);
  214. }
  215. return kStatus_Success;
  216. }
  217. void CTIMER_UpdatePwmDutycycle(CTIMER_Type *base, ctimer_match_t matchChannel, uint8_t dutyCyclePercent)
  218. {
  219. uint32_t pulsePeriod = 0, period;
  220. /* Match channel 3 defines the PWM period */
  221. period = base->MR[kCTIMER_Match_3];
  222. /* Calculate pulse width match value */
  223. pulsePeriod = (period * dutyCyclePercent) / 100;
  224. /* For 0% dutycyle, make pulse period greater than period so the event will never occur */
  225. if (dutyCyclePercent == 0)
  226. {
  227. pulsePeriod = period + 1;
  228. }
  229. else
  230. {
  231. pulsePeriod = (period * (100 - dutyCyclePercent)) / 100;
  232. }
  233. /* Update dutycycle */
  234. base->MR[matchChannel] = pulsePeriod;
  235. }
  236. void CTIMER_SetupMatch(CTIMER_Type *base, ctimer_match_t matchChannel, const ctimer_match_config_t *config)
  237. {
  238. uint32_t reg;
  239. uint32_t index = CTIMER_GetInstance(base);
  240. /* Set the counter operation when a match on this channel occurs */
  241. reg = base->MCR;
  242. reg &= ~((CTIMER_MCR_MR0R_MASK | CTIMER_MCR_MR0S_MASK | CTIMER_MCR_MR0I_MASK) << (matchChannel * 3));
  243. reg |= (uint32_t)((uint32_t)(config->enableCounterReset) << (CTIMER_MCR_MR0R_SHIFT + (matchChannel * 3)));
  244. reg |= (uint32_t)((uint32_t)(config->enableCounterStop) << (CTIMER_MCR_MR0S_SHIFT + (matchChannel * 3)));
  245. reg |= (uint32_t)((uint32_t)(config->enableInterrupt) << (CTIMER_MCR_MR0I_SHIFT + (matchChannel * 3)));
  246. base->MCR = reg;
  247. reg = base->EMR;
  248. /* Set the match output operation when a match on this channel occurs */
  249. reg &= ~(CTIMER_EMR_EMC0_MASK << (matchChannel * 2));
  250. reg |= (uint32_t)config->outControl << (CTIMER_EMR_EMC0_SHIFT + (matchChannel * 2));
  251. /* Set the initial state of the EM bit/output */
  252. reg &= ~(CTIMER_EMR_EM0_MASK << matchChannel);
  253. reg |= (uint32_t)config->outPinInitState << matchChannel;
  254. base->EMR = reg;
  255. /* Set the match value */
  256. base->MR[matchChannel] = config->matchValue;
  257. /* Clear status flags */
  258. CTIMER_ClearStatusFlags(base, CTIMER_IR_MR0INT_MASK << matchChannel);
  259. /* If interrupt is enabled then enable interrupt and update the call back function */
  260. if (config->enableInterrupt)
  261. {
  262. EnableIRQ(s_ctimerIRQ[index]);
  263. }
  264. }
  265. void CTIMER_SetupCapture(CTIMER_Type *base,
  266. ctimer_capture_channel_t capture,
  267. ctimer_capture_edge_t edge,
  268. bool enableInt)
  269. {
  270. uint32_t reg = base->CCR;
  271. uint32_t index = CTIMER_GetInstance(base);
  272. /* Set the capture edge */
  273. reg &= ~((CTIMER_CCR_CAP0RE_MASK | CTIMER_CCR_CAP0FE_MASK | CTIMER_CCR_CAP0I_MASK) << (capture * 3));
  274. reg |= (uint32_t)edge << (CTIMER_CCR_CAP0RE_SHIFT + (capture * 3));
  275. /* Clear status flags */
  276. CTIMER_ClearStatusFlags(base, (kCTIMER_Capture0Flag << capture));
  277. /* If call back function is valid then enable capture interrupt for the channel and update the call back function */
  278. if (enableInt)
  279. {
  280. reg |= CTIMER_CCR_CAP0I_MASK << (capture * 3);
  281. EnableIRQ(s_ctimerIRQ[index]);
  282. }
  283. base->CCR = reg;
  284. }
  285. void CTIMER_RegisterCallBack(CTIMER_Type *base, ctimer_callback_t *cb_func, ctimer_callback_type_t cb_type)
  286. {
  287. uint32_t index = CTIMER_GetInstance(base);
  288. s_ctimerCallback[index] = cb_func;
  289. ctimerCallbackType[index] = cb_type;
  290. }
  291. void CTIMER_GenericIRQHandler(uint32_t index)
  292. {
  293. uint32_t int_stat, i, mask;
  294. /* Get Interrupt status flags */
  295. int_stat = CTIMER_GetStatusFlags(s_ctimerBases[index]);
  296. /* Clear the status flags that were set */
  297. CTIMER_ClearStatusFlags(s_ctimerBases[index], int_stat);
  298. if (ctimerCallbackType[index] == kCTIMER_SingleCallback)
  299. {
  300. if (s_ctimerCallback[index][0])
  301. {
  302. s_ctimerCallback[index][0](int_stat);
  303. }
  304. }
  305. else
  306. {
  307. #if defined(FSL_FEATURE_CTIMER_HAS_IR_CR3INT) && FSL_FEATURE_CTIMER_HAS_IR_CR3INT
  308. for (i = 0; i <= CTIMER_IR_CR3INT_SHIFT; i++)
  309. #else
  310. for (i = 0; i <= CTIMER_IR_CR2INT_SHIFT; i++)
  311. #endif /* FSL_FEATURE_CTIMER_HAS_IR_CR3INT */
  312. {
  313. mask = 0x01 << i;
  314. /* For each status flag bit that was set call the callback function if it is valid */
  315. if ((int_stat & mask) && (s_ctimerCallback[index][i]))
  316. {
  317. s_ctimerCallback[index][i](int_stat);
  318. }
  319. }
  320. }
  321. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  322. exception return operation might vector to incorrect interrupt */
  323. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  324. __DSB();
  325. #endif
  326. }
  327. /* IRQ handler functions overloading weak symbols in the startup */
  328. #if defined(CTIMER0)
  329. void CTIMER0_DriverIRQHandler(void)
  330. {
  331. CTIMER_GenericIRQHandler(0);
  332. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  333. exception return operation might vector to incorrect interrupt */
  334. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  335. __DSB();
  336. #endif
  337. }
  338. #endif
  339. #if defined(CTIMER1)
  340. void CTIMER1_DriverIRQHandler(void)
  341. {
  342. CTIMER_GenericIRQHandler(1);
  343. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  344. exception return operation might vector to incorrect interrupt */
  345. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  346. __DSB();
  347. #endif
  348. }
  349. #endif
  350. #if defined(CTIMER2)
  351. void CTIMER2_DriverIRQHandler(void)
  352. {
  353. CTIMER_GenericIRQHandler(2);
  354. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  355. exception return operation might vector to incorrect interrupt */
  356. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  357. __DSB();
  358. #endif
  359. }
  360. #endif
  361. #if defined(CTIMER3)
  362. void CTIMER3_DriverIRQHandler(void)
  363. {
  364. CTIMER_GenericIRQHandler(3);
  365. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  366. exception return operation might vector to incorrect interrupt */
  367. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  368. __DSB();
  369. #endif
  370. }
  371. #endif
  372. #if defined(CTIMER4)
  373. void CTIMER4_DriverIRQHandler(void)
  374. {
  375. CTIMER_GenericIRQHandler(4);
  376. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  377. exception return operation might vector to incorrect interrupt */
  378. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  379. __DSB();
  380. #endif
  381. }
  382. #endif