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