system_api.c 2.8 KB

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