rtc.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include "soc/soc.h"
  11. #include "soc/clk_tree_defs.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * @file rtc.h
  17. * @brief Low-level RTC power, clock, and sleep functions.
  18. *
  19. * Functions in this file facilitate configuration of ESP32's RTC_CNTL peripheral.
  20. * RTC_CNTL peripheral handles many functions:
  21. * - enables/disables clocks and power to various parts of the chip; this is
  22. * done using direct register access (forcing power up or power down) or by
  23. * allowing state machines to control power and clocks automatically
  24. * - handles sleep and wakeup functions
  25. * - maintains a 48-bit counter which can be used for timekeeping
  26. *
  27. * These functions are not thread safe, and should not be viewed as high level
  28. * APIs. For example, while this file provides a function which can switch
  29. * CPU frequency, this function is on its own is not sufficient to implement
  30. * frequency switching in ESP-IDF context: some coordination with RTOS,
  31. * peripheral drivers, and WiFi/BT stacks is also required.
  32. *
  33. * These functions will normally not be used in applications directly.
  34. * ESP-IDF provides, or will provide, drivers and other facilities to use
  35. * RTC subsystem functionality.
  36. *
  37. * The functions are loosely split into the following groups:
  38. * - rtc_clk: clock switching, calibration
  39. * - rtc_time: reading RTC counter, conversion between counter values and time
  40. * - rtc_sleep: entry into sleep modes
  41. * - rtc_init: initialization
  42. */
  43. #define MHZ (1000000)
  44. #define RTC_SLOW_CLK_X32K_CAL_TIMEOUT_THRES(cycles) (cycles << 12)
  45. #define RTC_SLOW_CLK_8MD256_CAL_TIMEOUT_THRES(cycles) (cycles << 12)
  46. #define RTC_SLOW_CLK_150K_CAL_TIMEOUT_THRES(cycles) (cycles << 10)
  47. #define OTHER_BLOCKS_POWERUP 1
  48. #define OTHER_BLOCKS_WAIT 1
  49. /* Approximate mapping of voltages to RTC_CNTL_DBIAS_WAK, RTC_CNTL_DBIAS_SLP,
  50. * RTC_CNTL_DIG_DBIAS_WAK, RTC_CNTL_DIG_DBIAS_SLP values.
  51. */
  52. #define RTC_CNTL_DBIAS_SLP 5 ///< sleep dig_dbias & rtc_dbias
  53. #define RTC_CNTL_DBIAS_0V90 13 ///< digital voltage
  54. #define RTC_CNTL_DBIAS_0V95 16
  55. #define RTC_CNTL_DBIAS_1V00 18
  56. #define RTC_CNTL_DBIAS_1V05 20
  57. #define RTC_CNTL_DBIAS_1V10 23
  58. #define RTC_CNTL_DBIAS_1V15 25
  59. #define RTC_CNTL_DBIAS_1V20 28
  60. #define RTC_CNTL_DBIAS_1V25 30
  61. #define RTC_CNTL_DBIAS_1V30 31 ///< voltage is about 1.34v in fact
  62. /* Delays for various clock sources to be enabled/switched.
  63. * All values are in microseconds.
  64. */
  65. #define SOC_DELAY_RTC_FAST_CLK_SWITCH 3
  66. #define SOC_DELAY_RTC_SLOW_CLK_SWITCH 300
  67. #define SOC_DELAY_RC_FAST_ENABLE 50
  68. #define SOC_DELAY_RC_FAST_DIGI_SWITCH 5
  69. /* Core voltage needs to be increased in two cases:
  70. * 1. running at 240 MHz
  71. * 2. running with 80MHz or 120M Flash frequency
  72. */
  73. #if CONFIG_ESPTOOLPY_FLASHFREQ_80M || CONFIG_ESPTOOLPY_FLASHFREQ_120M
  74. #define DIG_DBIAS_80M_160M RTC_CNTL_DBIAS_1V25
  75. #else
  76. #define DIG_DBIAS_80M_160M RTC_CNTL_DBIAS_1V10
  77. #endif
  78. #define DIG_DBIAS_240M RTC_CNTL_DBIAS_1V25
  79. #define DIG_DBIAS_XTAL RTC_CNTL_DBIAS_1V10
  80. #define DIG_DBIAS_2M RTC_CNTL_DBIAS_1V00
  81. #define RTC_CNTL_PLL_BUF_WAIT_DEFAULT 20
  82. #define RTC_CNTL_XTL_BUF_WAIT_DEFAULT 100
  83. #define RTC_CNTL_CK8M_WAIT_DEFAULT 20
  84. #define RTC_CK8M_ENABLE_WAIT_DEFAULT 5
  85. /* Various delays to be programmed into power control state machines */
  86. #define RTC_CNTL_XTL_BUF_WAIT_SLP_US (250)
  87. #define RTC_CNTL_PLL_BUF_WAIT_SLP_CYCLES (1)
  88. #define RTC_CNTL_CK8M_WAIT_SLP_CYCLES (4)
  89. #define RTC_CNTL_WAKEUP_DELAY_CYCLES (4)
  90. #define RTC_CNTL_MIN_SLP_VAL_MIN (2)
  91. #define RTC_CNTL_CK8M_DFREQ_DEFAULT 100
  92. #define RTC_CNTL_SCK_DCAP_DEFAULT 255
  93. #define RTC_CNTL_ULPCP_TOUCH_START_WAIT_IN_SLEEP (0xFF)
  94. #define RTC_CNTL_ULPCP_TOUCH_START_WAIT_DEFAULT (0x10)
  95. /*
  96. set sleep_init default param
  97. */
  98. #define RTC_CNTL_DBG_ATTEN_LIGHTSLEEP_DEFAULT 5
  99. #define RTC_CNTL_DBG_ATTEN_LIGHTSLEEP_NODROP 0
  100. #define RTC_CNTL_DBG_ATTEN_DEEPSLEEP_DEFAULT 14
  101. #define RTC_CNTL_DBG_ATTEN_DEEPSLEEP_ULTRA_LOW 15
  102. #define RTC_CNTL_DBG_ATTEN_DEEPSLEEP_NODROP 0
  103. #define RTC_CNTL_BIASSLP_SLEEP_DEFAULT 1
  104. #define RTC_CNTL_BIASSLP_SLEEP_ON 0
  105. #define RTC_CNTL_PD_CUR_SLEEP_DEFAULT 1
  106. #define RTC_CNTL_PD_CUR_SLEEP_ON 0
  107. #define RTC_CNTL_DG_VDD_DRV_B_SLP_DEFAULT 0xf
  108. #define RTC_CNTL_DBG_ATTEN_MONITOR_DEFAULT 0
  109. #define RTC_CNTL_BIASSLP_MONITOR_DEFAULT 1
  110. #define RTC_CNTL_BIASSLP_MONITOR_ON 0
  111. #define RTC_CNTL_PD_CUR_MONITOR_DEFAULT 1
  112. #define RTC_CNTL_PD_CUR_MONITOR_ON 0
  113. /*
  114. The follow value is used to get a reasonable rtc voltage dbias value according to digital dbias & some other value
  115. storing in efuse
  116. */
  117. #define K_RTC_MID_MUL10000 198
  118. #define K_DIG_MID_MUL10000 211
  119. #define V_RTC_MID_MUL10000 10181
  120. #define V_DIG_MID_MUL10000 10841
  121. /*
  122. set LDO slave during CPU switch
  123. */
  124. #define DEFAULT_LDO_SLAVE 0x7
  125. /**
  126. * @brief Possible main XTAL frequency values.
  127. *
  128. * Enum values should be equal to frequency in MHz.
  129. */
  130. typedef enum {
  131. RTC_XTAL_FREQ_32M = 32,
  132. RTC_XTAL_FREQ_40M = 40, //!< 40 MHz XTAL
  133. } rtc_xtal_freq_t;
  134. /**
  135. * @brief CPU clock configuration structure
  136. */
  137. typedef struct rtc_cpu_freq_config_s {
  138. soc_cpu_clk_src_t source; //!< The clock from which CPU clock is derived
  139. uint32_t source_freq_mhz; //!< Source clock frequency
  140. uint32_t div; //!< Divider, freq_mhz = source_freq_mhz / div
  141. uint32_t freq_mhz; //!< CPU clock frequency
  142. } rtc_cpu_freq_config_t;
  143. #define RTC_CLK_CAL_FRACT 19 //!< Number of fractional bits in values returned by rtc_clk_cal
  144. #define RTC_VDDSDIO_TIEH_1_8V 0 //!< TIEH field value for 1.8V VDDSDIO
  145. #define RTC_VDDSDIO_TIEH_3_3V 1 //!< TIEH field value for 3.3V VDDSDIO
  146. /**
  147. * @brief Clock source to be calibrated using rtc_clk_cal function
  148. */
  149. typedef enum {
  150. RTC_CAL_RTC_MUX = 0, //!< Currently selected RTC SLOW_CLK
  151. RTC_CAL_8MD256 = 1, //!< Internal 8 MHz RC oscillator, divided by 256
  152. RTC_CAL_32K_XTAL = 2, //!< External 32 kHz XTAL
  153. RTC_CAL_INTERNAL_OSC = 3 //!< Internal 150 kHz oscillator
  154. } rtc_cal_sel_t;
  155. /**
  156. * Initialization parameters for rtc_clk_init
  157. */
  158. typedef struct {
  159. rtc_xtal_freq_t xtal_freq : 8; //!< Main XTAL frequency
  160. uint32_t cpu_freq_mhz : 10; //!< CPU frequency to set, in MHz
  161. soc_rtc_fast_clk_src_t fast_clk_src : 2; //!< RTC_FAST_CLK clock source to choose
  162. soc_rtc_slow_clk_src_t slow_clk_src : 2; //!< RTC_SLOW_CLK clock source to choose
  163. uint32_t clk_rtc_clk_div : 8;
  164. uint32_t clk_8m_clk_div : 3; //!< RTC 8M clock divider (division is by clk_8m_div+1, i.e. 0 means 8MHz frequency)
  165. uint32_t slow_clk_dcap : 8; //!< RTC 150k clock adjustment parameter (higher value leads to lower frequency)
  166. uint32_t clk_8m_dfreq : 8; //!< RTC 8m clock adjustment parameter (higher value leads to higher frequency)
  167. } rtc_clk_config_t;
  168. /**
  169. * Default initializer for rtc_clk_config_t
  170. */
  171. #define RTC_CLK_CONFIG_DEFAULT() { \
  172. .xtal_freq = CONFIG_XTAL_FREQ, \
  173. .cpu_freq_mhz = 80, \
  174. .fast_clk_src = SOC_RTC_FAST_CLK_SRC_RC_FAST, \
  175. .slow_clk_src = SOC_RTC_SLOW_CLK_SRC_RC_SLOW, \
  176. .clk_rtc_clk_div = 0, \
  177. .clk_8m_clk_div = 0, \
  178. .slow_clk_dcap = RTC_CNTL_SCK_DCAP_DEFAULT, \
  179. .clk_8m_dfreq = RTC_CNTL_CK8M_DFREQ_DEFAULT, \
  180. }
  181. typedef struct {
  182. uint16_t wifi_powerup_cycles : 7;
  183. uint16_t wifi_wait_cycles : 9;
  184. uint16_t bt_powerup_cycles : 7;
  185. uint16_t bt_wait_cycles : 9;
  186. uint16_t rtc_powerup_cycles : 7;
  187. uint16_t rtc_wait_cycles : 9;
  188. uint16_t cpu_top_powerup_cycles : 7;
  189. uint16_t cpu_top_wait_cycles : 9;
  190. uint16_t dg_wrap_powerup_cycles : 7;
  191. uint16_t dg_wrap_wait_cycles : 9;
  192. uint16_t dg_peri_powerup_cycles : 7;
  193. uint16_t dg_peri_wait_cycles : 9;
  194. uint16_t rtc_mem_powerup_cycles : 7;
  195. uint16_t rtc_mem_wait_cycles : 9;
  196. } rtc_init_config_t;
  197. #define RTC_INIT_CONFIG_DEFAULT() { \
  198. .wifi_powerup_cycles = OTHER_BLOCKS_POWERUP, \
  199. .wifi_wait_cycles = OTHER_BLOCKS_WAIT, \
  200. .bt_powerup_cycles = OTHER_BLOCKS_POWERUP, \
  201. .bt_wait_cycles = OTHER_BLOCKS_WAIT, \
  202. .rtc_powerup_cycles = OTHER_BLOCKS_POWERUP, \
  203. .rtc_wait_cycles = OTHER_BLOCKS_WAIT, \
  204. .cpu_top_powerup_cycles = OTHER_BLOCKS_POWERUP, \
  205. .cpu_top_wait_cycles = OTHER_BLOCKS_WAIT, \
  206. .dg_wrap_powerup_cycles = OTHER_BLOCKS_POWERUP, \
  207. .dg_wrap_wait_cycles = OTHER_BLOCKS_WAIT, \
  208. .dg_peri_powerup_cycles = OTHER_BLOCKS_POWERUP, \
  209. .dg_peri_wait_cycles = OTHER_BLOCKS_WAIT, \
  210. .rtc_mem_powerup_cycles = OTHER_BLOCKS_POWERUP, \
  211. .rtc_mem_wait_cycles = OTHER_BLOCKS_WAIT, \
  212. }
  213. void rtc_clk_divider_set(uint32_t div);
  214. void rtc_clk_8m_divider_set(uint32_t div);
  215. /**
  216. * Initialize clocks and set CPU frequency
  217. *
  218. * @param cfg clock configuration as rtc_clk_config_t
  219. */
  220. void rtc_clk_init(rtc_clk_config_t cfg);
  221. /**
  222. * @brief Get main XTAL frequency
  223. *
  224. * This is the value stored in RTC register RTC_XTAL_FREQ_REG by the bootloader. As passed to
  225. * rtc_clk_init function
  226. *
  227. * @return XTAL frequency, one of rtc_xtal_freq_t
  228. */
  229. rtc_xtal_freq_t rtc_clk_xtal_freq_get(void);
  230. /**
  231. * @brief Update XTAL frequency
  232. *
  233. * Updates the XTAL value stored in RTC_XTAL_FREQ_REG. Usually this value is ignored
  234. * after startup.
  235. *
  236. * @param xtal_freq New frequency value
  237. */
  238. void rtc_clk_xtal_freq_update(rtc_xtal_freq_t xtal_freq);
  239. /**
  240. * @brief Enable or disable 32 kHz XTAL oscillator
  241. * @param en true to enable, false to disable
  242. */
  243. void rtc_clk_32k_enable(bool en);
  244. /**
  245. * @brief Configure 32 kHz XTAL oscillator to accept external clock signal
  246. */
  247. void rtc_clk_32k_enable_external(void);
  248. /**
  249. * @brief Get the state of 32k XTAL oscillator
  250. * @return true if 32k XTAL oscillator has been enabled
  251. */
  252. bool rtc_clk_32k_enabled(void);
  253. /**
  254. * @brief Enable 32k oscillator, configuring it for fast startup time.
  255. * Note: to achieve higher frequency stability, rtc_clk_32k_enable function
  256. * must be called one the 32k XTAL oscillator has started up. This function
  257. * will initially disable the 32k XTAL oscillator, so it should not be called
  258. * when the system is using 32k XTAL as RTC_SLOW_CLK.
  259. *
  260. * @param cycle Number of 32kHz cycles to bootstrap external crystal.
  261. * If 0, no square wave will be used to bootstrap crystal oscillation.
  262. */
  263. void rtc_clk_32k_bootstrap(uint32_t cycle);
  264. /**
  265. * @brief Enable or disable 8 MHz internal oscillator
  266. *
  267. * Output from 8 MHz internal oscillator is passed into a configurable
  268. * divider, which by default divides the input clock frequency by 256.
  269. * Output of the divider may be used as RTC_SLOW_CLK source.
  270. * Output of the divider is referred to in register descriptions and code as
  271. * 8md256 or simply d256. Divider values other than 256 may be configured, but
  272. * this facility is not currently needed, so is not exposed in the code.
  273. *
  274. * When 8MHz/256 divided output is not needed, the divider should be disabled
  275. * to reduce power consumption.
  276. *
  277. * @param clk_8m_en true to enable 8MHz generator
  278. * @param d256_en true to enable /256 divider
  279. */
  280. void rtc_clk_8m_enable(bool clk_8m_en, bool d256_en);
  281. /**
  282. * @brief Get the state of 8 MHz internal oscillator
  283. * @return true if the oscillator is enabled
  284. */
  285. bool rtc_clk_8m_enabled(void);
  286. /**
  287. * @brief Get the state of /256 divider which is applied to 8MHz clock
  288. * @return true if the divided output is enabled
  289. */
  290. bool rtc_clk_8md256_enabled(void);
  291. /**
  292. * @brief Select source for RTC_SLOW_CLK
  293. * @param clk_src clock source (one of soc_rtc_slow_clk_src_t values)
  294. */
  295. void rtc_clk_slow_src_set(soc_rtc_slow_clk_src_t clk_src);
  296. /**
  297. * @brief Get the RTC_SLOW_CLK source
  298. * @return currently selected clock source (one of soc_rtc_slow_clk_src_t values)
  299. */
  300. soc_rtc_slow_clk_src_t rtc_clk_slow_src_get(void);
  301. /**
  302. * @brief Get the approximate frequency of RTC_SLOW_CLK, in Hz
  303. *
  304. * - if SOC_RTC_SLOW_CLK_SRC_RC_SLOW is selected, returns ~150000
  305. * - if SOC_RTC_SLOW_CLK_SRC_XTAL32K is selected, returns 32768
  306. * - if SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256 is selected, returns ~68000
  307. *
  308. * rtc_clk_cal function can be used to get more precise value by comparing
  309. * RTC_SLOW_CLK frequency to the frequency of main XTAL.
  310. *
  311. * @return RTC_SLOW_CLK frequency, in Hz
  312. */
  313. uint32_t rtc_clk_slow_freq_get_hz(void);
  314. /**
  315. * @brief Select source for RTC_FAST_CLK
  316. * @param clk_src clock source (one of soc_rtc_fast_clk_src_t values)
  317. */
  318. void rtc_clk_fast_src_set(soc_rtc_fast_clk_src_t clk_src);
  319. /**
  320. * @brief Get the RTC_FAST_CLK source
  321. * @return currently selected clock source (one of soc_rtc_fast_clk_src_t values)
  322. */
  323. soc_rtc_fast_clk_src_t rtc_clk_fast_src_get(void);
  324. /**
  325. * @brief Get CPU frequency config for a given frequency
  326. * @param freq_mhz Frequency in MHz
  327. * @param[out] out_config Output, CPU frequency configuration structure
  328. * @return true if frequency can be obtained, false otherwise
  329. */
  330. bool rtc_clk_cpu_freq_mhz_to_config(uint32_t freq_mhz, rtc_cpu_freq_config_t *out_config);
  331. /**
  332. * @brief Switch CPU frequency
  333. *
  334. * This function sets CPU frequency according to the given configuration
  335. * structure. It enables PLLs, if necessary.
  336. *
  337. * @note This function in not intended to be called by applications in FreeRTOS
  338. * environment. This is because it does not adjust various timers based on the
  339. * new CPU frequency.
  340. *
  341. * @param config CPU frequency configuration structure
  342. */
  343. void rtc_clk_cpu_freq_set_config(const rtc_cpu_freq_config_t *config);
  344. /**
  345. * @brief Switch CPU frequency (optimized for speed)
  346. *
  347. * This function is a faster equivalent of rtc_clk_cpu_freq_set_config.
  348. * It works faster because it does not disable PLLs when switching from PLL to
  349. * XTAL and does not enabled them when switching back. If PLL is not already
  350. * enabled when this function is called to switch from XTAL to PLL frequency,
  351. * or the PLL which is enabled is the wrong one, this function will fall back
  352. * to calling rtc_clk_cpu_freq_set_config.
  353. *
  354. * Unlike rtc_clk_cpu_freq_set_config, this function relies on static data,
  355. * so it is less safe to use it e.g. from a panic handler (when memory might
  356. * be corrupted).
  357. *
  358. * @note This function in not intended to be called by applications in FreeRTOS
  359. * environment. This is because it does not adjust various timers based on the
  360. * new CPU frequency.
  361. *
  362. * @param config CPU frequency configuration structure
  363. */
  364. void rtc_clk_cpu_freq_set_config_fast(const rtc_cpu_freq_config_t *config);
  365. /**
  366. * @brief Get the currently used CPU frequency configuration
  367. * @param[out] out_config Output, CPU frequency configuration structure
  368. */
  369. void rtc_clk_cpu_freq_get_config(rtc_cpu_freq_config_t *out_config);
  370. /**
  371. * @brief Switch CPU clock source to XTAL
  372. *
  373. * Short form for filling in rtc_cpu_freq_config_t structure and calling
  374. * rtc_clk_cpu_freq_set_config when a switch to XTAL is needed.
  375. * Assumes that XTAL frequency has been determined — don't call in startup code.
  376. *
  377. * @note This function always disables BBPLL after switching the CPU clock source to XTAL for power saving purpose.
  378. * If this is unwanted, please use rtc_clk_cpu_freq_set_config. It helps to check whether USB Serial JTAG is in use,
  379. * if so, then BBPLL will not be turned off.
  380. */
  381. void rtc_clk_cpu_freq_set_xtal(void);
  382. /**
  383. * @brief Store new APB frequency value in RAM
  384. *
  385. * This function doesn't change any hardware clocks.
  386. *
  387. * Functions which perform frequency switching and change APB frequency call
  388. * this function to update the value of APB frequency stored in RAM.
  389. * (This should not normally be called from application code.)
  390. *
  391. * @param apb_freq new APB frequency, in Hz
  392. */
  393. void rtc_clk_apb_freq_update(uint32_t apb_freq);
  394. /**
  395. * @brief Get the current stored APB frequency.
  396. * @return The APB frequency value as last set via rtc_clk_apb_freq_update(), in Hz.
  397. */
  398. uint32_t rtc_clk_apb_freq_get(void);
  399. uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cycles);
  400. /**
  401. * @brief Measure RTC slow clock's period, based on main XTAL frequency
  402. *
  403. * This function will time out and return 0 if the time for the given number
  404. * of cycles to be counted exceeds the expected time twice. This may happen if
  405. * 32k XTAL is being calibrated, but the oscillator has not started up (due to
  406. * incorrect loading capacitance, board design issue, or lack of 32 XTAL on board).
  407. *
  408. * @note When 32k CLK is being calibrated, this function will check the accuracy
  409. * of the clock. Since the xtal 32k or ext osc 32k is generally very stable, if
  410. * the check fails, then consider this an invalid 32k clock and return 0. This
  411. * check can filter some jamming signal.
  412. *
  413. * @param cal_clk clock to be measured
  414. * @param slow_clk_cycles number of slow clock cycles to average
  415. * @return average slow clock period in microseconds, Q13.19 fixed point format,
  416. * or 0 if calibration has timed out
  417. */
  418. uint32_t rtc_clk_cal(rtc_cal_sel_t cal_clk, uint32_t slow_clk_cycles);
  419. /**
  420. * @brief Measure ratio between XTAL frequency and RTC slow clock frequency
  421. * @param cal_clk slow clock to be measured
  422. * @param slow_clk_cycles number of slow clock cycles to average
  423. * @return average ratio between XTAL frequency and slow clock frequency,
  424. * Q13.19 fixed point format, or 0 if calibration has timed out.
  425. */
  426. uint32_t rtc_clk_cal_ratio(rtc_cal_sel_t cal_clk, uint32_t slow_clk_cycles);
  427. /**
  428. * @brief Convert time interval from microseconds to RTC_SLOW_CLK cycles
  429. * @param time_in_us Time interval in microseconds
  430. * @param slow_clk_period Period of slow clock in microseconds, Q13.19
  431. * fixed point format (as returned by rtc_slowck_cali).
  432. * @return number of slow clock cycles
  433. */
  434. uint64_t rtc_time_us_to_slowclk(uint64_t time_in_us, uint32_t period);
  435. /**
  436. * @brief Convert time interval from RTC_SLOW_CLK to microseconds
  437. * @param time_in_us Time interval in RTC_SLOW_CLK cycles
  438. * @param slow_clk_period Period of slow clock in microseconds, Q13.19
  439. * fixed point format (as returned by rtc_slowck_cali).
  440. * @return time interval in microseconds
  441. */
  442. uint64_t rtc_time_slowclk_to_us(uint64_t rtc_cycles, uint32_t period);
  443. /**
  444. * @brief Get current value of RTC counter
  445. *
  446. * RTC has a 48-bit counter which is incremented by 2 every 2 RTC_SLOW_CLK
  447. * cycles. Counter value is not writable by software. The value is not adjusted
  448. * when switching to a different RTC_SLOW_CLK source.
  449. *
  450. * Note: this function may take up to 1 RTC_SLOW_CLK cycle to execute
  451. *
  452. * @return current value of RTC counter
  453. */
  454. uint64_t rtc_time_get(void);
  455. /**
  456. * @brief Busy loop until next RTC_SLOW_CLK cycle
  457. *
  458. * This function returns not earlier than the next RTC_SLOW_CLK clock cycle.
  459. * In some cases (e.g. when RTC_SLOW_CLK cycle is very close), it may return
  460. * one RTC_SLOW_CLK cycle later.
  461. */
  462. void rtc_clk_wait_for_slow_cycle(void);
  463. /**
  464. * @brief Enable the rtc digital 8M clock
  465. *
  466. * This function is used to enable the digital rtc 8M clock to support peripherals.
  467. * For enabling the analog 8M clock, using `rtc_clk_8M_enable` function above.
  468. */
  469. void rtc_dig_clk8m_enable(void);
  470. /**
  471. * @brief Disable the rtc digital 8M clock
  472. *
  473. * This function is used to disable the digital rtc 8M clock, which is only used to support peripherals.
  474. */
  475. void rtc_dig_clk8m_disable(void);
  476. /**
  477. * @brief Get whether the rtc digital 8M clock is enabled
  478. */
  479. bool rtc_dig_8m_enabled(void);
  480. /**
  481. * @brief Calculate the real clock value after the clock calibration
  482. *
  483. * @param cal_val Average slow clock period in microseconds, fixed point value as returned from `rtc_clk_cal`
  484. * @return Frequency of the clock in Hz
  485. */
  486. uint32_t rtc_clk_freq_cal(uint32_t cal_val);
  487. /**
  488. * @brief Power up flags for rtc_sleep_pd function
  489. */
  490. typedef struct {
  491. uint32_t dig_fpu : 1; //!< Set to 1 to power UP digital part in sleep
  492. uint32_t rtc_fpu : 1; //!< Set to 1 to power UP RTC memories in sleep
  493. uint32_t cpu_fpu : 1; //!< Set to 1 to power UP digital memories and CPU in sleep
  494. uint32_t i2s_fpu : 1; //!< Set to 1 to power UP I2S in sleep
  495. uint32_t bb_fpu : 1; //!< Set to 1 to power UP WiFi in sleep
  496. uint32_t nrx_fpu : 1; //!< Set to 1 to power UP WiFi in sleep
  497. uint32_t fe_fpu : 1; //!< Set to 1 to power UP WiFi in sleep
  498. uint32_t sram_fpu : 1; //!< Set to 1 to power UP SRAM in sleep
  499. uint32_t rom_ram_fpu : 1; //!< Set to 1 to power UP ROM/IRAM0_DRAM0 in sleep
  500. } rtc_sleep_pu_config_t;
  501. /**
  502. * Initializer for rtc_sleep_pu_config_t which sets all flags to the same value
  503. */
  504. #define RTC_SLEEP_PU_CONFIG_ALL(val) {\
  505. .dig_fpu = (val), \
  506. .rtc_fpu = (val), \
  507. .cpu_fpu = (val), \
  508. .i2s_fpu = (val), \
  509. .bb_fpu = (val), \
  510. .nrx_fpu = (val), \
  511. .fe_fpu = (val), \
  512. .sram_fpu = (val), \
  513. .rom_ram_fpu = (val), \
  514. }
  515. void rtc_sleep_pu(rtc_sleep_pu_config_t cfg);
  516. /**
  517. * @brief sleep configuration for rtc_sleep_init function
  518. */
  519. typedef struct {
  520. uint32_t lslp_mem_inf_fpu : 1; //!< force normal voltage in sleep mode (digital domain memory)
  521. uint32_t rtc_mem_inf_follow_cpu : 1;//!< keep low voltage in sleep mode (even if ULP/touch is used)
  522. uint32_t rtc_fastmem_pd_en : 1; //!< power down RTC fast memory
  523. uint32_t rtc_slowmem_pd_en : 1; //!< power down RTC slow memory
  524. uint32_t rtc_peri_pd_en : 1; //!< power down RTC peripherals
  525. uint32_t modem_pd_en : 1; //!< power down Modem(wifi and ble)
  526. uint32_t cpu_pd_en : 1; //!< power down CPU, but not restart when lightsleep.
  527. uint32_t int_8m_pd_en : 1; //!< Power down Internal 8M oscillator
  528. uint32_t dig_peri_pd_en : 1; //!< power down digital peripherals
  529. uint32_t deep_slp : 1; //!< power down digital domain
  530. uint32_t wdt_flashboot_mod_en : 1; //!< enable WDT flashboot mode
  531. uint32_t dig_dbias_slp : 5; //!< set bias for digital domain, in sleep mode
  532. uint32_t rtc_dbias_slp : 5; //!< set bias for RTC domain, in sleep mode
  533. uint32_t bias_sleep_monitor : 1; //!< circuit control parameter, in monitor mode
  534. uint32_t dbg_atten_slp : 4; //!< voltage parameter, in sleep mode
  535. uint32_t bias_sleep_slp : 1; //!< circuit control parameter, in sleep mode
  536. uint32_t pd_cur_monitor : 1; //!< circuit control parameter, in monitor mode
  537. uint32_t pd_cur_slp : 1; //!< circuit control parameter, in sleep mode
  538. uint32_t vddsdio_pd_en : 1; //!< power down VDDSDIO regulator
  539. uint32_t xtal_fpu : 1; //!< keep main XTAL powered up in sleep
  540. uint32_t rtc_regulator_fpu : 1; //!< keep rtc regulator powered up in sleep
  541. uint32_t deep_slp_reject : 1; //!< enable deep sleep reject
  542. uint32_t light_slp_reject : 1; //!< enable light sleep reject
  543. } rtc_sleep_config_t;
  544. #define RTC_SLEEP_PD_DIG BIT(0) //!< Deep sleep (power down digital domain)
  545. #define RTC_SLEEP_PD_RTC_PERIPH BIT(1) //!< Power down RTC peripherals
  546. #define RTC_SLEEP_PD_RTC_SLOW_MEM BIT(2) //!< Power down RTC SLOW memory
  547. #define RTC_SLEEP_PD_RTC_FAST_MEM BIT(3) //!< Power down RTC FAST memory
  548. #define RTC_SLEEP_PD_RTC_MEM_FOLLOW_CPU BIT(4) //!< RTC FAST and SLOW memories are automatically powered up and down along with the CPU
  549. #define RTC_SLEEP_PD_VDDSDIO BIT(5) //!< Power down VDDSDIO regulator
  550. #define RTC_SLEEP_PD_MODEM BIT(6) //!< Power down Modem(wifi and ble)
  551. #define RTC_SLEEP_PD_CPU BIT(8) //!< Power down CPU when in lightsleep, but not restart
  552. #define RTC_SLEEP_PD_DIG_PERIPH BIT(9) //!< Power down DIG peripherals
  553. #define RTC_SLEEP_PD_INT_8M BIT(10) //!< Power down Internal 8M oscillator
  554. #define RTC_SLEEP_PD_XTAL BIT(11) //!< Power down main XTAL
  555. //These flags are not power domains, but will affect some sleep parameters
  556. #define RTC_SLEEP_DIG_USE_8M BIT(16)
  557. #define RTC_SLEEP_USE_ADC_TESEN_MONITOR BIT(17)
  558. #define RTC_SLEEP_NO_ULTRA_LOW BIT(18) //!< Avoid using ultra low power in deep sleep, in which RTCIO cannot be used as input, and RTCMEM can't work under high temperature
  559. /**
  560. * Default initializer for rtc_sleep_config_t
  561. *
  562. * This initializer sets all fields to "reasonable" values (e.g. suggested for
  563. * production use) based on a combination of RTC_SLEEP_PD_x flags.
  564. *
  565. * @param RTC_SLEEP_PD_x flags combined using bitwise OR
  566. */
  567. void rtc_sleep_get_default_config(uint32_t sleep_flags, rtc_sleep_config_t *out_config);
  568. /**
  569. * @brief Prepare the chip to enter sleep mode
  570. *
  571. * This function configures various power control state machines to handle
  572. * entry into light sleep or deep sleep mode, switches APB and CPU clock source
  573. * (usually to XTAL), and sets bias voltages for digital and RTC power domains.
  574. *
  575. * This function does not actually enter sleep mode; this is done using
  576. * rtc_sleep_start function. Software may do some other actions between
  577. * rtc_sleep_init and rtc_sleep_start, such as set wakeup timer and configure
  578. * wakeup sources.
  579. * @param cfg sleep mode configuration
  580. */
  581. void rtc_sleep_init(rtc_sleep_config_t cfg);
  582. /**
  583. * @brief Low level initialize for rtc state machine waiting cycles after waking up
  584. *
  585. * This function configures the cycles chip need to wait for internal 8MHz
  586. * oscillator and external 40MHz crystal. As we configure fixed time for waiting
  587. * crystal, we need to pass period to calculate cycles. Now this function only
  588. * used in lightsleep mode.
  589. *
  590. * @param slowclk_period re-calibrated slow clock period
  591. */
  592. void rtc_sleep_low_init(uint32_t slowclk_period);
  593. #define RTC_EXT0_TRIG_EN BIT(0) //!< EXT0 GPIO wakeup
  594. #define RTC_EXT1_TRIG_EN BIT(1) //!< EXT1 GPIO wakeup
  595. #define RTC_GPIO_TRIG_EN BIT(2) //!< GPIO wakeup (light sleep only)
  596. #define RTC_TIMER_TRIG_EN BIT(3) //!< Timer wakeup
  597. #define RTC_SDIO_TRIG_EN BIT(4) //!< SDIO wakeup (light sleep only)
  598. #define RTC_WIFI_TRIG_EN BIT(5) //!< WIFI wakeup (light sleep only)
  599. #define RTC_UART0_TRIG_EN BIT(6) //!< UART0 wakeup (light sleep only)
  600. #define RTC_UART1_TRIG_EN BIT(7) //!< UART1 wakeup (light sleep only)
  601. #define RTC_TOUCH_TRIG_EN BIT(8) //!< Touch wakeup
  602. #define RTC_ULP_TRIG_EN BIT(9) //!< ULP wakeup
  603. #define RTC_BT_TRIG_EN BIT(10) //!< BT wakeup (light sleep only)
  604. #define RTC_COCPU_TRIG_EN BIT(11)
  605. #define RTC_XTAL32K_DEAD_TRIG_EN BIT(12)
  606. #define RTC_COCPU_TRAP_TRIG_EN BIT(13)
  607. #define RTC_USB_TRIG_EN BIT(14)
  608. /**
  609. * RTC_SLEEP_REJECT_MASK records sleep reject sources supported by chip
  610. */
  611. #define RTC_SLEEP_REJECT_MASK (RTC_EXT0_TRIG_EN | \
  612. RTC_EXT1_TRIG_EN | \
  613. RTC_GPIO_TRIG_EN | \
  614. RTC_TIMER_TRIG_EN | \
  615. RTC_SDIO_TRIG_EN | \
  616. RTC_WIFI_TRIG_EN | \
  617. RTC_UART0_TRIG_EN | \
  618. RTC_UART1_TRIG_EN | \
  619. RTC_TOUCH_TRIG_EN | \
  620. RTC_ULP_TRIG_EN | \
  621. RTC_BT_TRIG_EN | \
  622. RTC_COCPU_TRIG_EN | \
  623. RTC_XTAL32K_DEAD_TRIG_EN | \
  624. RTC_COCPU_TRAP_TRIG_EN | \
  625. RTC_USB_TRIG_EN)
  626. /**
  627. * @brief Enter deep or light sleep mode
  628. *
  629. * This function enters the sleep mode previously configured using rtc_sleep_init
  630. * function. Before entering sleep, software should configure wake up sources
  631. * appropriately (set up GPIO wakeup registers, timer wakeup registers,
  632. * and so on).
  633. *
  634. * If deep sleep mode was configured using rtc_sleep_init, and sleep is not
  635. * rejected by hardware (based on reject_opt flags), this function never returns.
  636. * When the chip wakes up from deep sleep, CPU is reset and execution starts
  637. * from ROM bootloader.
  638. *
  639. * If light sleep mode was configured using rtc_sleep_init, this function
  640. * returns on wakeup, or if sleep is rejected by hardware.
  641. *
  642. * @param wakeup_opt bit mask wake up reasons to enable (RTC_xxx_TRIG_EN flags
  643. * combined with OR)
  644. * @param reject_opt bit mask of sleep reject reasons:
  645. * - RTC_CNTL_GPIO_REJECT_EN
  646. * - RTC_CNTL_SDIO_REJECT_EN
  647. * These flags are used to prevent entering sleep when e.g.
  648. * an external host is communicating via SDIO slave
  649. * @param lslp_mem_inf_fpu If non-zero then the low power config is restored
  650. * immediately on wake. Recommended for light sleep,
  651. * has no effect if the system goes into deep sleep.
  652. *
  653. * @return non-zero if sleep was rejected by hardware
  654. */
  655. uint32_t rtc_sleep_start(uint32_t wakeup_opt, uint32_t reject_opt, uint32_t lslp_mem_inf_fpu);
  656. /**
  657. * @brief Enter deep sleep mode
  658. *
  659. * Similar to rtc_sleep_start(), but additionally uses hardware to calculate the CRC value
  660. * of RTC FAST memory. On wake, this CRC is used to determine if a deep sleep wake
  661. * stub is valid to execute (if a wake address is set).
  662. *
  663. * No RAM is accessed while calculating the CRC and going into deep sleep, which makes
  664. * this function safe to use even if the caller's stack is in RTC FAST memory.
  665. *
  666. * @note If no deep sleep wake stub address is set then calling rtc_sleep_start() will
  667. * have the same effect and takes less time as CRC calculation is skipped.
  668. *
  669. * @note This function should only be called after rtc_sleep_init() has been called to
  670. * configure the system for deep sleep.
  671. *
  672. * @param wakeup_opt - same as for rtc_sleep_start
  673. * @param reject_opt - same as for rtc_sleep_start
  674. *
  675. * @return non-zero if sleep was rejected by hardware
  676. */
  677. uint32_t rtc_deep_sleep_start(uint32_t wakeup_opt, uint32_t reject_opt);
  678. /**
  679. * RTC power and clock control initialization settings
  680. */
  681. typedef struct {
  682. uint32_t ck8m_wait : 8; //!< Number of rtc_fast_clk cycles to wait for 8M clock to be ready
  683. uint32_t xtal_wait : 8; //!< Number of rtc_fast_clk cycles to wait for XTAL clock to be ready
  684. uint32_t pll_wait : 8; //!< Number of rtc_fast_clk cycles to wait for PLL to be ready
  685. uint32_t clkctl_init : 1; //!< Perform clock control related initialization
  686. uint32_t pwrctl_init : 1; //!< Perform power control related initialization
  687. uint32_t rtc_dboost_fpd : 1; //!< Force power down RTC_DBOOST
  688. uint32_t xtal_fpu : 1;
  689. uint32_t bbpll_fpu : 1;
  690. uint32_t cpu_waiti_clk_gate : 1;
  691. uint32_t cali_ocode : 1; //!< Calibrate Ocode to make bangap voltage more precise.
  692. } rtc_config_t;
  693. /**
  694. * Default initializer of rtc_config_t.
  695. *
  696. * This initializer sets all fields to "reasonable" values (e.g. suggested for
  697. * production use).
  698. */
  699. #define RTC_CONFIG_DEFAULT() {\
  700. .ck8m_wait = RTC_CNTL_CK8M_WAIT_DEFAULT, \
  701. .xtal_wait = RTC_CNTL_XTL_BUF_WAIT_DEFAULT, \
  702. .pll_wait = RTC_CNTL_PLL_BUF_WAIT_DEFAULT, \
  703. .clkctl_init = 1, \
  704. .pwrctl_init = 1, \
  705. .rtc_dboost_fpd = 1, \
  706. .xtal_fpu = 0, \
  707. .bbpll_fpu = 0, \
  708. .cpu_waiti_clk_gate = 1, \
  709. .cali_ocode = 0 \
  710. }
  711. /**
  712. * Initialize RTC clock and power control related functions
  713. * @param cfg configuration options as rtc_config_t
  714. */
  715. void rtc_init(rtc_config_t cfg);
  716. /**
  717. * Structure describing vddsdio configuration
  718. */
  719. typedef struct {
  720. uint32_t force : 1; //!< If 1, use configuration from RTC registers; if 0, use EFUSE/bootstrapping pins.
  721. uint32_t enable : 1; //!< Enable VDDSDIO regulator
  722. uint32_t tieh : 1; //!< Select VDDSDIO voltage. One of RTC_VDDSDIO_TIEH_1_8V, RTC_VDDSDIO_TIEH_3_3V
  723. uint32_t drefh : 2; //!< Tuning parameter for VDDSDIO regulator
  724. uint32_t drefm : 2; //!< Tuning parameter for VDDSDIO regulator
  725. uint32_t drefl : 2; //!< Tuning parameter for VDDSDIO regulator
  726. } rtc_vddsdio_config_t;
  727. /**
  728. * Get current VDDSDIO configuration
  729. * If VDDSDIO configuration is overridden by RTC, get values from RTC
  730. * Otherwise, if VDDSDIO is configured by EFUSE, get values from EFUSE
  731. * Otherwise, use default values and the level of MTDI bootstrapping pin.
  732. * @return currently used VDDSDIO configuration
  733. */
  734. rtc_vddsdio_config_t rtc_vddsdio_get_config(void);
  735. /**
  736. * Set new VDDSDIO configuration using RTC registers.
  737. * If config.force == 1, this overrides configuration done using bootstrapping
  738. * pins and EFUSE.
  739. *
  740. * @param config new VDDSDIO configuration
  741. */
  742. void rtc_vddsdio_set_config(rtc_vddsdio_config_t config);
  743. // -------------------------- CLOCK TREE DEFS ALIAS ----------------------------
  744. // **WARNING**: The following are only for backwards compatibility.
  745. // Please use the declarations in soc/clk_tree_defs.h instead.
  746. /**
  747. * @brief CPU clock source
  748. */
  749. typedef soc_cpu_clk_src_t rtc_cpu_freq_src_t;
  750. #define RTC_CPU_FREQ_SRC_XTAL SOC_CPU_CLK_SRC_XTAL //!< XTAL
  751. #define RTC_CPU_FREQ_SRC_PLL SOC_CPU_CLK_SRC_PLL //!< PLL (480M or 320M)
  752. #define RTC_CPU_FREQ_SRC_8M SOC_CPU_CLK_SRC_RC_FAST //!< Internal 17.5M RTC oscillator
  753. /**
  754. * @brief RTC SLOW_CLK frequency values
  755. */
  756. typedef soc_rtc_slow_clk_src_t rtc_slow_freq_t;
  757. #define RTC_SLOW_FREQ_RTC SOC_RTC_SLOW_CLK_SRC_RC_SLOW //!< Internal 150 kHz RC oscillator
  758. #define RTC_SLOW_FREQ_32K_XTAL SOC_RTC_SLOW_CLK_SRC_XTAL32K //!< External 32 kHz XTAL
  759. #define RTC_SLOW_FREQ_8MD256 SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256 //!< Internal 17.5 MHz RC oscillator, divided by 256
  760. /**
  761. * @brief RTC FAST_CLK frequency values
  762. */
  763. typedef soc_rtc_fast_clk_src_t rtc_fast_freq_t;
  764. #define RTC_FAST_FREQ_XTALD4 SOC_RTC_FAST_CLK_SRC_XTAL_DIV //!< Main XTAL, divided by 2
  765. #define RTC_FAST_FREQ_8M SOC_RTC_FAST_CLK_SRC_RC_FAST //!< Internal 17.5 MHz RC oscillator
  766. /* Alias of frequency related macros */
  767. #define RTC_FAST_CLK_FREQ_APPROX SOC_CLK_RC_FAST_FREQ_APPROX
  768. #define RTC_FAST_CLK_FREQ_8M SOC_CLK_RC_FAST_FREQ_APPROX
  769. #define RTC_SLOW_CLK_FREQ_150K SOC_CLK_RC_SLOW_FREQ_APPROX
  770. #define RTC_SLOW_CLK_FREQ_8MD256 SOC_CLK_RC_FAST_D256_FREQ_APPROX
  771. #define RTC_SLOW_CLK_FREQ_32K SOC_CLK_XTAL32K_FREQ_APPROX
  772. /* Alias of deprecated function names */
  773. #define rtc_clk_slow_freq_set(slow_freq) rtc_clk_slow_src_set(slow_freq)
  774. #define rtc_clk_slow_freq_get() rtc_clk_slow_src_get()
  775. #define rtc_clk_fast_freq_set(fast_freq) rtc_clk_fast_src_set(fast_freq)
  776. #define rtc_clk_fast_freq_get() rtc_clk_fast_src_get()
  777. #ifdef __cplusplus
  778. }
  779. #endif