1*61c4878aSAndroid Build Coastguard Worker // Copyright 2021 The Pigweed Authors 2*61c4878aSAndroid Build Coastguard Worker // 3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of 5*61c4878aSAndroid Build Coastguard Worker // the License at 6*61c4878aSAndroid Build Coastguard Worker // 7*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0 8*61c4878aSAndroid Build Coastguard Worker // 9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under 13*61c4878aSAndroid Build Coastguard Worker // the License. 14*61c4878aSAndroid Build Coastguard Worker 15*61c4878aSAndroid Build Coastguard Worker #include "pw_boot/boot.h" 16*61c4878aSAndroid Build Coastguard Worker 17*61c4878aSAndroid Build Coastguard Worker #include <array> 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker #include "FreeRTOS.h" 20*61c4878aSAndroid Build Coastguard Worker #include "pw_assert/check.h" 21*61c4878aSAndroid Build Coastguard Worker #include "pw_boot_cortex_m/boot.h" 22*61c4878aSAndroid Build Coastguard Worker #include "pw_malloc/malloc.h" 23*61c4878aSAndroid Build Coastguard Worker #include "pw_preprocessor/compiler.h" 24*61c4878aSAndroid Build Coastguard Worker #include "pw_string/util.h" 25*61c4878aSAndroid Build Coastguard Worker #include "pw_sys_io_stm32cube/init.h" 26*61c4878aSAndroid Build Coastguard Worker #include "pw_system/init.h" 27*61c4878aSAndroid Build Coastguard Worker #include "stm32f4xx.h" 28*61c4878aSAndroid Build Coastguard Worker #include "task.h" 29*61c4878aSAndroid Build Coastguard Worker 30*61c4878aSAndroid Build Coastguard Worker // Initializes clock to its max, 180Mhz. Note that this naming follows CubeMX's 31*61c4878aSAndroid Build Coastguard Worker // naming out of convention. It's not required that this target provides a 32*61c4878aSAndroid Build Coastguard Worker // symbol named SystemClock_Config. This function shares the same purpose as 33*61c4878aSAndroid Build Coastguard Worker // the symbol of the same name that is generated by CubeMX. SystemClock_Config()34*61c4878aSAndroid Build Coastguard Workerextern "C" void SystemClock_Config() { 35*61c4878aSAndroid Build Coastguard Worker RCC_OscInitTypeDef RCC_OscInitStruct = {}; 36*61c4878aSAndroid Build Coastguard Worker RCC_ClkInitTypeDef RCC_ClkInitStruct = {}; 37*61c4878aSAndroid Build Coastguard Worker 38*61c4878aSAndroid Build Coastguard Worker __HAL_RCC_PWR_CLK_ENABLE(); 39*61c4878aSAndroid Build Coastguard Worker __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); 40*61c4878aSAndroid Build Coastguard Worker 41*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 42*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.HSEState = RCC_HSE_ON; 43*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 44*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 45*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.PLL.PLLM = 4; 46*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.PLL.PLLN = 180; 47*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; 48*61c4878aSAndroid Build Coastguard Worker RCC_OscInitStruct.PLL.PLLQ = 8; 49*61c4878aSAndroid Build Coastguard Worker if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { 50*61c4878aSAndroid Build Coastguard Worker pw_boot_PostMain(); 51*61c4878aSAndroid Build Coastguard Worker } 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker // OverDrive required for operation > 168Mhz 54*61c4878aSAndroid Build Coastguard Worker if (HAL_PWREx_EnableOverDrive() != HAL_OK) { 55*61c4878aSAndroid Build Coastguard Worker pw_boot_PostMain(); 56*61c4878aSAndroid Build Coastguard Worker } 57*61c4878aSAndroid Build Coastguard Worker 58*61c4878aSAndroid Build Coastguard Worker RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | 59*61c4878aSAndroid Build Coastguard Worker RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; 60*61c4878aSAndroid Build Coastguard Worker RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 61*61c4878aSAndroid Build Coastguard Worker RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 62*61c4878aSAndroid Build Coastguard Worker RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 63*61c4878aSAndroid Build Coastguard Worker RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 64*61c4878aSAndroid Build Coastguard Worker 65*61c4878aSAndroid Build Coastguard Worker if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { 66*61c4878aSAndroid Build Coastguard Worker pw_boot_PostMain(); 67*61c4878aSAndroid Build Coastguard Worker } 68*61c4878aSAndroid Build Coastguard Worker } 69*61c4878aSAndroid Build Coastguard Worker 70*61c4878aSAndroid Build Coastguard Worker // Functions needed when configGENERATE_RUN_TIME_STATS is on. configureTimerForRunTimeStats(void)71*61c4878aSAndroid Build Coastguard Workerextern "C" void configureTimerForRunTimeStats(void) {} getRunTimeCounterValue(void)72*61c4878aSAndroid Build Coastguard Workerextern "C" unsigned long getRunTimeCounterValue(void) { return uwTick; } 73*61c4878aSAndroid Build Coastguard Worker pw_boot_PreStaticMemoryInit()74*61c4878aSAndroid Build Coastguard Workerextern "C" void pw_boot_PreStaticMemoryInit() {} 75*61c4878aSAndroid Build Coastguard Worker pw_boot_PreStaticConstructorInit()76*61c4878aSAndroid Build Coastguard Workerextern "C" void pw_boot_PreStaticConstructorInit() { 77*61c4878aSAndroid Build Coastguard Worker // Provided by STMicroelectronics SDK. Can be configured to be provided 78*61c4878aSAndroid Build Coastguard Worker // elsewhere by changing pw_third_party_stm32cube_CMSIS_INIT. 79*61c4878aSAndroid Build Coastguard Worker SystemInit(); 80*61c4878aSAndroid Build Coastguard Worker 81*61c4878aSAndroid Build Coastguard Worker // Provided by the STMicroelectronics SDK. 82*61c4878aSAndroid Build Coastguard Worker HAL_Init(); 83*61c4878aSAndroid Build Coastguard Worker 84*61c4878aSAndroid Build Coastguard Worker // Typically provided by CubeMX codegen, SystemClock_Config() is instead 85*61c4878aSAndroid Build Coastguard Worker // provided as part of this target. 86*61c4878aSAndroid Build Coastguard Worker SystemClock_Config(); 87*61c4878aSAndroid Build Coastguard Worker 88*61c4878aSAndroid Build Coastguard Worker #if PW_MALLOC_ACTIVE 89*61c4878aSAndroid Build Coastguard Worker pw_MallocInit(&pw_boot_heap_low_addr, &pw_boot_heap_high_addr); 90*61c4878aSAndroid Build Coastguard Worker #endif // PW_MALLOC_ACTIVE 91*61c4878aSAndroid Build Coastguard Worker pw_sys_io_Init(); 92*61c4878aSAndroid Build Coastguard Worker } 93*61c4878aSAndroid Build Coastguard Worker 94*61c4878aSAndroid Build Coastguard Worker // TODO(amontanez): pw_boot_PreMainInit() should get renamed to 95*61c4878aSAndroid Build Coastguard Worker // pw_boot_FinalizeBoot or similar when main() is removed. pw_boot_PreMainInit()96*61c4878aSAndroid Build Coastguard Workerextern "C" void pw_boot_PreMainInit() { 97*61c4878aSAndroid Build Coastguard Worker pw::system::Init(); 98*61c4878aSAndroid Build Coastguard Worker vTaskStartScheduler(); 99*61c4878aSAndroid Build Coastguard Worker PW_UNREACHABLE; 100*61c4878aSAndroid Build Coastguard Worker } 101*61c4878aSAndroid Build Coastguard Worker 102*61c4878aSAndroid Build Coastguard Worker // This `main()` stub prevents another main function from being linked since 103*61c4878aSAndroid Build Coastguard Worker // this target deliberately doesn't run `main()`. main()104*61c4878aSAndroid Build Coastguard Workerint main() {} 105*61c4878aSAndroid Build Coastguard Worker pw_boot_PostMain()106*61c4878aSAndroid Build Coastguard Workerextern "C" PW_NO_RETURN void pw_boot_PostMain() { 107*61c4878aSAndroid Build Coastguard Worker // In case main() returns, just sit here until the device is reset. 108*61c4878aSAndroid Build Coastguard Worker while (true) { 109*61c4878aSAndroid Build Coastguard Worker } 110*61c4878aSAndroid Build Coastguard Worker PW_UNREACHABLE; 111*61c4878aSAndroid Build Coastguard Worker } 112