cpu_start.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include <string.h>
  15. #include "esp_attr.h"
  16. #include "esp_err.h"
  17. #include "rom/ets_sys.h"
  18. #include "rom/uart.h"
  19. #include "soc/cpu.h"
  20. #include "soc/dport_reg.h"
  21. #include "soc/io_mux_reg.h"
  22. #include "soc/rtc_cntl_reg.h"
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/task.h"
  25. #include "freertos/semphr.h"
  26. #include "freertos/queue.h"
  27. #include "freertos/portmacro.h"
  28. #include "tcpip_adapter.h"
  29. #include "heap_alloc_caps.h"
  30. #include "sdkconfig.h"
  31. #include "esp_system.h"
  32. #include "esp_spi_flash.h"
  33. #include "nvs_flash.h"
  34. #include "esp_event.h"
  35. #include "esp_spi_flash.h"
  36. #include "esp_ipc.h"
  37. #include "esp_crosscore_int.h"
  38. #include "esp_log.h"
  39. #include "esp_vfs_dev.h"
  40. #include "esp_newlib.h"
  41. #include "esp_brownout.h"
  42. #include "esp_int_wdt.h"
  43. #include "esp_task_wdt.h"
  44. #include "trax.h"
  45. void start_cpu0(void) __attribute__((weak, alias("start_cpu0_default")));
  46. void start_cpu0_default(void) IRAM_ATTR;
  47. #if !CONFIG_FREERTOS_UNICORE
  48. static void IRAM_ATTR call_start_cpu1();
  49. void start_cpu1(void) __attribute__((weak, alias("start_cpu1_default")));
  50. void start_cpu1_default(void) IRAM_ATTR;
  51. static bool app_cpu_started = false;
  52. #endif //!CONFIG_FREERTOS_UNICORE
  53. static void do_global_ctors(void);
  54. static void main_task(void* args);
  55. extern void app_main(void);
  56. extern int _bss_start;
  57. extern int _bss_end;
  58. extern int _init_start;
  59. extern void (*__init_array_start)(void);
  60. extern void (*__init_array_end)(void);
  61. extern volatile int port_xSchedulerRunning[2];
  62. static const char* TAG = "cpu_start";
  63. /*
  64. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  65. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  66. */
  67. void IRAM_ATTR call_start_cpu0()
  68. {
  69. //Kill wdt
  70. REG_CLR_BIT(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN);
  71. REG_CLR_BIT(0x6001f048, BIT(14)); //DR_REG_BB_BASE+48
  72. cpu_configure_region_protection();
  73. //Move exception vectors to IRAM
  74. asm volatile (\
  75. "wsr %0, vecbase\n" \
  76. ::"r"(&_init_start));
  77. uartAttach();
  78. ets_install_uart_printf();
  79. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  80. // Initialize heap allocator
  81. heap_alloc_caps_init();
  82. ESP_EARLY_LOGI(TAG, "Pro cpu up.");
  83. #if !CONFIG_FREERTOS_UNICORE
  84. ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  85. //Un-stall the app cpu; the panic handler may have stalled it.
  86. CLEAR_PERI_REG_MASK(RTC_CNTL_SW_CPU_STALL_REG, RTC_CNTL_SW_STALL_APPCPU_C1_M);
  87. CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_STALL_APPCPU_C0_M);
  88. //Enable clock gating and reset the app cpu.
  89. SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  90. CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  91. SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  92. CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  93. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  94. while (!app_cpu_started) {
  95. ets_delay_us(100);
  96. }
  97. #else
  98. ESP_EARLY_LOGI(TAG, "Single core mode");
  99. CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  100. #endif
  101. ESP_EARLY_LOGI(TAG, "Pro cpu start user code");
  102. start_cpu0();
  103. }
  104. #if !CONFIG_FREERTOS_UNICORE
  105. void IRAM_ATTR call_start_cpu1()
  106. {
  107. asm volatile (\
  108. "wsr %0, vecbase\n" \
  109. ::"r"(&_init_start));
  110. cpu_configure_region_protection();
  111. ESP_EARLY_LOGI(TAG, "App cpu up.");
  112. app_cpu_started = 1;
  113. start_cpu1();
  114. }
  115. #endif //!CONFIG_FREERTOS_UNICORE
  116. void start_cpu0_default(void)
  117. {
  118. //Enable trace memory and immediately start trace.
  119. #if CONFIG_MEMMAP_TRACEMEM
  120. #if CONFIG_MEMMAP_TRACEMEM_TWOBANKS
  121. trax_enable(TRAX_ENA_PRO_APP);
  122. #else
  123. trax_enable(TRAX_ENA_PRO);
  124. #endif
  125. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  126. #endif
  127. esp_set_cpu_freq(); // set CPU frequency configured in menuconfig
  128. uart_div_modify(0, (APB_CLK_FREQ << 4) / 115200);
  129. #if CONFIG_BROWNOUT_DET
  130. esp_brownout_init();
  131. #endif
  132. #if CONFIG_INT_WDT
  133. esp_int_wdt_init();
  134. #endif
  135. #if CONFIG_TASK_WDT
  136. esp_task_wdt_init();
  137. #endif
  138. esp_setup_syscalls();
  139. esp_vfs_dev_uart_register();
  140. esp_reent_init(_GLOBAL_REENT);
  141. const char* default_uart_dev = "/dev/uart/0";
  142. _GLOBAL_REENT->_stdout = fopen(default_uart_dev, "w");
  143. _GLOBAL_REENT->_stderr = fopen(default_uart_dev, "w");
  144. _GLOBAL_REENT->_stdin = fopen(default_uart_dev, "r");
  145. do_global_ctors();
  146. #if !CONFIG_FREERTOS_UNICORE
  147. esp_crosscore_int_init();
  148. #endif
  149. esp_ipc_init();
  150. spi_flash_init();
  151. xTaskCreatePinnedToCore(&main_task, "main",
  152. ESP_TASK_MAIN_STACK, NULL,
  153. ESP_TASK_MAIN_PRIO, NULL, 0);
  154. ESP_LOGI(TAG, "Starting scheduler on PRO CPU.");
  155. vTaskStartScheduler();
  156. }
  157. #if !CONFIG_FREERTOS_UNICORE
  158. void start_cpu1_default(void)
  159. {
  160. #if CONFIG_MEMMAP_TRACEMEM_TWOBANKS
  161. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  162. #endif
  163. // Wait for FreeRTOS initialization to finish on PRO CPU
  164. while (port_xSchedulerRunning[0] == 0) {
  165. ;
  166. }
  167. esp_crosscore_int_init();
  168. ESP_LOGI(TAG, "Starting scheduler on APP CPU.");
  169. xPortStartScheduler();
  170. }
  171. #endif //!CONFIG_FREERTOS_UNICORE
  172. static void do_global_ctors(void)
  173. {
  174. void (**p)(void);
  175. for (p = &__init_array_end - 1; p >= &__init_array_start; --p) {
  176. (*p)();
  177. }
  178. }
  179. static void main_task(void* args)
  180. {
  181. app_main();
  182. vTaskDelete(NULL);
  183. }