system_api.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/rtc.h"
  7. #include "soc/rtc_cntl_reg.h"
  8. #include "panic_internal.h"
  9. #if CONFIG_IDF_TARGET_ESP32
  10. #include "esp32/rom/uart.h"
  11. #elif CONFIG_IDF_TARGET_ESP32S2
  12. #include "esp32s2/rom/uart.h"
  13. #include "esp32s2/memprot.h"
  14. #endif
  15. #define SHUTDOWN_HANDLERS_NO 2
  16. static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
  17. esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
  18. {
  19. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  20. if (shutdown_handlers[i] == handler) {
  21. return ESP_ERR_INVALID_STATE;
  22. } else if (shutdown_handlers[i] == NULL) {
  23. shutdown_handlers[i] = handler;
  24. return ESP_OK;
  25. }
  26. }
  27. return ESP_ERR_NO_MEM;
  28. }
  29. esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler)
  30. {
  31. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  32. if (shutdown_handlers[i] == handler) {
  33. shutdown_handlers[i] = NULL;
  34. return ESP_OK;
  35. }
  36. }
  37. return ESP_ERR_INVALID_STATE;
  38. }
  39. void IRAM_ATTR esp_restart_noos_dig(void)
  40. {
  41. // make sure all the panic handler output is sent from UART FIFO
  42. if (CONFIG_ESP_CONSOLE_UART_NUM >= 0) {
  43. uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  44. }
  45. // switch to XTAL (otherwise we will keep running from the PLL)
  46. rtc_clk_cpu_freq_set_xtal();
  47. #if CONFIG_IDF_TARGET_ESP32
  48. esp_cpu_unstall(PRO_CPU_NUM);
  49. #endif
  50. // reset the digital part
  51. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  52. while (true) {
  53. ;
  54. }
  55. }
  56. void IRAM_ATTR esp_restart(void)
  57. {
  58. for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) {
  59. if (shutdown_handlers[i]) {
  60. shutdown_handlers[i]();
  61. }
  62. }
  63. // Disable scheduler on this core.
  64. vTaskSuspendAll();
  65. #if CONFIG_IDF_TARGET_ESP32S2
  66. if (esp_memprot_is_intr_ena_any() || esp_memprot_is_locked_any()) {
  67. esp_restart_noos_dig();
  68. }
  69. #endif
  70. esp_restart_noos();
  71. }
  72. uint32_t esp_get_free_heap_size( void )
  73. {
  74. return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
  75. }
  76. uint32_t esp_get_minimum_free_heap_size( void )
  77. {
  78. return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
  79. }
  80. const char *esp_get_idf_version(void)
  81. {
  82. return IDF_VER;
  83. }
  84. void __attribute__((noreturn)) esp_system_abort(const char *details)
  85. {
  86. panic_abort(details);
  87. }