drv_timer.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. * 2011-01-18 onelife Initial creation for EFM32
  9. * 2011-06-17 onelife Modify init function for efm32lib v2 upgrading
  10. */
  11. /***************************************************************************//**
  12. * @addtogroup efm32
  13. * @{
  14. ******************************************************************************/
  15. /* Includes ------------------------------------------------------------------*/
  16. #include "board.h"
  17. #include "drv_timer.h"
  18. #if (defined(RT_USING_TIMER0) || defined(RT_USING_TIMER1) || defined(RT_USING_TIMER2))
  19. /* Private typedef -----------------------------------------------------------*/
  20. /* Private define ------------------------------------------------------------*/
  21. /* Private macro -------------------------------------------------------------*/
  22. #ifdef RT_TIMER_DEBUG
  23. #define timer_debug(format,args...) rt_kprintf(format, ##args)
  24. #else
  25. #define timer_debug(format,args...)
  26. #endif
  27. #define TIMER_TopCalculate(p) \
  28. (p * (EFM32_HFXO_FREQUENCY / (1 << TMR_CFG_PRESCALER) / 1000))
  29. /* Private variables ---------------------------------------------------------*/
  30. #ifdef RT_USING_TIMER0
  31. static struct rt_device timer0_device;
  32. #endif
  33. #ifdef RT_USING_TIMER1
  34. static struct rt_device timer1_device;
  35. #endif
  36. #ifdef RT_USING_TIMER2
  37. static struct rt_device timer2_device;
  38. #endif
  39. /* Private function prototypes -----------------------------------------------*/
  40. /* Private functions ---------------------------------------------------------*/
  41. /***************************************************************************//**
  42. * @brief
  43. * Initialize Timer device
  44. *
  45. * @details
  46. *
  47. * @note
  48. *
  49. * @param[in] dev
  50. * Pointer to device descriptor
  51. *
  52. * @return
  53. * Error code
  54. ******************************************************************************/
  55. static rt_err_t rt_hs_timer_init (rt_device_t dev)
  56. {
  57. RT_ASSERT(dev != RT_NULL);
  58. struct efm32_timer_device_t *timer;
  59. timer = (struct efm32_timer_device_t *)(dev->user_data);
  60. timer->hook.cbFunc = RT_NULL;
  61. timer->hook.userPtr = RT_NULL;
  62. return RT_EOK;
  63. }
  64. /***************************************************************************//**
  65. * @brief
  66. * Configure Timer device
  67. *
  68. * @details
  69. *
  70. * @note
  71. *
  72. * @param[in] dev
  73. * Pointer to device descriptor
  74. *
  75. * @param[in] cmd
  76. * Timer control command
  77. *
  78. * @param[in] args
  79. * Arguments
  80. *
  81. * @return
  82. * Error code
  83. ******************************************************************************/
  84. static rt_err_t rt_hs_timer_control (
  85. rt_device_t dev,
  86. rt_uint8_t cmd,
  87. void *args)
  88. {
  89. RT_ASSERT(dev != RT_NULL);
  90. struct efm32_timer_device_t *timer;
  91. timer = (struct efm32_timer_device_t *)(dev->user_data);
  92. switch (cmd)
  93. {
  94. case RT_DEVICE_CTRL_SUSPEND:
  95. /* Suspend device */
  96. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  97. TIMER_Enable(timer->timer_device, false);
  98. break;
  99. case RT_DEVICE_CTRL_RESUME:
  100. /* Resume device */
  101. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  102. TIMER_Enable(timer->timer_device, true);
  103. break;
  104. case RT_DEVICE_CTRL_TIMER_PERIOD:
  105. {
  106. /* change device setting */
  107. struct efm32_timer_control_t *control;
  108. rt_uint32_t running;
  109. control = (struct efm32_timer_control_t *)args;
  110. running = timer->timer_device->STATUS & 0x00000001;
  111. TIMER_Enable(timer->timer_device, false);
  112. timer->timer_device->CNT = _TIMER_CNT_RESETVALUE;
  113. TIMER_TopSet(timer->timer_device, TIMER_TopCalculate(control->period));
  114. timer->hook.cbFunc = control->hook.cbFunc;
  115. timer->hook.userPtr = control->hook.userPtr;
  116. if (running)
  117. {
  118. TIMER_Enable(timer->timer_device, true);
  119. }
  120. }
  121. break;
  122. }
  123. return RT_EOK;
  124. }
  125. /***************************************************************************//**
  126. * @brief
  127. * Register Timer device
  128. *
  129. * @details
  130. *
  131. * @note
  132. *
  133. * @param[in] device
  134. * Pointer to device descriptor
  135. *
  136. * @param[in] name
  137. * Device name
  138. *
  139. * @param[in] flag
  140. * Configuration flags
  141. *
  142. * @param[in] timer
  143. * Pointer to Timer device descriptor
  144. *
  145. * @return
  146. * Error code
  147. ******************************************************************************/
  148. rt_err_t rt_hw_timer_register(
  149. rt_device_t device,
  150. const char *name,
  151. rt_uint32_t flag,
  152. struct efm32_timer_device_t *timer)
  153. {
  154. RT_ASSERT(device != RT_NULL);
  155. device->type = RT_Device_Class_Char; /* fixme: should be timer type*/
  156. device->rx_indicate = RT_NULL;
  157. device->tx_complete = RT_NULL;
  158. device->init = rt_hs_timer_init;
  159. device->open = RT_NULL;
  160. device->close = RT_NULL;
  161. device->read = RT_NULL;
  162. device->write = RT_NULL;
  163. device->control = rt_hs_timer_control;
  164. device->user_data = timer;
  165. /* register a character device */
  166. return rt_device_register(device, name, flag);
  167. }
  168. /***************************************************************************//**
  169. * @brief
  170. * Timer counter overflow interrupt handler
  171. *
  172. * @details
  173. *
  174. * @note
  175. ******************************************************************************/
  176. void rt_hw_timer_isr(rt_device_t dev)
  177. {
  178. RT_ASSERT(dev != RT_NULL);
  179. struct efm32_timer_device_t *timer;
  180. timer = (struct efm32_timer_device_t *)(dev->user_data);
  181. if (timer->hook.cbFunc != RT_NULL)
  182. {
  183. (timer->hook.cbFunc)(timer->hook.userPtr);
  184. }
  185. }
  186. /***************************************************************************//**
  187. * @brief
  188. * Initialize the specified TIMER unit
  189. *
  190. * @details
  191. *
  192. * @note
  193. *
  194. * @param[in] device
  195. * Pointer to device descriptor
  196. *
  197. * @param[in] unitNumber
  198. * Unit number
  199. *
  200. * @return
  201. * Pointer to TIMER device
  202. ******************************************************************************/
  203. static struct efm32_timer_device_t *rt_hw_timer_unit_init(
  204. rt_device_t device,
  205. rt_uint8_t unitNumber,
  206. rt_uint8_t mode)
  207. {
  208. struct efm32_timer_device_t *timer;
  209. TIMER_Init_TypeDef init;
  210. efm32_irq_hook_init_t hook;
  211. CMU_Clock_TypeDef timerClock;
  212. IRQn_Type timerIrq;
  213. do
  214. {
  215. /* Allocate device */
  216. timer = rt_malloc(sizeof(struct efm32_timer_device_t));
  217. if (timer == RT_NULL)
  218. {
  219. timer_debug("no memory for TIMER%d driver\n", unitNumber);
  220. break;
  221. }
  222. /* Initialization */
  223. if (unitNumber >= TIMER_COUNT)
  224. {
  225. break;
  226. }
  227. switch (unitNumber)
  228. {
  229. case 0:
  230. timer->timer_device = TIMER0;
  231. timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER0;
  232. timerIrq = TIMER0_IRQn;
  233. break;
  234. case 1:
  235. timer->timer_device = TIMER1;
  236. timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER1;
  237. timerIrq = TIMER1_IRQn;
  238. break;
  239. case 2:
  240. timer->timer_device = TIMER2;
  241. timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER2;
  242. timerIrq = TIMER2_IRQn;
  243. break;
  244. default:
  245. break;
  246. }
  247. /* Enable TIMER clock */
  248. CMU_ClockEnable(timerClock, true);
  249. /* Reset */
  250. TIMER_Reset(timer->timer_device);
  251. /* Init specified TIMER unit */
  252. init.enable = false;
  253. init.debugRun = true;
  254. init.prescale = TMR_CFG_PRESCALER;
  255. init.clkSel = timerClkSelHFPerClk;
  256. init.fallAction = timerInputActionNone;
  257. init.riseAction = timerInputActionNone;
  258. init.mode = timerModeUp;
  259. init.dmaClrAct = false;
  260. init.quadModeX4 = false;
  261. init.oneShot = (mode > 0) ? true : false;
  262. init.sync = false;
  263. TIMER_Init(timer->timer_device, &init);
  264. /* Config interrupt and NVIC */
  265. hook.type = efm32_irq_type_timer;
  266. hook.unit = unitNumber;
  267. hook.cbFunc = rt_hw_timer_isr;
  268. hook.userPtr = device;
  269. efm32_irq_hook_register(&hook);
  270. /* Enable overflow interrupt */
  271. TIMER_IntEnable(timer->timer_device, TIMER_IF_OF);
  272. TIMER_IntClear(timer->timer_device, TIMER_IF_OF);
  273. /* Enable TIMERn interrupt vector in NVIC */
  274. NVIC_ClearPendingIRQ(timerIrq);
  275. NVIC_SetPriority(timerIrq, EFM32_IRQ_PRI_DEFAULT);
  276. NVIC_EnableIRQ(timerIrq);
  277. return timer;
  278. } while(0);
  279. if (timer)
  280. {
  281. rt_free(timer);
  282. }
  283. rt_kprintf("TIMER: Init failed!\n");
  284. return RT_NULL;
  285. }
  286. /***************************************************************************//**
  287. * @brief
  288. * Initialize all Timer module related hardware and register Timer device to
  289. * kernel
  290. *
  291. * @details
  292. *
  293. * @note
  294. ******************************************************************************/
  295. void rt_hw_timer_init(void)
  296. {
  297. struct efm32_timer_device_t *timer;
  298. #ifdef RT_USING_TIMER0
  299. if ((timer = rt_hw_timer_unit_init(&timer0_device, 0, RT_USING_TIMER0)) \
  300. != RT_NULL)
  301. {
  302. rt_hw_timer_register(&timer0_device, RT_TIMER0_NAME, 0, timer);
  303. }
  304. #endif
  305. #ifdef RT_USING_TIMER1
  306. if ((timer = rt_hw_timer_unit_init(&timer1_device, 1, RT_USING_TIMER1)) \
  307. != RT_NULL)
  308. {
  309. rt_hw_timer_register(&timer1_device, RT_TIMER1_NAME, 0, timer);
  310. }
  311. #endif
  312. #ifdef RT_USING_TIMER2
  313. if ((timer = rt_hw_timer_unit_init(&timer2_device, 2, RT_USING_TIMER2)) \
  314. != RT_NULL)
  315. {
  316. rt_hw_timer_register(&timer2_device, RT_TIMER2_NAME, 0, timer);
  317. }
  318. #endif
  319. }
  320. #endif
  321. /***************************************************************************//**
  322. * @}
  323. ******************************************************************************/