nu_etimer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**************************************************************************//**
  2. * @file etimer.c
  3. * @brief ETIMER driver source file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include "nuc980.h"
  9. #include "nu_sys.h"
  10. /// @cond HIDDEN_SYMBOLS
  11. /**
  12. * @brief This API is used to get the clock frequency of Timer
  13. * @param[in] timer ETIMER number. Range from 0 ~ 5
  14. * @return Timer clock frequency
  15. * @note This API cannot return correct clock rate if timer source is external clock input.
  16. */
  17. UINT ETIMER_GetModuleClock(UINT timer)
  18. {
  19. UINT src;
  20. src = (inpw(REG_CLK_DIVCTL8) >> (16 + timer * 2)) & 0x3;
  21. if (src == 0)
  22. return 12000000;
  23. else if (src == 1)
  24. return (sysGetClock(SYS_PCLK01) * 1000000);
  25. else if (src == 2)
  26. return (sysGetClock(SYS_PCLK01) * 1000000 / 4096);
  27. else
  28. return 32768;
  29. }
  30. /// @endcond /* HIDDEN_SYMBOLS */
  31. /** @addtogroup Standard_Driver Standard Driver
  32. @{
  33. */
  34. /** @addtogroup ETIMER_Driver ETIMER Driver
  35. @{
  36. */
  37. /** @addtogroup ETIMER_EXPORTED_FUNCTIONS ETIMER Exported Functions
  38. @{
  39. */
  40. /**
  41. * @brief This API is used to configure timer to operate in specified mode
  42. * and frequency. If timer cannot work in target frequency, a closest
  43. * frequency will be chose and returned.
  44. * @param[in] timer ETIMER number. Range from 0 ~ 5
  45. * @param[in] u32Mode Operation mode. Possible options are
  46. * - \ref ETIMER_ONESHOT_MODE
  47. * - \ref ETIMER_PERIODIC_MODE
  48. * - \ref ETIMER_TOGGLE_MODE
  49. * - \ref ETIMER_CONTINUOUS_MODE
  50. * @param[in] u32Freq Target working frequency
  51. * @return Real Timer working frequency
  52. * @note After calling this API, Timer is \b NOT running yet. But could start timer running be calling
  53. * \ref ETIMER_Start macro or program registers directly
  54. */
  55. UINT ETIMER_Open(UINT timer, UINT u32Mode, UINT u32Freq)
  56. {
  57. UINT u32Clk = ETIMER_GetModuleClock(timer);
  58. UINT u32Cmpr = 0, u32Prescale = 0;
  59. // Fastest possible timer working freq is u32Clk / 2. While cmpr = 2, pre-scale = 0
  60. if (u32Freq > (u32Clk / 2))
  61. {
  62. u32Cmpr = 2;
  63. }
  64. else
  65. {
  66. if (u32Clk >= 0x8000000)
  67. {
  68. u32Prescale = 15; // real prescaler value is 16
  69. u32Clk >>= 4;
  70. }
  71. else if (u32Clk >= 0x4000000)
  72. {
  73. u32Prescale = 7; // real prescaler value is 8
  74. u32Clk >>= 3;
  75. }
  76. else if (u32Clk >= 0x2000000)
  77. {
  78. u32Prescale = 3; // real prescaler value is 4
  79. u32Clk >>= 2;
  80. }
  81. else if (u32Clk >= 0x1000000)
  82. {
  83. u32Prescale = 1; // real prescaler value is 2
  84. u32Clk >>= 1;
  85. }
  86. u32Cmpr = u32Clk / u32Freq;
  87. }
  88. if (timer == 0)
  89. {
  90. outpw(REG_ETMR0_CMPR, u32Cmpr);
  91. outpw(REG_ETMR0_PRECNT, u32Prescale);
  92. outpw(REG_ETMR0_CTL, 1 | u32Mode);
  93. }
  94. else if (timer == 1)
  95. {
  96. outpw(REG_ETMR1_CMPR, u32Cmpr);
  97. outpw(REG_ETMR1_PRECNT, u32Prescale);
  98. outpw(REG_ETMR1_CTL, 1 | u32Mode);
  99. }
  100. else if (timer == 2)
  101. {
  102. outpw(REG_ETMR2_CMPR, u32Cmpr);
  103. outpw(REG_ETMR2_PRECNT, u32Prescale);
  104. outpw(REG_ETMR2_CTL, 1 | u32Mode);
  105. }
  106. else if (timer == 3)
  107. {
  108. outpw(REG_ETMR3_CMPR, u32Cmpr);
  109. outpw(REG_ETMR3_PRECNT, u32Prescale);
  110. outpw(REG_ETMR3_CTL, 1 | u32Mode);
  111. }
  112. else if (timer == 4)
  113. {
  114. outpw(REG_ETMR4_CMPR, u32Cmpr);
  115. outpw(REG_ETMR4_PRECNT, u32Prescale);
  116. outpw(REG_ETMR4_CTL, 1 | u32Mode);
  117. }
  118. else
  119. {
  120. outpw(REG_ETMR5_CMPR, u32Cmpr);
  121. outpw(REG_ETMR5_PRECNT, u32Prescale);
  122. outpw(REG_ETMR5_CTL, 1 | u32Mode);
  123. }
  124. return (u32Clk / (u32Cmpr * (u32Prescale + 1)));
  125. }
  126. /**
  127. * @brief This API stops Timer counting and disable the Timer interrupt function
  128. * @param[in] timer ETIMER number. Range from 0 ~ 5
  129. * @return None
  130. */
  131. void ETIMER_Close(UINT timer)
  132. {
  133. if (timer == 0)
  134. {
  135. outpw(REG_ETMR0_CTL, 0);
  136. outpw(REG_ETMR0_IER, 0);
  137. outpw(REG_ETMR0_DR, 0);
  138. }
  139. else if (timer == 1)
  140. {
  141. outpw(REG_ETMR1_CTL, 0);
  142. outpw(REG_ETMR1_IER, 0);
  143. outpw(REG_ETMR1_DR, 0);
  144. }
  145. else if (timer == 2)
  146. {
  147. outpw(REG_ETMR2_CTL, 0);
  148. outpw(REG_ETMR2_IER, 0);
  149. outpw(REG_ETMR2_DR, 0);
  150. }
  151. else if (timer == 3)
  152. {
  153. outpw(REG_ETMR3_CTL, 0);
  154. outpw(REG_ETMR3_IER, 0);
  155. outpw(REG_ETMR3_DR, 0);
  156. }
  157. else if (timer == 4)
  158. {
  159. outpw(REG_ETMR4_CTL, 0);
  160. outpw(REG_ETMR4_IER, 0);
  161. outpw(REG_ETMR4_DR, 0);
  162. }
  163. else
  164. {
  165. outpw(REG_ETMR5_CTL, 0);
  166. outpw(REG_ETMR5_IER, 0);
  167. outpw(REG_ETMR5_DR, 0);
  168. }
  169. }
  170. /**
  171. * @brief This API is used to create a delay loop for u32usec micro seconds
  172. * @param[in] timer ETIMER number. Range from 0 ~ 5
  173. * @param[in] u32Usec Delay period in micro seconds with 10 usec every step. Valid values are between 10~1000000 (10 micro second ~ 1 second)
  174. * @return None
  175. * @note This API overwrites the register setting of the timer used to count the delay time.
  176. * @note This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay
  177. */
  178. void ETIMER_Delay(UINT timer, UINT u32Usec)
  179. {
  180. UINT u32Clk = ETIMER_GetModuleClock(timer);
  181. UINT u32Prescale = 0, delay = 300000000 / u32Clk;
  182. float fCmpr;
  183. // Clear current timer configuration
  184. if (timer == 0)
  185. {
  186. outpw(REG_ETMR0_CTL, 0);
  187. }
  188. else if (timer == 1)
  189. {
  190. outpw(REG_ETMR1_CTL, 0);
  191. }
  192. else if (timer == 2)
  193. {
  194. outpw(REG_ETMR2_CTL, 0);
  195. }
  196. else if (timer == 3)
  197. {
  198. outpw(REG_ETMR3_CTL, 0);
  199. }
  200. else if (timer == 4)
  201. {
  202. outpw(REG_ETMR4_CTL, 0);
  203. }
  204. else
  205. {
  206. outpw(REG_ETMR5_CTL, 0);
  207. }
  208. if (u32Clk == 10000) // min delay is 100us if timer clock source is LIRC 10k
  209. {
  210. u32Usec = ((u32Usec + 99) / 100) * 100;
  211. }
  212. else // 10 usec every step
  213. {
  214. u32Usec = ((u32Usec + 9) / 10) * 10;
  215. }
  216. if (u32Clk >= 0x4000000)
  217. {
  218. u32Prescale = 7; // real prescaler value is 8
  219. u32Clk >>= 3;
  220. }
  221. else if (u32Clk >= 0x2000000)
  222. {
  223. u32Prescale = 3; // real prescaler value is 4
  224. u32Clk >>= 2;
  225. }
  226. else if (u32Clk >= 0x1000000)
  227. {
  228. u32Prescale = 1; // real prescaler value is 2
  229. u32Clk >>= 1;
  230. }
  231. // u32Usec * u32Clk might overflow if using UINT
  232. fCmpr = ((float)u32Usec * (float)u32Clk) / 1000000.0;
  233. if (timer == 0)
  234. {
  235. outpw(REG_ETMR0_CMPR, (UINT)fCmpr);
  236. outpw(REG_ETMR0_PRECNT, u32Prescale);
  237. outpw(REG_ETMR0_CTL, 1);
  238. }
  239. else if (timer == 1)
  240. {
  241. outpw(REG_ETMR1_CMPR, (UINT)fCmpr);
  242. outpw(REG_ETMR1_PRECNT, u32Prescale);
  243. outpw(REG_ETMR1_CTL, 1);
  244. }
  245. else if (timer == 2)
  246. {
  247. outpw(REG_ETMR2_CMPR, (UINT)fCmpr);
  248. outpw(REG_ETMR2_PRECNT, u32Prescale);
  249. outpw(REG_ETMR2_CTL, 1);
  250. }
  251. else if (timer == 3)
  252. {
  253. outpw(REG_ETMR3_CMPR, (UINT)fCmpr);
  254. outpw(REG_ETMR3_PRECNT, u32Prescale);
  255. outpw(REG_ETMR3_CTL, 1);
  256. }
  257. else if (timer == 4)
  258. {
  259. outpw(REG_ETMR4_CMPR, (UINT)fCmpr);
  260. outpw(REG_ETMR4_PRECNT, u32Prescale);
  261. outpw(REG_ETMR4_CTL, 1);
  262. }
  263. else
  264. {
  265. outpw(REG_ETMR5_CMPR, (UINT)fCmpr);
  266. outpw(REG_ETMR5_PRECNT, u32Prescale);
  267. outpw(REG_ETMR5_CTL, 1);
  268. }
  269. // When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
  270. // And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag.
  271. for (; delay > 0; delay--)
  272. {
  273. #if defined (__GNUC__) && !(__CC_ARM)
  274. __asm__ __volatile__
  275. (
  276. "nop \n"
  277. );
  278. #else
  279. __asm
  280. {
  281. NOP
  282. }
  283. #endif
  284. }
  285. if (timer == 0)
  286. {
  287. while (inpw(REG_ETMR0_CTL) & 0x80);
  288. }
  289. else if (timer == 1)
  290. {
  291. while (inpw(REG_ETMR1_CTL) & 0x80);
  292. }
  293. else if (timer == 2)
  294. {
  295. while (inpw(REG_ETMR2_CTL) & 0x80);
  296. }
  297. else if (timer == 3)
  298. {
  299. while (inpw(REG_ETMR3_CTL) & 0x80);
  300. }
  301. else if (timer == 4)
  302. {
  303. while (inpw(REG_ETMR4_CTL) & 0x80);
  304. }
  305. else
  306. {
  307. while (inpw(REG_ETMR5_CTL) & 0x80);
  308. }
  309. }
  310. /**
  311. * @brief This API is used to enable timer capture function with specified mode and capture edge
  312. * @param[in] timer ETIMER number. Range from 0 ~ 5
  313. * @param[in] u32CapMode Timer capture mode. Could be
  314. * - \ref ETIMER_CAPTURE_FREE_COUNTING_MODE
  315. * - \ref ETIMER_CAPTURE_TRIGGER_COUNTING_MODE
  316. * - \ref ETIMER_CAPTURE_COUNTER_RESET_MODE
  317. * @param[in] u32Edge Timer capture edge. Possible values are
  318. * - \ref ETIMER_CAPTURE_FALLING_EDGE
  319. * - \ref ETIMER_CAPTURE_RISING_EDGE
  320. * - \ref ETIMER_CAPTURE_FALLING_THEN_RISING_EDGE
  321. * - \ref ETIMER_CAPTURE_RISING_THEN_FALLING_EDGE
  322. * @return None
  323. * @note Timer frequency should be configured separately by using \ref ETIMER_Open API, or program registers directly
  324. */
  325. void ETIMER_EnableCapture(UINT timer, UINT u32CapMode, UINT u32Edge)
  326. {
  327. if (timer == 0)
  328. {
  329. outpw(REG_ETMR0_CTL, (inpw(REG_ETMR0_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
  330. }
  331. else if (timer == 1)
  332. {
  333. outpw(REG_ETMR1_CTL, (inpw(REG_ETMR1_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
  334. }
  335. else if (timer == 2)
  336. {
  337. outpw(REG_ETMR2_CTL, (inpw(REG_ETMR2_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
  338. }
  339. else if (timer == 3)
  340. {
  341. outpw(REG_ETMR3_CTL, (inpw(REG_ETMR3_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
  342. }
  343. else if (timer == 4)
  344. {
  345. outpw(REG_ETMR4_CTL, (inpw(REG_ETMR4_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
  346. }
  347. else
  348. {
  349. outpw(REG_ETMR5_CTL, (inpw(REG_ETMR5_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
  350. }
  351. }
  352. /**
  353. * @brief This API is used to disable the Timer capture function
  354. * @param[in] timer ETIMER number. Range from 0 ~ 5
  355. * @return None
  356. */
  357. void ETIMER_DisableCapture(UINT timer)
  358. {
  359. if (timer == 0)
  360. {
  361. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~0x10000);
  362. }
  363. else if (timer == 1)
  364. {
  365. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~0x10000);
  366. }
  367. else if (timer == 2)
  368. {
  369. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~0x10000);
  370. }
  371. else if (timer == 3)
  372. {
  373. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~0x10000);
  374. }
  375. else if (timer == 4)
  376. {
  377. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) & ~0x10000);
  378. }
  379. else
  380. {
  381. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) & ~0x10000);
  382. }
  383. }
  384. /**
  385. * @brief This function is used to enable the Timer counter function with specify detection edge
  386. * @param[in] timer ETIMER number. Range from 0 ~ 5
  387. * @param[in] u32Edge Detection edge of counter pin. Could be ether
  388. * - \ref TIMER_COUNTER_RISING_EDGE, or
  389. * - \ref TIMER_COUNTER_FALLING_EDGE
  390. * @return None
  391. * @note Timer compare value should be configured separately by using \ref ETIMER_SET_CMP_VALUE macro or program registers directly
  392. */
  393. void ETIMER_EnableEventCounter(UINT timer, uint32_t u32Edge)
  394. {
  395. if (timer == 0)
  396. {
  397. outpw(REG_ETMR0_CTL, (inpw(REG_ETMR0_CTL) & ~0x2000) | u32Edge | 0x1000);
  398. }
  399. else if (timer == 1)
  400. {
  401. outpw(REG_ETMR1_CTL, (inpw(REG_ETMR1_CTL) & ~0x2000) | u32Edge | 0x1000);
  402. }
  403. else if (timer == 2)
  404. {
  405. outpw(REG_ETMR2_CTL, (inpw(REG_ETMR2_CTL) & ~0x2000) | u32Edge | 0x1000);
  406. }
  407. else if (timer == 3)
  408. {
  409. outpw(REG_ETMR3_CTL, (inpw(REG_ETMR3_CTL) & ~0x2000) | u32Edge | 0x1000);
  410. }
  411. else if (timer == 4)
  412. {
  413. outpw(REG_ETMR4_CTL, (inpw(REG_ETMR4_CTL) & ~0x2000) | u32Edge | 0x1000);
  414. }
  415. else
  416. {
  417. outpw(REG_ETMR5_CTL, (inpw(REG_ETMR5_CTL) & ~0x2000) | u32Edge | 0x1000);
  418. }
  419. }
  420. /**
  421. * @brief This API is used to disable the Timer event counter function.
  422. * @param[in] timer ETIMER number. Range from 0 ~ 5
  423. * @return None
  424. */
  425. void ETIMER_DisableEventCounter(UINT timer)
  426. {
  427. if (timer == 0)
  428. {
  429. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~0x1000);
  430. }
  431. else if (timer == 1)
  432. {
  433. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~0x1000);
  434. }
  435. else if (timer == 2)
  436. {
  437. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~0x1000);
  438. }
  439. else if (timer == 3)
  440. {
  441. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~0x1000);
  442. }
  443. else if (timer == 4)
  444. {
  445. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) & ~0x1000);
  446. }
  447. else
  448. {
  449. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) & ~0x1000);
  450. }
  451. }
  452. /*@}*/ /* end of group ETIMER_EXPORTED_FUNCTIONS */
  453. /*@}*/ /* end of group ETIMER_Driver */
  454. /*@}*/ /* end of group Standard_Driver */
  455. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/