【问题标题】:Run STM32F103 from internal oscillator with PlatformIO (Arduino)?使用 PlatformIO (Arduino) 从内部振荡器运行 STM32F103?
【发布时间】:2021-02-15 04:39:45
【问题描述】:
如何将 STM32F103C8 配置为使用内部 RC 振荡器/HSI 和 PLL 运行,即无需使用 PlatformIO 中的 Arduino 框架使用外部晶体振荡器(如“蓝色药丸”板上的那样)?
platformio.ini:
[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
#board = genericSTM32F103C8
#board_build.mcu = stm32f103c8t6
#board_build.f_cpu = 72000000L
framework = arduino
lib_deps = olikraus/U8g2 @ ^2.28.7
upload_protocol = stlink
【问题讨论】:
标签:
arduino
stm32
platformio
【解决方案1】:
我在 PlatformIO 目录中搜索了一下。
以下路径用于“通用”:
C:\Users\<user>\.platformio\platforms\ststm32\boards\genericSTM32F103C8.json
C:\Users\<user>\.platformio\packages\framework-arduinoststm32\variants\Generic_F103Cx\variant.cpp
定义您自己的系统时钟配置,例如你的main.cpp:
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
/* Initializes the CPU, AHB and APB busses clocks */
// RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
// 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.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
while (1);
}
/* Initializes the CPU, AHB and APB busses 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) {
while (1);
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC ;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
while (1);
}
}
那么 ini 就是:
[env:bluepill_f103c8]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
upload_protocol = stlink
lib_deps = olikraus/U8g2 @ ^2.28.7
我建议重命名 env 以避免混淆。
我还没有验证实际的时钟速度。
它应该以 36 MHz (8 MHz/2*9) 运行。然后 ini 需要 `board_build.f_cpu = 36000000L`