pm_impl.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <sys/param.h>
  11. #include "esp_attr.h"
  12. #include "esp_err.h"
  13. #include "esp_pm.h"
  14. #include "esp_log.h"
  15. #include "esp_cpu.h"
  16. #include "esp_private/crosscore_int.h"
  17. #include "esp_private/uart_private.h"
  18. #include "soc/rtc.h"
  19. #include "hal/uart_ll.h"
  20. #include "hal/uart_types.h"
  21. #include "driver/uart.h"
  22. #include "driver/gpio.h"
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/task.h"
  25. #if CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  26. #include "freertos/xtensa_timer.h"
  27. #include "xtensa/core-macros.h"
  28. #endif
  29. #if SOC_SPI_MEM_SUPPORT_TIMING_TUNING
  30. #include "esp_private/mspi_timing_tuning.h"
  31. #endif
  32. #include "esp_private/pm_impl.h"
  33. #include "esp_private/pm_trace.h"
  34. #include "esp_private/esp_timer_private.h"
  35. #include "esp_private/esp_clk.h"
  36. #include "esp_private/sleep_cpu.h"
  37. #include "esp_private/sleep_gpio.h"
  38. #include "esp_private/sleep_modem.h"
  39. #include "esp_sleep.h"
  40. #include "esp_memory_utils.h"
  41. #include "sdkconfig.h"
  42. #define MHZ (1000000)
  43. #ifdef CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  44. /* CCOMPARE update timeout, in CPU cycles. Any value above ~600 cycles will work
  45. * for the purpose of detecting a deadlock.
  46. */
  47. #define CCOMPARE_UPDATE_TIMEOUT 1000000
  48. /* When changing CCOMPARE, don't allow changes if the difference is less
  49. * than this. This is to prevent setting CCOMPARE below CCOUNT.
  50. */
  51. #define CCOMPARE_MIN_CYCLES_IN_FUTURE 1000
  52. #endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  53. /* When light sleep is used, wake this number of microseconds earlier than
  54. * the next tick.
  55. */
  56. #define LIGHT_SLEEP_EARLY_WAKEUP_US 100
  57. #if CONFIG_IDF_TARGET_ESP32
  58. /* Minimal divider at which REF_CLK_FREQ can be obtained */
  59. #define REF_CLK_DIV_MIN 10
  60. #elif CONFIG_IDF_TARGET_ESP32S2
  61. /* Minimal divider at which REF_CLK_FREQ can be obtained */
  62. #define REF_CLK_DIV_MIN 2
  63. #elif CONFIG_IDF_TARGET_ESP32S3
  64. /* Minimal divider at which REF_CLK_FREQ can be obtained */
  65. #define REF_CLK_DIV_MIN 2 // TODO: IDF-5660
  66. #elif CONFIG_IDF_TARGET_ESP32C3
  67. #define REF_CLK_DIV_MIN 2
  68. #elif CONFIG_IDF_TARGET_ESP32C2
  69. #define REF_CLK_DIV_MIN 2
  70. #elif CONFIG_IDF_TARGET_ESP32C6
  71. #define REF_CLK_DIV_MIN 2
  72. #elif CONFIG_IDF_TARGET_ESP32H2
  73. #define REF_CLK_DIV_MIN 2
  74. #elif CONFIG_IDF_TARGET_ESP32P4
  75. #define REF_CLK_DIV_MIN 2
  76. #endif
  77. #ifdef CONFIG_PM_PROFILING
  78. #define WITH_PROFILING
  79. #endif
  80. static portMUX_TYPE s_switch_lock = portMUX_INITIALIZER_UNLOCKED;
  81. /* The following state variables are protected using s_switch_lock: */
  82. /* Current sleep mode; When switching, contains old mode until switch is complete */
  83. static pm_mode_t s_mode = PM_MODE_CPU_MAX;
  84. /* True when switch is in progress */
  85. static volatile bool s_is_switching;
  86. /* Number of times each mode was locked */
  87. static size_t s_mode_lock_counts[PM_MODE_COUNT];
  88. /* Bit mask of locked modes. BIT(i) is set iff s_mode_lock_counts[i] > 0. */
  89. static uint32_t s_mode_mask;
  90. #if CONFIG_FREERTOS_USE_TICKLESS_IDLE
  91. #define PERIPH_SKIP_LIGHT_SLEEP_NO 2
  92. /* Indicates if light sleep shoule be skipped by peripherals. */
  93. static skip_light_sleep_cb_t s_periph_skip_light_sleep_cb[PERIPH_SKIP_LIGHT_SLEEP_NO];
  94. /* Indicates if light sleep entry was skipped in vApplicationSleep for given CPU.
  95. * This in turn gets used in IDLE hook to decide if `waiti` needs
  96. * to be invoked or not.
  97. */
  98. static bool s_skipped_light_sleep[portNUM_PROCESSORS];
  99. #if portNUM_PROCESSORS == 2
  100. /* When light sleep is finished on one CPU, it is possible that the other CPU
  101. * will enter light sleep again very soon, before interrupts on the first CPU
  102. * get a chance to run. To avoid such situation, set a flag for the other CPU to
  103. * skip light sleep attempt.
  104. */
  105. static bool s_skip_light_sleep[portNUM_PROCESSORS];
  106. #endif // portNUM_PROCESSORS == 2
  107. static _lock_t s_skip_light_sleep_lock;
  108. #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
  109. /* A flag indicating that Idle hook has run on a given CPU;
  110. * Next interrupt on the same CPU will take s_rtos_lock_handle.
  111. */
  112. static bool s_core_idle[portNUM_PROCESSORS];
  113. /* When no RTOS tasks are active, these locks are released to allow going into
  114. * a lower power mode. Used by ISR hook and idle hook.
  115. */
  116. static esp_pm_lock_handle_t s_rtos_lock_handle[portNUM_PROCESSORS];
  117. /* Lookup table of CPU frequency configs to be used in each mode.
  118. * Initialized by esp_pm_impl_init and modified by esp_pm_configure.
  119. */
  120. static rtc_cpu_freq_config_t s_cpu_freq_by_mode[PM_MODE_COUNT];
  121. /* Whether automatic light sleep is enabled */
  122. static bool s_light_sleep_en = false;
  123. /* When configuration is changed, current frequency may not match the
  124. * newly configured frequency for the current mode. This is an indicator
  125. * to the mode switch code to get the actual current frequency instead of
  126. * relying on the current mode.
  127. */
  128. static bool s_config_changed = false;
  129. #ifdef WITH_PROFILING
  130. /* Time, in microseconds, spent so far in each mode */
  131. static pm_time_t s_time_in_mode[PM_MODE_COUNT];
  132. /* Timestamp, in microseconds, when the mode switch last happened */
  133. static pm_time_t s_last_mode_change_time;
  134. /* User-readable mode names, used by esp_pm_impl_dump_stats */
  135. static const char* s_mode_names[] = {
  136. "SLEEP",
  137. "APB_MIN",
  138. "APB_MAX",
  139. "CPU_MAX"
  140. };
  141. static uint32_t s_light_sleep_counts, s_light_sleep_reject_counts;
  142. #endif // WITH_PROFILING
  143. #ifdef CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  144. /* Indicates to the ISR hook that CCOMPARE needs to be updated on the given CPU.
  145. * Used in conjunction with cross-core interrupt to update CCOMPARE on the other CPU.
  146. */
  147. static volatile bool s_need_update_ccompare[portNUM_PROCESSORS];
  148. /* Divider and multiplier used to adjust (ccompare - ccount) duration.
  149. * Only set to non-zero values when switch is in progress.
  150. */
  151. static uint32_t s_ccount_div;
  152. static uint32_t s_ccount_mul;
  153. static void update_ccompare(void);
  154. #endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  155. static const char* TAG = "pm";
  156. static void do_switch(pm_mode_t new_mode);
  157. static void leave_idle(void);
  158. static void on_freq_update(uint32_t old_ticks_per_us, uint32_t ticks_per_us);
  159. pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg)
  160. {
  161. (void) arg;
  162. if (type == ESP_PM_CPU_FREQ_MAX) {
  163. return PM_MODE_CPU_MAX;
  164. } else if (type == ESP_PM_APB_FREQ_MAX) {
  165. return PM_MODE_APB_MAX;
  166. } else if (type == ESP_PM_NO_LIGHT_SLEEP) {
  167. return PM_MODE_APB_MIN;
  168. } else {
  169. // unsupported mode
  170. abort();
  171. }
  172. }
  173. #if CONFIG_PM_LIGHT_SLEEP_CALLBACKS
  174. /**
  175. * @brief Function entry parameter types for light sleep callback functions (if CONFIG_FREERTOS_USE_TICKLESS_IDLE)
  176. */
  177. struct _esp_pm_sleep_cb_config_t {
  178. /**
  179. * Callback function defined by user.
  180. */
  181. esp_pm_light_sleep_cb_t cb;
  182. /**
  183. * Input parameters of callback function defined by user.
  184. */
  185. void *arg;
  186. /**
  187. * Execution priority of callback function defined by user.
  188. */
  189. uint32_t prior;
  190. /**
  191. * Next callback function defined by user.
  192. */
  193. struct _esp_pm_sleep_cb_config_t *next;
  194. };
  195. typedef struct _esp_pm_sleep_cb_config_t esp_pm_sleep_cb_config_t;
  196. static esp_pm_sleep_cb_config_t *s_light_sleep_enter_cb_config;
  197. static esp_pm_sleep_cb_config_t *s_light_sleep_exit_cb_config;
  198. static portMUX_TYPE s_sleep_pm_cb_mutex = portMUX_INITIALIZER_UNLOCKED;
  199. esp_err_t esp_pm_light_sleep_register_cbs(esp_pm_sleep_cbs_register_config_t *cbs_conf)
  200. {
  201. if (cbs_conf->enter_cb == NULL && cbs_conf->exit_cb == NULL) {
  202. return ESP_ERR_INVALID_ARG;
  203. }
  204. portENTER_CRITICAL(&s_sleep_pm_cb_mutex);
  205. if (cbs_conf->enter_cb != NULL) {
  206. esp_pm_sleep_cb_config_t **current_enter_ptr = &(s_light_sleep_enter_cb_config);
  207. while (*current_enter_ptr != NULL) {
  208. if (((*current_enter_ptr)->cb) == (cbs_conf->enter_cb)) {
  209. portEXIT_CRITICAL(&s_sleep_pm_cb_mutex);
  210. return ESP_FAIL;
  211. }
  212. current_enter_ptr = &((*current_enter_ptr)->next);
  213. }
  214. esp_pm_sleep_cb_config_t *new_enter_config = (esp_pm_sleep_cb_config_t *)heap_caps_malloc(sizeof(esp_pm_sleep_cb_config_t), MALLOC_CAP_INTERNAL);
  215. if (new_enter_config == NULL) {
  216. portEXIT_CRITICAL(&s_sleep_pm_cb_mutex);
  217. return ESP_ERR_NO_MEM; /* Memory allocation failed */
  218. }
  219. new_enter_config->cb = cbs_conf->enter_cb;
  220. new_enter_config->arg = cbs_conf->enter_cb_user_arg;
  221. new_enter_config->prior = cbs_conf->enter_cb_prior;
  222. while (*current_enter_ptr != NULL && (*current_enter_ptr)->prior <= new_enter_config->prior) {
  223. current_enter_ptr = &((*current_enter_ptr)->next);
  224. }
  225. new_enter_config->next = *current_enter_ptr;
  226. *current_enter_ptr = new_enter_config;
  227. }
  228. if (cbs_conf->exit_cb != NULL) {
  229. esp_pm_sleep_cb_config_t **current_exit_ptr = &(s_light_sleep_exit_cb_config);
  230. while (*current_exit_ptr != NULL) {
  231. if (((*current_exit_ptr)->cb) == (cbs_conf->exit_cb)) {
  232. portEXIT_CRITICAL(&s_sleep_pm_cb_mutex);
  233. return ESP_FAIL;
  234. }
  235. current_exit_ptr = &((*current_exit_ptr)->next);
  236. }
  237. esp_pm_sleep_cb_config_t *new_exit_config = (esp_pm_sleep_cb_config_t *)heap_caps_malloc(sizeof(esp_pm_sleep_cb_config_t), MALLOC_CAP_INTERNAL);
  238. if (new_exit_config == NULL) {
  239. portEXIT_CRITICAL(&s_sleep_pm_cb_mutex);
  240. return ESP_ERR_NO_MEM; /* Memory allocation failed */
  241. }
  242. new_exit_config->cb = cbs_conf->exit_cb;
  243. new_exit_config->arg = cbs_conf->exit_cb_user_arg;
  244. new_exit_config->prior = cbs_conf->exit_cb_prior;
  245. while (*current_exit_ptr != NULL && (*current_exit_ptr)->prior <= new_exit_config->prior) {
  246. current_exit_ptr = &((*current_exit_ptr)->next);
  247. }
  248. new_exit_config->next = *current_exit_ptr;
  249. *current_exit_ptr = new_exit_config;
  250. }
  251. portEXIT_CRITICAL(&s_sleep_pm_cb_mutex);
  252. return ESP_OK;
  253. }
  254. esp_err_t esp_pm_light_sleep_unregister_cbs(esp_pm_sleep_cbs_register_config_t *cbs_conf)
  255. {
  256. if (cbs_conf->enter_cb == NULL && cbs_conf->exit_cb == NULL) {
  257. return ESP_ERR_INVALID_ARG;
  258. }
  259. portENTER_CRITICAL(&s_sleep_pm_cb_mutex);
  260. if (cbs_conf->enter_cb != NULL) {
  261. esp_pm_sleep_cb_config_t **current_enter_ptr = &(s_light_sleep_enter_cb_config);
  262. while (*current_enter_ptr != NULL) {
  263. if ((*current_enter_ptr)->cb == cbs_conf->enter_cb) {
  264. esp_pm_sleep_cb_config_t *temp = *current_enter_ptr;
  265. *current_enter_ptr = (*current_enter_ptr)->next;
  266. free(temp);
  267. break;
  268. }
  269. current_enter_ptr = &((*current_enter_ptr)->next);
  270. }
  271. }
  272. if (cbs_conf->exit_cb != NULL) {
  273. esp_pm_sleep_cb_config_t **current_exit_ptr = &(s_light_sleep_exit_cb_config);
  274. while (*current_exit_ptr != NULL) {
  275. if ((*current_exit_ptr)->cb == cbs_conf->exit_cb) {
  276. esp_pm_sleep_cb_config_t *temp = *current_exit_ptr;
  277. *current_exit_ptr = (*current_exit_ptr)->next;
  278. free(temp);
  279. break;
  280. }
  281. current_exit_ptr = &((*current_exit_ptr)->next);
  282. }
  283. }
  284. portEXIT_CRITICAL(&s_sleep_pm_cb_mutex);
  285. return ESP_OK;
  286. }
  287. static void IRAM_ATTR esp_pm_execute_enter_sleep_callbacks(int64_t sleep_time_us)
  288. {
  289. esp_pm_sleep_cb_config_t *enter_current = s_light_sleep_enter_cb_config;
  290. while (enter_current != NULL) {
  291. if (enter_current->cb != NULL) {
  292. if (ESP_OK != (*enter_current->cb)(sleep_time_us, enter_current->arg)) {
  293. ESP_EARLY_LOGW(TAG, "esp_pm_execute_enter_sleep_callbacks has an err, enter_current = %p", enter_current);
  294. }
  295. }
  296. enter_current = enter_current->next;
  297. }
  298. }
  299. static void IRAM_ATTR esp_pm_execute_exit_sleep_callbacks(int64_t sleep_time_us)
  300. {
  301. esp_pm_sleep_cb_config_t *exit_current = s_light_sleep_exit_cb_config;
  302. while (exit_current != NULL) {
  303. if (exit_current->cb != NULL) {
  304. if (ESP_OK != (*exit_current->cb)(sleep_time_us, exit_current->arg)) {
  305. ESP_EARLY_LOGW(TAG, "esp_pm_execute_exit_sleep_callbacks has an err, exit_current = %p", exit_current);
  306. }
  307. }
  308. exit_current = exit_current->next;
  309. }
  310. }
  311. #endif
  312. static esp_err_t esp_pm_sleep_configure(const void *vconfig)
  313. {
  314. esp_err_t err = ESP_OK;
  315. const esp_pm_config_t* config = (const esp_pm_config_t*) vconfig;
  316. #if SOC_PM_SUPPORT_CPU_PD
  317. err = sleep_cpu_configure(config->light_sleep_enable);
  318. if (err != ESP_OK) {
  319. return err;
  320. }
  321. #endif
  322. err = sleep_modem_configure(config->max_freq_mhz, config->min_freq_mhz, config->light_sleep_enable);
  323. return err;
  324. }
  325. esp_err_t esp_pm_configure(const void* vconfig)
  326. {
  327. #ifndef CONFIG_PM_ENABLE
  328. return ESP_ERR_NOT_SUPPORTED;
  329. #endif
  330. const esp_pm_config_t* config = (const esp_pm_config_t*) vconfig;
  331. #ifndef CONFIG_FREERTOS_USE_TICKLESS_IDLE
  332. if (config->light_sleep_enable) {
  333. return ESP_ERR_NOT_SUPPORTED;
  334. }
  335. #endif
  336. int min_freq_mhz = config->min_freq_mhz;
  337. int max_freq_mhz = config->max_freq_mhz;
  338. if (min_freq_mhz > max_freq_mhz) {
  339. return ESP_ERR_INVALID_ARG;
  340. }
  341. rtc_cpu_freq_config_t freq_config;
  342. if (!rtc_clk_cpu_freq_mhz_to_config(min_freq_mhz, &freq_config)) {
  343. ESP_LOGW(TAG, "invalid min_freq_mhz value (%d)", min_freq_mhz);
  344. return ESP_ERR_INVALID_ARG;
  345. }
  346. int xtal_freq_mhz = esp_clk_xtal_freq() / MHZ;
  347. if (min_freq_mhz < xtal_freq_mhz && min_freq_mhz * MHZ / REF_CLK_FREQ < REF_CLK_DIV_MIN) {
  348. ESP_LOGW(TAG, "min_freq_mhz should be >= %d", REF_CLK_FREQ * REF_CLK_DIV_MIN / MHZ);
  349. return ESP_ERR_INVALID_ARG;
  350. }
  351. if (!rtc_clk_cpu_freq_mhz_to_config(max_freq_mhz, &freq_config)) {
  352. ESP_LOGW(TAG, "invalid max_freq_mhz value (%d)", max_freq_mhz);
  353. return ESP_ERR_INVALID_ARG;
  354. }
  355. #if CONFIG_IDF_TARGET_ESP32
  356. int apb_max_freq = max_freq_mhz; /* CPU frequency in APB_MAX mode */
  357. if (max_freq_mhz == 240) {
  358. /* We can't switch between 240 and 80/160 without disabling PLL,
  359. * so use 240MHz CPU frequency when 80MHz APB frequency is requested.
  360. */
  361. apb_max_freq = 240;
  362. } else if (max_freq_mhz == 160 || max_freq_mhz == 80) {
  363. /* Otherwise, can use 80MHz
  364. * CPU frequency when 80MHz APB frequency is requested.
  365. */
  366. apb_max_freq = 80;
  367. }
  368. #else
  369. /* Maximum SOC APB clock frequency is 40 MHz, maximum Modem (WiFi,
  370. * Bluetooth, etc..) APB clock frequency is 80 MHz */
  371. int apb_clk_freq = esp_clk_apb_freq() / MHZ;
  372. #if CONFIG_ESP_WIFI_ENABLED || CONFIG_BT_ENABLED || CONFIG_IEEE802154_ENABLED
  373. apb_clk_freq = MAX(apb_clk_freq, MODEM_REQUIRED_MIN_APB_CLK_FREQ / MHZ);
  374. #endif
  375. int apb_max_freq = MIN(max_freq_mhz, apb_clk_freq); /* CPU frequency in APB_MAX mode */
  376. #endif
  377. apb_max_freq = MAX(apb_max_freq, min_freq_mhz);
  378. ESP_LOGI(TAG, "Frequency switching config: "
  379. "CPU_MAX: %d, APB_MAX: %d, APB_MIN: %d, Light sleep: %s",
  380. max_freq_mhz,
  381. apb_max_freq,
  382. min_freq_mhz,
  383. config->light_sleep_enable ? "ENABLED" : "DISABLED");
  384. portENTER_CRITICAL(&s_switch_lock);
  385. bool res __attribute__((unused));
  386. res = rtc_clk_cpu_freq_mhz_to_config(max_freq_mhz, &s_cpu_freq_by_mode[PM_MODE_CPU_MAX]);
  387. assert(res);
  388. res = rtc_clk_cpu_freq_mhz_to_config(apb_max_freq, &s_cpu_freq_by_mode[PM_MODE_APB_MAX]);
  389. assert(res);
  390. res = rtc_clk_cpu_freq_mhz_to_config(min_freq_mhz, &s_cpu_freq_by_mode[PM_MODE_APB_MIN]);
  391. assert(res);
  392. s_cpu_freq_by_mode[PM_MODE_LIGHT_SLEEP] = s_cpu_freq_by_mode[PM_MODE_APB_MIN];
  393. s_light_sleep_en = config->light_sleep_enable;
  394. s_config_changed = true;
  395. portEXIT_CRITICAL(&s_switch_lock);
  396. esp_pm_sleep_configure(config);
  397. return ESP_OK;
  398. }
  399. esp_err_t esp_pm_get_configuration(void* vconfig)
  400. {
  401. if (vconfig == NULL) {
  402. return ESP_ERR_INVALID_ARG;
  403. }
  404. esp_pm_config_t* config = (esp_pm_config_t*) vconfig;
  405. portENTER_CRITICAL(&s_switch_lock);
  406. config->light_sleep_enable = s_light_sleep_en;
  407. config->max_freq_mhz = s_cpu_freq_by_mode[PM_MODE_CPU_MAX].freq_mhz;
  408. config->min_freq_mhz = s_cpu_freq_by_mode[PM_MODE_APB_MIN].freq_mhz;
  409. portEXIT_CRITICAL(&s_switch_lock);
  410. return ESP_OK;
  411. }
  412. static pm_mode_t IRAM_ATTR get_lowest_allowed_mode(void)
  413. {
  414. /* TODO: optimize using ffs/clz */
  415. if (s_mode_mask >= BIT(PM_MODE_CPU_MAX)) {
  416. return PM_MODE_CPU_MAX;
  417. } else if (s_mode_mask >= BIT(PM_MODE_APB_MAX)) {
  418. return PM_MODE_APB_MAX;
  419. } else if (s_mode_mask >= BIT(PM_MODE_APB_MIN) || !s_light_sleep_en) {
  420. return PM_MODE_APB_MIN;
  421. } else {
  422. return PM_MODE_LIGHT_SLEEP;
  423. }
  424. }
  425. void IRAM_ATTR esp_pm_impl_switch_mode(pm_mode_t mode,
  426. pm_mode_switch_t lock_or_unlock, pm_time_t now)
  427. {
  428. bool need_switch = false;
  429. uint32_t mode_mask = BIT(mode);
  430. portENTER_CRITICAL_SAFE(&s_switch_lock);
  431. uint32_t count;
  432. if (lock_or_unlock == MODE_LOCK) {
  433. count = ++s_mode_lock_counts[mode];
  434. } else {
  435. count = s_mode_lock_counts[mode]--;
  436. }
  437. if (count == 1) {
  438. if (lock_or_unlock == MODE_LOCK) {
  439. s_mode_mask |= mode_mask;
  440. } else {
  441. s_mode_mask &= ~mode_mask;
  442. }
  443. need_switch = true;
  444. }
  445. pm_mode_t new_mode = s_mode;
  446. if (need_switch) {
  447. new_mode = get_lowest_allowed_mode();
  448. #ifdef WITH_PROFILING
  449. if (s_last_mode_change_time != 0) {
  450. pm_time_t diff = now - s_last_mode_change_time;
  451. s_time_in_mode[s_mode] += diff;
  452. }
  453. s_last_mode_change_time = now;
  454. #endif // WITH_PROFILING
  455. }
  456. portEXIT_CRITICAL_SAFE(&s_switch_lock);
  457. if (need_switch) {
  458. do_switch(new_mode);
  459. }
  460. }
  461. /**
  462. * @brief Update clock dividers in esp_timer and FreeRTOS, and adjust CCOMPARE
  463. * values on both CPUs.
  464. * @param old_ticks_per_us old CPU frequency
  465. * @param ticks_per_us new CPU frequency
  466. */
  467. static void IRAM_ATTR on_freq_update(uint32_t old_ticks_per_us, uint32_t ticks_per_us)
  468. {
  469. uint32_t old_apb_ticks_per_us = MIN(old_ticks_per_us, 80);
  470. uint32_t apb_ticks_per_us = MIN(ticks_per_us, 80);
  471. /* Update APB frequency value used by the timer */
  472. if (old_apb_ticks_per_us != apb_ticks_per_us) {
  473. esp_timer_private_update_apb_freq(apb_ticks_per_us);
  474. }
  475. #ifdef CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  476. #ifdef XT_RTOS_TIMER_INT
  477. /* Calculate new tick divisor */
  478. _xt_tick_divisor = ticks_per_us * MHZ / XT_TICK_PER_SEC;
  479. #endif
  480. int core_id = xPortGetCoreID();
  481. if (s_rtos_lock_handle[core_id] != NULL) {
  482. ESP_PM_TRACE_ENTER(CCOMPARE_UPDATE, core_id);
  483. /* ccount_div and ccount_mul are used in esp_pm_impl_update_ccompare
  484. * to calculate new CCOMPARE value.
  485. */
  486. s_ccount_div = old_ticks_per_us;
  487. s_ccount_mul = ticks_per_us;
  488. /* Update CCOMPARE value on this CPU */
  489. update_ccompare();
  490. #if portNUM_PROCESSORS == 2
  491. /* Send interrupt to the other CPU to update CCOMPARE value */
  492. int other_core_id = (core_id == 0) ? 1 : 0;
  493. s_need_update_ccompare[other_core_id] = true;
  494. esp_crosscore_int_send_freq_switch(other_core_id);
  495. int timeout = 0;
  496. while (s_need_update_ccompare[other_core_id]) {
  497. if (++timeout == CCOMPARE_UPDATE_TIMEOUT) {
  498. assert(false && "failed to update CCOMPARE, possible deadlock");
  499. }
  500. }
  501. #endif // portNUM_PROCESSORS == 2
  502. s_ccount_mul = 0;
  503. s_ccount_div = 0;
  504. ESP_PM_TRACE_EXIT(CCOMPARE_UPDATE, core_id);
  505. }
  506. #endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  507. }
  508. /**
  509. * Perform the switch to new power mode.
  510. * Currently only changes the CPU frequency and adjusts clock dividers.
  511. * No light sleep yet.
  512. * @param new_mode mode to switch to
  513. */
  514. static void IRAM_ATTR do_switch(pm_mode_t new_mode)
  515. {
  516. const int core_id = xPortGetCoreID();
  517. do {
  518. portENTER_CRITICAL_ISR(&s_switch_lock);
  519. if (!s_is_switching) {
  520. break;
  521. }
  522. #ifdef CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  523. if (s_need_update_ccompare[core_id]) {
  524. s_need_update_ccompare[core_id] = false;
  525. }
  526. #endif
  527. portEXIT_CRITICAL_ISR(&s_switch_lock);
  528. } while (true);
  529. if (new_mode == s_mode) {
  530. portEXIT_CRITICAL_ISR(&s_switch_lock);
  531. return;
  532. }
  533. s_is_switching = true;
  534. bool config_changed = s_config_changed;
  535. s_config_changed = false;
  536. portEXIT_CRITICAL_ISR(&s_switch_lock);
  537. rtc_cpu_freq_config_t new_config = s_cpu_freq_by_mode[new_mode];
  538. rtc_cpu_freq_config_t old_config;
  539. if (!config_changed) {
  540. old_config = s_cpu_freq_by_mode[s_mode];
  541. } else {
  542. rtc_clk_cpu_freq_get_config(&old_config);
  543. }
  544. if (new_config.freq_mhz != old_config.freq_mhz) {
  545. uint32_t old_ticks_per_us = old_config.freq_mhz;
  546. uint32_t new_ticks_per_us = new_config.freq_mhz;
  547. bool switch_down = new_ticks_per_us < old_ticks_per_us;
  548. ESP_PM_TRACE_ENTER(FREQ_SWITCH, core_id);
  549. if (switch_down) {
  550. on_freq_update(old_ticks_per_us, new_ticks_per_us);
  551. }
  552. if (new_config.source == SOC_CPU_CLK_SRC_PLL) {
  553. rtc_clk_cpu_freq_set_config_fast(&new_config);
  554. #if SOC_SPI_MEM_SUPPORT_TIMING_TUNING
  555. mspi_timing_change_speed_mode_cache_safe(false);
  556. #endif
  557. } else {
  558. #if SOC_SPI_MEM_SUPPORT_TIMING_TUNING
  559. mspi_timing_change_speed_mode_cache_safe(true);
  560. #endif
  561. rtc_clk_cpu_freq_set_config_fast(&new_config);
  562. }
  563. if (!switch_down) {
  564. on_freq_update(old_ticks_per_us, new_ticks_per_us);
  565. }
  566. ESP_PM_TRACE_EXIT(FREQ_SWITCH, core_id);
  567. }
  568. portENTER_CRITICAL_ISR(&s_switch_lock);
  569. s_mode = new_mode;
  570. s_is_switching = false;
  571. portEXIT_CRITICAL_ISR(&s_switch_lock);
  572. }
  573. #ifdef CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  574. /**
  575. * @brief Calculate new CCOMPARE value based on s_ccount_{mul,div}
  576. *
  577. * Adjusts CCOMPARE value so that the interrupt happens at the same time as it
  578. * would happen without the frequency change.
  579. * Assumes that the new_frequency = old_frequency * s_ccount_mul / s_ccount_div.
  580. */
  581. static void IRAM_ATTR update_ccompare(void)
  582. {
  583. #if CONFIG_PM_UPDATE_CCOMPARE_HLI_WORKAROUND
  584. /* disable level 4 and below */
  585. uint32_t irq_status = XTOS_SET_INTLEVEL(XCHAL_DEBUGLEVEL - 2);
  586. #endif
  587. uint32_t ccount = esp_cpu_get_cycle_count();
  588. uint32_t ccompare = XTHAL_GET_CCOMPARE(XT_TIMER_INDEX);
  589. if ((ccompare - CCOMPARE_MIN_CYCLES_IN_FUTURE) - ccount < UINT32_MAX / 2) {
  590. uint32_t diff = ccompare - ccount;
  591. uint32_t diff_scaled = (diff * s_ccount_mul + s_ccount_div - 1) / s_ccount_div;
  592. if (diff_scaled < _xt_tick_divisor) {
  593. uint32_t new_ccompare = ccount + diff_scaled;
  594. XTHAL_SET_CCOMPARE(XT_TIMER_INDEX, new_ccompare);
  595. }
  596. }
  597. #if CONFIG_PM_UPDATE_CCOMPARE_HLI_WORKAROUND
  598. XTOS_RESTORE_INTLEVEL(irq_status);
  599. #endif
  600. }
  601. #endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  602. static void IRAM_ATTR leave_idle(void)
  603. {
  604. int core_id = xPortGetCoreID();
  605. if (s_core_idle[core_id]) {
  606. // TODO: possible optimization: raise frequency here first
  607. esp_pm_lock_acquire(s_rtos_lock_handle[core_id]);
  608. s_core_idle[core_id] = false;
  609. }
  610. }
  611. #if CONFIG_FREERTOS_USE_TICKLESS_IDLE
  612. esp_err_t esp_pm_register_skip_light_sleep_callback(skip_light_sleep_cb_t cb)
  613. {
  614. _lock_acquire(&s_skip_light_sleep_lock);
  615. for (int i = 0; i < PERIPH_SKIP_LIGHT_SLEEP_NO; i++) {
  616. if (s_periph_skip_light_sleep_cb[i] == cb) {
  617. _lock_release(&s_skip_light_sleep_lock);
  618. return ESP_OK;
  619. } else if (s_periph_skip_light_sleep_cb[i] == NULL) {
  620. s_periph_skip_light_sleep_cb[i] = cb;
  621. _lock_release(&s_skip_light_sleep_lock);
  622. return ESP_OK;
  623. }
  624. }
  625. _lock_release(&s_skip_light_sleep_lock);
  626. return ESP_ERR_NO_MEM;
  627. }
  628. esp_err_t esp_pm_unregister_skip_light_sleep_callback(skip_light_sleep_cb_t cb)
  629. {
  630. _lock_acquire(&s_skip_light_sleep_lock);
  631. for (int i = 0; i < PERIPH_SKIP_LIGHT_SLEEP_NO; i++) {
  632. if (s_periph_skip_light_sleep_cb[i] == cb) {
  633. s_periph_skip_light_sleep_cb[i] = NULL;
  634. _lock_release(&s_skip_light_sleep_lock);
  635. return ESP_OK;
  636. }
  637. }
  638. _lock_release(&s_skip_light_sleep_lock);
  639. return ESP_ERR_INVALID_STATE;
  640. }
  641. static inline bool IRAM_ATTR periph_should_skip_light_sleep(void)
  642. {
  643. if (s_light_sleep_en) {
  644. for (int i = 0; i < PERIPH_SKIP_LIGHT_SLEEP_NO; i++) {
  645. if (s_periph_skip_light_sleep_cb[i]) {
  646. if (s_periph_skip_light_sleep_cb[i]() == true) {
  647. return true;
  648. }
  649. }
  650. }
  651. }
  652. return false;
  653. }
  654. static inline bool IRAM_ATTR should_skip_light_sleep(int core_id)
  655. {
  656. #if portNUM_PROCESSORS == 2
  657. if (s_skip_light_sleep[core_id]) {
  658. s_skip_light_sleep[core_id] = false;
  659. s_skipped_light_sleep[core_id] = true;
  660. return true;
  661. }
  662. #endif // portNUM_PROCESSORS == 2
  663. if (s_mode != PM_MODE_LIGHT_SLEEP || s_is_switching || periph_should_skip_light_sleep()) {
  664. s_skipped_light_sleep[core_id] = true;
  665. } else {
  666. s_skipped_light_sleep[core_id] = false;
  667. }
  668. return s_skipped_light_sleep[core_id];
  669. }
  670. static inline void IRAM_ATTR other_core_should_skip_light_sleep(int core_id)
  671. {
  672. #if portNUM_PROCESSORS == 2
  673. s_skip_light_sleep[!core_id] = true;
  674. #endif
  675. }
  676. void IRAM_ATTR vApplicationSleep( TickType_t xExpectedIdleTime )
  677. {
  678. portENTER_CRITICAL(&s_switch_lock);
  679. int core_id = xPortGetCoreID();
  680. if (!should_skip_light_sleep(core_id)) {
  681. /* Calculate how much we can sleep */
  682. int64_t next_esp_timer_alarm = esp_timer_get_next_alarm_for_wake_up();
  683. int64_t now = esp_timer_get_time();
  684. int64_t time_until_next_alarm = next_esp_timer_alarm - now;
  685. int64_t wakeup_delay_us = portTICK_PERIOD_MS * 1000LL * xExpectedIdleTime;
  686. int64_t sleep_time_us = MIN(wakeup_delay_us, time_until_next_alarm);
  687. int64_t slept_us = 0;
  688. #if CONFIG_PM_LIGHT_SLEEP_CALLBACKS
  689. uint32_t cycle = esp_cpu_get_cycle_count();
  690. esp_pm_execute_enter_sleep_callbacks(sleep_time_us);
  691. sleep_time_us -= (esp_cpu_get_cycle_count() - cycle) / (esp_clk_cpu_freq() / 1000000ULL);
  692. #endif
  693. if (sleep_time_us >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP * portTICK_PERIOD_MS * 1000LL) {
  694. esp_sleep_enable_timer_wakeup(sleep_time_us - LIGHT_SLEEP_EARLY_WAKEUP_US);
  695. #if CONFIG_PM_TRACE && SOC_PM_SUPPORT_RTC_PERIPH_PD
  696. /* to force tracing GPIOs to keep state */
  697. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  698. #endif
  699. /* Enter sleep */
  700. ESP_PM_TRACE_ENTER(SLEEP, core_id);
  701. int64_t sleep_start = esp_timer_get_time();
  702. if (esp_light_sleep_start() != ESP_OK){
  703. #ifdef WITH_PROFILING
  704. s_light_sleep_reject_counts++;
  705. } else {
  706. s_light_sleep_counts++;
  707. #endif
  708. }
  709. slept_us = esp_timer_get_time() - sleep_start;
  710. ESP_PM_TRACE_EXIT(SLEEP, core_id);
  711. uint32_t slept_ticks = slept_us / (portTICK_PERIOD_MS * 1000LL);
  712. if (slept_ticks > 0) {
  713. /* Adjust RTOS tick count based on the amount of time spent in sleep */
  714. vTaskStepTick(slept_ticks);
  715. #ifdef CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
  716. /* Trigger tick interrupt, since sleep time was longer
  717. * than portTICK_PERIOD_MS. Note that setting INTSET does not
  718. * work for timer interrupt, and changing CCOMPARE would clear
  719. * the interrupt flag.
  720. */
  721. esp_cpu_set_cycle_count(XTHAL_GET_CCOMPARE(XT_TIMER_INDEX) - 16);
  722. while (!(XTHAL_GET_INTERRUPT() & BIT(XT_TIMER_INTNUM))) {
  723. ;
  724. }
  725. #else
  726. portYIELD_WITHIN_API();
  727. #endif
  728. }
  729. other_core_should_skip_light_sleep(core_id);
  730. }
  731. #if CONFIG_PM_LIGHT_SLEEP_CALLBACKS
  732. esp_pm_execute_exit_sleep_callbacks(slept_us);
  733. #endif
  734. }
  735. portEXIT_CRITICAL(&s_switch_lock);
  736. }
  737. #endif //CONFIG_FREERTOS_USE_TICKLESS_IDLE
  738. #ifdef WITH_PROFILING
  739. void esp_pm_impl_dump_stats(FILE* out)
  740. {
  741. pm_time_t time_in_mode[PM_MODE_COUNT];
  742. portENTER_CRITICAL_ISR(&s_switch_lock);
  743. memcpy(time_in_mode, s_time_in_mode, sizeof(time_in_mode));
  744. pm_time_t last_mode_change_time = s_last_mode_change_time;
  745. pm_mode_t cur_mode = s_mode;
  746. pm_time_t now = pm_get_time();
  747. bool light_sleep_en = s_light_sleep_en;
  748. uint32_t light_sleep_counts = s_light_sleep_counts;
  749. uint32_t light_sleep_reject_counts = s_light_sleep_reject_counts;
  750. portEXIT_CRITICAL_ISR(&s_switch_lock);
  751. time_in_mode[cur_mode] += now - last_mode_change_time;
  752. fprintf(out, "\nMode stats:\n");
  753. fprintf(out, "%-8s %-10s %-10s %-10s\n", "Mode", "CPU_freq", "Time(us)", "Time(%)");
  754. for (int i = 0; i < PM_MODE_COUNT; ++i) {
  755. if (i == PM_MODE_LIGHT_SLEEP && !light_sleep_en) {
  756. /* don't display light sleep mode if it's not enabled */
  757. continue;
  758. }
  759. fprintf(out, "%-8s %-3"PRIu32"M%-7s %-10lld %-2d%%\n",
  760. s_mode_names[i],
  761. s_cpu_freq_by_mode[i].freq_mhz,
  762. "", //Empty space to align columns
  763. time_in_mode[i],
  764. (int) (time_in_mode[i] * 100 / now));
  765. }
  766. if (light_sleep_en){
  767. fprintf(out, "\nSleep stats:\n");
  768. fprintf(out, "light_sleep_counts:%ld light_sleep_reject_counts:%ld\n", light_sleep_counts, light_sleep_reject_counts);
  769. }
  770. }
  771. #endif // WITH_PROFILING
  772. int esp_pm_impl_get_cpu_freq(pm_mode_t mode)
  773. {
  774. int freq_mhz;
  775. if (mode >= PM_MODE_LIGHT_SLEEP && mode < PM_MODE_COUNT) {
  776. portENTER_CRITICAL(&s_switch_lock);
  777. freq_mhz = s_cpu_freq_by_mode[mode].freq_mhz;
  778. portEXIT_CRITICAL(&s_switch_lock);
  779. } else {
  780. abort();
  781. }
  782. return freq_mhz;
  783. }
  784. void esp_pm_impl_init(void)
  785. {
  786. #if defined(CONFIG_ESP_CONSOLE_UART)
  787. //This clock source should be a source which won't be affected by DFS
  788. uart_sclk_t clk_source = UART_SCLK_DEFAULT;
  789. #if SOC_UART_SUPPORT_REF_TICK
  790. clk_source = UART_SCLK_REF_TICK;
  791. #elif SOC_UART_SUPPORT_XTAL_CLK
  792. clk_source = UART_SCLK_XTAL;
  793. #else
  794. #error "No UART clock source is aware of DFS"
  795. #endif // SOC_UART_SUPPORT_xxx
  796. while (!uart_ll_is_tx_idle(UART_LL_GET_HW(CONFIG_ESP_CONSOLE_UART_NUM))) {
  797. ;
  798. }
  799. /* When DFS is enabled, override system setting and use REFTICK as UART clock source */
  800. HP_UART_SRC_CLK_ATOMIC() {
  801. uart_ll_set_sclk(UART_LL_GET_HW(CONFIG_ESP_CONSOLE_UART_NUM), (soc_module_clk_t)clk_source);
  802. }
  803. uint32_t sclk_freq;
  804. esp_err_t err = uart_get_sclk_freq(clk_source, &sclk_freq);
  805. assert(err == ESP_OK);
  806. HP_UART_SRC_CLK_ATOMIC() {
  807. uart_ll_set_baudrate(UART_LL_GET_HW(CONFIG_ESP_CONSOLE_UART_NUM), CONFIG_ESP_CONSOLE_UART_BAUDRATE, sclk_freq);
  808. }
  809. #endif // CONFIG_ESP_CONSOLE_UART
  810. #ifdef CONFIG_PM_TRACE
  811. esp_pm_trace_init();
  812. #endif
  813. ESP_ERROR_CHECK(esp_pm_lock_create(ESP_PM_CPU_FREQ_MAX, 0, "rtos0",
  814. &s_rtos_lock_handle[0]));
  815. ESP_ERROR_CHECK(esp_pm_lock_acquire(s_rtos_lock_handle[0]));
  816. #if portNUM_PROCESSORS == 2
  817. ESP_ERROR_CHECK(esp_pm_lock_create(ESP_PM_CPU_FREQ_MAX, 0, "rtos1",
  818. &s_rtos_lock_handle[1]));
  819. ESP_ERROR_CHECK(esp_pm_lock_acquire(s_rtos_lock_handle[1]));
  820. #endif // portNUM_PROCESSORS == 2
  821. /* Configure all modes to use the default CPU frequency.
  822. * This will be modified later by a call to esp_pm_configure.
  823. */
  824. rtc_cpu_freq_config_t default_config;
  825. if (!rtc_clk_cpu_freq_mhz_to_config(CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, &default_config)) {
  826. assert(false && "unsupported frequency");
  827. }
  828. for (size_t i = 0; i < PM_MODE_COUNT; ++i) {
  829. s_cpu_freq_by_mode[i] = default_config;
  830. }
  831. #ifdef CONFIG_PM_DFS_INIT_AUTO
  832. int xtal_freq_mhz = esp_clk_xtal_freq() / MHZ;
  833. esp_pm_config_t cfg = {
  834. .max_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ,
  835. .min_freq_mhz = xtal_freq_mhz,
  836. };
  837. esp_pm_configure(&cfg);
  838. #endif //CONFIG_PM_DFS_INIT_AUTO
  839. }
  840. void esp_pm_impl_idle_hook(void)
  841. {
  842. int core_id = xPortGetCoreID();
  843. #if CONFIG_FREERTOS_SMP
  844. uint32_t state = portDISABLE_INTERRUPTS();
  845. #else
  846. uint32_t state = portSET_INTERRUPT_MASK_FROM_ISR();
  847. #endif
  848. if (!s_core_idle[core_id]
  849. #ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE
  850. && !periph_should_skip_light_sleep()
  851. #endif
  852. ) {
  853. esp_pm_lock_release(s_rtos_lock_handle[core_id]);
  854. s_core_idle[core_id] = true;
  855. }
  856. #if CONFIG_FREERTOS_SMP
  857. portRESTORE_INTERRUPTS(state);
  858. #else
  859. portCLEAR_INTERRUPT_MASK_FROM_ISR(state);
  860. #endif
  861. ESP_PM_TRACE_ENTER(IDLE, core_id);
  862. }
  863. void IRAM_ATTR esp_pm_impl_isr_hook(void)
  864. {
  865. int core_id = xPortGetCoreID();
  866. ESP_PM_TRACE_ENTER(ISR_HOOK, core_id);
  867. /* Prevent higher level interrupts (than the one this function was called from)
  868. * from happening in this section, since they will also call into esp_pm_impl_isr_hook.
  869. */
  870. #if CONFIG_FREERTOS_SMP
  871. uint32_t state = portDISABLE_INTERRUPTS();
  872. #else
  873. uint32_t state = portSET_INTERRUPT_MASK_FROM_ISR();
  874. #endif
  875. #if defined(CONFIG_FREERTOS_SYSTICK_USES_CCOUNT) && (portNUM_PROCESSORS == 2)
  876. if (s_need_update_ccompare[core_id]) {
  877. update_ccompare();
  878. s_need_update_ccompare[core_id] = false;
  879. } else {
  880. leave_idle();
  881. }
  882. #else
  883. leave_idle();
  884. #endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT && portNUM_PROCESSORS == 2
  885. #if CONFIG_FREERTOS_SMP
  886. portRESTORE_INTERRUPTS(state);
  887. #else
  888. portCLEAR_INTERRUPT_MASK_FROM_ISR(state);
  889. #endif
  890. ESP_PM_TRACE_EXIT(ISR_HOOK, core_id);
  891. }
  892. void esp_pm_impl_waiti(void)
  893. {
  894. #if CONFIG_FREERTOS_USE_TICKLESS_IDLE
  895. int core_id = xPortGetCoreID();
  896. if (s_skipped_light_sleep[core_id]) {
  897. esp_cpu_wait_for_intr();
  898. /* Interrupt took the CPU out of waiti and s_rtos_lock_handle[core_id]
  899. * is now taken. However since we are back to idle task, we can release
  900. * the lock so that vApplicationSleep can attempt to enter light sleep.
  901. */
  902. esp_pm_impl_idle_hook();
  903. }
  904. s_skipped_light_sleep[core_id] = true;
  905. #else
  906. esp_cpu_wait_for_intr();
  907. #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
  908. }