rtc_io_ll.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*******************************************************************************
  7. * NOTICE
  8. * The ll is not public api, don't use in application code.
  9. * See readme.md in hal/include/hal/readme.md
  10. ******************************************************************************/
  11. #pragma once
  12. #include <stdlib.h>
  13. #include "soc/rtc_io_periph.h"
  14. #include "soc/rtc_io_struct.h"
  15. #include "hal/rtc_io_types.h"
  16. #include "hal/gpio_types.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. typedef enum {
  21. RTCIO_FUNC_RTC = 0x0, /*!< The pin controled by RTC module. */
  22. RTCIO_FUNC_DIGITAL = 0x1, /*!< The pin controlled by DIGITAL module. */
  23. } rtcio_ll_func_t;
  24. typedef enum {
  25. RTCIO_WAKEUP_DISABLE = 0, /*!< Disable GPIO interrupt */
  26. RTCIO_WAKEUP_LOW_LEVEL = 0x4, /*!< GPIO interrupt type : input low level trigger */
  27. RTCIO_WAKEUP_HIGH_LEVEL = 0x5, /*!< GPIO interrupt type : input high level trigger */
  28. } rtcio_ll_wake_type_t;
  29. typedef enum {
  30. RTCIO_OUTPUT_NORMAL = 0, /*!< RTCIO output mode is normal. */
  31. RTCIO_OUTPUT_OD = 0x1, /*!< RTCIO output mode is open-drain. */
  32. } rtcio_ll_out_mode_t;
  33. /**
  34. * @brief Select the rtcio function.
  35. *
  36. * @note The RTC function must be selected before the pad analog function is enabled.
  37. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  38. * @param func Select pin function.
  39. */
  40. static inline void rtcio_ll_function_select(int rtcio_num, rtcio_ll_func_t func)
  41. {
  42. if (func == RTCIO_FUNC_RTC) {
  43. SENS.sar_peri_clk_gate_conf.iomux_clk_en = 1;
  44. // 0: GPIO connected to digital GPIO module. 1: GPIO connected to analog RTC module.
  45. SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, (rtc_io_desc[rtcio_num].mux));
  46. //0:RTC FUNCTION 1,2,3:Reserved
  47. SET_PERI_REG_BITS(rtc_io_desc[rtcio_num].reg, RTC_IO_TOUCH_PAD1_FUN_SEL_V, RTCIO_LL_PIN_FUNC, rtc_io_desc[rtcio_num].func);
  48. } else if (func == RTCIO_FUNC_DIGITAL) {
  49. CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, (rtc_io_desc[rtcio_num].mux));
  50. SENS.sar_peri_clk_gate_conf.iomux_clk_en = 0;
  51. }
  52. }
  53. /**
  54. * Enable rtcio output.
  55. *
  56. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  57. */
  58. static inline void rtcio_ll_output_enable(int rtcio_num)
  59. {
  60. RTCIO.enable_w1ts.w1ts = (1U << rtcio_num);
  61. }
  62. /**
  63. * Disable rtcio output.
  64. *
  65. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  66. */
  67. static inline void rtcio_ll_output_disable(int rtcio_num)
  68. {
  69. RTCIO.enable_w1tc.w1tc = (1U << rtcio_num);
  70. }
  71. /**
  72. * Set RTCIO output level.
  73. *
  74. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  75. * @param level 0: output low; ~0: output high.
  76. */
  77. static inline void rtcio_ll_set_level(int rtcio_num, uint32_t level)
  78. {
  79. if (level) {
  80. RTCIO.out_w1ts.w1ts = (1U << rtcio_num);
  81. } else {
  82. RTCIO.out_w1tc.w1tc = (1U << rtcio_num);
  83. }
  84. }
  85. /**
  86. * Enable rtcio input.
  87. *
  88. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  89. */
  90. static inline void rtcio_ll_input_enable(int rtcio_num)
  91. {
  92. SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].ie);
  93. }
  94. /**
  95. * Disable rtcio input.
  96. *
  97. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  98. */
  99. static inline void rtcio_ll_input_disable(int rtcio_num)
  100. {
  101. CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].ie);
  102. }
  103. /**
  104. * Get RTCIO input level.
  105. *
  106. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  107. * @return 0: input low; ~0: input high.
  108. */
  109. static inline uint32_t rtcio_ll_get_level(int rtcio_num)
  110. {
  111. return (uint32_t)(RTCIO.in_val.in >> rtcio_num) & 0x1;
  112. }
  113. /**
  114. * @brief Set RTC GPIO pad drive capability
  115. *
  116. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  117. * @param strength Drive capability of the pad. Range: 0 ~ 3.
  118. */
  119. static inline void rtcio_ll_set_drive_capability(int rtcio_num, uint32_t strength)
  120. {
  121. if (rtc_io_desc[rtcio_num].drv_v) {
  122. SET_PERI_REG_BITS(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].drv_v, strength, rtc_io_desc[rtcio_num].drv_s);
  123. }
  124. }
  125. /**
  126. * @brief Get RTC GPIO pad drive capability.
  127. *
  128. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  129. * @return Drive capability of the pad. Range: 0 ~ 3.
  130. */
  131. static inline uint32_t rtcio_ll_get_drive_capability(int rtcio_num)
  132. {
  133. return GET_PERI_REG_BITS2(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].drv_v, rtc_io_desc[rtcio_num].drv_s);
  134. }
  135. /**
  136. * @brief Set RTC GPIO pad output mode.
  137. *
  138. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  139. * @return mode Output mode.
  140. */
  141. static inline void rtcio_ll_output_mode_set(int rtcio_num, rtcio_ll_out_mode_t mode)
  142. {
  143. RTCIO.pin[rtcio_num].pad_driver = mode;
  144. }
  145. /**
  146. * RTC GPIO pullup enable.
  147. *
  148. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  149. */
  150. static inline void rtcio_ll_pullup_enable(int rtcio_num)
  151. {
  152. if (rtc_io_desc[rtcio_num].pullup) {
  153. SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pullup);
  154. }
  155. }
  156. /**
  157. * RTC GPIO pullup disable.
  158. *
  159. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  160. */
  161. static inline void rtcio_ll_pullup_disable(int rtcio_num)
  162. {
  163. if (rtc_io_desc[rtcio_num].pullup) {
  164. CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pullup);
  165. }
  166. }
  167. /**
  168. * RTC GPIO pulldown enable.
  169. *
  170. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  171. */
  172. static inline void rtcio_ll_pulldown_enable(int rtcio_num)
  173. {
  174. if (rtc_io_desc[rtcio_num].pulldown) {
  175. SET_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pulldown);
  176. }
  177. }
  178. /**
  179. * RTC GPIO pulldown disable.
  180. *
  181. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  182. */
  183. static inline void rtcio_ll_pulldown_disable(int rtcio_num)
  184. {
  185. if (rtc_io_desc[rtcio_num].pulldown) {
  186. CLEAR_PERI_REG_MASK(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].pulldown);
  187. }
  188. }
  189. /**
  190. * Enable force hold function for RTC IO pad.
  191. *
  192. * Enabling HOLD function will cause the pad to lock current status, such as,
  193. * input/output enable, input/output value, function, drive strength values.
  194. * This function is useful when going into light or deep sleep mode to prevent
  195. * the pin configuration from changing.
  196. *
  197. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  198. */
  199. static inline void rtcio_ll_force_hold_enable(int rtcio_num)
  200. {
  201. SET_PERI_REG_MASK(RTC_CNTL_PAD_HOLD_REG, rtc_io_desc[rtcio_num].hold_force);
  202. }
  203. /**
  204. * Disable hold function on an RTC IO pad
  205. *
  206. * @note If disable the pad hold, the status of pad maybe changed in sleep mode.
  207. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  208. */
  209. static inline void rtcio_ll_force_hold_disable(int rtcio_num)
  210. {
  211. CLEAR_PERI_REG_MASK(RTC_CNTL_PAD_HOLD_REG, rtc_io_desc[rtcio_num].hold_force);
  212. }
  213. /**
  214. * Enable force hold function for RTC IO pad.
  215. *
  216. * Enabling HOLD function will cause the pad to lock current status, such as,
  217. * input/output enable, input/output value, function, drive strength values.
  218. * This function is useful when going into light or deep sleep mode to prevent
  219. * the pin configuration from changing.
  220. *
  221. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  222. */
  223. static inline void rtcio_ll_force_hold_all(void)
  224. {
  225. SET_PERI_REG_MASK(RTC_CNTL_PWC_REG, RTC_CNTL_PAD_FORCE_HOLD_M);
  226. }
  227. /**
  228. * Disable hold function on an RTC IO pad
  229. *
  230. * @note If disable the pad hold, the status of pad maybe changed in sleep mode.
  231. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  232. */
  233. static inline void rtcio_ll_force_unhold_all(void)
  234. {
  235. CLEAR_PERI_REG_MASK(RTC_CNTL_PWC_REG, RTC_CNTL_PAD_FORCE_HOLD_M);
  236. }
  237. /**
  238. * Enable wakeup function and set wakeup type from light sleep status for rtcio.
  239. *
  240. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  241. * @param type Wakeup on high level or low level.
  242. */
  243. static inline void rtcio_ll_wakeup_enable(int rtcio_num, rtcio_ll_wake_type_t type)
  244. {
  245. SENS.sar_peri_clk_gate_conf.iomux_clk_en = 1;
  246. RTCIO.pin[rtcio_num].wakeup_enable = 0x1;
  247. RTCIO.pin[rtcio_num].int_type = type;
  248. }
  249. /**
  250. * Disable wakeup function from light sleep status for rtcio.
  251. *
  252. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  253. */
  254. static inline void rtcio_ll_wakeup_disable(int rtcio_num)
  255. {
  256. RTCIO.pin[rtcio_num].wakeup_enable = 0;
  257. RTCIO.pin[rtcio_num].int_type = RTCIO_WAKEUP_DISABLE;
  258. }
  259. /**
  260. * Enable rtc io output in deep sleep.
  261. *
  262. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  263. */
  264. static inline void rtcio_ll_enable_output_in_sleep(gpio_num_t gpio_num)
  265. {
  266. if (rtc_io_desc[gpio_num].slpoe) {
  267. SET_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpoe);
  268. }
  269. }
  270. /**
  271. * Disable rtc io output in deep sleep.
  272. *
  273. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  274. */
  275. static inline void rtcio_ll_in_sleep_disable_output(gpio_num_t gpio_num)
  276. {
  277. if (rtc_io_desc[gpio_num].slpoe) {
  278. CLEAR_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpoe);
  279. }
  280. }
  281. /**
  282. * Enable rtc io input in deep sleep.
  283. *
  284. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  285. */
  286. static inline void rtcio_ll_in_sleep_enable_input(gpio_num_t gpio_num)
  287. {
  288. SET_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpie);
  289. }
  290. /**
  291. * Disable rtc io input in deep sleep.
  292. *
  293. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  294. */
  295. static inline void rtcio_ll_in_sleep_disable_input(gpio_num_t gpio_num)
  296. {
  297. CLEAR_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpie);
  298. }
  299. /**
  300. * Enable rtc io keep another setting in deep sleep.
  301. *
  302. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  303. */
  304. static inline void rtcio_ll_enable_sleep_setting(gpio_num_t gpio_num)
  305. {
  306. SET_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpsel);
  307. }
  308. /**
  309. * Disable rtc io keep another setting in deep sleep. (Default)
  310. *
  311. * @param rtcio_num The index of rtcio. 0 ~ MAX(rtcio).
  312. */
  313. static inline void rtcio_ll_disable_sleep_setting(gpio_num_t gpio_num)
  314. {
  315. CLEAR_PERI_REG_MASK(rtc_io_desc[gpio_num].reg, rtc_io_desc[gpio_num].slpsel);
  316. }
  317. static inline void rtcio_ll_ext0_set_wakeup_pin(int rtcio_num, int level)
  318. {
  319. REG_SET_FIELD(RTC_IO_EXT_WAKEUP0_REG, RTC_IO_EXT_WAKEUP0_SEL, rtcio_num);
  320. // Set level which will trigger wakeup
  321. SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1,
  322. level , RTC_CNTL_EXT_WAKEUP0_LV_S);
  323. }
  324. #ifdef __cplusplus
  325. }
  326. #endif