1 /*
2 * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <string.h>
8 #include "FreeRTOS.h"
9 #include "task.h"
10 #include "portmacro.h"
11 #include "esp_private/esp_int_wdt.h"
12 #include "esp_system.h"
13 #include "esp_heap_caps_init.h"
14 #include "esp_task_wdt.h"
15 #include "esp_task.h"
16 #include "esp_private/crosscore_int.h"
17 #include "esp_log.h"
18 #include "esp_memory_utils.h"
19 #include "esp_freertos_hooks.h"
20 #include "sdkconfig.h"
21 #include "esp_freertos_hooks.h"
22
23 #if CONFIG_SPIRAM
24 #include "esp_psram.h"
25 #include "esp_private/esp_psram_extram.h"
26 #endif
27
28 #if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
29 static const char* TAG = "cpu_start";
30 #endif
31
32 /* Architecture-agnostic parts of the FreeRTOS ESP-IDF port layer can go here.
33 *
34 * The actual call flow will be to call esp_startup_start_app() in <ARCH>/port.c,
35 * which will then call esp_startup_start_app_common()
36 */
37
38 // Duplicate of inaccessible xSchedulerRunning; needed at startup to avoid counting nesting
39 volatile unsigned port_xSchedulerRunning[portNUM_PROCESSORS] = {0};
40
41 // For now, running FreeRTOS on one core and a bare metal on the other (or other OSes)
42 // is not supported. For now CONFIG_FREERTOS_UNICORE and CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
43 // should mirror each other's values.
44 //
45 // And since this should be true, we can just check for CONFIG_FREERTOS_UNICORE.
46 #if CONFIG_FREERTOS_UNICORE != CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
47 #error "FreeRTOS and system configuration mismatch regarding the use of multiple cores."
48 #endif
49
50 #if !defined CONFIG_IDF_RTOS_RTTHREAD
51 static void main_task(void* args);
52 #endif
53
54 #ifdef CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
55 void esp_gdbstub_init(void);
56 #endif // CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
57
58 extern void app_main(void);
59
esp_startup_start_app_common(void)60 void esp_startup_start_app_common(void)
61 {
62 #if CONFIG_ESP_INT_WDT
63 esp_int_wdt_init();
64 //Initialize the interrupt watch dog for CPU0.
65 esp_int_wdt_cpu_init();
66 #endif
67
68 esp_crosscore_int_init();
69
70 #if CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME && !CONFIG_IDF_TARGET_ESP32C2
71 esp_gdbstub_init();
72 #endif // CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
73
74 #if !defined CONFIG_IDF_RTOS_RTTHREAD
75 portBASE_TYPE res = xTaskCreatePinnedToCore(&main_task, "main",
76 ESP_TASK_MAIN_STACK, NULL,
77 ESP_TASK_MAIN_PRIO, NULL, ESP_TASK_MAIN_CORE);
78 assert(res == pdTRUE);
79 (void)res;
80 #endif
81 }
82
83 #if !CONFIG_FREERTOS_UNICORE
84 static volatile bool s_other_cpu_startup_done = false;
other_cpu_startup_idle_hook_cb(void)85 static bool other_cpu_startup_idle_hook_cb(void)
86 {
87 s_other_cpu_startup_done = true;
88 return true;
89 }
90 #endif
91
92 #if !defined CONFIG_IDF_RTOS_RTTHREAD
main_task(void * args)93 static void main_task(void* args)
94 {
95 #if !CONFIG_FREERTOS_UNICORE
96 // Wait for FreeRTOS initialization to finish on other core, before replacing its startup stack
97 esp_register_freertos_idle_hook_for_cpu(other_cpu_startup_idle_hook_cb, !xPortGetCoreID());
98 while (!s_other_cpu_startup_done) {
99 ;
100 }
101 esp_deregister_freertos_idle_hook_for_cpu(other_cpu_startup_idle_hook_cb, !xPortGetCoreID());
102 #endif
103
104 // [refactor-todo] check if there is a way to move the following block to esp_system startup
105 heap_caps_enable_nonos_stack_heaps();
106
107 // Now we have startup stack RAM available for heap, enable any DMA pool memory
108 #if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
109 if (esp_psram_is_initialized()) {
110 esp_err_t r = esp_psram_extram_reserve_dma_pool(CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL);
111 if (r != ESP_OK) {
112 ESP_EARLY_LOGE(TAG, "Could not reserve internal/DMA pool (error 0x%x)", r);
113 abort();
114 }
115 }
116 #endif
117
118 //Initialize TWDT if configured to do so
119 #if CONFIG_ESP_TASK_WDT
120 esp_task_wdt_config_t twdt_config = {
121 .timeout_ms = CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000,
122 .idle_core_mask = 0,
123 #if CONFIG_ESP_TASK_WDT_PANIC
124 .trigger_panic = true,
125 #endif
126 };
127 #if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
128 twdt_config.idle_core_mask |= (1 << 0);
129 #endif
130 #if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
131 twdt_config.idle_core_mask |= (1 << 1);
132 #endif
133 ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config));
134 #endif // CONFIG_ESP_TASK_WDT
135
136 app_main();
137 vTaskDelete(NULL);
138 }
139 #endif
140