Well but if you can still read the USART output at the same baud rate, that means the inner clock setup is the same and youβre probably not calling SystemCoreClockUpdate()
which updates the internal state (reads out the state of the HSI and HSE and the PLL and calculates back the frequency). Otherwise the UART output would be at a different baud rate (double the baudrate if double the clock)
Just like in the official example, Iβm taking care of the clock init and update myself.
Using the platformio.ini
[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = stm32cube
upload_protocol = stlink
build_flags = -Wl,-u_printf_float -D HSE_VALUE=8000000
board_build.f_cpu = 72000000L
; if using HSI, comment out above and use
;board_build.f_cpu = 64000000L
and only a src\main.c
#include "stm32f1xx_hal.h"
#include <string.h>
#include <stdio.h>
#define LED_PIN GPIO_PIN_13
#define LED_GPIO_PORT GPIOC
#define LED_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
/* for reference: HSI setup */
void SystemClock_Config_HSI(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
return;
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
return;
}
}
/* boots to 72MHz with HSE(8MHz) + PLL */
void SystemClock_Config_HSE(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
return;
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
return;
}
}
int main(void)
{
__HAL_RCC_AFIO_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
HAL_Init();
//SystemClock_Config_HSI();
SystemClock_Config_HSE();
SystemCoreClockUpdate();
/* LED setup PC13 */
LED_GPIO_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
/* UART init. USART1, 115200 baud, 8N1. PA9 = TX, PA10 = RX */
/* start clocks, config GPIOs, config USART peripheral */
__USART1_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
UART_HandleTypeDef huart;
huart.Instance = USART1;
huart.Init.BaudRate = 115200;
huart.Init.WordLength = UART_WORDLENGTH_8B;
huart.Init.Parity = UART_PARITY_NONE;
huart.Init.StopBits = UART_STOPBITS_1;
huart.Init.Mode = UART_MODE_TX_RX;
huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart);
char str[64] = {0};
uint32_t timme = 0;
while (1)
{
if ((HAL_GetTick() - timme) > 1000) // 10sec interval
{
HAL_GPIO_TogglePin(LED_GPIO_PORT, LED_PIN);
snprintf(str, 64, "SEK: %d \r\n", 5);
HAL_UART_Transmit(&huart, (uint8_t *)str, strlen(str), 1000);
snprintf(str, 64, "Core = %d, %d MHz \r\n", SystemCoreClock, SystemCoreClock / 1000000);
HAL_UART_Transmit(&huart, (uint8_t *)str, strlen(str), 1000);
snprintf(str, 64, "HCLK = %d \r\n", HAL_RCC_GetHCLKFreq());
HAL_UART_Transmit(&huart, (uint8_t *)str, strlen(str), 1000);
snprintf(str, 64, "APB1 = %d \r\n", HAL_RCC_GetPCLK1Freq());
HAL_UART_Transmit(&huart, (uint8_t *)str, strlen(str), 1000);
snprintf(str, 64, "APB2 = %d \r\n", HAL_RCC_GetPCLK2Freq());
HAL_UART_Transmit(&huart, (uint8_t *)str, strlen(str), 1000);
timme = HAL_GetTick();
}
}
}
void SysTick_Handler(void)
{
HAL_IncTick();
}
void NMI_Handler(void)
{
}
void HardFault_Handler(void)
{
while (1)
{
}
}
void MemManage_Handler(void)
{
while (1)
{
}
}
void BusFault_Handler(void)
{
while (1)
{
}
}
void UsageFault_Handler(void)
{
while (1)
{
}
}
void SVC_Handler(void)
{
}
void DebugMon_Handler(void)
{
}
void PendSV_Handler(void)
{
}
I get the output
SEK: 5
Core = 72000000, 72 MHz
HCLK = 72000000
APB1 = 36000000
APB2 = 72000000
which is correct since Iβm running explicitly from HSE (8MHz) and PLL to 72MHz.
If I donβt call SystemClock_Config_HSE()
(while still calling SystemCoreClockUpdate
) I get
SEK: 5
Core = 8000000, 8 MHz
HCLK = 8000000
APB1 = 8000000
APB2 = 8000000
so thatβs probably the default HSI.
And if I call SystemClock_Config_HSI()
I get
SEK: 5
Core = 64000000, 64 MHz
HCLK = 64000000
APB1 = 32000000
APB2 = 64000000
which is correct for the HSI+PLL setup.
So Iβd recommend to explicitly do the clock setup and call SystemCoreClockUpdate()
after that