xref: /Kernel-v10.6.2/portable/ThirdParty/GCC/Xtensa_ESP32/port_common.c (revision 963abe6c4833e7c969279b537c103d7a0bdd5116)
1 /*
2  * SPDX-FileCopyrightText: 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_system.h"
12 #include "esp_heap_caps_init.h"
13 #include "esp_int_wdt.h"
14 #include "esp_task_wdt.h"
15 #include "esp_task.h"
16 #include "esp_private/crosscore_int.h"
17 #include "esp_private/startup_internal.h"    /* Required by g_spiram_ok. [refactor-todo] for g_spiram_ok */
18 #include "esp_log.h"
19 #include "soc/soc_memory_types.h"
20 #include "soc/dport_access.h"
21 #include "sdkconfig.h"
22 #include "esp_freertos_hooks.h"
23 
24 #if CONFIG_IDF_TARGET_ESP32
25 #include "esp32/spiram.h"
26 #elif CONFIG_IDF_TARGET_ESP32S2
27 #include "esp32s2/spiram.h"
28 #elif CONFIG_IDF_TARGET_ESP32S3
29 #include "esp32s3/spiram.h"
30 #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
31 // SPIRAM is not supported on ESP32-C3
32 #endif
33 
34 #if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
35 static const char* TAG = "cpu_start";
36 #endif
37 
38 /* Architecture-agnostic parts of the FreeRTOS ESP-IDF port layer can go here.
39  *
40  * The actual call flow will be to call esp_startup_start_app() in <ARCH>/port.c,
41  * which will then call esp_startup_start_app_common()
42  */
43 
44 // Duplicate of inaccessible xSchedulerRunning; needed at startup to avoid counting nesting
45 volatile unsigned port_xSchedulerRunning[portNUM_PROCESSORS] = {0};
46 
47 // For now, running FreeRTOS on one core and a bare metal on the other (or other OSes)
48 // is not supported. For now CONFIG_FREERTOS_UNICORE and CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
49 // should mirror each other's values.
50 //
51 // And since this should be true, we can just check for CONFIG_FREERTOS_UNICORE.
52 #if CONFIG_FREERTOS_UNICORE != CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
53     #error "FreeRTOS and system configuration mismatch regarding the use of multiple cores."
54 #endif
55 
56 static void main_task(void* args);
57 
58 #ifdef CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
59 void esp_gdbstub_init(void);
60 #endif // CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
61 
62 extern void app_main(void);
63 
esp_startup_start_app_common(void)64 void esp_startup_start_app_common(void)
65 {
66 #if CONFIG_ESP_INT_WDT
67     esp_int_wdt_init();
68     //Initialize the interrupt watch dog for CPU0.
69     esp_int_wdt_cpu_init();
70 #endif
71 
72     esp_crosscore_int_init();
73 
74 #ifdef CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
75     esp_gdbstub_init();
76 #endif // CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
77 
78     portBASE_TYPE res = xTaskCreatePinnedToCore(&main_task, "main",
79                                                 ESP_TASK_MAIN_STACK, NULL,
80                                                 ESP_TASK_MAIN_PRIO, NULL, ESP_TASK_MAIN_CORE);
81     assert(res == pdTRUE);
82     (void)res;
83 }
84 
85 #if !CONFIG_FREERTOS_UNICORE
86 static volatile bool s_other_cpu_startup_done = false;
other_cpu_startup_idle_hook_cb(void)87 static bool other_cpu_startup_idle_hook_cb(void)
88 {
89     s_other_cpu_startup_done = true;
90     return true;
91 }
92 #endif
93 
main_task(void * args)94 static void main_task(void* args)
95 {
96 #if !CONFIG_FREERTOS_UNICORE
97     // Wait for FreeRTOS initialization to finish on other core, before replacing its startup stack
98     esp_register_freertos_idle_hook_for_cpu(other_cpu_startup_idle_hook_cb, !xPortGetCoreID());
99     while (!s_other_cpu_startup_done) {
100         ;
101     }
102     esp_deregister_freertos_idle_hook_for_cpu(other_cpu_startup_idle_hook_cb, !xPortGetCoreID());
103 #endif
104 
105     // [refactor-todo] check if there is a way to move the following block to esp_system startup
106     heap_caps_enable_nonos_stack_heaps();
107 
108     // Now we have startup stack RAM available for heap, enable any DMA pool memory
109 #if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
110     if (g_spiram_ok) {
111         esp_err_t r = esp_spiram_reserve_dma_pool(CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL);
112         if (r != ESP_OK) {
113             ESP_EARLY_LOGE(TAG, "Could not reserve internal/DMA pool (error 0x%x)", r);
114             abort();
115         }
116     }
117 #endif
118 
119     //Initialize task wdt if configured to do so
120 #ifdef CONFIG_ESP_TASK_WDT_PANIC
121     ESP_ERROR_CHECK(esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, true));
122 #elif CONFIG_ESP_TASK_WDT
123     ESP_ERROR_CHECK(esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, false));
124 #endif
125 
126     //Add IDLE 0 to task wdt
127 #ifdef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
128     TaskHandle_t idle_0 = xTaskGetIdleTaskHandleForCPU(0);
129     if(idle_0 != NULL){
130         ESP_ERROR_CHECK(esp_task_wdt_add(idle_0));
131     }
132 #endif
133     //Add IDLE 1 to task wdt
134 #ifdef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
135     TaskHandle_t idle_1 = xTaskGetIdleTaskHandleForCPU(1);
136     if(idle_1 != NULL){
137         ESP_ERROR_CHECK(esp_task_wdt_add(idle_1));
138     }
139 #endif
140 
141     app_main();
142     vTaskDelete(NULL);
143 }
144 
145 // -------------------- Heap Related -----------------------
146 
xPortCheckValidTCBMem(const void * ptr)147 bool xPortCheckValidTCBMem(const void *ptr)
148 {
149     return esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr);
150 }
151 
xPortcheckValidStackMem(const void * ptr)152 bool xPortcheckValidStackMem(const void *ptr)
153 {
154 #ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
155     return esp_ptr_byte_accessible(ptr);
156 #else
157     return esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr);
158 #endif
159 }
160