nu_etimer.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /**************************************************************************//**
  2. * @file etimer.h
  3. * @brief ETIMER driver header file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #ifndef __NU_ETIMER_H__
  9. #define __NU_ETIMER_H__
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. #include "nuc980.h"
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup ETIMER_Driver ETIMER Driver
  19. @{
  20. */
  21. /** @addtogroup ETIMER_EXPORTED_CONSTANTS ETIMER Exported Constants
  22. @{
  23. */
  24. #define ETIMER_ONESHOT_MODE (0UL) /*!< Timer working in one shot mode */
  25. #define ETIMER_PERIODIC_MODE (1UL << 4) /*!< Timer working in periodic mode */
  26. #define ETIMER_TOGGLE_MODE (2UL << 4) /*!< Timer working in toggle mode */
  27. #define ETIMER_CONTINUOUS_MODE (3UL << 4) /*!< Timer working in continuous mode */
  28. #define ETIMER_CAPTURE_FREE_COUNTING_MODE (0UL) /*!< Free counting mode */
  29. #define ETIMER_CAPTURE_TRIGGER_COUNTING_MODE (1UL << 20) /*!< Trigger counting mode */
  30. #define ETIMER_CAPTURE_COUNTER_RESET_MODE (1UL << 17) /*!< Counter reset mode */
  31. #define ETIMER_CAPTURE_FALLING_EDGE (0UL) /*!< Falling edge trigger timer capture */
  32. #define ETIMER_CAPTURE_RISING_EDGE (1UL << 18) /*!< Rising edge trigger timer capture */
  33. #define ETIMER_CAPTURE_FALLING_THEN_RISING_EDGE (2UL << 18) /*!< Falling edge then rising edge trigger timer capture */
  34. #define ETIMER_CAPTURE_RISING_THEN_FALLING_EDGE (3UL << 18) /*!< Rising edge then falling edge trigger timer capture */
  35. #define TIMER_TIMEOUT_TRIGGER (0UL) /*!< Timer timeout trigger other modules */
  36. #define TIMER_CAPTURE_TRIGGER (1UL << 11) /*!< Timer capture trigger other modules */
  37. #define TIMER_COUNTER_RISING_EDGE (1UL << 13) /*!< Counter increase on rising edge */
  38. #define TIMER_COUNTER_FALLING_EDGE (0UL) /*!< Counter increase on falling edge */
  39. /*@}*/ /* end of group ETIMER_EXPORTED_CONSTANTS */
  40. /** @addtogroup ETIMER_EXPORTED_FUNCTIONS ETIMER Exported Functions
  41. @{
  42. */
  43. /**
  44. * @brief This macro is used to set new Timer compared value
  45. * @param[in] timer ETIMER number. Range from 0 ~ 5
  46. * @param[in] u32Value Timer compare value. Valid values are between 2 to 0xFFFFFF
  47. * @return None
  48. * \hideinitializer
  49. */
  50. #define ETIMER_SET_CMP_VALUE(timer, u32Value) \
  51. do{\
  52. if((timer) == 0) {\
  53. outpw(REG_ETMR0_CMPR, u32Value);\
  54. } else if((timer) == 1) {\
  55. outpw(REG_ETMR1_CMPR, u32Value);\
  56. } else if((timer) == 2) {\
  57. outpw(REG_ETMR2_CMPR, u32Value);\
  58. } else if((timer) == 3) {\
  59. outpw(REG_ETMR3_CMPR, u32Value);\
  60. } else if((timer) == 4) {\
  61. outpw(REG_ETMR4_CMPR, u32Value);\
  62. } else {\
  63. outpw(REG_ETMR5_CMPR, u32Value);\
  64. }\
  65. }while(0)
  66. /**
  67. * @brief This macro is used to set new Timer prescale value
  68. * @param[in] timer ETIMER number. Range from 0 ~ 5
  69. * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF
  70. * @return None
  71. * @note Clock input is divided by (prescale + 1) before it is fed into timer
  72. * \hideinitializer
  73. */
  74. #define ETIMER_SET_PRESCALE_VALUE(timer, u32Value) \
  75. do{\
  76. if((timer) == 0) {\
  77. outpw(REG_ETMR0_PRECNT, u32Value);\
  78. } else if((timer) == 1) {\
  79. outpw(REG_ETMR1_PRECNT, u32Value);\
  80. } else if((timer) == 2) {\
  81. outpw(REG_ETMR2_PRECNT, u32Value);\
  82. } else if((timer) == 3) {\
  83. outpw(REG_ETMR3_PRECNT, u32Value);\
  84. } else if((timer) == 4) {\
  85. outpw(REG_ETMR4_PRECNT, u32Value);\
  86. } else {\
  87. outpw(REG_ETMR5_PRECNT, u32Value);\
  88. }\
  89. }while(0)
  90. /**
  91. * @brief Select Timer operating mode
  92. *
  93. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  94. * @param[in] u32OpMode Operation mode. Possible options are
  95. * - \ref ETIMER_ONESHOT_MODE
  96. * - \ref ETIMER_PERIODIC_MODE
  97. * - \ref ETIMER_TOGGLE_MODE
  98. * - \ref ETIMER_CONTINUOUS_MODE
  99. *
  100. * @return None
  101. * \hideinitializer
  102. */
  103. #define ETIMER_SET_OPMODE(timer, u32OpMode) \
  104. do{\
  105. if((timer) == 0) {\
  106. outpw(REG_ETMR0_CTL, (inpw(REG_ETMR0_CTL)&~0x30) | u32OpMode);\
  107. } else if((timer) == 1) {\
  108. outpw(REG_ETMR1_CTL, (inpw(REG_ETMR1_CTL)&~0x30) | u32OpMode);\
  109. } else if((timer) == 2) {\
  110. outpw(REG_ETMR2_CTL, (inpw(REG_ETMR2_CTL)&~0x30) | u32OpMode);\
  111. } else if((timer) == 3) {\
  112. outpw(REG_ETMR3_CTL, (inpw(REG_ETMR3_CTL)&~0x30) | u32OpMode);\
  113. } else if((timer) == 4) {\
  114. outpw(REG_ETMR4_CTL, (inpw(REG_ETMR4_CTL)&~0x30) | u32OpMode);\
  115. } else {\
  116. outpw(REG_ETMR5_CTL, (inpw(REG_ETMR5_CTL)&~0x30) | u32OpMode);\
  117. }\
  118. }while(0)
  119. /**
  120. * @brief This macro is used to check if specify Timer is inactive or active
  121. * @param[in] timer ETIMER number. Range from 0 ~ 5
  122. * @return timer is activate or inactivate
  123. * @retval 0 Timer 24-bit up counter is inactive
  124. * @retval 1 Timer 24-bit up counter is active
  125. * \hideinitializer
  126. */
  127. static __inline int ETIMER_Is_Active(UINT timer)
  128. {
  129. int reg;
  130. if (timer == 0)
  131. {
  132. reg = inpw(REG_ETMR0_CTL);
  133. }
  134. else if (timer == 1)
  135. {
  136. reg = inpw(REG_ETMR1_CTL);
  137. }
  138. else if (timer == 2)
  139. {
  140. reg = inpw(REG_ETMR2_CTL);
  141. }
  142. else if (timer == 3)
  143. {
  144. reg = inpw(REG_ETMR3_CTL);
  145. }
  146. else if (timer == 4)
  147. {
  148. reg = inpw(REG_ETMR4_CTL);
  149. }
  150. else
  151. {
  152. reg = inpw(REG_ETMR5_CTL);
  153. }
  154. return (reg & 0x80) ? 1 : 0;
  155. }
  156. /**
  157. * @brief This function is used to start Timer counting
  158. * @param[in] timer ETIMER number. Range from 0 ~ 5
  159. * @return None
  160. */
  161. static __inline void ETIMER_Start(UINT timer)
  162. {
  163. if (timer == 0)
  164. {
  165. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) | 1);
  166. }
  167. else if (timer == 1)
  168. {
  169. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) | 1);
  170. }
  171. else if (timer == 2)
  172. {
  173. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) | 1);
  174. }
  175. else if (timer == 3)
  176. {
  177. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) | 1);
  178. }
  179. else if (timer == 4)
  180. {
  181. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) | 1);
  182. }
  183. else
  184. {
  185. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) | 1);
  186. }
  187. }
  188. /**
  189. * @brief This function is used to stop Timer counting
  190. * @param[in] timer ETIMER number. Range from 0 ~ 5
  191. * @return None
  192. */
  193. static __inline void ETIMER_Stop(UINT timer)
  194. {
  195. if (timer == 0)
  196. {
  197. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~1);
  198. }
  199. else if (timer == 1)
  200. {
  201. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~1);
  202. }
  203. else if (timer == 2)
  204. {
  205. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~1);
  206. }
  207. else if (timer == 3)
  208. {
  209. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~1);
  210. }
  211. else if (timer == 4)
  212. {
  213. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) & ~1);
  214. }
  215. else
  216. {
  217. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) & ~1);
  218. }
  219. }
  220. /**
  221. * @brief This function is used to enable the Timer wake-up function
  222. * @param[in] timer ETIMER number. Range from 0 ~ 5
  223. * @return None
  224. * @note To wake the system from power down mode, timer clock source must be ether LXT or LIRC
  225. */
  226. static __inline void ETIMER_EnableWakeup(UINT timer)
  227. {
  228. if (timer == 0)
  229. {
  230. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) | 4);
  231. }
  232. else if (timer == 1)
  233. {
  234. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) | 4);
  235. }
  236. else if (timer == 2)
  237. {
  238. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) | 4);
  239. }
  240. else if (timer == 3)
  241. {
  242. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) | 4);
  243. }
  244. else if (timer == 4)
  245. {
  246. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) | 4);
  247. }
  248. else
  249. {
  250. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) | 4);
  251. }
  252. }
  253. /**
  254. * @brief This function is used to disable the Timer wake-up function
  255. * @param[in] timer ETIMER number. Range from 0 ~ 5
  256. * @return None
  257. */
  258. static __inline void ETIMER_DisableWakeup(UINT timer)
  259. {
  260. if (timer == 0)
  261. {
  262. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~4);
  263. }
  264. else if (timer == 1)
  265. {
  266. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~4);
  267. }
  268. else if (timer == 2)
  269. {
  270. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~4);
  271. }
  272. else if (timer == 3)
  273. {
  274. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~4);
  275. }
  276. else if (timer == 4)
  277. {
  278. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) & ~4);
  279. }
  280. else
  281. {
  282. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) & ~4);
  283. }
  284. }
  285. /**
  286. * @brief This function is used to enable the capture pin detection de-bounce function.
  287. * @param[in] timer ETIMER number. Range from 0 ~ 5
  288. * @return None
  289. */
  290. static __inline void ETIMER_EnableCaptureDebounce(UINT timer)
  291. {
  292. if (timer == 0)
  293. {
  294. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) | 0x400000);
  295. }
  296. else if (timer == 1)
  297. {
  298. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) | 0x400000);
  299. }
  300. else if (timer == 2)
  301. {
  302. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) | 0x400000);
  303. }
  304. else if (timer == 3)
  305. {
  306. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) | 0x400000);
  307. }
  308. else if (timer == 4)
  309. {
  310. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) | 0x400000);
  311. }
  312. else
  313. {
  314. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) | 0x400000);
  315. }
  316. }
  317. /**
  318. * @brief This function is used to disable the capture pin detection de-bounce function.
  319. * @param[in] timer ETIMER number. Range from 0 ~ 5
  320. * @return None
  321. */
  322. static __inline void ETIMER_DisableCaptureDebounce(UINT timer)
  323. {
  324. if (timer == 0)
  325. {
  326. outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~0x400000);
  327. }
  328. else if (timer == 1)
  329. {
  330. outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~0x400000);
  331. }
  332. else if (timer == 2)
  333. {
  334. outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~0x400000);
  335. }
  336. else if (timer == 3)
  337. {
  338. outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~0x400000);
  339. }
  340. else if (timer == 4)
  341. {
  342. outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) & ~0x400000);
  343. }
  344. else
  345. {
  346. outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) & ~0x400000);
  347. }
  348. }
  349. /**
  350. * @brief This function is used to enable the Timer time-out interrupt function.
  351. * @param[in] timer ETIMER number. Range from 0 ~ 5
  352. * @return None
  353. */
  354. static __inline void ETIMER_EnableInt(UINT timer)
  355. {
  356. if (timer == 0)
  357. {
  358. outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) | 1);
  359. }
  360. else if (timer == 1)
  361. {
  362. outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) | 1);
  363. }
  364. else if (timer == 2)
  365. {
  366. outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) | 1);
  367. }
  368. else if (timer == 3)
  369. {
  370. outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) | 1);
  371. }
  372. else if (timer == 4)
  373. {
  374. outpw(REG_ETMR4_IER, inpw(REG_ETMR4_IER) | 1);
  375. }
  376. else
  377. {
  378. outpw(REG_ETMR5_IER, inpw(REG_ETMR5_IER) | 1);
  379. }
  380. }
  381. /**
  382. * @brief This function is used to disable the Timer time-out interrupt function.
  383. * @param[in] timer ETIMER number. Range from 0 ~ 5
  384. * @return None
  385. */
  386. static __inline void ETIMER_DisableInt(UINT timer)
  387. {
  388. if (timer == 0)
  389. {
  390. outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) & ~1);
  391. }
  392. else if (timer == 1)
  393. {
  394. outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) & ~1);
  395. }
  396. else if (timer == 2)
  397. {
  398. outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) & ~1);
  399. }
  400. else if (timer == 3)
  401. {
  402. outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) & ~1);
  403. }
  404. else if (timer == 4)
  405. {
  406. outpw(REG_ETMR4_IER, inpw(REG_ETMR4_IER) & ~1);
  407. }
  408. else
  409. {
  410. outpw(REG_ETMR5_IER, inpw(REG_ETMR5_IER) & ~1);
  411. }
  412. }
  413. /**
  414. * @brief This function is used to enable the Timer capture trigger interrupt function.
  415. * @param[in] timer ETIMER number. Range from 0 ~ 5
  416. * @return None
  417. */
  418. static __inline void ETIMER_EnableCaptureInt(UINT timer)
  419. {
  420. if (timer == 0)
  421. {
  422. outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) | 2);
  423. }
  424. else if (timer == 1)
  425. {
  426. outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) | 2);
  427. }
  428. else if (timer == 2)
  429. {
  430. outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) | 2);
  431. }
  432. else if (timer == 3)
  433. {
  434. outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) | 2);
  435. }
  436. else if (timer == 4)
  437. {
  438. outpw(REG_ETMR4_IER, inpw(REG_ETMR4_IER) | 2);
  439. }
  440. else
  441. {
  442. outpw(REG_ETMR5_IER, inpw(REG_ETMR5_IER) | 2);
  443. }
  444. }
  445. /**
  446. * @brief This function is used to disable the Timer capture trigger interrupt function.
  447. * @param[in] timer ETIMER number. Range from 0 ~ 5
  448. * @return None
  449. */
  450. static __inline void ETIMER_DisableCaptureInt(UINT timer)
  451. {
  452. if (timer == 0)
  453. {
  454. outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) & ~2);
  455. }
  456. else if (timer == 1)
  457. {
  458. outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) & ~2);
  459. }
  460. else if (timer == 2)
  461. {
  462. outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) & ~2);
  463. }
  464. else if (timer == 3)
  465. {
  466. outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) & ~2);
  467. }
  468. else if (timer == 4)
  469. {
  470. outpw(REG_ETMR4_IER, inpw(REG_ETMR4_IER) & ~2);
  471. }
  472. else
  473. {
  474. outpw(REG_ETMR5_IER, inpw(REG_ETMR5_IER) & ~2);
  475. }
  476. }
  477. /**
  478. * @brief This function indicates Timer time-out interrupt occurred or not.
  479. * @param[in] timer ETIMER number. Range from 0 ~ 5
  480. * @return Timer time-out interrupt occurred or not
  481. * @retval 0 Timer time-out interrupt did not occur
  482. * @retval 1 Timer time-out interrupt occurred
  483. */
  484. static __inline UINT ETIMER_GetIntFlag(UINT timer)
  485. {
  486. int reg;
  487. if (timer == 0)
  488. {
  489. reg = inpw(REG_ETMR0_ISR);
  490. }
  491. else if (timer == 1)
  492. {
  493. reg = inpw(REG_ETMR1_ISR);
  494. }
  495. else if (timer == 2)
  496. {
  497. reg = inpw(REG_ETMR2_ISR);
  498. }
  499. else if (timer == 3)
  500. {
  501. reg = inpw(REG_ETMR3_ISR);
  502. }
  503. else if (timer == 4)
  504. {
  505. reg = inpw(REG_ETMR4_ISR);
  506. }
  507. else
  508. {
  509. reg = inpw(REG_ETMR5_ISR);
  510. }
  511. return reg & 1;
  512. }
  513. /**
  514. * @brief This function clears the Timer time-out interrupt flag.
  515. * @param[in] timer ETIMER number. Range from 0 ~ 5
  516. * @return None
  517. */
  518. static __inline void ETIMER_ClearIntFlag(UINT timer)
  519. {
  520. if (timer == 0)
  521. {
  522. outpw(REG_ETMR0_ISR, 1);
  523. }
  524. else if (timer == 1)
  525. {
  526. outpw(REG_ETMR1_ISR, 1);
  527. }
  528. else if (timer == 2)
  529. {
  530. outpw(REG_ETMR2_ISR, 1);
  531. }
  532. else if (timer == 3)
  533. {
  534. outpw(REG_ETMR3_ISR, 1);
  535. }
  536. else if (timer == 4)
  537. {
  538. outpw(REG_ETMR4_ISR, 1);
  539. }
  540. else
  541. {
  542. outpw(REG_ETMR5_ISR, 1);
  543. }
  544. }
  545. /**
  546. * @brief This function indicates Timer capture interrupt occurred or not.
  547. * @param[in] timer ETIMER number. Range from 0 ~ 5
  548. * @return Timer capture interrupt occurred or not
  549. * @retval 0 Timer capture interrupt did not occur
  550. * @retval 1 Timer capture interrupt occurred
  551. */
  552. static __inline UINT ETIMER_GetCaptureIntFlag(UINT timer)
  553. {
  554. int reg;
  555. if (timer == 0)
  556. {
  557. reg = inpw(REG_ETMR0_ISR);
  558. }
  559. else if (timer == 1)
  560. {
  561. reg = inpw(REG_ETMR1_ISR);
  562. }
  563. else if (timer == 2)
  564. {
  565. reg = inpw(REG_ETMR2_ISR);
  566. }
  567. else if (timer == 3)
  568. {
  569. reg = inpw(REG_ETMR3_ISR);
  570. }
  571. else if (timer == 4)
  572. {
  573. reg = inpw(REG_ETMR4_ISR);
  574. }
  575. else
  576. {
  577. reg = inpw(REG_ETMR5_ISR);
  578. }
  579. return (reg & 2) >> 1;
  580. }
  581. /**
  582. * @brief This function clears the Timer capture interrupt flag.
  583. * @param[in] timer ETIMER number. Range from 0 ~ 5
  584. * @return None
  585. */
  586. static __inline void ETIMER_ClearCaptureIntFlag(UINT timer)
  587. {
  588. if (timer == 0)
  589. {
  590. outpw(REG_ETMR0_ISR, 2);
  591. }
  592. else if (timer == 1)
  593. {
  594. outpw(REG_ETMR1_ISR, 2);
  595. }
  596. else if (timer == 2)
  597. {
  598. outpw(REG_ETMR2_ISR, 2);
  599. }
  600. else if (timer == 3)
  601. {
  602. outpw(REG_ETMR3_ISR, 2);
  603. }
  604. else if (timer == 4)
  605. {
  606. outpw(REG_ETMR4_ISR, 2);
  607. }
  608. else
  609. {
  610. outpw(REG_ETMR5_ISR, 2);
  611. }
  612. }
  613. /**
  614. * @brief This function gets the Timer capture falling edge flag.
  615. * @param[in] timer ETIMER number. Range from 0 ~ 5
  616. * @return None
  617. */
  618. static __inline UINT8 ETIMER_GetCaptureFallingEdgeFlag(UINT timer)
  619. {
  620. UINT ret;
  621. if (timer == 0)
  622. {
  623. ret = inpw(REG_ETMR0_ISR);
  624. }
  625. else if (timer == 1)
  626. {
  627. ret = inpw(REG_ETMR1_ISR);
  628. }
  629. else if (timer == 2)
  630. {
  631. ret = inpw(REG_ETMR2_ISR);
  632. }
  633. else if (timer == 3)
  634. {
  635. ret = inpw(REG_ETMR3_ISR);
  636. }
  637. else if (timer == 4)
  638. {
  639. ret = inpw(REG_ETMR4_ISR);
  640. }
  641. else
  642. {
  643. ret = inpw(REG_ETMR5_ISR);
  644. }
  645. return (ret & (1 << 6)) >> 6;
  646. }
  647. /**
  648. * @brief This function indicates Timer has waked up system or not.
  649. * @param[in] timer ETIMER number. Range from 0 ~ 5
  650. * @return Timer has waked up system or not
  651. * @retval 0 Timer did not wake up system
  652. * @retval 1 Timer wake up system
  653. */
  654. static __inline UINT ETIMER_GetWakeupFlag(UINT timer)
  655. {
  656. int reg;
  657. if (timer == 0)
  658. {
  659. reg = inpw(REG_ETMR0_ISR);
  660. }
  661. else if (timer == 1)
  662. {
  663. reg = inpw(REG_ETMR1_ISR);
  664. }
  665. else if (timer == 2)
  666. {
  667. reg = inpw(REG_ETMR2_ISR);
  668. }
  669. else if (timer == 3)
  670. {
  671. reg = inpw(REG_ETMR3_ISR);
  672. }
  673. else if (timer == 4)
  674. {
  675. reg = inpw(REG_ETMR4_ISR);
  676. }
  677. else
  678. {
  679. reg = inpw(REG_ETMR5_ISR);
  680. }
  681. return (reg & 0x10) >> 4;
  682. }
  683. /**
  684. * @brief This function clears the Timer wakeup interrupt flag.
  685. * @param[in] timer ETIMER number. Range from 0 ~ 5
  686. * @return None
  687. */
  688. static __inline void ETIMER_ClearWakeupFlag(UINT timer)
  689. {
  690. if (timer == 0)
  691. {
  692. outpw(REG_ETMR0_ISR, 0x10);
  693. }
  694. else if (timer == 1)
  695. {
  696. outpw(REG_ETMR1_ISR, 0x10);
  697. }
  698. else if (timer == 2)
  699. {
  700. outpw(REG_ETMR2_ISR, 0x10);
  701. }
  702. else if (timer == 3)
  703. {
  704. outpw(REG_ETMR3_ISR, 0x10);
  705. }
  706. else if (timer == 4)
  707. {
  708. outpw(REG_ETMR4_ISR, 0x10);
  709. }
  710. else
  711. {
  712. outpw(REG_ETMR5_ISR, 0x10);
  713. }
  714. }
  715. /**
  716. * @brief This function gets the Timer compare value.
  717. * @param[in] timer ETIMER number. Range from 0 ~ 5
  718. * @return Timer compare data value
  719. */
  720. static __inline UINT ETIMER_GetCompareData(UINT timer)
  721. {
  722. if (timer == 0)
  723. {
  724. return inpw(REG_ETMR0_CMPR);
  725. }
  726. else if (timer == 1)
  727. {
  728. return inpw(REG_ETMR1_CMPR);
  729. }
  730. else if (timer == 2)
  731. {
  732. return inpw(REG_ETMR2_CMPR);
  733. }
  734. else if (timer == 3)
  735. {
  736. return inpw(REG_ETMR3_CMPR);
  737. }
  738. else if (timer == 4)
  739. {
  740. return inpw(REG_ETMR4_CMPR);
  741. }
  742. else
  743. {
  744. return inpw(REG_ETMR5_CMPR);
  745. }
  746. }
  747. /**
  748. * @brief This function gets the Timer capture data.
  749. * @param[in] timer ETIMER number. Range from 0 ~ 5
  750. * @return Timer capture data value
  751. */
  752. static __inline UINT ETIMER_GetCaptureData(UINT timer)
  753. {
  754. if (timer == 0)
  755. {
  756. return inpw(REG_ETMR0_TCAP);
  757. }
  758. else if (timer == 1)
  759. {
  760. return inpw(REG_ETMR1_TCAP);
  761. }
  762. else if (timer == 2)
  763. {
  764. return inpw(REG_ETMR2_TCAP);
  765. }
  766. else if (timer == 3)
  767. {
  768. return inpw(REG_ETMR3_TCAP);
  769. }
  770. else if (timer == 4)
  771. {
  772. return inpw(REG_ETMR4_TCAP);
  773. }
  774. else
  775. {
  776. return inpw(REG_ETMR5_TCAP);
  777. }
  778. }
  779. /**
  780. * @brief This function reports the current timer counter value.
  781. * @param[in] timer ETIMER number. Range from 0 ~ 5
  782. * @return Timer counter value
  783. */
  784. static __inline UINT ETIMER_GetCounter(UINT timer)
  785. {
  786. if (timer == 0)
  787. {
  788. return inpw(REG_ETMR0_DR);
  789. }
  790. else if (timer == 1)
  791. {
  792. return inpw(REG_ETMR1_DR);
  793. }
  794. else if (timer == 2)
  795. {
  796. return inpw(REG_ETMR2_DR);
  797. }
  798. else if (timer == 3)
  799. {
  800. return inpw(REG_ETMR3_DR);
  801. }
  802. else if (timer == 4)
  803. {
  804. return inpw(REG_ETMR4_DR);
  805. }
  806. else
  807. {
  808. return inpw(REG_ETMR5_DR);
  809. }
  810. }
  811. static __inline UINT ETIMER_ClearCounter(UINT timer)
  812. {
  813. if (timer == 0)
  814. {
  815. return outpw(REG_ETMR0_DR, 0);
  816. }
  817. else if (timer == 1)
  818. {
  819. return outpw(REG_ETMR1_DR, 0);
  820. }
  821. else if (timer == 2)
  822. {
  823. return outpw(REG_ETMR2_DR, 0);
  824. }
  825. else if (timer == 3)
  826. {
  827. return outpw(REG_ETMR3_DR, 0);
  828. }
  829. else if (timer == 4)
  830. {
  831. return outpw(REG_ETMR4_DR, 0);
  832. }
  833. else
  834. {
  835. return outpw(REG_ETMR5_DR, 0);
  836. }
  837. }
  838. UINT ETIMER_Open(UINT timer, UINT u32Mode, UINT u32Freq);
  839. void ETIMER_Close(UINT timer);
  840. void ETIMER_Delay(UINT timer, UINT u32Usec);
  841. void ETIMER_EnableCapture(UINT timer, UINT u32CapMode, UINT u32Edge);
  842. void ETIMER_DisableCapture(UINT timer);
  843. void ETIMER_EnableEventCounter(UINT timer, uint32_t u32Edge);
  844. void ETIMER_DisableEventCounter(UINT timer);
  845. UINT ETIMER_GetModuleClock(UINT timer);
  846. /*@}*/ /* end of group ETIMER_EXPORTED_FUNCTIONS */
  847. /*@}*/ /* end of group ETIMER_Driver */
  848. /*@}*/ /* end of group Standard_Driver */
  849. #ifdef __cplusplus
  850. }
  851. #endif
  852. #endif //__NU_ETIMER_H__