system_api.c 2.6 KB

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