STM32 的USART1(PA9、PA10)串口下载踩坑

STM32 的USART1(PA9、PA10)串口下载踩坑

JLink、ST-Link之类的高端下载、调试工具是单片机开发的万能小钢炮,但是万一哪天局势不利,手上只有一个USB-to-TTL转接器外加四根线的时候也可以用STM32的串口下载救救急(当然也有不少能忍受得了龟爬的下载速度和有限的功能的老哥,解决问题才是最重要的,不多评价....)

一直用ST-Link(当然是淘宝10元爆款)下载,偶然一次遇到需要串口下载的机会是那天临时找不到ST-Link... 然后就想试试串口效果,反正PlatformIO也支持串口下载,万万没想到事情还真没那么简单,下载失败,留我在原地一脸懵逼。

排坑之路从度娘开始,首先看到这个:如何通过串口STM32 烧录HEX文件_百度经验

mcuisp

打开发现 ... 是用的某第三方软件mcuisp,而且好像只能手动选文件、搜索端口、然后好像也只能下载MDK生成的hex,对PlatformIO编译出来的elf格式不是很友好,而且还是想在PlatformIO上一键(呸,三键)上传更方便。

然后就看到了其实PlatformIO使用的是一个开源工具stm32flash,遂继续百度,找到了项目开源地址 http://stm32flash.googlecode.com/
结果...emmm...

stm32@googlecode

然后还有这个。
stm32flash download | SourceForge.net
貌似可以访问,而且可以看到源码,舒服了。。。

可是并没有解决问题啊,我遇到的依然一直是这个结果:


Failed to init device.

然后再搜索错误代码,找到这个[SOLVED] Failed to init device. stm32flash 0.4
里面似乎有相同的错误,但是这老哥其实就是RX、TX反接所以才下载失败 [/黑人脸]。。。。

image.png

不过看到这么一段回复,似乎有点线索:

Which board?

Which upload method did you select in Arduino IDE?
Have you flashed the stm32duino bootloader?

难不成这个串口下载是要通过这老哥口中的stm32duino bootloader来实现?
继续挖坑。。。
进一步找到这个wiki:Blue Pill - STM32duino wiki
好像是跟我手里一模一样的板子,原来还有个帅气的代号Bule Pill

Blue Pill

感叹一下万能的淘宝... 8元包邮可以用到老外们玩的科技产品 ... 不,这不是重点。然后又看到里面有这个链接:Flashing the bootloader
点开进一步看到这个:

Most STM32F103 boards do not come with a USB bootloader installed. The main exception is the Maple mini. The STM32F4 series MCU has a built in Serial and USB (DFU) bootloader, so there is no need to install an additional bootloader.

On the STM32F103 there are 2 ways to flash the bootloader onto the board

  • Using the native bootloader (built into the STM32F103), by connecting to USART1 using a USB to serial TTL adapter.
  • Using a SWD programmer tool like the ST-Link or Black Magic Probe.

这么说的话,Bootloader 是一段特殊功能程序,而多数STM32F103的MCU没有安装bootloader,但是可以通过下载的方式添加。而添加的方式是:

  1. STLink等工具下载
  2. 使用STM32F103内置bootloader(native bootloader (built into the STM32F103)),用USART1串口下载???

其实看到这里我还是懵逼的,为啥没有bootloader又有内置bootloader,到底是有还是没有呢?

先继续看解释:

Flashing the bootloader onto the Black Pill via USB to Serial converter using UART1

Windows
The Black Pill does not come with any USB bootloader. This process should be roughly the same for all F103 boards.

  1. Download the correct bootloader binary, in this case generic_boot20_pb12.bin [2]
  2. Set the 'boot 0' pin/jumper high, and 'boot 1' low
    • B0+ to center pin
    • B1- to center pin
  3. Connect the board to the PC using a USB to serial converter
    • RX to PA9
    • TX to PA10
    • GND to G on the ST-Link connector
    • 3.3V to V3 on the ST-Link connector
    • Ensure the USB to serial converter is in 3.3 V mode
  4. Reset the board, the user LED should now be off
  5. Download and install Flash Loader Demonstrator from here: [3]
  6. Use Flash Loader Demonstrator to flash the bootloader
    • See screenshots below
    • Select the correct COM port, yours will be different from the screenshots
  7. For normal use, set both boot pins low
    • B0- to center pin B1 stays the same

继续看发现了一个似乎有些熟悉的东东,为啥这里下载步骤要修改BOOT0BOOT1的跳线方式???不解,所以又去查了下STM32F103的芯片手册。。。

STM32芯片手册中关于BOOT0和BOOT1的解释

官方手册果然是比较硬核?似乎很厉害的样子,但是除了定义启动代码位置以外,没找到想要的东西啊。。。
底下还有一段蹩脚的机翻:

内嵌的自举程序?

内嵌的自举程序存放在系统存储区,由ST在生产线上写入,用于通过可用的串行接口对闪存存储器进行重新编程

这段似乎就比较接近真相了... 继续挖出里面提到的 AN2606

STM32 microcontroller system memory boot mode

Introduction

The bootloader is stored in the internal boot ROM memory (system memory) of STM32 devices. It is programmed by ST during production. Its main task is to download the application program to the internal Flash memory through one of the available serial peripherals (USART, CAN, USB, I2C, SPI, etc.)...

按ST官方的说法是:STM32芯片出厂的时候就烧录了一段用于串行下载的 bootloader ,支持用任意串口方式烧录程序,包括 USART, CAN, USB, I2C, SPI... 好像就是这里了。。。

关于 bootloader 的一些官方解释:


似乎发现了问题的本质???

参照第一行:

The STM32F10xxx bootloader is activated by applying pattern1(described in Table 2:Bootloader activation patterns). The following table shows the hardware resources used bythis bootloader.

The system clock is derived from the embedded internal high-speed RC, no external quartz is required for the bootloader execution.

我终于找到了..... :

The STM32F10xxx bootloader is activated by applying pattern1(described in Table 2:Bootloader activation patterns).

好吧,跳线帽改成这样:

  • BOOT0 - HIGH
  • BOOT1 - LOW
    再用 stm32flash 扫描一遍看看:
C:\Users\dexfire\.platformio\packages\tool-stm32duino>stm32flash.exe COM5
stm32flash 0.4

http://stm32flash.googlecode.com/

Interface serial_w32: 57600 8E1
Version      : 0x22
Option 1     : 0x00
Option 2     : 0x00
Device ID    : 0x0410 (Medium-density)
- RAM        : 20KiB  (512b reserved by bootloader)
- Flash      : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB

连接成功~~~

用PlatformIO Upload试试....


PlatformIO Upload

也成功了。。。
坑虽然多,走过的时候一个一个填起来,慢慢也发现其实也不是填不完嘛。。。虽然这个官方内置 bootloader 的简介藏的是隐蔽了点... awsl ... o( ̄▽ ̄)o

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

推荐阅读更多精彩内容