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