esp_system.c 3.4 KB

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