• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2020 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 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 #include "esp_osal.h"
17 #include "task.h"
18 #include "portmacro.h"
19 #include "esp_system.h"
20 #include "esp_heap_caps_init.h"
21 #include "esp_int_wdt.h"
22 #include "esp_task_wdt.h"
23 #include "esp_task.h"
24 #include "esp_private/crosscore_int.h"
25 #include "esp_private/startup_internal.h"
26 #include "esp_log.h"
27 #include "soc/dport_access.h"
28 #include "sdkconfig.h"
29 
30 #if CONFIG_IDF_TARGET_ESP32
31 #include "esp32/spiram.h"
32 #elif CONFIG_IDF_TARGET_ESP32S2
33 #include "esp32s2/spiram.h"
34 #elif CONFIG_IDF_TARGET_ESP32S3
35 #include "esp32s3/spiram.h"
36 #elif CONFIG_IDF_TARGET_ESP32C3
37 // SPIRAM is not supported on ESP32-C3
38 #endif
39 
40 #if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
41 static const char* TAG = "cpu_start";
42 #endif
43 
44 /* Architecture-agnostic parts of the FreeRTOS ESP-IDF port layer can go here.
45  *
46  * The actual call flow will be to call esp_startup_start_app() in <ARCH>/port.c,
47  * which will then call esp_startup_start_app_common()
48  */
49 
50 // Duplicate of inaccessible xSchedulerRunning; needed at startup to avoid counting nesting
51 // volatile unsigned port_xSchedulerRunning[portNUM_PROCESSORS] = {0};
52 
53 // For now, running FreeRTOS on one core and a bare metal on the other (or other OSes)
54 // is not supported. For now CONFIG_FREERTOS_UNICORE and CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
55 // should mirror each other's values.
56 //
57 // And since this should be true, we can just check for CONFIG_FREERTOS_UNICORE.
58 #if CONFIG_FREERTOS_UNICORE != CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
59 	#error "FreeRTOS and system configuration mismatch regarding the use of multiple cores."
60 #endif
61 
62 static void main_task(void* args);
63 
64 extern void app_main(void);
65 
esp_startup_start_app_common(void)66 void esp_startup_start_app_common(void)
67 {
68 #if CONFIG_ESP_INT_WDT
69 	esp_int_wdt_init();
70 	//Initialize the interrupt watch dog for CPU0.
71 	esp_int_wdt_cpu_init();
72 #endif
73 
74 	esp_crosscore_int_init();
75 
76 #ifndef CONFIG_FREERTOS_UNICORE
77 #if CONFIG_IDF_TARGET_ESP32
78 	esp_dport_access_int_init();
79 #endif
80 #endif
81 	main_task(NULL);
82 //	portBASE_TYPE res = xTaskCreatePinnedToCore(&main_task, "main",
83 //												ESP_TASK_MAIN_STACK, NULL,
84 //												ESP_TASK_MAIN_PRIO, NULL, 0);
85 //	assert(res == pdTRUE);
86 }
87 
main_task(void * args)88 static void main_task(void* args)
89 {
90 #if !CONFIG_FREERTOS_UNICORE
91 	// Wait for FreeRTOS initialization to finish on APP CPU, before replacing its startup stack
92 	while (port_xSchedulerRunning[1] == 0) {
93 		;
94 	}
95 #endif
96 
97 	// [refactor-todo] check if there is a way to move the following block to esp_system startup
98 	heap_caps_enable_nonos_stack_heaps();
99 
100 	// Now we have startup stack RAM available for heap, enable any DMA pool memory
101 #if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
102 	if (g_spiram_ok) {
103 		esp_err_t r = esp_spiram_reserve_dma_pool(CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL);
104 		if (r != ESP_OK) {
105 			ESP_EARLY_LOGE(TAG, "Could not reserve internal/DMA pool (error 0x%x)", r);
106 			abort();
107 		}
108 	}
109 #endif
110 
111 	//Initialize task wdt if configured to do so
112 #ifdef CONFIG_ESP_TASK_WDT_PANIC
113 	ESP_ERROR_CHECK(esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, true));
114 #elif CONFIG_ESP_TASK_WDT
115 	ESP_ERROR_CHECK(esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, false));
116 #endif
117 
118 	//Add IDLE 0 to task wdt
119 #ifdef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
120 	TaskHandle_t idle_0 = xTaskGetIdleTaskHandleForCPU(0);
121 	if(idle_0 != NULL){
122 		ESP_ERROR_CHECK(esp_task_wdt_add(idle_0));
123 	}
124 #endif
125 	//Add IDLE 1 to task wdt
126 #ifdef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
127 	TaskHandle_t idle_1 = xTaskGetIdleTaskHandleForCPU(1);
128 	if(idle_1 != NULL){
129 		ESP_ERROR_CHECK(esp_task_wdt_add(idle_1));
130 	}
131 #endif
132 
133 	app_main();
134 //	vTaskDelete(NULL);
135 }
136