cpu_start.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // Copyright 2015-2018 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 <stdint.h>
  15. #include <string.h>
  16. #include <stdbool.h>
  17. #include "esp_attr.h"
  18. #include "esp_err.h"
  19. #include "esp_log.h"
  20. #include "esp_system.h"
  21. #include "esp_efuse.h"
  22. #include "esp_clk_internal.h"
  23. #include "esp_rom_efuse.h"
  24. #include "esp_rom_uart.h"
  25. #include "esp_rom_sys.h"
  26. #include "sdkconfig.h"
  27. #if CONFIG_IDF_TARGET_ESP32
  28. #include "soc/dport_reg.h"
  29. #include "esp32/rtc.h"
  30. #include "esp32/cache_err_int.h"
  31. #include "esp32/rom/cache.h"
  32. #include "esp32/rom/rtc.h"
  33. #include "esp32/spiram.h"
  34. #elif CONFIG_IDF_TARGET_ESP32S2
  35. #include "esp32s2/rtc.h"
  36. #include "esp32s2/brownout.h"
  37. #include "esp32s2/cache_err_int.h"
  38. #include "esp32s2/rom/cache.h"
  39. #include "esp32s2/rom/rtc.h"
  40. #include "esp32s2/spiram.h"
  41. #include "esp32s2/dport_access.h"
  42. #include "esp32s2/memprot.h"
  43. #elif CONFIG_IDF_TARGET_ESP32S3
  44. #include "esp32s3/rtc.h"
  45. #include "esp32s3/brownout.h"
  46. #include "esp32s3/cache_err_int.h"
  47. #include "esp32s3/rom/cache.h"
  48. #include "esp32s3/rom/rtc.h"
  49. #include "esp32s3/spiram.h"
  50. #include "esp32s3/dport_access.h"
  51. #include "esp32s3/memprot.h"
  52. #include "soc/assist_debug_reg.h"
  53. #include "soc/cache_memory.h"
  54. #include "soc/system_reg.h"
  55. #elif CONFIG_IDF_TARGET_ESP32C3
  56. #include "esp32c3/rtc.h"
  57. #include "esp32c3/cache_err_int.h"
  58. #include "esp32s3/rom/cache.h"
  59. #include "esp32c3/rom/rtc.h"
  60. #include "soc/cache_memory.h"
  61. #include "esp32c3/memprot.h"
  62. #endif
  63. #include "bootloader_flash_config.h"
  64. #include "esp_private/crosscore_int.h"
  65. #include "esp_flash_encrypt.h"
  66. #include "hal/rtc_io_hal.h"
  67. #include "hal/gpio_hal.h"
  68. #include "hal/wdt_hal.h"
  69. #include "soc/rtc.h"
  70. #include "soc/efuse_reg.h"
  71. #include "soc/periph_defs.h"
  72. #include "soc/cpu.h"
  73. #include "soc/rtc.h"
  74. #include "soc/spinlock.h"
  75. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  76. #include "trax.h"
  77. #endif
  78. #include "bootloader_mem.h"
  79. #if CONFIG_APP_BUILD_TYPE_ELF_RAM
  80. #if CONFIG_IDF_TARGET_ESP32
  81. #include "esp32/rom/spi_flash.h"
  82. #endif // CONFIG_IDF_TARGET_ESP32
  83. #if CONFIG_IDF_TARGET_ESP32S2
  84. #include "esp32s2/rom/spi_flash.h"
  85. #endif // CONFIG_IDF_TARGET_ESP32S2
  86. #if CONFIG_IDF_TARGET_ESP32S3
  87. #include "esp32s3/rom/spi_flash.h"
  88. #endif // CONFIG_IDF_TARGET_ESP32S3
  89. #if CONFIG_IDF_TARGET_ESP32C3
  90. #include "esp32c3/rom/spi_flash.h"
  91. #endif // CONFIG_IDF_TARGET_ESP32C3
  92. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  93. // Set efuse ROM_LOG_MODE on first boot
  94. //
  95. // For CONFIG_BOOT_ROM_LOG_ALWAYS_ON (default) or undefined (ESP32), leave
  96. // ROM_LOG_MODE undefined (no need to call this function during startup)
  97. #if CONFIG_BOOT_ROM_LOG_ALWAYS_OFF
  98. #define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ALWAYS_OFF
  99. #elif CONFIG_BOOT_ROM_LOG_ON_GPIO_LOW
  100. #define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_LOW
  101. #elif CONFIG_BOOT_ROM_LOG_ON_GPIO_HIGH
  102. #define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_HIGH
  103. #endif
  104. #include "esp_private/startup_internal.h"
  105. #include "esp_private/system_internal.h"
  106. extern int _bss_start;
  107. extern int _bss_end;
  108. extern int _rtc_bss_start;
  109. extern int _rtc_bss_end;
  110. extern int _vector_table;
  111. static const char *TAG = "cpu_start";
  112. #if CONFIG_IDF_TARGET_ESP32
  113. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  114. extern int _ext_ram_bss_start;
  115. extern int _ext_ram_bss_end;
  116. #endif
  117. #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
  118. extern int _iram_bss_start;
  119. extern int _iram_bss_end;
  120. #endif
  121. #endif // CONFIG_IDF_TARGET_ESP32
  122. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  123. static volatile bool s_cpu_up[SOC_CPU_CORES_NUM] = { false };
  124. static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false };
  125. static volatile bool s_resume_cores;
  126. #endif
  127. // If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false.
  128. bool g_spiram_ok = true;
  129. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  130. void startup_resume_other_cores(void)
  131. {
  132. s_resume_cores = true;
  133. }
  134. void IRAM_ATTR call_start_cpu1(void)
  135. {
  136. cpu_hal_set_vecbase(&_vector_table);
  137. ets_set_appcpu_boot_addr(0);
  138. bootloader_init_mem();
  139. #if CONFIG_ESP_CONSOLE_UART_NONE
  140. esp_rom_install_channel_putc(1, NULL);
  141. esp_rom_install_channel_putc(2, NULL);
  142. #else // CONFIG_ESP_CONSOLE_UART_NONE
  143. esp_rom_install_uart_printf();
  144. esp_rom_uart_set_as_console(CONFIG_ESP_CONSOLE_UART_NUM);
  145. #endif
  146. #if CONFIG_IDF_TARGET_ESP32
  147. DPORT_REG_SET_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_PDEBUG_ENABLE | DPORT_APP_CPU_RECORD_ENABLE);
  148. DPORT_REG_CLR_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_RECORD_ENABLE);
  149. #else
  150. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_PDEBUGENABLE_REG, 1);
  151. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_RECORDING_REG, 1);
  152. #endif
  153. s_cpu_up[1] = true;
  154. ESP_EARLY_LOGI(TAG, "App cpu up.");
  155. //Take care putting stuff here: if asked, FreeRTOS will happily tell you the scheduler
  156. //has started, but it isn't active *on this CPU* yet.
  157. esp_cache_err_int_init();
  158. #if CONFIG_IDF_TARGET_ESP32
  159. #if CONFIG_ESP32_TRAX_TWOBANKS
  160. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  161. #endif
  162. #endif
  163. s_cpu_inited[1] = true;
  164. while (!s_resume_cores) {
  165. esp_rom_delay_us(100);
  166. }
  167. SYS_STARTUP_FN();
  168. }
  169. static void start_other_core(void)
  170. {
  171. // If not the single core variant of ESP32 - check this since there is
  172. // no separate soc_caps.h for the single core variant.
  173. bool is_single_core = false;
  174. #if CONFIG_IDF_TARGET_ESP32
  175. is_single_core = REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_DIS_APP_CPU);
  176. #endif
  177. if (!is_single_core) {
  178. ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  179. #if CONFIG_IDF_TARGET_ESP32
  180. Cache_Flush(1);
  181. Cache_Read_Enable(1);
  182. #endif
  183. esp_cpu_unstall(1);
  184. // Enable clock and reset APP CPU. Note that OpenOCD may have already
  185. // enabled clock and taken APP CPU out of reset. In this case don't reset
  186. // APP CPU again, as that will clear the breakpoints which may have already
  187. // been set.
  188. #if CONFIG_IDF_TARGET_ESP32
  189. if (!DPORT_GET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN)) {
  190. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  191. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  192. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  193. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  194. }
  195. #elif CONFIG_IDF_TARGET_ESP32S3
  196. if (!REG_GET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN)) {
  197. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  198. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  199. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  200. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  201. }
  202. #endif
  203. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  204. volatile bool cpus_up = false;
  205. while (!cpus_up) {
  206. cpus_up = true;
  207. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  208. cpus_up &= s_cpu_up[i];
  209. }
  210. esp_rom_delay_us(100);
  211. }
  212. }
  213. }
  214. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  215. static void intr_matrix_clear(void)
  216. {
  217. for (int i = 0; i < ETS_MAX_INTR_SOURCE; i++) {
  218. intr_matrix_set(0, i, ETS_INVALID_INUM);
  219. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  220. intr_matrix_set(1, i, ETS_INVALID_INUM);
  221. #endif
  222. }
  223. }
  224. /*
  225. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  226. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  227. */
  228. void IRAM_ATTR call_start_cpu0(void)
  229. {
  230. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  231. RESET_REASON rst_reas[SOC_CPU_CORES_NUM];
  232. #else
  233. RESET_REASON rst_reas[1];
  234. #endif
  235. #ifdef __riscv
  236. // Configure the global pointer register
  237. // (This should be the first thing IDF app does, as any other piece of code could be
  238. // relaxed by the linker to access something relative to __global_pointer$)
  239. __asm__ __volatile__ (
  240. ".option push\n"
  241. ".option norelax\n"
  242. "la gp, __global_pointer$\n"
  243. ".option pop"
  244. );
  245. #endif
  246. // Move exception vectors to IRAM
  247. cpu_hal_set_vecbase(&_vector_table);
  248. rst_reas[0] = rtc_get_reset_reason(0);
  249. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  250. rst_reas[1] = rtc_get_reset_reason(1);
  251. #endif
  252. #ifndef CONFIG_BOOTLOADER_WDT_ENABLE
  253. // from panic handler we can be reset by RWDT or TG0WDT
  254. if (rst_reas[0] == RTCWDT_SYS_RESET || rst_reas[0] == TG0WDT_SYS_RESET
  255. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  256. || rst_reas[1] == RTCWDT_SYS_RESET || rst_reas[1] == TG0WDT_SYS_RESET
  257. #endif
  258. ) {
  259. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  260. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  261. wdt_hal_disable(&rtc_wdt_ctx);
  262. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  263. }
  264. #endif
  265. //Clear BSS. Please do not attempt to do any complex stuff (like early logging) before this.
  266. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  267. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
  268. // Clear IRAM BSS
  269. memset(&_iram_bss_start, 0, (&_iram_bss_end - &_iram_bss_start) * sizeof(_iram_bss_start));
  270. #endif
  271. /* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
  272. if (rst_reas[0] != DEEPSLEEP_RESET) {
  273. memset(&_rtc_bss_start, 0, (&_rtc_bss_end - &_rtc_bss_start) * sizeof(_rtc_bss_start));
  274. }
  275. #if CONFIG_IDF_TARGET_ESP32S2
  276. /* Configure the mode of instruction cache : cache size, cache associated ways, cache line size. */
  277. extern void esp_config_instruction_cache_mode(void);
  278. esp_config_instruction_cache_mode();
  279. /* If we need use SPIRAM, we should use data cache, or if we want to access rodata, we also should use data cache.
  280. Configure the mode of data : cache size, cache associated ways, cache line size.
  281. Enable data cache, so if we don't use SPIRAM, it just works. */
  282. #if CONFIG_SPIRAM_BOOT_INIT
  283. extern void esp_config_data_cache_mode(void);
  284. esp_config_data_cache_mode();
  285. Cache_Enable_DCache(0);
  286. #endif
  287. #endif
  288. #if CONFIG_IDF_TARGET_ESP32S3
  289. /* Configure the mode of instruction cache : cache size, cache line size. */
  290. extern void rom_config_instruction_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  291. rom_config_instruction_cache_mode(CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE, CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE);
  292. /* If we need use SPIRAM, we should use data cache.
  293. Configure the mode of data : cache size, cache line size.*/
  294. Cache_Suspend_DCache();
  295. extern void rom_config_data_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  296. rom_config_data_cache_mode(CONFIG_ESP32S3_DATA_CACHE_SIZE, CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE);
  297. Cache_Resume_DCache(0);
  298. #endif // CONFIG_IDF_TARGET_ESP32S3
  299. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  300. /* Configure the Cache MMU size for instruction and rodata in flash. */
  301. extern uint32_t Cache_Set_IDROM_MMU_Size(uint32_t irom_size, uint32_t drom_size);
  302. extern int _rodata_reserved_start;
  303. uint32_t rodata_reserved_start_align = (uint32_t)&_rodata_reserved_start & ~(MMU_PAGE_SIZE - 1);
  304. uint32_t cache_mmu_irom_size = ((rodata_reserved_start_align - SOC_DROM_LOW) / MMU_PAGE_SIZE) * sizeof(uint32_t);
  305. Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size);
  306. #endif // CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  307. bootloader_init_mem();
  308. #if CONFIG_SPIRAM_BOOT_INIT
  309. if (esp_spiram_init() != ESP_OK) {
  310. #if CONFIG_IDF_TARGET_ESP32
  311. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  312. ESP_EARLY_LOGE(TAG, "Failed to init external RAM, needed for external .bss segment");
  313. abort();
  314. #endif
  315. #endif
  316. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  317. ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it.");
  318. g_spiram_ok = false;
  319. #else
  320. ESP_EARLY_LOGE(TAG, "Failed to init external RAM!");
  321. abort();
  322. #endif
  323. }
  324. if (g_spiram_ok) {
  325. esp_spiram_init_cache();
  326. }
  327. #endif
  328. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  329. s_cpu_up[0] = true;
  330. #endif
  331. ESP_EARLY_LOGI(TAG, "Pro cpu up.");
  332. #if SOC_CPU_CORES_NUM > 1 // there is no 'single-core mode' for natively single-core processors
  333. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  334. start_other_core();
  335. #else
  336. ESP_EARLY_LOGI(TAG, "Single core mode");
  337. #if CONFIG_IDF_TARGET_ESP32
  338. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); // stop the other core
  339. #elif CONFIG_IDF_TARGET_ESP32S3
  340. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  341. #endif
  342. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  343. #endif // SOC_CPU_CORES_NUM > 1
  344. #if CONFIG_SPIRAM_MEMTEST
  345. if (g_spiram_ok) {
  346. bool ext_ram_ok = esp_spiram_test();
  347. if (!ext_ram_ok) {
  348. ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
  349. abort();
  350. }
  351. }
  352. #endif
  353. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  354. extern void instruction_flash_page_info_init(void);
  355. instruction_flash_page_info_init();
  356. #endif
  357. #if CONFIG_SPIRAM_RODATA
  358. extern void rodata_flash_page_info_init(void);
  359. rodata_flash_page_info_init();
  360. #endif
  361. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  362. extern void esp_spiram_enable_instruction_access(void);
  363. esp_spiram_enable_instruction_access();
  364. #endif
  365. #if CONFIG_SPIRAM_RODATA
  366. extern void esp_spiram_enable_rodata_access(void);
  367. esp_spiram_enable_rodata_access();
  368. #endif
  369. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S2_DATA_CACHE_WRAP
  370. uint32_t icache_wrap_enable = 0, dcache_wrap_enable = 0;
  371. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP
  372. icache_wrap_enable = 1;
  373. #endif
  374. #if CONFIG_ESP32S2_DATA_CACHE_WRAP
  375. dcache_wrap_enable = 1;
  376. #endif
  377. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable, uint32_t dcache_wrap_enable);
  378. esp_enable_cache_wrap(icache_wrap_enable, dcache_wrap_enable);
  379. #endif
  380. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  381. memset(&_ext_ram_bss_start, 0, (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
  382. #endif
  383. //Enable trace memory and immediately start trace.
  384. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  385. #if CONFIG_IDF_TARGET_ESP32
  386. #if CONFIG_ESP32_TRAX_TWOBANKS
  387. trax_enable(TRAX_ENA_PRO_APP);
  388. #else
  389. trax_enable(TRAX_ENA_PRO);
  390. #endif
  391. #elif CONFIG_IDF_TARGET_ESP32S2
  392. trax_enable(TRAX_ENA_PRO);
  393. #endif
  394. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  395. #endif // CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  396. esp_clk_init();
  397. esp_perip_clk_init();
  398. // Now that the clocks have been set-up, set the startup time from RTC
  399. // and default RTC-backed system time provider.
  400. g_startup_time = esp_rtc_get_time_us();
  401. intr_matrix_clear();
  402. #ifdef CONFIG_ESP_CONSOLE_UART
  403. uint32_t clock_hz = rtc_clk_apb_freq_get();
  404. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  405. clock_hz = UART_CLK_FREQ_ROM; // From esp32-s3 on, UART clock source is selected to XTAL in ROM
  406. #endif
  407. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  408. esp_rom_uart_set_clock_baudrate(CONFIG_ESP_CONSOLE_UART_NUM, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  409. #endif
  410. #if SOC_RTCIO_HOLD_SUPPORTED
  411. rtcio_hal_unhold_all();
  412. #else
  413. gpio_hal_force_unhold_all();
  414. #endif
  415. esp_cache_err_int_init();
  416. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  417. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  418. esp_memprot_set_prot(true, true, NULL);
  419. #else
  420. esp_memprot_set_prot(true, false, NULL);
  421. #endif
  422. #endif
  423. bootloader_flash_update_id();
  424. // Read the application binary image header. This will also decrypt the header if the image is encrypted.
  425. __attribute__((unused)) esp_image_header_t fhdr = {0};
  426. #ifdef CONFIG_APP_BUILD_TYPE_ELF_RAM
  427. fhdr.spi_mode = ESP_IMAGE_SPI_MODE_DIO;
  428. fhdr.spi_speed = ESP_IMAGE_SPI_SPEED_40M;
  429. fhdr.spi_size = ESP_IMAGE_FLASH_SIZE_4MB;
  430. extern void esp_rom_spiflash_attach(uint32_t, bool);
  431. esp_rom_spiflash_attach(esp_rom_efuse_get_flash_gpio_info(), false);
  432. esp_rom_spiflash_unlock();
  433. #else
  434. // This assumes that DROM is the first segment in the application binary, i.e. that we can read
  435. // the binary header through cache by accessing SOC_DROM_LOW address.
  436. memcpy(&fhdr, (void *) SOC_DROM_LOW, sizeof(fhdr));
  437. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  438. #if CONFIG_IDF_TARGET_ESP32
  439. #if !CONFIG_SPIRAM_BOOT_INIT
  440. // If psram is uninitialized, we need to improve some flash configuration.
  441. bootloader_flash_clock_config(&fhdr);
  442. bootloader_flash_gpio_config(&fhdr);
  443. bootloader_flash_dummy_config(&fhdr);
  444. bootloader_flash_cs_timing_config();
  445. #endif //!CONFIG_SPIRAM_BOOT_INIT
  446. #endif //CONFIG_IDF_TARGET_ESP32
  447. #if CONFIG_SPI_FLASH_SIZE_OVERRIDE
  448. int app_flash_size = esp_image_get_flash_size(fhdr.spi_size);
  449. if (app_flash_size < 1 * 1024 * 1024) {
  450. ESP_LOGE(TAG, "Invalid flash size in app image header.");
  451. abort();
  452. }
  453. bootloader_flash_update_size(app_flash_size);
  454. #endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
  455. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  456. s_cpu_inited[0] = true;
  457. volatile bool cpus_inited = false;
  458. while (!cpus_inited) {
  459. cpus_inited = true;
  460. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  461. cpus_inited &= s_cpu_inited[i];
  462. }
  463. esp_rom_delay_us(100);
  464. }
  465. #endif
  466. #ifdef ROM_LOG_MODE
  467. esp_efuse_set_rom_log_scheme(ROM_LOG_MODE);
  468. #endif
  469. SYS_STARTUP_FN();
  470. }