system_api.c 2.6 KB

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