drv_hwtimer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2006-2018, 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. #define EXAMPLE_GPT_CLK_FREQ (CLOCK_GetFreq(kCLOCK_IpgClk) / (EXAMPLE_GPT_CLOCK_DIVIDER_SELECT + 1U))
  28. static void NVIC_Configuration(void)
  29. {
  30. #ifdef BSP_USING_HWTIMER1
  31. EnableIRQ(GPT1_IRQn);
  32. #endif
  33. #ifdef BSP_USING_HWTIMER2
  34. EnableIRQ(GPT2_IRQn);
  35. #endif
  36. }
  37. static rt_err_t imxrt_hwtimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
  38. {
  39. rt_err_t err = RT_EOK;
  40. GPT_Type *hwtimer_dev;
  41. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  42. RT_ASSERT(timer != RT_NULL);
  43. switch (cmd)
  44. {
  45. case HWTIMER_CTRL_FREQ_SET:
  46. {
  47. uint32_t clk;
  48. uint32_t pre;
  49. clk = EXAMPLE_GPT_CLK_FREQ;
  50. pre = clk / *((uint32_t *)args) - 1;
  51. GPT_SetClockDivider(hwtimer_dev, pre);
  52. }
  53. break;
  54. default:
  55. err = -RT_ENOSYS;
  56. break;
  57. }
  58. return err;
  59. }
  60. static rt_uint32_t imxrt_hwtimer_count_get(rt_hwtimer_t *timer)
  61. {
  62. rt_uint32_t CurrentTimer_Count;
  63. GPT_Type *hwtimer_dev;
  64. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  65. RT_ASSERT(timer != RT_NULL);
  66. CurrentTimer_Count = GPT_GetCurrentTimerCount(hwtimer_dev);
  67. return CurrentTimer_Count;
  68. }
  69. static void imxrt_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  70. {
  71. GPT_Type *hwtimer_dev;
  72. gpt_config_t gptConfig;
  73. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  74. RT_ASSERT(timer != RT_NULL);
  75. if (state == 1)
  76. {
  77. /*Clock setting for GPT*/
  78. CLOCK_SetMux(kCLOCK_PerclkMux, EXAMPLE_GPT_CLOCK_SOURCE_SELECT);
  79. CLOCK_SetDiv(kCLOCK_PerclkDiv, EXAMPLE_GPT_CLOCK_DIVIDER_SELECT);
  80. /* Initialize GPT module by default config */
  81. GPT_GetDefaultConfig(&gptConfig);
  82. GPT_Init(hwtimer_dev, &gptConfig);
  83. }
  84. }
  85. static rt_err_t imxrt_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  86. {
  87. GPT_Type *hwtimer_dev;
  88. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  89. RT_ASSERT(timer != RT_NULL);
  90. hwtimer_dev->CR |= (mode != HWTIMER_MODE_PERIOD) ? GPT_CR_FRR_MASK : 0U;
  91. GPT_SetOutputCompareValue(hwtimer_dev, kGPT_OutputCompare_Channel1, cnt);
  92. GPT_EnableInterrupts(hwtimer_dev, kGPT_OutputCompare1InterruptEnable);
  93. NVIC_Configuration();
  94. GPT_StartTimer(hwtimer_dev);
  95. return RT_EOK;
  96. }
  97. static void imxrt_hwtimer_stop(rt_hwtimer_t *timer)
  98. {
  99. GPT_Type *hwtimer_dev;
  100. hwtimer_dev = (GPT_Type *)timer->parent.user_data;
  101. RT_ASSERT(timer != RT_NULL);
  102. GPT_StopTimer(hwtimer_dev);
  103. }
  104. static const struct rt_hwtimer_ops imxrt_hwtimer_ops =
  105. {
  106. .init = imxrt_hwtimer_init,
  107. .start = imxrt_hwtimer_start,
  108. .stop = imxrt_hwtimer_stop,
  109. .count_get = imxrt_hwtimer_count_get,
  110. .control = imxrt_hwtimer_control,
  111. };
  112. static const struct rt_hwtimer_info imxrt_hwtimer_info =
  113. {
  114. 25000000, /* the maximum count frequency can be set */
  115. 6103, /* the minimum count frequency can be set */
  116. 0xFFFFFFFF,
  117. HWTIMER_CNTMODE_UP,
  118. };
  119. #ifdef BSP_USING_HWTIMER1
  120. static rt_hwtimer_t GPT_timer1;
  121. #endif /*BSP_USING_HWTIMER1*/
  122. #ifdef BSP_USING_HWTIMER2
  123. static rt_hwtimer_t GPT_timer2;
  124. #endif
  125. int rt_hw_hwtimer_init(void)
  126. {
  127. int ret = RT_EOK;
  128. #ifdef BSP_USING_HWTIMER1
  129. GPT_timer1.info = &imxrt_hwtimer_info;
  130. GPT_timer1.ops = &imxrt_hwtimer_ops;
  131. ret = rt_device_hwtimer_register(&GPT_timer1, "gpt1", GPT1);
  132. if (ret != RT_EOK)
  133. {
  134. LOG_E("gpt1 register failed\n");
  135. }
  136. #endif
  137. #ifdef BSP_USING_HWTIMER2
  138. GPT_timer2.info = &imxrt_hwtimer_info;
  139. GPT_timer2.ops = &imxrt_hwtimer_ops;
  140. ret = rt_device_hwtimer_register(&GPT_timer2, "gpt2", GPT2);
  141. if (ret != RT_EOK)
  142. {
  143. LOG_E("gpt1 register failed\n");
  144. }
  145. #endif
  146. return ret;
  147. }
  148. #ifdef BSP_USING_HWTIMER1
  149. void GPT1_IRQHandler(void)
  150. {
  151. if (GPT_GetStatusFlags(GPT1, kGPT_OutputCompare1Flag) != 0)
  152. {
  153. GPT_ClearStatusFlags(GPT1, kGPT_OutputCompare1Flag);
  154. rt_device_hwtimer_isr(&GPT_timer1);
  155. }
  156. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F, Cortex-M7, Cortex-M7F Store immediate overlapping
  157. exception return operation might vector to incorrect interrupt */
  158. #if defined __CORTEX_M && (__CORTEX_M == 4U || __CORTEX_M == 7U)
  159. __DSB();
  160. #endif
  161. }
  162. #endif /*BSP_USING_HWTIMER1*/
  163. #ifdef BSP_USING_HWTIMER2
  164. void GPT2_IRQHandler(void)
  165. {
  166. if (GPT_GetStatusFlags(GPT2, kGPT_OutputCompare1Flag) != 0)
  167. {
  168. GPT_ClearStatusFlags(GPT2, kGPT_OutputCompare1Flag);
  169. rt_device_hwtimer_isr(&GPT_timer2);
  170. }
  171. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F, Cortex-M7, Cortex-M7F Store immediate overlapping
  172. exception return operation might vector to incorrect interrupt */
  173. #if defined __CORTEX_M && (__CORTEX_M == 4U || __CORTEX_M == 7U)
  174. __DSB();
  175. #endif
  176. }
  177. #endif /*BSP_USING_HWTIMER2*/
  178. INIT_DEVICE_EXPORT(rt_hw_hwtimer_init);
  179. #endif /* BSP_USING_HWTIMER */