timer_ll.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 and 1 are 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 18.2.1
  40. if (divider == 65536) {
  41. divider = 0;
  42. } else if (divider == 1) {
  43. divider = 2;
  44. }
  45. int timer_en = hw->hw_timer[timer_num].config.enable;
  46. hw->hw_timer[timer_num].config.enable = 0;
  47. hw->hw_timer[timer_num].config.divider = divider;
  48. hw->hw_timer[timer_num].config.enable = timer_en;
  49. }
  50. /**
  51. * @brief Get timer clock prescale value
  52. *
  53. * @param hw Beginning address of the peripheral registers.
  54. * @param timer_num The timer number
  55. * @param divider Pointer to accept the prescale value
  56. *
  57. * @return None
  58. */
  59. static inline void timer_ll_get_divider(timg_dev_t *hw, timer_idx_t timer_num, uint32_t *divider)
  60. {
  61. uint32_t d = hw->hw_timer[timer_num].config.divider;
  62. if (d == 0) {
  63. d = 65536;
  64. } else if (d == 1) {
  65. d = 2;
  66. }
  67. *divider = d;
  68. }
  69. /**
  70. * @brief Load counter value into time-base counter
  71. *
  72. * @param hw Beginning address of the peripheral registers.
  73. * @param timer_num The timer number
  74. * @param load_val Counter value
  75. *
  76. * @return None
  77. */
  78. static inline void timer_ll_set_counter_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t load_val)
  79. {
  80. hw->hw_timer[timer_num].load_high = (uint32_t) (load_val >> 32);
  81. hw->hw_timer[timer_num].load_low = (uint32_t) load_val;
  82. hw->hw_timer[timer_num].reload = 1;
  83. }
  84. /**
  85. * @brief Get counter value from time-base counter
  86. *
  87. * @param hw Beginning address of the peripheral registers.
  88. * @param timer_num The timer number
  89. * @param timer_val Pointer to accept the counter value
  90. *
  91. * @return None
  92. */
  93. FORCE_INLINE_ATTR void timer_ll_get_counter_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t *timer_val)
  94. {
  95. hw->hw_timer[timer_num].update = 1;
  96. while (hw->hw_timer[timer_num].update) {}
  97. *timer_val = ((uint64_t) hw->hw_timer[timer_num].cnt_high << 32) | (hw->hw_timer[timer_num].cnt_low);
  98. }
  99. /**
  100. * @brief Set counter mode, include increment mode and decrement mode.
  101. *
  102. * @param hw Beginning address of the peripheral registers.
  103. * @param timer_num The timer number
  104. * @param increase_en True to increment mode, fasle to decrement mode
  105. *
  106. * @return None
  107. */
  108. static inline void timer_ll_set_counter_increase(timg_dev_t *hw, timer_idx_t timer_num, bool increase_en)
  109. {
  110. hw->hw_timer[timer_num].config.increase = increase_en;
  111. }
  112. /**
  113. * @brief Get counter mode, include increment mode and decrement mode.
  114. *
  115. * @param hw Beginning address of the peripheral registers.
  116. * @param timer_num The timer number
  117. *
  118. * @return
  119. * - true Increment mode
  120. * - false Decrement mode
  121. */
  122. static inline bool timer_ll_get_counter_increase(timg_dev_t *hw, timer_idx_t timer_num)
  123. {
  124. return hw->hw_timer[timer_num].config.increase;
  125. }
  126. /**
  127. * @brief Set counter status, enable or disable counter.
  128. *
  129. * @param hw Beginning address of the peripheral registers.
  130. * @param timer_num The timer number
  131. * @param counter_en True to enable counter, false to disable counter
  132. *
  133. * @return None
  134. */
  135. FORCE_INLINE_ATTR void timer_ll_set_counter_enable(timg_dev_t *hw, timer_idx_t timer_num, bool counter_en)
  136. {
  137. hw->hw_timer[timer_num].config.enable = counter_en;
  138. }
  139. /**
  140. * @brief Get counter status.
  141. *
  142. * @param hw Beginning address of the peripheral registers.
  143. * @param timer_num The timer number
  144. *
  145. * @return
  146. * - true Enable counter
  147. * - false Disable conuter
  148. */
  149. static inline bool timer_ll_get_counter_enable(timg_dev_t *hw, timer_idx_t timer_num)
  150. {
  151. return hw->hw_timer[timer_num].config.enable;
  152. }
  153. /**
  154. * @brief Set auto reload mode.
  155. *
  156. * @param hw Beginning address of the peripheral registers.
  157. * @param timer_num The timer number
  158. * @param auto_reload_en True to enable auto reload mode, flase to disable auto reload mode
  159. *
  160. * @return None
  161. */
  162. static inline void timer_ll_set_auto_reload(timg_dev_t *hw, timer_idx_t timer_num, bool auto_reload_en)
  163. {
  164. hw->hw_timer[timer_num].config.autoreload = auto_reload_en;
  165. }
  166. /**
  167. * @brief Get auto reload mode.
  168. *
  169. * @param hw Beginning address of the peripheral registers.
  170. * @param timer_num The timer number
  171. *
  172. * @return
  173. * - true Enable auto reload mode
  174. * - false Disable auto reload mode
  175. */
  176. FORCE_INLINE_ATTR bool timer_ll_get_auto_reload(timg_dev_t *hw, timer_idx_t timer_num)
  177. {
  178. return hw->hw_timer[timer_num].config.autoreload;
  179. }
  180. /**
  181. * @brief Set the counter value to trigger the alarm.
  182. *
  183. * @param hw Beginning address of the peripheral registers.
  184. * @param timer_num The timer number
  185. * @param alarm_value Counter value to trigger the alarm
  186. *
  187. * @return None
  188. */
  189. FORCE_INLINE_ATTR void timer_ll_set_alarm_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t alarm_value)
  190. {
  191. hw->hw_timer[timer_num].alarm_high = (uint32_t) (alarm_value >> 32);
  192. hw->hw_timer[timer_num].alarm_low = (uint32_t) alarm_value;
  193. }
  194. /**
  195. * @brief Get the counter value to trigger the alarm.
  196. *
  197. * @param hw Beginning address of the peripheral registers.
  198. * @param timer_num The timer number
  199. * @param alarm_value Pointer to accept the counter value to trigger the alarm
  200. *
  201. * @return None
  202. */
  203. static inline void timer_ll_get_alarm_value(timg_dev_t *hw, timer_idx_t timer_num, uint64_t *alarm_value)
  204. {
  205. *alarm_value = ((uint64_t) hw->hw_timer[timer_num].alarm_high << 32) | (hw->hw_timer[timer_num].alarm_low);
  206. }
  207. /**
  208. * @brief Set the alarm status, enable or disable the alarm.
  209. *
  210. * @param hw Beginning address of the peripheral registers.
  211. * @param timer_num The timer number
  212. * @param alarm_en True to enable alarm, false to disable alarm
  213. *
  214. * @return None
  215. */
  216. FORCE_INLINE_ATTR void timer_ll_set_alarm_enable(timg_dev_t *hw, timer_idx_t timer_num, bool alarm_en)
  217. {
  218. hw->hw_timer[timer_num].config.alarm_en = alarm_en;
  219. }
  220. /**
  221. * @brief Get the alarm status.
  222. *
  223. * @param hw Beginning address of the peripheral registers.
  224. * @param timer_num The timer number
  225. *
  226. * @return
  227. * - true Enable alarm
  228. * - false Disable alarm
  229. */
  230. static inline bool timer_ll_get_alarm_enable(timg_dev_t *hw, timer_idx_t timer_num)
  231. {
  232. return hw->hw_timer[timer_num].config.alarm_en;
  233. }
  234. /**
  235. * @brief Enable timer interrupt.
  236. *
  237. * @param hw Beginning address of the peripheral registers.
  238. * @param timer_num The timer number
  239. *
  240. * @return None
  241. */
  242. FORCE_INLINE_ATTR void timer_ll_intr_enable(timg_dev_t *hw, timer_idx_t timer_num)
  243. {
  244. hw->int_ena.val |= BIT(timer_num);
  245. hw->hw_timer[timer_num].config.level_int_en = 1;
  246. }
  247. /**
  248. * @brief Disable timer interrupt.
  249. *
  250. * @param hw Beginning address of the peripheral registers.
  251. * @param timer_num The timer number
  252. *
  253. * @return None
  254. */
  255. FORCE_INLINE_ATTR void timer_ll_intr_disable(timg_dev_t *hw, timer_idx_t timer_num)
  256. {
  257. hw->int_ena.val &= (~BIT(timer_num));
  258. hw->hw_timer[timer_num].config.level_int_en = 0;
  259. }
  260. /**
  261. * @brief Disable timer interrupt.
  262. *
  263. * @param hw Beginning address of the peripheral registers.
  264. * @param timer_num The timer number
  265. *
  266. * @return None
  267. */
  268. FORCE_INLINE_ATTR void timer_ll_clear_intr_status(timg_dev_t *hw, timer_idx_t timer_num)
  269. {
  270. hw->int_clr_timers.val |= BIT(timer_num);
  271. }
  272. /**
  273. * @brief Get interrupt status.
  274. *
  275. * @param hw Beginning address of the peripheral registers.
  276. * @param intr_status Interrupt status
  277. *
  278. * @return None
  279. */
  280. FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_status)
  281. {
  282. *intr_status = hw->int_st_timers.val & 0x03;
  283. }
  284. /**
  285. * @brief Get interrupt raw status.
  286. *
  287. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  288. * @param intr_raw_status Interrupt raw status
  289. *
  290. * @return None
  291. */
  292. FORCE_INLINE_ATTR void timer_ll_get_intr_raw_status(timer_group_t group_num, uint32_t *intr_raw_status)
  293. {
  294. timg_dev_t *hw = TIMER_LL_GET_HW(group_num);
  295. *intr_raw_status = hw->int_raw.val & 0x03;
  296. }
  297. /**
  298. * @brief Set the level interrupt status, enable or disable the level interrupt.
  299. *
  300. * @param hw Beginning address of the peripheral registers.
  301. * @param timer_num The timer number
  302. * @param level_int_en True to enable level interrupt, false to disable level interrupt
  303. *
  304. * @return None
  305. */
  306. static inline void timer_ll_set_level_int_enable(timg_dev_t *hw, timer_idx_t timer_num, bool level_int_en)
  307. {
  308. hw->hw_timer[timer_num].config.level_int_en = level_int_en;
  309. }
  310. /**
  311. * @brief Get the level interrupt status.
  312. *
  313. * @param hw Beginning address of the peripheral registers.
  314. * @param timer_num The timer number
  315. *
  316. * @return
  317. * - true Enable level interrupt
  318. * - false Disable level interrupt
  319. */
  320. static inline bool timer_ll_get_level_int_enable(timg_dev_t *hw, timer_idx_t timer_num)
  321. {
  322. return hw->hw_timer[timer_num].config.level_int_en;
  323. }
  324. /**
  325. * @brief Set the edge interrupt status, enable or disable the edge interrupt.
  326. *
  327. * @param hw Beginning address of the peripheral registers.
  328. * @param timer_num The timer number
  329. * @param edge_int_en True to enable edge interrupt, false to disable edge interrupt
  330. *
  331. * @return None
  332. */
  333. static inline void timer_ll_set_edge_int_enable(timg_dev_t *hw, timer_idx_t timer_num, bool edge_int_en)
  334. {
  335. hw->hw_timer[timer_num].config.edge_int_en = edge_int_en;
  336. }
  337. /**
  338. * @brief Get the edge interrupt status.
  339. *
  340. * @param hw Beginning address of the peripheral registers.
  341. * @param timer_num The timer number
  342. *
  343. * @return
  344. * - true Enable edge interrupt
  345. * - false Disable edge interrupt
  346. */
  347. static inline bool timer_ll_get_edge_int_enable(timg_dev_t *hw, timer_idx_t timer_num)
  348. {
  349. return hw->hw_timer[timer_num].config.edge_int_en;
  350. }
  351. /**
  352. * @brief Get interrupt status register address.
  353. *
  354. * @param hw Beginning address of the peripheral registers.
  355. *
  356. * @return Interrupt status register address
  357. */
  358. static inline uint32_t timer_ll_get_intr_status_reg(timg_dev_t *hw)
  359. {
  360. return (uint32_t) & (hw->int_st_timers.val);
  361. }
  362. static inline uint32_t timer_ll_get_intr_mask_bit(timg_dev_t *hw, timer_idx_t timer_num)
  363. {
  364. return (1U << timer_num);
  365. }
  366. #ifdef __cplusplus
  367. }
  368. #endif