drv_timer.c 10 KB

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