esp_system.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_system.h"
  7. #include "esp_private/system_internal.h"
  8. #include "esp_heap_caps.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_cpu.h"
  12. #include "soc/rtc.h"
  13. #include "soc/rtc_cntl_reg.h"
  14. #include "esp_private/panic_internal.h"
  15. #include "esp_rom_uart.h"
  16. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  17. #if CONFIG_IDF_TARGET_ESP32S2
  18. #include "esp32s2/memprot.h"
  19. #elif CONFIG_IDF_TARGET_ESP32C2
  20. #include "esp32c2/memprot.h"
  21. #else
  22. #include "esp_memprot.h"
  23. #endif
  24. #endif
  25. #define SHUTDOWN_HANDLERS_NO 5
  26. static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
  27. void IRAM_ATTR esp_restart_noos_dig(void)
  28. {
  29. // make sure all the panic handler output is sent from UART FIFO
  30. if (CONFIG_ESP_CONSOLE_UART_NUM >= 0) {
  31. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  32. }
  33. // switch to XTAL (otherwise we will keep running from the PLL)
  34. rtc_clk_cpu_freq_set_xtal();
  35. #if CONFIG_IDF_TARGET_ESP32
  36. esp_cpu_unstall(PRO_CPU_NUM);
  37. #endif
  38. // reset the digital part
  39. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  40. while (true) {
  41. ;
  42. }
  43. }
  44. esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
  45. {
  46. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  47. if (shutdown_handlers[i] == handler) {
  48. return ESP_ERR_INVALID_STATE;
  49. } else if (shutdown_handlers[i] == NULL) {
  50. shutdown_handlers[i] = handler;
  51. return ESP_OK;
  52. }
  53. }
  54. return ESP_ERR_NO_MEM;
  55. }
  56. esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler)
  57. {
  58. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  59. if (shutdown_handlers[i] == handler) {
  60. shutdown_handlers[i] = NULL;
  61. return ESP_OK;
  62. }
  63. }
  64. return ESP_ERR_INVALID_STATE;
  65. }
  66. void IRAM_ATTR esp_restart(void)
  67. {
  68. for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) {
  69. if (shutdown_handlers[i]) {
  70. shutdown_handlers[i]();
  71. }
  72. }
  73. #ifdef CONFIG_FREERTOS_SMP
  74. //Note: Scheduler suspension behavior changed in FreeRTOS SMP
  75. vTaskPreemptionDisable(NULL);
  76. #else
  77. // Disable scheduler on this core.
  78. vTaskSuspendAll();
  79. #endif // CONFIG_FREERTOS_SMP
  80. bool digital_reset_needed = false;
  81. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  82. #if CONFIG_IDF_TARGET_ESP32S2
  83. if (esp_memprot_is_intr_ena_any() || esp_memprot_is_locked_any()) {
  84. digital_reset_needed = true;
  85. }
  86. #else
  87. bool is_on = false;
  88. if (esp_mprot_is_intr_ena_any(&is_on) != ESP_OK || is_on) {
  89. digital_reset_needed = true;
  90. } else if (esp_mprot_is_conf_locked_any(&is_on) != ESP_OK || is_on) {
  91. digital_reset_needed = true;
  92. }
  93. #endif
  94. #endif
  95. if (digital_reset_needed) {
  96. esp_restart_noos_dig();
  97. }
  98. esp_restart_noos();
  99. }
  100. uint32_t esp_get_free_heap_size( void )
  101. {
  102. return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
  103. }
  104. uint32_t esp_get_free_internal_heap_size( void )
  105. {
  106. return heap_caps_get_free_size( MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
  107. }
  108. uint32_t esp_get_minimum_free_heap_size( void )
  109. {
  110. return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
  111. }
  112. const char *esp_get_idf_version(void)
  113. {
  114. return IDF_VER;
  115. }
  116. void __attribute__((noreturn)) esp_system_abort(const char *details)
  117. {
  118. panic_abort(details);
  119. }