1. Archlinux下 stm32 烧写工具配置

1. 使用的开发板子

使用的开发板是好几年好几年前买的了,板子一直在家里睡觉。最近闲着没事,所以打算学习一下stm32.
板子是普中STM32-PZ6806L开发板。开发板的图见下

puzhongkaifaban.png

2. stm32 烧入程序原理

我们这里使用ISP(In-System Programming)烧入.
ISP 烧入使用串口.
这个开发板子提供了上图的6的位置是一个miniUSB5的位置是一个usb转串口的芯片。5,6的原理图见下:

yuanli.png

其中串口的 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

注意:

  1. 上面可能执行make的时候会出现编译错误。因为archlinux是滚动更新的。不能保证代码每次都编译通过。我这次编译就出现了错误。但是也很好解决,就不多说了。

  2. make install 命令会默认安装stm32flash命令到/usr/local下面。如过不想安装到这个里面请修改Makefile

  3. 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我们可以看到如下配置:应该是通过rstboot两个口去配置芯片进入和退出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 低电平

所以,我们执行我们根据开发板的原理图和这里的手册尝试执行如下命令。发现就成功了。
注意:

  1. 我的芯片是stm32f1, 而stm32w的配置可能有所不同。
  2. 芯片进入和退出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. 其他

  1. 上面烧写环境是在archlinux进行的。但是配置过程好像跟linux发行版本没有什么太大的关系。唯一可能失败的地方应该就是编译stm32flash的地方。

  2. 另外串口/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 ## 重启生效

  1. 如果你的archlinux 配置了AUR,那么也可以从AUR仓库下载
yay -S stm32flash

查看man stm32flash 其使用方法与上面的稍微有点不同。现在的序列为-i rts,,,-dtr,,,dtr:-rts,,,-dtr,,,dtr。 其中每个逗号代表两个信号之间相隔100ms。当然具体的配置,要查看你们电脑里面的man pages。不能一概而论。多尝试尝试。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,692评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,482评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,995评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,223评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,245评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,208评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,091评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,929评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,346评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,570评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,739评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,437评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,037评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,677评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,833评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,760评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,647评论 2 354

推荐阅读更多精彩内容