Configure clock at runtime on STM32F401CCU6 causes lockup/crash?
I have an STM32F401CCU6 with an 25MHz ext. oscillator. I believe the internal RC oscillator is 16MHz. I have tried to configure to use the internal oscillator and as well as the ext. one with no success. Please note that my code _works_ if I don't do any clock configuration at runtime but just use the default settings. My code is below. In my code I have a delay(6000) in the main function. With a delay of 6000 the LED stays lit up when I turn power on. If I set the delay to 5000 the LED barely flashes ON and then stays OFF. Based on this I assume it is crashing or getting stuck some time AFTER the call to set up the clock. The systemClock_config function is based on the STM32CubeF4 [1] STM32F401-Discovery/. I am using the startup_stm32f401xc.s, system_stm32f4xx.c, and STM32F401VCTx_FLASH.ld files from the same folder.
Note that I currently don't have debugger attached to this target so I cannot step through.
Any ideas what I am missing? Do I need to do something more? The values I used for the clock configuration is what the ST clock configuration tool (xls) spit out.
Appreciate any insight!
#include "stm32f4xx_hal.h"
static void init_LEDs(void);
static void systemClock_config(void);
static void delay(unsigned int delay);
int main(void)
{
systemClock_config();
init_LEDs();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
delay(6000);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
}
static void init_LEDs(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef BoardLEDs;
BoardLEDs.Mode = GPIO_MODE_OUTPUT_PP;
BoardLEDs.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOC, &BoardLEDs);
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 84000000
* HCLK(Hz) = 84000000
* AHB Prescaler = 1
* APB1 Prescaler = 2
* APB2 Prescaler = 1
* HSE Frequency(Hz) = 25000000
* PLL_M = 25
* PLL_N = 336
* PLL_P = 4
* PLL_Q = 7
* VDD(V) = 3.3
* Main regulator output voltage = Scale2 mode
* Flash Latency(WS) = 2
* @param None
* @retval None
*/
static void systemClock_config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
//RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
//RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
//RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
//RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 25;
//RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
}