• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_boot/boot.h"
16 
17 #include <array>
18 
19 #include "FreeRTOS.h"
20 #include "pw_boot_cortex_m/boot.h"
21 #include "pw_malloc/malloc.h"
22 #include "pw_preprocessor/compiler.h"
23 #include "pw_string/util.h"
24 #include "pw_sys_io_stm32cube/init.h"
25 #include "pw_system/init.h"
26 #include "stm32f4xx.h"
27 #include "task.h"
28 
29 namespace {
30 
31 std::array<StackType_t, configMINIMAL_STACK_SIZE> freertos_idle_stack;
32 StaticTask_t freertos_idle_tcb;
33 
34 std::array<StackType_t, configTIMER_TASK_STACK_DEPTH> freertos_timer_stack;
35 StaticTask_t freertos_timer_tcb;
36 
37 std::array<char, configMAX_TASK_NAME_LEN> temp_thread_name_buffer;
38 
39 }  // namespace
40 
41 // Initializes clock to its max, 180Mhz. Note that this naming follows CubeMX's
42 // naming out of convention. It's not required that this target provides a
43 // symbol named SystemClock_Config. This function shares the same purpose as
44 // the symbol of the same name that is generated by CubeMX.
SystemClock_Config()45 extern "C" void SystemClock_Config() {
46   RCC_OscInitTypeDef RCC_OscInitStruct = {};
47   RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
48 
49   __HAL_RCC_PWR_CLK_ENABLE();
50   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
51 
52   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
53   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
54   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
55   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
56   RCC_OscInitStruct.PLL.PLLM = 4;
57   RCC_OscInitStruct.PLL.PLLN = 180;
58   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
59   RCC_OscInitStruct.PLL.PLLQ = 8;
60   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
61     pw_boot_PostMain();
62   }
63 
64   // OverDrive required for operation > 168Mhz
65   if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
66     pw_boot_PostMain();
67   }
68 
69   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
70                                 RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
71   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
72   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
73   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
74   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
75 
76   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
77     pw_boot_PostMain();
78   }
79 }
80 
81 // Functions needed when configGENERATE_RUN_TIME_STATS is on.
configureTimerForRunTimeStats(void)82 extern "C" void configureTimerForRunTimeStats(void) {}
getRunTimeCounterValue(void)83 extern "C" unsigned long getRunTimeCounterValue(void) { return uwTick; }
84 
85 // Required for configCHECK_FOR_STACK_OVERFLOW.
vApplicationStackOverflowHook(TaskHandle_t,char * pcTaskName)86 extern "C" void vApplicationStackOverflowHook(TaskHandle_t, char* pcTaskName) {
87   pw::string::Copy(pcTaskName, temp_thread_name_buffer);
88   PW_CRASH("Stack OVF for task %s", temp_thread_name_buffer.data());
89 }
90 
91 // Required for configUSE_TIMERS.
vApplicationGetTimerTaskMemory(StaticTask_t ** ppxTimerTaskTCBBuffer,StackType_t ** ppxTimerTaskStackBuffer,uint32_t * pulTimerTaskStackSize)92 extern "C" void vApplicationGetTimerTaskMemory(
93     StaticTask_t** ppxTimerTaskTCBBuffer,
94     StackType_t** ppxTimerTaskStackBuffer,
95     uint32_t* pulTimerTaskStackSize) {
96   *ppxTimerTaskTCBBuffer = &freertos_timer_tcb;
97   *ppxTimerTaskStackBuffer = freertos_timer_stack.data();
98   *pulTimerTaskStackSize = freertos_timer_stack.size();
99 }
100 
vApplicationGetIdleTaskMemory(StaticTask_t ** ppxIdleTaskTCBBuffer,StackType_t ** ppxIdleTaskStackBuffer,uint32_t * pulIdleTaskStackSize)101 extern "C" void vApplicationGetIdleTaskMemory(
102     StaticTask_t** ppxIdleTaskTCBBuffer,
103     StackType_t** ppxIdleTaskStackBuffer,
104     uint32_t* pulIdleTaskStackSize) {
105   *ppxIdleTaskTCBBuffer = &freertos_idle_tcb;
106   *ppxIdleTaskStackBuffer = freertos_idle_stack.data();
107   *pulIdleTaskStackSize = freertos_idle_stack.size();
108 }
109 
pw_boot_PreStaticMemoryInit()110 extern "C" void pw_boot_PreStaticMemoryInit() {}
111 
pw_boot_PreStaticConstructorInit()112 extern "C" void pw_boot_PreStaticConstructorInit() {
113   // Provided by STMicroelectronics SDK. Can be configured to be provided
114   // elsewhere by changing pw_third_party_stm32cube_CMSIS_INIT.
115   SystemInit();
116 
117   // Provided by the STMicroelectronics SDK.
118   HAL_Init();
119 
120   // Typically provided by CubeMX codegen, SystemClock_Config() is instead
121   // provided as part of this target.
122   SystemClock_Config();
123 
124 #if PW_MALLOC_ACTIVE
125   pw_MallocInit(&pw_boot_heap_low_addr, &pw_boot_heap_high_addr);
126 #endif  // PW_MALLOC_ACTIVE
127   pw_sys_io_Init();
128 }
129 
130 // TODO(amontanez): pw_boot_PreMainInit() should get renamed to
131 // pw_boot_FinalizeBoot or similar when main() is removed.
pw_boot_PreMainInit()132 extern "C" void pw_boot_PreMainInit() {
133   pw::system::Init();
134   vTaskStartScheduler();
135   PW_UNREACHABLE;
136 }
137 
138 // This `main()` stub prevents another main function from being linked since
139 // this target deliberately doesn't run `main()`.
main()140 extern "C" int main() {}
141 
pw_boot_PostMain()142 extern "C" PW_NO_RETURN void pw_boot_PostMain() {
143   // In case main() returns, just sit here until the device is reset.
144   while (true) {
145   }
146   PW_UNREACHABLE;
147 }
148