timer_ll.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The LL layer for Timer Group register operations.
  15. // Note that most of the register operations in this layer are non-atomic operations.
  16. #pragma once
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <stdlib.h>
  21. #include "hal/timer_types.h"
  22. #include "soc/timer_periph.h"
  23. _Static_assert(TIMER_INTR_T0 == TIMG_T0_INT_CLR, "Add mapping to LL interrupt handling, since it's no longer naturally compatible with the timer_intr_t");
  24. _Static_assert(TIMER_INTR_T1 == TIMG_T1_INT_CLR, "Add mapping to LL interrupt handling, since it's no longer naturally compatible with the timer_intr_t");
  25. _Static_assert(TIMER_INTR_WDT == TIMG_WDT_INT_CLR, "Add mapping to LL interrupt handling, since it's no longer naturally compatible with the timer_intr_t");
  26. // Get timer group instance with giving group number
  27. #define TIMER_LL_GET_HW(num) ((num == 0) ? (&TIMERG0) : (&TIMERG1))
  28. /**
  29. * @brief Set timer clock prescale value
  30. *
  31. * @param hw Beginning address of the peripheral registers.
  32. * @param timer_num The timer number
  33. * @param divider Prescale value (0 is not valid)
  34. *
  35. * @return None
  36. */
  37. static inline void timer_ll_set_divider(timg_dev_t *hw, timer_idx_t timer_num, uint32_t divider)
  38. {
  39. // refer to TRM 12.2.1
  40. if (divider == 65536) {
  41. divider = 0;
  42. }
  43. int timer_en = hw->hw_timer[timer_num].config.enable;
  44. hw->hw_timer[timer_num].config.enable = 0;
  45. hw->hw_timer[timer_num].config.divider = divider;
  46. hw->hw_timer[timer_num].config.enable = timer_en;
  47. }
  48. /**
  49. * @brief Get timer clock prescale value
  50. *
  51. * @param hw Beginning address of the peripheral registers.
  52. * @param timer_num The timer number
  53. * @param divider Pointer to accept the prescale value
  54. *
  55. * @return None
  56. */
  57. static inline void timer_ll_get_divider(timg_dev_t *hw, timer_idx_t timer_num, uint32_t *divider)
  58. {
  59. uint32_t d = hw->hw_timer[timer_num].config.divider;
  60. if (d == 0) {
  61. d = 65536;
  62. }
  63. *divider = d;
  64. }
  65. /**
  66. * @brief Load counter value into time-base counter
  67. *
  68. * @param hw Beginning address of the peripheral registers.
  69. * @param timer_num The timer number
  70. * @param load_val Counter value
  71. *
  72. * @return None
  73. */
  74. static inline void timer_ll_set_counter_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t load_val)
  75. {
  76. hw->hw_timer[timer_num].load_high = (uint32_t) (load_val >> 32);
  77. hw->hw_timer[timer_num].load_low = (uint32_t) load_val;
  78. hw->hw_timer[timer_num].reload = 1;
  79. }
  80. /**
  81. * @brief Get counter value from time-base counter
  82. *
  83. * @param hw Beginning address of the peripheral registers.
  84. * @param timer_num The timer number
  85. * @param timer_val Pointer to accept the counter value
  86. *
  87. * @return None
  88. */
  89. FORCE_INLINE_ATTR void timer_ll_get_counter_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t *timer_val)
  90. {
  91. hw->hw_timer[timer_num].update.update = 1;
  92. *timer_val = ((uint64_t) hw->hw_timer[timer_num].cnt_high << 32) | (hw->hw_timer[timer_num].cnt_low);
  93. }
  94. /**
  95. * @brief Set counter mode, include increment mode and decrement mode.
  96. *
  97. * @param hw Beginning address of the peripheral registers.
  98. * @param timer_num The timer number
  99. * @param increase_en True to increment mode, fasle to decrement mode
  100. *
  101. * @return None
  102. */
  103. static inline void timer_ll_set_counter_increase(timg_dev_t *hw, timer_idx_t timer_num, bool increase_en)
  104. {
  105. hw->hw_timer[timer_num].config.increase = increase_en;
  106. }
  107. /**
  108. * @brief Get counter mode, include increment mode and decrement mode.
  109. *
  110. * @param hw Beginning address of the peripheral registers.
  111. * @param timer_num The timer number
  112. *
  113. * @return
  114. * - true Increment mode
  115. * - false Decrement mode
  116. */
  117. static inline bool timer_ll_get_counter_increase(timg_dev_t *hw, timer_idx_t timer_num)
  118. {
  119. return hw->hw_timer[timer_num].config.increase;
  120. }
  121. /**
  122. * @brief Set counter status, enable or disable counter.
  123. *
  124. * @param hw Beginning address of the peripheral registers.
  125. * @param timer_num The timer number
  126. * @param counter_en True to enable counter, false to disable counter
  127. *
  128. * @return None
  129. */
  130. FORCE_INLINE_ATTR void timer_ll_set_counter_enable(timg_dev_t *hw, timer_idx_t timer_num, bool counter_en)
  131. {
  132. hw->hw_timer[timer_num].config.enable = counter_en;
  133. }
  134. /**
  135. * @brief Get counter status.
  136. *
  137. * @param hw Beginning address of the peripheral registers.
  138. * @param timer_num The timer number
  139. *
  140. * @return
  141. * - true Enable counter
  142. * - false Disable conuter
  143. */
  144. static inline bool timer_ll_get_counter_enable(timg_dev_t *hw, timer_idx_t timer_num)
  145. {
  146. return hw->hw_timer[timer_num].config.enable;
  147. }
  148. /**
  149. * @brief Set auto reload mode.
  150. *
  151. * @param hw Beginning address of the peripheral registers.
  152. * @param timer_num The timer number
  153. * @param auto_reload_en True to enable auto reload mode, flase to disable auto reload mode
  154. *
  155. * @return None
  156. */
  157. static inline void timer_ll_set_auto_reload(timg_dev_t *hw, timer_idx_t timer_num, bool auto_reload_en)
  158. {
  159. hw->hw_timer[timer_num].config.autoreload = auto_reload_en;
  160. }
  161. /**
  162. * @brief Get auto reload mode.
  163. *
  164. * @param hw Beginning address of the peripheral registers.
  165. * @param timer_num The timer number
  166. *
  167. * @return
  168. * - true Enable auto reload mode
  169. * - false Disable auto reload mode
  170. */
  171. FORCE_INLINE_ATTR bool timer_ll_get_auto_reload(timg_dev_t *hw, timer_idx_t timer_num)
  172. {
  173. return hw->hw_timer[timer_num].config.autoreload;
  174. }
  175. /**
  176. * @brief Set the counter value to trigger the alarm.
  177. *
  178. * @param hw Beginning address of the peripheral registers.
  179. * @param timer_num The timer number
  180. * @param alarm_value Counter value to trigger the alarm
  181. *
  182. * @return None
  183. */
  184. FORCE_INLINE_ATTR void timer_ll_set_alarm_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t alarm_value)
  185. {
  186. hw->hw_timer[timer_num].alarm_high = (uint32_t) (alarm_value >> 32);
  187. hw->hw_timer[timer_num].alarm_low = (uint32_t) alarm_value;
  188. }
  189. /**
  190. * @brief Get the counter value to trigger the alarm.
  191. *
  192. * @param hw Beginning address of the peripheral registers.
  193. * @param timer_num The timer number
  194. * @param alarm_value Pointer to accept the counter value to trigger the alarm
  195. *
  196. * @return None
  197. */
  198. static inline void timer_ll_get_alarm_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t *alarm_value)
  199. {
  200. *alarm_value = ((uint64_t) hw->hw_timer[timer_num].alarm_high << 32) | (hw->hw_timer[timer_num].alarm_low);
  201. }
  202. /**
  203. * @brief Set the alarm status, enable or disable the alarm.
  204. *
  205. * @param hw Beginning address of the peripheral registers.
  206. * @param timer_num The timer number
  207. * @param alarm_en True to enable alarm, false to disable alarm
  208. *
  209. * @return None
  210. */
  211. FORCE_INLINE_ATTR void timer_ll_set_alarm_enable(timg_dev_t *hw, timer_idx_t timer_num, bool alarm_en)
  212. {
  213. hw->hw_timer[timer_num].config.alarm_en = alarm_en;
  214. }
  215. /**
  216. * @brief Get the alarm status.
  217. *
  218. * @param hw Beginning address of the peripheral registers.
  219. * @param timer_num The timer number
  220. *
  221. * @return
  222. * - true Enable alarm
  223. * - false Disable alarm
  224. */
  225. static inline bool timer_ll_get_alarm_enable(timg_dev_t *hw, timer_idx_t timer_num)
  226. {
  227. return hw->hw_timer[timer_num].config.alarm_en;
  228. }
  229. /**
  230. * @brief Enable timer interrupt.
  231. *
  232. * @param hw Beginning address of the peripheral registers.
  233. * @param timer_num The timer number
  234. *
  235. * @return None
  236. */
  237. FORCE_INLINE_ATTR void timer_ll_intr_enable(timg_dev_t *hw, timer_idx_t timer_num)
  238. {
  239. hw->int_ena.val |= BIT(timer_num);
  240. hw->hw_timer[timer_num].config.level_int_en = 1;
  241. }
  242. /**
  243. * @brief Disable timer interrupt.
  244. *
  245. * @param hw Beginning address of the peripheral registers.
  246. * @param timer_num The timer number
  247. *
  248. * @return None
  249. */
  250. FORCE_INLINE_ATTR void timer_ll_intr_disable(timg_dev_t *hw, timer_idx_t timer_num)
  251. {
  252. hw->int_ena.val &= (~BIT(timer_num));
  253. hw->hw_timer[timer_num].config.level_int_en = 0;
  254. }
  255. /**
  256. * @brief Disable timer interrupt.
  257. *
  258. * @param hw Beginning address of the peripheral registers.
  259. * @param timer_num The timer number
  260. *
  261. * @return None
  262. */
  263. FORCE_INLINE_ATTR void timer_ll_clear_intr_status(timg_dev_t *hw, timer_idx_t timer_num)
  264. {
  265. hw->int_clr.val |= BIT(timer_num);
  266. }
  267. /**
  268. * @brief Get interrupt status.
  269. *
  270. * @param hw Beginning address of the peripheral registers.
  271. * @param intr_status Interrupt status
  272. *
  273. * @return None
  274. */
  275. FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_status)
  276. {
  277. *intr_status = hw->int_st.val;
  278. }
  279. /**
  280. * @brief Get interrupt raw status.
  281. *
  282. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  283. * @param intr_raw_status Interrupt raw status
  284. *
  285. * @return None
  286. */
  287. FORCE_INLINE_ATTR void timer_ll_get_intr_raw_status(timer_group_t group_num, uint32_t *intr_raw_status)
  288. {
  289. timg_dev_t *hw = TIMER_LL_GET_HW(group_num);
  290. *intr_raw_status = hw->int_raw.val;
  291. }
  292. /**
  293. * @brief Set the level interrupt status, enable or disable the level interrupt.
  294. *
  295. * @param hw Beginning address of the peripheral registers.
  296. * @param timer_num The timer number
  297. * @param level_int_en True to enable level interrupt, false to disable level interrupt
  298. *
  299. * @return None
  300. */
  301. static inline void timer_ll_set_level_int_enable(timg_dev_t *hw, timer_idx_t timer_num, bool level_int_en)
  302. {
  303. hw->hw_timer[timer_num].config.level_int_en = level_int_en;
  304. }
  305. /**
  306. * @brief Get the level interrupt status.
  307. *
  308. * @param hw Beginning address of the peripheral registers.
  309. * @param timer_num The timer number
  310. *
  311. * @return
  312. * - true Enable level interrupt
  313. * - false Disable level interrupt
  314. */
  315. static inline bool timer_ll_get_level_int_enable(timg_dev_t *hw, timer_idx_t timer_num)
  316. {
  317. return hw->hw_timer[timer_num].config.level_int_en;
  318. }
  319. /**
  320. * @brief Set the edge interrupt status, enable or disable the edge interrupt.
  321. *
  322. * @param hw Beginning address of the peripheral registers.
  323. * @param timer_num The timer number
  324. * @param edge_int_en True to enable edge interrupt, false to disable edge interrupt
  325. *
  326. * @return None
  327. */
  328. static inline void timer_ll_set_edge_int_enable(timg_dev_t *hw, timer_idx_t timer_num, bool edge_int_en)
  329. {
  330. hw->hw_timer[timer_num].config.edge_int_en = edge_int_en;
  331. }
  332. /**
  333. * @brief Get the edge interrupt status.
  334. *
  335. * @param hw Beginning address of the peripheral registers.
  336. * @param timer_num The timer number
  337. *
  338. * @return
  339. * - true Enable edge interrupt
  340. * - false Disable edge interrupt
  341. */
  342. static inline bool timer_ll_get_edge_int_enable(timg_dev_t *hw, timer_idx_t timer_num)
  343. {
  344. return hw->hw_timer[timer_num].config.edge_int_en;
  345. }
  346. /**
  347. * @brief Get interrupt status register address.
  348. *
  349. * @param hw Beginning address of the peripheral registers.
  350. *
  351. * @return uint32_t Interrupt status register address
  352. */
  353. static inline uint32_t timer_ll_get_intr_status_reg(timg_dev_t *hw)
  354. {
  355. return (uint32_t) & (hw->int_st.val);
  356. }
  357. static inline uint32_t timer_ll_get_intr_mask_bit(timg_dev_t *hw, timer_idx_t timer_num)
  358. {
  359. return (1U << timer_num);
  360. }
  361. /**
  362. * @brief Set clock source.
  363. *
  364. * @param hal Context of the HAL layer
  365. * @param use_xtal_en True to use XTAL clock, flase to use APB clock
  366. *
  367. * @return None
  368. */
  369. static inline void timer_ll_set_use_xtal(timg_dev_t *hw, timer_idx_t timer_num, bool use_xtal_en)
  370. {
  371. hw->hw_timer[timer_num].config.use_xtal = use_xtal_en;
  372. }
  373. /**
  374. * @brief Get clock source.
  375. *
  376. * @param hal Context of the HAL layer
  377. *
  378. * @return
  379. * - true Use XTAL clock
  380. * - false Use APB clock
  381. */
  382. static inline bool timer_ll_get_use_xtal(timg_dev_t *hw, timer_idx_t timer_num)
  383. {
  384. return hw->hw_timer[timer_num].config.use_xtal;
  385. }
  386. /* WDT operations */
  387. /**
  388. * @brief Unlock/lock the WDT register in case of mis-operations.
  389. *
  390. * @param hw Beginning address of the peripheral registers.
  391. * @param protect true to lock, false to unlock before operations.
  392. */
  393. FORCE_INLINE_ATTR void timer_ll_wdt_set_protect(timg_dev_t* hw, bool protect)
  394. {
  395. hw->wdt_wprotect=(protect? 0: TIMG_WDT_WKEY_VALUE);
  396. }
  397. /**
  398. * @brief Initialize WDT.
  399. *
  400. * @param hw Beginning address of the peripheral registers.
  401. *
  402. * @note Call `timer_ll_wdt_set_protect` first
  403. */
  404. FORCE_INLINE_ATTR void timer_ll_wdt_init(timg_dev_t* hw)
  405. {
  406. hw->wdt_config0.sys_reset_length=7; //3.2uS
  407. hw->wdt_config0.cpu_reset_length=7; //3.2uS
  408. //currently only level interrupt is supported
  409. hw->wdt_config0.level_int_en = 1;
  410. hw->wdt_config0.edge_int_en = 0;
  411. }
  412. /**
  413. * @brief Set the WDT tick time.
  414. *
  415. * @param hw Beginning address of the peripheral registers.
  416. * @param tick_time_us Tick time.
  417. */
  418. FORCE_INLINE_ATTR void timer_ll_wdt_set_tick(timg_dev_t* hw, int tick_time_us)
  419. {
  420. hw->wdt_config1.clk_prescale=80*tick_time_us;
  421. }
  422. /**
  423. * @brief Feed the WDT.
  424. *
  425. * @param hw Beginning address of the peripheral registers.
  426. */
  427. FORCE_INLINE_ATTR void timer_ll_wdt_feed(timg_dev_t* hw)
  428. {
  429. hw->wdt_feed = 1;
  430. }
  431. /**
  432. * @brief Set the WDT timeout.
  433. *
  434. * @param hw Beginning address of the peripheral registers.
  435. * @param stage Stage number of WDT.
  436. * @param timeout_Tick tick threshold of timeout.
  437. */
  438. FORCE_INLINE_ATTR void timer_ll_wdt_set_timeout(timg_dev_t* hw, int stage, uint32_t timeout_tick)
  439. {
  440. switch (stage) {
  441. case 0:
  442. hw->wdt_config2=timeout_tick;
  443. break;
  444. case 1:
  445. hw->wdt_config3=timeout_tick;
  446. break;
  447. case 2:
  448. hw->wdt_config4=timeout_tick;
  449. break;
  450. case 3:
  451. hw->wdt_config5=timeout_tick;
  452. break;
  453. default:
  454. abort();
  455. }
  456. }
  457. _Static_assert(TIMER_WDT_OFF == TIMG_WDT_STG_SEL_OFF, "Add mapping to LL watchdog timeout behavior, since it's no longer naturally compatible with the timer_wdt_behavior_t");
  458. _Static_assert(TIMER_WDT_INT == TIMG_WDT_STG_SEL_INT, "Add mapping to LL watchdog timeout behavior, since it's no longer naturally compatible with the timer_wdt_behavior_t");
  459. _Static_assert(TIMER_WDT_RESET_CPU == TIMG_WDT_STG_SEL_RESET_CPU, "Add mapping to LL watchdog timeout behavior, since it's no longer naturally compatible with the timer_wdt_behavior_t");
  460. _Static_assert(TIMER_WDT_RESET_SYSTEM == TIMG_WDT_STG_SEL_RESET_SYSTEM, "Add mapping to LL watchdog timeout behavior, since it's no longer naturally compatible with the timer_wdt_behavior_t");
  461. /**
  462. * @brief Set the WDT timeout behavior.
  463. *
  464. * @param hw Beginning address of the peripheral registers.
  465. * @param stage Stage number of WDT.
  466. * @param behavior Behavior of WDT, please see enum timer_wdt_behavior_t.
  467. */
  468. FORCE_INLINE_ATTR void timer_ll_wdt_set_timeout_behavior(timg_dev_t* hw, int stage, timer_wdt_behavior_t behavior)
  469. {
  470. switch (stage) {
  471. case 0:
  472. hw->wdt_config0.stg0 = behavior;
  473. break;
  474. case 1:
  475. hw->wdt_config0.stg1 = behavior;
  476. break;
  477. case 2:
  478. hw->wdt_config0.stg2 = behavior;
  479. break;
  480. case 3:
  481. hw->wdt_config0.stg3 = behavior;
  482. break;
  483. default:
  484. abort();
  485. }
  486. }
  487. /**
  488. * @brief Enable/Disable the WDT enable.
  489. *
  490. * @param hw Beginning address of the peripheral registers.
  491. * @param enable True to enable WDT, false to disable WDT.
  492. */
  493. FORCE_INLINE_ATTR void timer_ll_wdt_set_enable(timg_dev_t* hw, bool enable)
  494. {
  495. hw->wdt_config0.en = enable;
  496. }
  497. /**
  498. * @brief Enable/Disable the WDT flashboot mode.
  499. *
  500. * @param hw Beginning address of the peripheral registers.
  501. * @param enable True to enable WDT flashboot mode, false to disable WDT flashboot mode.
  502. */
  503. FORCE_INLINE_ATTR void timer_ll_wdt_flashboot_en(timg_dev_t* hw, bool enable)
  504. {
  505. hw->wdt_config0.flashboot_mod_en = enable;
  506. }
  507. /**
  508. * @brief Clear the WDT interrupt status.
  509. *
  510. * @param hw Beginning address of the peripheral registers.
  511. */
  512. FORCE_INLINE_ATTR void timer_ll_wdt_clear_intr_status(timg_dev_t* hw)
  513. {
  514. hw->int_clr.wdt = 1;
  515. }
  516. /**
  517. * @brief Enable the WDT interrupt.
  518. *
  519. * @param hw Beginning address of the peripheral registers.
  520. */
  521. FORCE_INLINE_ATTR void timer_ll_wdt_enable_intr(timg_dev_t* hw)
  522. {
  523. hw->int_ena.wdt = 1;
  524. }
  525. #ifdef __cplusplus
  526. }
  527. #endif