1. 使用的开发板子
使用的开发板是好几年好几年前买的了,板子一直在家里睡觉。最近闲着没事,所以打算学习一下stm32.
板子是普中STM32-PZ6806L开发板。开发板的图见下
2. stm32 烧入程序原理
我们这里使用ISP(In-System Programming)烧入.
ISP 烧入使用串口.
这个开发板子提供了上图的6
的位置是一个miniUSB
,5
的位置是一个usb转串口的芯片。5
,6
的原理图见下:
其中串口的 DTR
引脚控制STM32
的重置。
RTS
引脚控制STM32
的boot选择。
我们要想烧入程序:要把开发版上面的34
的位置的跳冒
BOOT0
连接到3.3V
BOOT1
连接到GND。
(PS,我试了试他们都连接到GND也是可以用的。。。)
3. 下载 stm32flash 程序
git clone https://github.com/ARMinARM/stm32flash.git
make
make install
注意:
上面可能执行make的时候会出现编译错误。因为
archlinux
是滚动更新的。不能保证代码每次都编译通过。我这次编译就出现了错误。但是也很好解决,就不多说了。make install
命令会默认安装stm32flash
命令到/usr/local
下面。如过不想安装到这个里面请修改Makefile
。在
Makefile
中也指定了man pages
安装到哪里了。请参考man page
的配置,去配置man
手册。(ps:如果不需要手册,这一步不需要考虑了)
4. 测试stm32flash程序能不能正常工作
我们把开发板通过usb
插入电脑。并查看man stm32flash
的配置,发现可以直接执行stm32flash <dev_id>
查看设备信息。 如下:
[vicking@archlinux ~]$ ls /dev/ttyUSB*
/dev/ttyUSB0 /dev/ttyUSB2
[vicking@archlinux ~]$ stm32flash /dev/ttyUSB2
stm32flash 0.4
http://stm32flash.googlecode.com/
Interface serial_posix: 57600 8E1
Failed to init device.
喔吼,执行失败了。查看man stm32flash
我们可以看到如下配置:应该是通过rst
和boot
两个口去配置芯片进入和退出bootloader
。
BOOTLOADER GPIO SEQUENCE
This feature is currently available on Linux host only.
As explained in ST application note AN2606, after reset the STM32 will execute either the application pro‐
gram in user flash or the bootloader, depending on the level applied at specific pins of STM32 during re‐
set.
STM32 bootloader is automatically activated by configuring the pins BOOT0="high" and BOOT1="low" and then
by applying a reset. Application program in user flash is activated by configuring the pin BOOT0="low"
(the level on BOOT1 is ignored) and then by applying a reset.
When GPIO from host computer are connected to either configuration and reset pins of STM32, stm32flash can
control the host GPIO to reset STM32 and to force execution of bootloader or execution of application pro‐
gram.
The sequence of GPIO values to entry to and exit from bootloader mode is provided with command line option
-i GPIO_string.
The format of GPIO_string is:
GPIO_string = [entry sequence][:[exit sequence]]
sequence = [-]n[,sequence]
In the above sequences, negative numbers correspond to GPIO at "low" level; numbers without sign corre‐
spond to GPIO at "high" level. The value "n" can either be the GPIO number on the host system or the
string "rts", "dtr" or "brk". The strings "rts" and "dtr" drive the corresponding UART's modem lines RTS
and DTR as GPIO. The string "brk" forces the UART to send a BREAK sequence on TX line; after BREAK the
UART is returned in normal "non-break" mode. Note: the string "-brk" has no effect and is ignored.
As example, let's suppose the following connection between host and STM32:
• host GPIO_3 connected to reset pin of STM32;
• host GPIO_4 connected to STM32 pin BOOT0;
• host GPIO_5 connected to STM32 pin BOOT1.
In this case, the sequence to enter in bootloader mode is: first put GPIO_4="high" and GPIO_5="low"; then
send reset pulse by GPIO_3="low" followed by GPIO_3="high". The corresponding string for GPIO_string is
"4,-5,-3,3".
To exit from bootloade and run the application program, the sequence is: put GPIO_4="low"; then send reset
pulse. The corresponding string for GPIO_string is "-4,-3,3".
The complete command line flag is "-i 4,-5,-3,3:-4,-3,3".
查看文档后,我们这个开发板子上面进入bootloader
和离开bootloader
的序列应该如下
rts,dtr,-dtr ## 进入bootloader序列: rts高电平,dtr高电平,dtr 低电平
-rts,dtr,-dtr ## 退出bootloader序列: rts低电平,dtr高电平,dtr 低电平
所以,我们执行我们根据开发板的原理图和这里的手册尝试执行如下命令。发现就成功了。
注意:
- 我的芯片是stm32f1, 而stm32w的配置可能有所不同。
- 芯片进入和退出bootloader的顺序设置可能跟你的ch34x(并转串)芯片和stm32芯片连接不同而有不同的设置。自己可以测试出自己的序列。
[vicking@archlinux ~]$ stm32flash -i rts,dtr,-dtr:-rts,dtr,-dtr /dev/ttyUSB2
stm32flash 0.4
http://stm32flash.googlecode.com/
Interface serial_posix: 57600 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0414 (High-density)
- RAM : 64KiB (512b reserved by bootloader)
- Flash : 512KiB (sector size: 2x2048)
上面读取的是设备信息,反正我暂时也看不懂,就先不说了。
5. 测试写入程序
stm32flash -b 9600 -i rts,dtr,-dtr:-rts,dtr,-dtr -w newhelloworld.hex -v -g 0x0 /dev/ttyUSB1
stm32flash 0.4
http://stm32flash.googlecode.com/
Using Parser : Intel HEX
Interface serial_posix: 9600 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0414 (High-density)
- RAM : 64KiB (512b reserved by bootloader)
- Flash : 512KiB (sector size: 2x2048)
- Option RAM : 16b
- System RAM : 2KiB
Write to memory
Erasing memory
Wrote and verified address 0x08000bbc (100.00%) Done.
Starting execution at address 0x08000000... done.
-w:指定要写入的hex文件,
-v:表示校验写入的数据
-g:表示从哪个地址执行程序
6. 其他
上面烧写环境是在
archlinux
进行的。但是配置过程好像跟linux
发行版本没有什么太大的关系。唯一可能失败的地方应该就是编译stm32flash
的地方。另外串口
/dev/ttyUSB2
这样的串口设备,普通用户可能没有办法使用。需要root权限。
下面给出archlinux 下面非root用户也可以使用的方法。
[vicking@archlinux ~]$ ls -l /dev/ttyUSB2
crw-rw---- 1 root uucp 188, 2 Jul 2日 01:36 /dev/ttyUSB2 ## 发现设备文件属于uucp组
[vicking@archlinux ~]$ sudo usermod -a -G uucp vicking ##把我们当前的用户加到uucp组
[sudo] vicking 的密码:
[vicking@archlinux ~]$ reboot ## 重启生效
- 如果你的archlinux 配置了AUR,那么也可以从AUR仓库下载
yay -S stm32flash
查看man stm32flash
其使用方法与上面的稍微有点不同。现在的序列为-i rts,,,-dtr,,,dtr:-rts,,,-dtr,,,dtr
。 其中每个逗号代表两个信号之间相隔100ms。当然具体的配置,要查看你们电脑里面的man pages
。不能一概而论。多尝试尝试。