# stm32f103_spl_clion STM32F103C8T6 标准外设库(StdPeriph V3.5.0)工程,基于 CLion + CMake + arm-none-eabi-gcc 工具链。 - **MCU**:STM32F103C8T6(Cortex-M3,Medium density,64K Flash / 20K RAM) - **库**:ST 标准外设库 V3.5.0(SPL) - **工具链**:GNU Tools for STM32(arm-none-eabi-gcc 13.3) - **构建**:CMake + Ninja - **烧录/调试**:ST-Link(SWD),STM32CubeProgrammer + ST-LINK_gdbserver ## 目录结构 ``` stm32f103_spl_clion/ ├── CMakeLists.txt # 工程主构建文件 ├── CMakePresets.json # Debug/Release 预设(CLion 零配置打开) ├── STM32F103XX_FLASH.ld # 链接脚本(64K Flash / 20K RAM) ├── cmake/ │ └── gcc-arm-none-eabi.cmake # 交叉工具链定义(-mcpu=cortex-m3, gc-sections 等) ├── CMSIS/ # 内核与启动文件 │ ├── startup_stm32f10x_md.s # GCC 版启动文件(Medium density,对应 C8T6) │ ├── core_cm3.{c,h} │ ├── stm32f10x.h │ └── system_stm32f10x.{c,h} ├── StdPeriph_Driver/ # 标准外设库 V3.5.0 │ ├── inc/ # 23 个外设头文件 │ └── src/ # 23 个外设源文件(未引用的会被 --gc-sections 自动裁掉) ├── User/ # 用户代码 │ ├── main.c # 示例:PC13 LED 闪烁 │ └── stm32f10x_conf.h # SPL 配置(包含所有外设头) └── tools/ ├── flash.sh # 一键 configure -> build -> 烧录 -> 复位 └── debug-server.sh # 启动 ST-LINK GDB Server(端口 61234) ``` ## 一、在 CLion 中打开 1. `File → Open` 选择本目录,CLion 自动识别 `CMakeLists.txt` + `CMakePresets.json`。 2. `Settings → Build, Execution, Deployment → Toolchains`: - 添加一个工具链,Debugger 选 `arm-none-eabi-gdb`(通常 CLion 会自动发现 CubeCLT 工具链)。 3. `Settings → Build, Execution, Deployment → CMake`: - Debug profile 的配置类型会读取 `CMakePresets.json` 的 `Debug` 预设,无需手填 toolchain file。 4. 加载完成后,右上角可直接 **Build**,产物为 `build/Debug/stm32f103_spl_clion.elf`。 ## 二、编译 - **CLion**:点 Build 按钮。 - **命令行**: ```bash cmake --preset Debug cmake --build --preset Debug ``` ## 三、烧录(ST-Link / SWD) - **一键脚本**(命令行,自包含 configure+build+flash): ```bash bash tools/flash.sh ``` > 注意:`flash.sh` 使用独立的 `build/` 目录;若想烧录 CLion 的产物,用下面的方式。 - **CLion 内烧录**:安装 STM32CubeProgrammer / OpenOCD 插件后配置 Run。 ## 四、调试(GDB + ST-Link) 1. 接好 ST-Link(SWD),给板子供电。 2. 终端启动 GDB Server: ```bash bash tools/debug-server.sh # 监听 localhost:61234,under-reset 模式 ``` 3. CLion 中新建 **Embedded GDB Server** 调试配置: - Target:`stm32f103_spl_clion.elf` - GDB:`arm-none-eabi-gdb` - 连接:`target remote localhost:61234` 4. 点 Debug,可下断点、单步。 ## 修改工程名 工程名集中在两处:`CMakeLists.txt` 的 `CMAKE_PROJECT_NAME` 与 `tools/flash.sh` 的 `ELF`。