cpu_start.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_log.h"
  38. void start_cpu0(void) __attribute__((weak, alias("start_cpu0_default")));
  39. void start_cpu0_default(void) IRAM_ATTR;
  40. #if !CONFIG_FREERTOS_UNICORE
  41. static void IRAM_ATTR call_start_cpu1();
  42. void start_cpu1(void) __attribute__((weak, alias("start_cpu1_default")));
  43. void start_cpu1_default(void) IRAM_ATTR;
  44. static bool app_cpu_started = false;
  45. #endif //!CONFIG_FREERTOS_UNICORE
  46. static void do_global_ctors(void);
  47. static void main_task(void* args);
  48. extern void ets_setup_syscalls(void);
  49. extern void app_main(void);
  50. extern int _bss_start;
  51. extern int _bss_end;
  52. extern int _init_start;
  53. extern void (*__init_array_start)(void);
  54. extern void (*__init_array_end)(void);
  55. extern volatile int port_xSchedulerRunning[2];
  56. static const char* TAG = "cpu_start";
  57. /*
  58. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  59. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  60. */
  61. void IRAM_ATTR call_start_cpu0()
  62. {
  63. //Kill wdt
  64. REG_CLR_BIT(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN);
  65. REG_CLR_BIT(0x6001f048, BIT(14)); //DR_REG_BB_BASE+48
  66. cpu_configure_region_protection();
  67. //Move exception vectors to IRAM
  68. asm volatile (\
  69. "wsr %0, vecbase\n" \
  70. ::"r"(&_init_start));
  71. uartAttach();
  72. ets_install_uart_printf();
  73. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  74. // Initialize heap allocator
  75. heap_alloc_caps_init();
  76. ESP_EARLY_LOGI(TAG, "Pro cpu up.");
  77. #if !CONFIG_FREERTOS_UNICORE
  78. ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  79. SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  80. CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  81. SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  82. CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  83. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  84. while (!app_cpu_started) {
  85. ets_delay_us(100);
  86. }
  87. #else
  88. ESP_EARLY_LOGI(TAG, "Single core mode");
  89. CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  90. #endif
  91. ESP_EARLY_LOGI(TAG, "Pro cpu start user code");
  92. start_cpu0();
  93. }
  94. #if !CONFIG_FREERTOS_UNICORE
  95. void IRAM_ATTR call_start_cpu1()
  96. {
  97. asm volatile (\
  98. "wsr %0, vecbase\n" \
  99. ::"r"(&_init_start));
  100. cpu_configure_region_protection();
  101. ESP_EARLY_LOGI(TAG, "App cpu up.");
  102. app_cpu_started = 1;
  103. start_cpu1();
  104. }
  105. #endif //!CONFIG_FREERTOS_UNICORE
  106. void start_cpu0_default(void)
  107. {
  108. esp_set_cpu_freq(); // set CPU frequency configured in menuconfig
  109. uart_div_modify(0, (APB_CLK_FREQ << 4) / 115200);
  110. ets_setup_syscalls();
  111. do_global_ctors();
  112. esp_ipc_init();
  113. spi_flash_init();
  114. xTaskCreatePinnedToCore(&main_task, "main",
  115. ESP_TASK_MAIN_STACK, NULL,
  116. ESP_TASK_MAIN_PRIO, NULL, 0);
  117. ESP_LOGI(TAG, "Starting scheduler on PRO CPU.");
  118. vTaskStartScheduler();
  119. }
  120. #if !CONFIG_FREERTOS_UNICORE
  121. void start_cpu1_default(void)
  122. {
  123. // Wait for FreeRTOS initialization to finish on PRO CPU
  124. while (port_xSchedulerRunning[0] == 0) {
  125. ;
  126. }
  127. ESP_LOGI(TAG, "Starting scheduler on APP CPU.");
  128. xPortStartScheduler();
  129. }
  130. #endif //!CONFIG_FREERTOS_UNICORE
  131. static void do_global_ctors(void)
  132. {
  133. void (**p)(void);
  134. for (p = &__init_array_end - 1; p >= &__init_array_start; --p) {
  135. (*p)();
  136. }
  137. }
  138. static void main_task(void* args)
  139. {
  140. app_main();
  141. vTaskDelete(NULL);
  142. }