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 
100         while( !s_other_cpu_startup_done )
101         {
102         }
103         esp_deregister_freertos_idle_hook_for_cpu( other_cpu_startup_idle_hook_cb, !xPortGetCoreID() );
104     #endif
105 
106     /* [refactor-todo] check if there is a way to move the following block to esp_system startup */
107     heap_caps_enable_nonos_stack_heaps();
108 
109     /* Now we have startup stack RAM available for heap, enable any DMA pool memory */
110     #if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
111         if( g_spiram_ok )
112         {
113             esp_err_t r = esp_spiram_reserve_dma_pool( CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL );
114 
115             if( r != ESP_OK )
116             {
117                 ESP_EARLY_LOGE( TAG, "Could not reserve internal/DMA pool (error 0x%x)", r );
118                 abort();
119             }
120         }
121     #endif /* if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL */
122 
123     /*Initialize task wdt if configured to do so */
124     #ifdef CONFIG_ESP_TASK_WDT_PANIC
125         ESP_ERROR_CHECK( esp_task_wdt_init( CONFIG_ESP_TASK_WDT_TIMEOUT_S, true ) );
126     #elif CONFIG_ESP_TASK_WDT
127         ESP_ERROR_CHECK( esp_task_wdt_init( CONFIG_ESP_TASK_WDT_TIMEOUT_S, false ) );
128     #endif
129 
130     /*Add IDLE 0 to task wdt */
131     #ifdef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
132         TaskHandle_t idle_0 = xTaskGetIdleTaskHandleForCPU( 0 );
133 
134         if( idle_0 != NULL )
135         {
136             ESP_ERROR_CHECK( esp_task_wdt_add( idle_0 ) );
137         }
138     #endif
139     /*Add IDLE 1 to task wdt */
140     #ifdef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
141         TaskHandle_t idle_1 = xTaskGetIdleTaskHandleForCPU( 1 );
142 
143         if( idle_1 != NULL )
144         {
145             ESP_ERROR_CHECK( esp_task_wdt_add( idle_1 ) );
146         }
147     #endif
148 
149     app_main();
150     vTaskDelete( NULL );
151 }
152 
153 /* -------------------- Heap Related ----------------------- */
154 
xPortCheckValidTCBMem(const void * ptr)155 bool xPortCheckValidTCBMem( const void * ptr )
156 {
157     return esp_ptr_internal( ptr ) && esp_ptr_byte_accessible( ptr );
158 }
159 
xPortcheckValidStackMem(const void * ptr)160 bool xPortcheckValidStackMem( const void * ptr )
161 {
162     #ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
163         return esp_ptr_byte_accessible( ptr );
164     #else
165         return esp_ptr_internal( ptr ) && esp_ptr_byte_accessible( ptr );
166     #endif
167 }
168