xref: /aosp_15_r20/external/pigweed/targets/stm32f429i_disc1_stm32cube/boot.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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_assert/check.h"
21 #include "pw_boot_cortex_m/boot.h"
22 #include "pw_malloc/malloc.h"
23 #include "pw_preprocessor/compiler.h"
24 #include "pw_string/util.h"
25 #include "pw_sys_io_stm32cube/init.h"
26 #include "pw_system/init.h"
27 #include "stm32f4xx.h"
28 #include "task.h"
29 
30 // Initializes clock to its max, 180Mhz. Note that this naming follows CubeMX's
31 // naming out of convention. It's not required that this target provides a
32 // symbol named SystemClock_Config. This function shares the same purpose as
33 // the symbol of the same name that is generated by CubeMX.
SystemClock_Config()34 extern "C" void SystemClock_Config() {
35   RCC_OscInitTypeDef RCC_OscInitStruct = {};
36   RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
37 
38   __HAL_RCC_PWR_CLK_ENABLE();
39   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
40 
41   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
42   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
43   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
44   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
45   RCC_OscInitStruct.PLL.PLLM = 4;
46   RCC_OscInitStruct.PLL.PLLN = 180;
47   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
48   RCC_OscInitStruct.PLL.PLLQ = 8;
49   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
50     pw_boot_PostMain();
51   }
52 
53   // OverDrive required for operation > 168Mhz
54   if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
55     pw_boot_PostMain();
56   }
57 
58   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
59                                 RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
60   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
61   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
62   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
63   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
64 
65   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
66     pw_boot_PostMain();
67   }
68 }
69 
70 // Functions needed when configGENERATE_RUN_TIME_STATS is on.
configureTimerForRunTimeStats(void)71 extern "C" void configureTimerForRunTimeStats(void) {}
getRunTimeCounterValue(void)72 extern "C" unsigned long getRunTimeCounterValue(void) { return uwTick; }
73 
pw_boot_PreStaticMemoryInit()74 extern "C" void pw_boot_PreStaticMemoryInit() {}
75 
pw_boot_PreStaticConstructorInit()76 extern "C" void pw_boot_PreStaticConstructorInit() {
77   // Provided by STMicroelectronics SDK. Can be configured to be provided
78   // elsewhere by changing pw_third_party_stm32cube_CMSIS_INIT.
79   SystemInit();
80 
81   // Provided by the STMicroelectronics SDK.
82   HAL_Init();
83 
84   // Typically provided by CubeMX codegen, SystemClock_Config() is instead
85   // provided as part of this target.
86   SystemClock_Config();
87 
88 #if PW_MALLOC_ACTIVE
89   pw_MallocInit(&pw_boot_heap_low_addr, &pw_boot_heap_high_addr);
90 #endif  // PW_MALLOC_ACTIVE
91   pw_sys_io_Init();
92 }
93 
94 // TODO(amontanez): pw_boot_PreMainInit() should get renamed to
95 // pw_boot_FinalizeBoot or similar when main() is removed.
pw_boot_PreMainInit()96 extern "C" void pw_boot_PreMainInit() {
97   pw::system::Init();
98   vTaskStartScheduler();
99   PW_UNREACHABLE;
100 }
101 
102 // This `main()` stub prevents another main function from being linked since
103 // this target deliberately doesn't run `main()`.
main()104 int main() {}
105 
pw_boot_PostMain()106 extern "C" PW_NO_RETURN void pw_boot_PostMain() {
107   // In case main() returns, just sit here until the device is reset.
108   while (true) {
109   }
110   PW_UNREACHABLE;
111 }
112