使用platformio+stm32cubemx开发stm32
1.使用stm32cubemx创建工程:
1. Access to MCU selector: 选择STM32F103ZET6
2. Start Project
3. 开始Pinout & Configuration
System Core中配置:
时钟:
调试口:
控制LED的GPIO:
Connectivity中配置串口(若有需要)
4.开始Clock Configuration
红框处填写72MHz,软件自动推算
5.Project Manager
Project配置
Code Generator配置
选择Copy only the necessary library files
6.右上角点击GENERATE CODE
2.使用VSCODE创建platformio配置
1.在创建好的工程中创建platformio.ini文件
这些配置都可以参考Makefile中的配置,这也是stm32cubemx生成Makefile的原因,主要是去掉framework的配置,增加build_flags的配置用来查找头文件,src_files用来查找源文件,ldscript指示链接文件;后续有一些middleware加入,比如RTOS,也可以参考Makefile直接加到platformio.ini中
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:genericSTM32F103ZE]
platform = ststm32
board = genericSTM32F103ZE
build_flags =
-D STM32F103xE
-D USE_HAL_DRIVER
-ICore/Inc
-IDrivers/CMSIS/Include
-IDrivers/CMSIS/Device/ST/STM32F1xx/Include
-IDrivers/STM32F1xx_HAL_Driver/Inc
-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy
src_filter = +<Core/Src> +<startup_stm32f103xe.s> +<Drivers/> +<Middlewares>
board_build.ldscript = ./STM32F103ZETx_FLASH.ld
upload_protocol = stlink
debug_tool = stlink
[platformio]
src_dir = ./
2.保存后platformIO自动创建工程
如果没有自动保存,尝试打开platformIO的控制面板
3.修改Main.c,加入led闪烁代码
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(500);
}
/* USER CODE END 3 */
3.编译、烧录、调试
编译和烧录以及查看串口
调试
或者使用vscode的调试按钮
4.添加第三方库,比如lvgl
使用platformIO的库管理功能添加库,比如lvgl,然后在.pio目录下复制一份lvgl_conf_templat.h为lvgl_conf.h,并修改必要的宏定义
1.打开PlatformIO home,添加lvgl,Add to Project,选择当前工程
添加成功后platformio.ini中会多一行:
lib_deps = lvgl/lvgl@^9.2.2
2.添加lvgl的配置
拷贝.pio目录下lvgl库中的lv_conf_template.h为lv_conf.h
修改lv_conf.h
- 文件开始的
#if 0
改为#if 1
,以启动相关内容 - 修改
LV_MEM_SIZE
为32 * 1024U,因为STM32F103ZET6的内存只有64KB,不能都给LVGL,这里改一个小一点的值,以便可以编译通过,否则会报错
.pio\build\genericSTM32F103ZE\firmware.elf section
.bss' will not fit in region
RAM'
修改main.c
添加头文件
/* USER CODE BEGIN Includes */
#include <lvgl.h>
/* USER CODE END Includes */
初始化lvgl:
/* USER CODE BEGIN 2 */
lv_init();
/* USER CODE END 2 */
由此可以实现
- STM32cubeMX建立demo
- PlatformIO管理依赖库,编译下载调试等功能
- VSCODE加MarsCode等工具自动补全,快速编程