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...
然后还有这个。
stm32flash download | SourceForge.net
貌似可以访问,而且可以看到源码,舒服了。。。
可是并没有解决问题啊,我遇到的依然一直是这个结果:
Failed to init device.
然后再搜索错误代码,找到这个[SOLVED] Failed to init device. stm32flash 0.4
里面似乎有相同的错误,但是这老哥其实就是RX、TX反接所以才下载失败 [/黑人脸]。。。。
不过看到这么一段回复,似乎有点线索:
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
感叹一下万能的淘宝... 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,但是可以通过下载的方式添加。而添加的方式是:
- STLink等工具下载
- 使用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.
- Download the correct bootloader binary, in this case generic_boot20_pb12.bin [2]
- Set the 'boot 0' pin/jumper high, and 'boot 1' low
- B0+ to center pin
- B1- to center pin
- 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
- Reset the board, the user LED should now be off
- Download and install Flash Loader Demonstrator from here: [3]
- Use Flash Loader Demonstrator to flash the bootloader
- See screenshots below
- Select the correct COM port, yours will be different from the screenshots
- For normal use, set both boot pins low
- B0- to center pin B1 stays the same
继续看发现了一个似乎有些熟悉的东东,为啥这里下载步骤要修改BOOT0
和BOOT1
的跳线方式???不解,所以又去查了下STM32F103的芯片手册。。。
官方手册果然是比较硬核?似乎很厉害的样子,但是除了定义启动代码位置以外,没找到想要的东西啊。。。
底下还有一段蹩脚的机翻:
内嵌的自举程序存放在系统存储区,由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
网友评论