blob: 81eae3298fe69442e731d1f31d01c9b282c7a46a [file] [log] [blame]
Armando Montanezc62a2d22021-12-08 16:02:07 -08001// 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"
Armando Montanez2c042f92021-12-15 14:55:36 -080025#include "pw_system/init.h"
Armando Montanezc62a2d22021-12-08 16:02:07 -080026#include "stm32f4xx.h"
27#include "task.h"
28
29namespace {
30
31std::array<StackType_t, configMINIMAL_STACK_SIZE> freertos_idle_stack;
32StaticTask_t freertos_idle_tcb;
33
34std::array<StackType_t, configTIMER_TASK_STACK_DEPTH> freertos_timer_stack;
35StaticTask_t freertos_timer_tcb;
36
Armando Montanez2c042f92021-12-15 14:55:36 -080037std::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.
45extern "C" void SystemClock_Config() {
Armando Montanezc62a2d22021-12-08 16:02:07 -080046 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
Armando Montanezc62a2d22021-12-08 16:02:07 -080081// Functions needed when configGENERATE_RUN_TIME_STATS is on.
82extern "C" void configureTimerForRunTimeStats(void) {}
83extern "C" unsigned long getRunTimeCounterValue(void) { return uwTick; }
84
85// Required for configCHECK_FOR_STACK_OVERFLOW.
86extern "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.
92extern "C" void vApplicationGetTimerTaskMemory(
93 StaticTask_t** ppxIdleTaskTCBBuffer,
94 StackType_t** ppxIdleTaskStackBuffer,
95 uint32_t* pulIdleTaskStackSize) {
96 *ppxIdleTaskTCBBuffer = &freertos_idle_tcb;
97 *ppxIdleTaskStackBuffer = freertos_idle_stack.data();
98 *pulIdleTaskStackSize = freertos_idle_stack.size();
99}
100
101extern "C" void vApplicationGetIdleTaskMemory(
102 StaticTask_t** ppxIdleTaskTCBBuffer,
103 StackType_t** ppxIdleTaskStackBuffer,
104 uint32_t* pulIdleTaskStackSize) {
105 *ppxIdleTaskTCBBuffer = &freertos_timer_tcb;
106 *ppxIdleTaskStackBuffer = freertos_timer_stack.data();
107 *pulIdleTaskStackSize = freertos_timer_stack.size();
108}
109
Armando Montanez2c042f92021-12-15 14:55:36 -0800110extern "C" void pw_boot_PreStaticMemoryInit() {}
Armando Montanezc62a2d22021-12-08 16:02:07 -0800111
112extern "C" void pw_boot_PreStaticConstructorInit() {
Armando Montanez2c042f92021-12-15 14:55:36 -0800113 // 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
Armando Montanezc62a2d22021-12-08 16:02:07 -0800124#if PW_MALLOC_ACTIVE
125 pw_MallocInit(&pw_boot_heap_low_addr, &pw_boot_heap_high_addr);
126#endif // PW_MALLOC_ACTIVE
Armando Montanezc62a2d22021-12-08 16:02:07 -0800127 pw_sys_io_Init();
128}
129
Armando Montanez2c042f92021-12-15 14:55:36 -0800130// TODO(amontanez): pw_boot_PreMainInit() should get renamed to
131// pw_boot_FinalizeBoot or similar when main() is removed.
132extern "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()`.
140extern "C" int main() {}
141
Armando Montanezc62a2d22021-12-08 16:02:07 -0800142extern "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}