system_api.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #if CONFIG_IDF_TARGET_ESP32S2
  7. #include "soc/rtc.h"
  8. #include "soc/rtc_cntl_reg.h"
  9. #include "esp32s2/rom/uart.h"
  10. #include "esp32s2/memprot.h"
  11. #endif
  12. #include "esp_system.h"
  13. #include "panic_internal.h"
  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. #if CONFIG_IDF_TARGET_ESP32S2
  39. static __attribute__((noreturn)) void esp_digital_reset(void)
  40. {
  41. // make sure all the panic handler output is sent from UART FIFO
  42. uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  43. // switch to XTAL (otherwise we will keep running from the PLL)
  44. rtc_clk_cpu_freq_set_xtal();
  45. // reset the digital part
  46. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  47. while (true) {
  48. ;
  49. }
  50. }
  51. #endif
  52. void IRAM_ATTR esp_restart(void)
  53. {
  54. for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) {
  55. if (shutdown_handlers[i]) {
  56. shutdown_handlers[i]();
  57. }
  58. }
  59. // Disable scheduler on this core.
  60. vTaskSuspendAll();
  61. #if CONFIG_IDF_TARGET_ESP32S2
  62. if ( esp_memprot_is_intr_ena_any() || esp_memprot_is_locked_any()) {
  63. esp_digital_reset();
  64. }
  65. #endif
  66. esp_restart_noos();
  67. }
  68. uint32_t esp_get_free_heap_size( void )
  69. {
  70. return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
  71. }
  72. uint32_t esp_get_minimum_free_heap_size( void )
  73. {
  74. return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
  75. }
  76. const char *esp_get_idf_version(void)
  77. {
  78. return IDF_VER;
  79. }
  80. void __attribute__((noreturn)) esp_system_abort(const char *details)
  81. {
  82. panic_abort(details);
  83. }