1 // Copyright 2015-2016 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 15 #include "esp_system.h" 16 #include "esp_private/system_internal.h" 17 #include "esp_heap_caps.h" 18 #include "esp_osal/esp_osal.h" 19 #include "esp_osal/task.h" 20 #include "soc/cpu.h" 21 #include "soc/rtc.h" 22 #include "soc/rtc_cntl_reg.h" 23 #include "esp_private/panic_internal.h" 24 #include "esp_rom_uart.h" 25 #if CONFIG_IDF_TARGET_ESP32S2 26 #include "esp32s2/memprot.h" 27 #elif CONFIG_IDF_TARGET_ESP32S3 28 #include "esp32s3/memprot.h" 29 #elif CONFIG_IDF_TARGET_ESP32C3 30 #include "esp32c3/memprot.h" 31 #endif 32 33 34 #define SHUTDOWN_HANDLERS_NO 4 35 static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO]; 36 esp_register_shutdown_handler(shutdown_handler_t handler)37esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler) 38 { 39 for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) { 40 if (shutdown_handlers[i] == handler) { 41 return ESP_ERR_INVALID_STATE; 42 } else if (shutdown_handlers[i] == NULL) { 43 shutdown_handlers[i] = handler; 44 return ESP_OK; 45 } 46 } 47 return ESP_ERR_NO_MEM; 48 } 49 esp_unregister_shutdown_handler(shutdown_handler_t handler)50esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler) 51 { 52 for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) { 53 if (shutdown_handlers[i] == handler) { 54 shutdown_handlers[i] = NULL; 55 return ESP_OK; 56 } 57 } 58 return ESP_ERR_INVALID_STATE; 59 } 60 esp_restart_noos_dig(void)61void IRAM_ATTR esp_restart_noos_dig(void) 62 { 63 // make sure all the panic handler output is sent from UART FIFO 64 if (CONFIG_ESP_CONSOLE_UART_NUM >= 0) { 65 esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); 66 } 67 68 // switch to XTAL (otherwise we will keep running from the PLL) 69 rtc_clk_cpu_freq_set_xtal(); 70 71 #if CONFIG_IDF_TARGET_ESP32 72 esp_cpu_unstall(PRO_CPU_NUM); 73 #endif 74 // reset the digital part 75 SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST); 76 while (true) { 77 ; 78 } 79 } 80 esp_restart(void)81void IRAM_ATTR esp_restart(void) 82 { 83 for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) { 84 if (shutdown_handlers[i]) { 85 shutdown_handlers[i](); 86 } 87 } 88 89 // Disable scheduler on this core. 90 LOS_TaskLock(); // vTaskSuspendAll(); 91 92 bool digital_reset_needed = false; 93 #if CONFIG_ESP_SYSTEM_CONFIG_MEMPROT_FEATURE 94 if (esp_memprot_is_intr_ena_any() || esp_memprot_is_locked_any()) { 95 digital_reset_needed = true; 96 } 97 #endif 98 if (digital_reset_needed) { 99 esp_restart_noos_dig(); 100 } 101 esp_restart_noos(); 102 } 103 esp_get_free_heap_size(void)104uint32_t esp_get_free_heap_size( void ) 105 { 106 return heap_caps_get_free_size( MALLOC_CAP_DEFAULT ); 107 } 108 esp_get_free_internal_heap_size(void)109uint32_t esp_get_free_internal_heap_size( void ) 110 { 111 return heap_caps_get_free_size( MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL ); 112 } 113 esp_get_minimum_free_heap_size(void)114uint32_t esp_get_minimum_free_heap_size( void ) 115 { 116 return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT ); 117 } 118 esp_get_idf_version(void)119const char *esp_get_idf_version(void) 120 { 121 return IDF_VER; 122 } 123 esp_system_abort(const char * details)124void __attribute__((noreturn)) esp_system_abort(const char *details) 125 { 126 panic_abort(details); 127 } 128