timer_ll.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "hal/timer_types.h"
  21. #include "soc/timer_periph.h"
  22. //Helper macro to get corresponding interrupt of a timer
  23. #define TIMER_LL_GET_INTR(TIMER_IDX) ((TIMER_IDX)==TIMER_0? TIMER_INTR_T0: TIMER_INTR_T1)
  24. #define TIMER_LL_GET_HW(TIMER_GROUP) ((TIMER_GROUP)==0? &TIMERG0: &TIMERG1)
  25. _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");
  26. _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");
  27. _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");
  28. /**
  29. * @brief Enable timer interrupt.
  30. *
  31. * @param hw Beginning address of the peripheral registers.
  32. * @param intr_mask Interrupt enable mask
  33. *
  34. * @return None
  35. */
  36. static inline void timer_ll_intr_enable(timg_dev_t *hw, timer_intr_t intr_mask)
  37. {
  38. hw->int_ena.val |= intr_mask;
  39. }
  40. /**
  41. * @brief Disable timer interrupt.
  42. *
  43. * @param hw Beginning address of the peripheral registers.
  44. * @param intr_mask Interrupt disable mask
  45. *
  46. * @return None
  47. */
  48. static inline void timer_ll_intr_disable(timg_dev_t *hw, timer_intr_t intr_mask)
  49. {
  50. hw->int_ena.val &= (~intr_mask);
  51. }
  52. /**
  53. * @brief Get timer interrupt status.
  54. *
  55. * @param hw Beginning address of the peripheral registers.
  56. *
  57. * @return Masked interrupt status
  58. */
  59. static inline timer_intr_t timer_ll_intr_status_get(timg_dev_t *hw)
  60. {
  61. return hw->int_raw.val;
  62. }
  63. /**
  64. * @brief Clear timer interrupt.
  65. *
  66. * @param hw Beginning address of the peripheral registers.
  67. * @param intr_mask Interrupt mask to clear
  68. *
  69. * @return None
  70. */
  71. static inline void timer_ll_intr_status_clear(timg_dev_t *hw, timer_intr_t intr_mask)
  72. {
  73. hw->int_clr_timers.val = intr_mask;
  74. }
  75. /**
  76. * @brief Get counter vaule from time-base counter
  77. *
  78. * @param hw Beginning address of the peripheral registers.
  79. * @param timer_num The timer number
  80. * @param timer_val Pointer to accept the counter value
  81. *
  82. * @return None
  83. */
  84. static inline void timer_ll_get_counter_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t *timer_val)
  85. {
  86. hw->hw_timer[timer_num].update = 1;
  87. *timer_val = ((uint64_t) hw->hw_timer[timer_num].cnt_high << 32) | (hw->hw_timer[timer_num].cnt_low);
  88. }
  89. /**
  90. * @brief Set counter status, enable or disable counter.
  91. *
  92. * @param hw Beginning address of the peripheral registers.
  93. * @param timer_num The timer number
  94. * @param counter_en Counter enable status
  95. *
  96. * @return None
  97. */
  98. static inline void timer_ll_set_counter_enable(timg_dev_t *hw, timer_idx_t timer_num, timer_start_t counter_en)
  99. {
  100. hw->hw_timer[timer_num].config.enable = counter_en;
  101. }
  102. /**
  103. * @brief Get auto reload mode.
  104. *
  105. * @param hw Beginning address of the peripheral registers.
  106. * @param timer_num The timer number
  107. * @param reload Pointer to accept the auto reload mode
  108. *
  109. * @return None
  110. */
  111. static inline bool timer_ll_get_auto_reload(timg_dev_t *hw, timer_idx_t timer_num)
  112. {
  113. return hw->hw_timer[timer_num].config.autoreload;
  114. }
  115. /**
  116. * @brief Set the counter value to trigger the alarm.
  117. *
  118. * @param hw Beginning address of the peripheral registers.
  119. * @param timer_num The timer number
  120. * @param alarm_value Counter value to trigger the alarm
  121. *
  122. * @return None
  123. */
  124. static inline void timer_ll_set_alarm_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t alarm_value)
  125. {
  126. hw->hw_timer[timer_num].alarm_high = (uint32_t) (alarm_value >> 32);
  127. hw->hw_timer[timer_num].alarm_low = (uint32_t) alarm_value;
  128. }
  129. /**
  130. * @brief Get the counter value to trigger the alarm.
  131. *
  132. * @param hw Beginning address of the peripheral registers.
  133. * @param timer_num The timer number
  134. * @param alarm_value Pointer to accept the counter value to trigger the alarm
  135. *
  136. * @return None
  137. */
  138. static inline void timer_ll_get_alarm_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t *alarm_value)
  139. {
  140. *alarm_value = ((uint64_t) hw->hw_timer[timer_num].alarm_high << 32) | (hw->hw_timer[timer_num].alarm_low);
  141. }
  142. /**
  143. * @brief Set the alarm status, enable or disable the alarm.
  144. *
  145. * @param hw Beginning address of the peripheral registers.
  146. * @param timer_num The timer number
  147. * @param alarm_en true to enable, false to disable
  148. *
  149. * @return None
  150. */
  151. static inline void timer_ll_set_alarm_enable(timg_dev_t *hw, timer_idx_t timer_num, bool alarm_en)
  152. {
  153. hw->hw_timer[timer_num].config.alarm_en = alarm_en;
  154. }
  155. /**
  156. * @brief Get the alarm status.
  157. *
  158. * @param hw Beginning address of the peripheral registers.
  159. * @param timer_num The timer number
  160. * @param alarm_en Pointer to accept the alarm status
  161. *
  162. * @return None
  163. */
  164. static inline void timer_ll_get_alarm_enable(timg_dev_t *hw, timer_idx_t timer_num, bool *alarm_en)
  165. {
  166. *alarm_en = hw->hw_timer[timer_num].config.alarm_en;
  167. }
  168. /* WDT operations */
  169. /**
  170. * Unlock/lock the WDT register in case of mis-operations.
  171. *
  172. * @param hw Beginning address of the peripheral registers.
  173. * @param protect true to lock, false to unlock before operations.
  174. */
  175. FORCE_INLINE_ATTR void timer_ll_wdt_set_protect(timg_dev_t* hw, bool protect)
  176. {
  177. hw->wdt_wprotect=(protect? 0: TIMG_WDT_WKEY_VALUE);
  178. }
  179. /**
  180. * Initialize WDT.
  181. *
  182. * @param hw Beginning address of the peripheral registers.
  183. *
  184. * @note Call ``timer_ll_wdt_set_protect first``
  185. */
  186. FORCE_INLINE_ATTR void timer_ll_wdt_init(timg_dev_t* hw)
  187. {
  188. hw->wdt_config0.sys_reset_length=7; //3.2uS
  189. hw->wdt_config0.cpu_reset_length=7; //3.2uS
  190. //currently only level interrupt is supported
  191. hw->wdt_config0.level_int_en = 1;
  192. hw->wdt_config0.edge_int_en = 0;
  193. }
  194. FORCE_INLINE_ATTR void timer_ll_wdt_set_tick(timg_dev_t* hw, int tick_time_us)
  195. {
  196. hw->wdt_config1.clk_prescale=80*tick_time_us;
  197. }
  198. FORCE_INLINE_ATTR void timer_ll_wdt_feed(timg_dev_t* hw)
  199. {
  200. hw->wdt_feed = 1;
  201. }
  202. FORCE_INLINE_ATTR void timer_ll_wdt_set_timeout(timg_dev_t* hw, int stage, uint32_t timeout_tick)
  203. {
  204. switch (stage) {
  205. case 0:
  206. hw->wdt_config2=timeout_tick;
  207. break;
  208. case 1:
  209. hw->wdt_config3=timeout_tick;
  210. break;
  211. case 2:
  212. hw->wdt_config4=timeout_tick;
  213. break;
  214. case 3:
  215. hw->wdt_config5=timeout_tick;
  216. break;
  217. default:
  218. abort();
  219. }
  220. }
  221. _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");
  222. _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");
  223. _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");
  224. _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");
  225. FORCE_INLINE_ATTR void timer_ll_wdt_set_timeout_behavior(timg_dev_t* hw, int stage, timer_wdt_behavior_t behavior)
  226. {
  227. switch (stage) {
  228. case 0:
  229. hw->wdt_config0.stg0 = behavior;
  230. break;
  231. case 1:
  232. hw->wdt_config0.stg1 = behavior;
  233. break;
  234. case 2:
  235. hw->wdt_config0.stg2 = behavior;
  236. break;
  237. case 3:
  238. hw->wdt_config0.stg3 = behavior;
  239. break;
  240. default:
  241. abort();
  242. }
  243. }
  244. FORCE_INLINE_ATTR void timer_ll_wdt_set_enable(timg_dev_t* hw, bool enable)
  245. {
  246. hw->wdt_config0.en = enable;
  247. }
  248. FORCE_INLINE_ATTR void timer_ll_wdt_flashboot_en(timg_dev_t* hw, bool enable)
  249. {
  250. hw->wdt_config0.flashboot_mod_en = enable;
  251. }
  252. #ifdef __cplusplus
  253. }
  254. #endif