STM32开发1:安装工作环境
Step 1 : 准备材料
本教程基于 Linux 编写(Archlinux),Windows 下网上已经有足够多的资料了也无需多说。
准备一块 STM32F103C8T6 最小系统板(笔者用的是国产 ST 芯片,淘宝上十来块钱),准备一个 ST-LINK v2(淘宝上十来块钱),4 根跳线。
注:购买的时候网上一般会有两种板子,一种是 C6T6 的板子,一种是 C8T6 的板子,两者的区别不大。给出一些比较明显的差别(ATMega328P 即为 Arduino Uno 使用的芯片):
| ATMega328P | STM32F108C6T6 | STM32F108C8T6 | |
|---|---|---|---|
| Flash | 32KB | 32KB | |
| EEPROM | 1KB | None | None |
| SRAM | 2KB | 10KB | 20KB |
| CPU 架构 | AVR | ARM 32Bit Cortex-M3 | ARM 32Bit Cortex-M3 |
| I/O 引脚数量 | 23 | 37 | 37 |
其他的可以看看这篇文章,可能有所帮助
Step 2 安装软件
首先,从ST 官网上下载下来他们的 STM32CubeMX 和 STM32CubeProgrammer,然后解压后安装。
然后使用 pacman 将其他的几个工具安装下来(vscode 自行想办法安装):
$ sudo pacman -S bear openocd inetutils stlink arm-none-eabi-gcc arm-none-eabi-gdb arm-none-eabi-newlibStep 3 后续配置
STM32CubeMX 中配置
首先,我们用 STM32CubeMX 配置一个基础项目。
依次点击 File-New Project,会弹出这样一个界面:
在 Commercial Part Number 里面输入你产品的编号,然后在左下角选中双击进入外设配置界面:
对于我这块系统板,时钟是 32.768kHz,外设晶振是 8kHz,和 ST 给出的默认配置相同,故无需多加设置。只需要依次点击 System Core-RCC-HSE,下拉菜单选中 Crystal/Ceramic Resonator 即可。(如果你不是这个频率的,那么自己找资料然后在 Clock Configuration 里面改)
接下来配置一下链接方式,选中 SYS-Debug,由于我使用的是 ST-Link,所以下拉菜单选中 Serial Wire 即可。
接下来在 Project Configuration 里面填写一下 Project Name,然后 Toolchain 选择 Makefile(为后续 bear 生成 compile_commands.json 做铺垫)。最后点击 GENERATE CODE 就生成完成了。
STM32CubeProgrammer 配置
新买到的板如果使用各种各样网上的办法都烧不进去的话,可以先用这个软件更新一下固件(猜测是国产芯片的固件编号识别不出)。
先点击 Firmware Upgrade,然后点击 Open in update mode,此时会显示 ST-Link is not in the DFU mode. Please restart it.然后重新插拔一次 ST-Link,此时再点击 Open in update mode 就成功了。然后点击 Upgrade 更新固件,此时 ST-Link 上面的红灯会闪烁。当显示 Upgrade successful 后,更新完成。此时就可以按照网上的办法烧录了。
VSCode 配置
安装 C/C++插件包,Arm Assembly, clangd, Cortex-Debug 插件。
在刚刚创建的项目根目录下执行bear -- make生成 compile_commands.json。(此前如果已经运行过 make 了,那么把 builds/文件夹删掉,然后再执行该命令)
接下来在 vscode 里面打开 main.c,发现报错数量就接近没了,但是还有一个__FPU_PRESENT 的问题等待解决。这是由于浮点运算支持的问题,我没有找到太好的办法,只能先用一个暂时的方法顶上:
打开 Makefile,在 ASFLAGS 和 CFLAGS 末尾都加上-mfloat-abi=soft,表示使用软件模拟浮点计算(这会导致性能损失,但是暂时能解决编译问题。)
然后再执行bear -- make,生成完毕后错误就没有了。
openocd 配置
在项目根目录下先建两个文件夹,然后将对应的脚本拷贝一下:
$ mkdir interface target
$ cp /usr/share/openocd/scripts/interface/stlink.cfg interface
$ cp /usr/share/openocd/scripts/target/stm32f1x.cfg target此时我们运行一下 openocd:
$ openocd -f interface/stlink.cfg -f target/stm32f1x.cfg如果是国产芯片,可能会出现如下报错:
Open On-Chip Debugger 0.11.0
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 1000 kHz
Info : STLINK V2J40S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.292205
Warn : UNEXPECTED idcode: 0x2ba01477
Error: expected 1 of 1: 0x1ba01477见招拆招,我们打开 target/stm32f1x.cfg,将其中set _CPUTAPID 0x1ba01477一行中的0x1ba01477改成0x02ba01477即可。
如果没有问题,就会有如下输出:
Open On-Chip Debugger 0.11.0
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 1000 kHz
Info : STLINK V2J40S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.298601
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f1x.cpu on 3333
Info : Listening on port 3333 for gdb connections此时 openocd 进入监听状态(ST-Link 上面的灯红蓝交替闪烁),我们就可以开始烧录程序了。
程序烧录
在项目根文件夹下依次执行这两个命令:
$ make
$ openocd -f interface/stlink.cfg -f target/stm32f1x.cfg当 openocd 进入监听状态后,就可以使用 telnet 连接了并烧录了(假定你的 hex 文件在/path/to/your/program/xxx.hex 下,一般是在项目根目录的 build/文件夹下)
$ telnet localhost 4444
Trying ::1...
Connection failed: 拒绝连接Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger
> program /path/to/your/program/xxx.hex
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080009c4 msp: 0x20005000
** Programming Started **
device id = 0x000fa410
flash size = 64kbytes
** Programming Finished **
> reset
> exit
Connection closed by foreign host.这样程序就烧录完成了。