drv_hwtimer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-04-17 WangBing the first version.
  9. * 2019-04-22 tyustli add imxrt series support
  10. *
  11. */
  12. #include <rtthread.h>
  13. #ifdef BSP_USING_HWTIMER
  14. #define LOG_TAG "drv.hwtimer"
  15. #include <drv_log.h>
  16. #include <rtdevice.h>
  17. #include "drv_hwtimer.h"
  18. #include "fsl_gpt.h"
  19. #if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
  20. #error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
  21. #endif
  22. /* Select IPG Clock as PERCLK_CLK clock source */
  23. #define EXAMPLE_GPT_CLOCK_SOURCE_SELECT (0U)
  24. /* Clock divider for PERCLK_CLK clock source */
  25. #define EXAMPLE_GPT_CLOCK_DIVIDER_SELECT (5U)
  26. /* Get source clock for GPT driver (GPT prescaler = 6) */
  27. #ifdef SOC_IMXRT1170_SERIES
  28. #undef EXAMPLE_GPT_CLOCK_DIVIDER_SELECT
  29. #define EXAMPLE_GPT_CLOCK_DIVIDER_SELECT (2U)
  30. // 1170 use this root directly, we have already divide this clk, can read it directly
  31. #define EXAMPLE_GPT_CLK_FREQ (CLOCK_GetRootClockFreq(kCLOCK_Root_Gpt1))
  32. #else
  33. #define EXAMPLE_GPT_CLK_FREQ (CLOCK_GetFreq(kCLOCK_IpgClk) / (EXAMPLE_GPT_CLOCK_DIVIDER_SELECT + 1U))
  34. #endif
  35. static void NVIC_Configuration(void)
  36. {
  37. #ifdef BSP_USING_HWTIMER1
  38. EnableIRQ(GPT1_IRQn);
  39. #endif
  40. #ifdef BSP_USING_HWTIMER2
  41. EnableIRQ(GPT2_IRQn);
  42. #endif
  43. }
  44. static rt_err_t imxrt_hwtimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
  45. {
  46. rt_err_t err = RT_EOK;
  47. GPT_Type *hwtimer_dev;
  48. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  49. RT_ASSERT(timer != RT_NULL);
  50. switch (cmd)
  51. {
  52. case HWTIMER_CTRL_FREQ_SET:
  53. {
  54. uint32_t clk;
  55. uint32_t pre;
  56. clk = EXAMPLE_GPT_CLK_FREQ;
  57. pre = clk / *((uint32_t *)args) - 1;
  58. GPT_SetClockDivider(hwtimer_dev, pre);
  59. }
  60. break;
  61. default:
  62. err = -RT_ENOSYS;
  63. break;
  64. }
  65. return err;
  66. }
  67. static rt_uint32_t imxrt_hwtimer_count_get(rt_hwtimer_t *timer)
  68. {
  69. rt_uint32_t CurrentTimer_Count;
  70. GPT_Type *hwtimer_dev;
  71. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  72. RT_ASSERT(timer != RT_NULL);
  73. CurrentTimer_Count = GPT_GetCurrentTimerCount(hwtimer_dev);
  74. return CurrentTimer_Count;
  75. }
  76. static void imxrt_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  77. {
  78. GPT_Type *hwtimer_dev;
  79. gpt_config_t gptConfig;
  80. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  81. RT_ASSERT(timer != RT_NULL);
  82. if (state == 1)
  83. {
  84. #ifdef SOC_IMXRT1170_SERIES
  85. #ifdef BSP_USING_HWTIMER1
  86. /*Clock setting for GPT*/
  87. CLOCK_SetRootClockMux(kCLOCK_Root_Gpt1, EXAMPLE_GPT_CLOCK_SOURCE_SELECT);
  88. CLOCK_SetRootClockDiv(kCLOCK_Root_Gpt1, EXAMPLE_GPT_CLOCK_DIVIDER_SELECT);
  89. #endif
  90. #ifdef BSP_USING_HWTIMER2
  91. /*Clock setting for GPT*/
  92. CLOCK_SetRootClockMux(kCLOCK_Root_Gpt2, EXAMPLE_GPT_CLOCK_SOURCE_SELECT);
  93. CLOCK_SetRootClockDiv(kCLOCK_Root_Gpt2, EXAMPLE_GPT_CLOCK_DIVIDER_SELECT);
  94. #endif
  95. #else
  96. /*Clock setting for GPT*/
  97. CLOCK_SetMux(kCLOCK_PerclkMux, EXAMPLE_GPT_CLOCK_SOURCE_SELECT);
  98. CLOCK_SetDiv(kCLOCK_PerclkDiv, EXAMPLE_GPT_CLOCK_DIVIDER_SELECT);
  99. #endif
  100. /* Initialize GPT module by default config */
  101. GPT_GetDefaultConfig(&gptConfig);
  102. GPT_Init(hwtimer_dev, &gptConfig);
  103. }
  104. }
  105. static rt_err_t imxrt_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  106. {
  107. GPT_Type *hwtimer_dev;
  108. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  109. RT_ASSERT(timer != RT_NULL);
  110. hwtimer_dev->CR |= (mode != HWTIMER_MODE_PERIOD) ? GPT_CR_FRR_MASK : 0U;
  111. GPT_SetOutputCompareValue(hwtimer_dev, kGPT_OutputCompare_Channel1, cnt);
  112. GPT_EnableInterrupts(hwtimer_dev, kGPT_OutputCompare1InterruptEnable);
  113. NVIC_Configuration();
  114. GPT_StartTimer(hwtimer_dev);
  115. return RT_EOK;
  116. }
  117. static void imxrt_hwtimer_stop(rt_hwtimer_t *timer)
  118. {
  119. GPT_Type *hwtimer_dev;
  120. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  121. RT_ASSERT(timer != RT_NULL);
  122. GPT_StopTimer(hwtimer_dev);
  123. }
  124. static const struct rt_hwtimer_ops imxrt_hwtimer_ops =
  125. {
  126. .init = imxrt_hwtimer_init,
  127. .start = imxrt_hwtimer_start,
  128. .stop = imxrt_hwtimer_stop,
  129. .count_get = imxrt_hwtimer_count_get,
  130. .control = imxrt_hwtimer_control,
  131. };
  132. static const struct rt_hwtimer_info imxrt_hwtimer_info =
  133. {
  134. 25000000, /* the maximum count frequency can be set */
  135. 6103, /* the minimum count frequency can be set */
  136. 0xFFFFFFFF,
  137. HWTIMER_CNTMODE_UP,
  138. };
  139. #ifdef BSP_USING_HWTIMER1
  140. static rt_hwtimer_t GPT_timer1;
  141. #endif /*BSP_USING_HWTIMER1*/
  142. #ifdef BSP_USING_HWTIMER2
  143. static rt_hwtimer_t GPT_timer2;
  144. #endif
  145. int rt_hw_hwtimer_init(void)
  146. {
  147. int ret = RT_EOK;
  148. #ifdef BSP_USING_HWTIMER1
  149. GPT_timer1.info = &imxrt_hwtimer_info;
  150. GPT_timer1.ops = &imxrt_hwtimer_ops;
  151. ret = rt_device_hwtimer_register(&GPT_timer1, "gpt1", GPT1);
  152. if (ret != RT_EOK)
  153. {
  154. LOG_E("gpt1 register failed\n");
  155. }
  156. #endif
  157. #ifdef BSP_USING_HWTIMER2
  158. GPT_timer2.info = &imxrt_hwtimer_info;
  159. GPT_timer2.ops = &imxrt_hwtimer_ops;
  160. ret = rt_device_hwtimer_register(&GPT_timer2, "gpt2", GPT2);
  161. if (ret != RT_EOK)
  162. {
  163. LOG_E("gpt1 register failed\n");
  164. }
  165. #endif
  166. return ret;
  167. }
  168. #ifdef BSP_USING_HWTIMER1
  169. void GPT1_IRQHandler(void)
  170. {
  171. if (GPT_GetStatusFlags(GPT1, kGPT_OutputCompare1Flag) != 0)
  172. {
  173. GPT_ClearStatusFlags(GPT1, kGPT_OutputCompare1Flag);
  174. rt_device_hwtimer_isr(&GPT_timer1);
  175. }
  176. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F, Cortex-M7, Cortex-M7F Store immediate overlapping
  177. exception return operation might vector to incorrect interrupt */
  178. #if defined __CORTEX_M && (__CORTEX_M == 4U || __CORTEX_M == 7U)
  179. __DSB();
  180. #endif
  181. }
  182. #endif /*BSP_USING_HWTIMER1*/
  183. #ifdef BSP_USING_HWTIMER2
  184. void GPT2_IRQHandler(void)
  185. {
  186. if (GPT_GetStatusFlags(GPT2, kGPT_OutputCompare1Flag) != 0)
  187. {
  188. GPT_ClearStatusFlags(GPT2, kGPT_OutputCompare1Flag);
  189. rt_device_hwtimer_isr(&GPT_timer2);
  190. }
  191. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F, Cortex-M7, Cortex-M7F Store immediate overlapping
  192. exception return operation might vector to incorrect interrupt */
  193. #if defined __CORTEX_M && (__CORTEX_M == 4U || __CORTEX_M == 7U)
  194. __DSB();
  195. #endif
  196. }
  197. #endif /*BSP_USING_HWTIMER2*/
  198. INIT_DEVICE_EXPORT(rt_hw_hwtimer_init);
  199. #endif /* BSP_USING_HWTIMER */