esp_system.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "esp_system.h"
  15. #include "esp_private/system_internal.h"
  16. #include "esp_heap_caps.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "soc/cpu.h"
  20. #include "soc/rtc.h"
  21. #include "soc/rtc_cntl_reg.h"
  22. #include "esp_private/panic_internal.h"
  23. #include "esp_rom_uart.h"
  24. #if CONFIG_IDF_TARGET_ESP32S2
  25. #include "esp32s2/memprot.h"
  26. #elif CONFIG_IDF_TARGET_ESP32S3
  27. #include "esp32s3/memprot.h"
  28. #elif CONFIG_IDF_TARGET_ESP32C3
  29. #include "esp32c3/memprot.h"
  30. #elif CONFIG_IDF_TARGET_ESP32H2
  31. #include "esp32h2/memprot.h"
  32. #endif
  33. #define SHUTDOWN_HANDLERS_NO 5
  34. static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
  35. void IRAM_ATTR esp_restart_noos_dig(void)
  36. {
  37. // make sure all the panic handler output is sent from UART FIFO
  38. if (CONFIG_ESP_CONSOLE_UART_NUM >= 0) {
  39. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  40. }
  41. // switch to XTAL (otherwise we will keep running from the PLL)
  42. rtc_clk_cpu_freq_set_xtal();
  43. #if CONFIG_IDF_TARGET_ESP32
  44. esp_cpu_unstall(PRO_CPU_NUM);
  45. #endif
  46. // reset the digital part
  47. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  48. while (true) {
  49. ;
  50. }
  51. }
  52. esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
  53. {
  54. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  55. if (shutdown_handlers[i] == handler) {
  56. return ESP_ERR_INVALID_STATE;
  57. } else if (shutdown_handlers[i] == NULL) {
  58. shutdown_handlers[i] = handler;
  59. return ESP_OK;
  60. }
  61. }
  62. return ESP_ERR_NO_MEM;
  63. }
  64. esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler)
  65. {
  66. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  67. if (shutdown_handlers[i] == handler) {
  68. shutdown_handlers[i] = NULL;
  69. return ESP_OK;
  70. }
  71. }
  72. return ESP_ERR_INVALID_STATE;
  73. }
  74. void IRAM_ATTR esp_restart(void)
  75. {
  76. for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) {
  77. if (shutdown_handlers[i]) {
  78. shutdown_handlers[i]();
  79. }
  80. }
  81. // Disable scheduler on this core.
  82. vTaskSuspendAll();
  83. bool digital_reset_needed = false;
  84. #if CONFIG_ESP_SYSTEM_CONFIG_MEMPROT_FEATURE
  85. if (esp_memprot_is_intr_ena_any() || esp_memprot_is_locked_any()) {
  86. digital_reset_needed = true;
  87. }
  88. #endif
  89. if (digital_reset_needed) {
  90. esp_restart_noos_dig();
  91. }
  92. esp_restart_noos();
  93. }
  94. uint32_t esp_get_free_heap_size( void )
  95. {
  96. return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
  97. }
  98. uint32_t esp_get_free_internal_heap_size( void )
  99. {
  100. return heap_caps_get_free_size( MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
  101. }
  102. uint32_t esp_get_minimum_free_heap_size( void )
  103. {
  104. return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
  105. }
  106. const char *esp_get_idf_version(void)
  107. {
  108. return IDF_VER;
  109. }
  110. void __attribute__((noreturn)) esp_system_abort(const char *details)
  111. {
  112. panic_abort(details);
  113. }