fsl_ctimer.c 12 KB

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