STM32 的USART1(PA9、PA10)串口下載踩坑
JLink、ST-Link之類的高端下載、調試工具是單片機開發(fā)的萬能小鋼炮,但是萬一哪天局勢不利,手上只有一個USB-to-TTL轉接器外加四根線的時候也可以用STM32的串口下載救救急(當然也有不少能忍受得了龜爬的下載速度和有限的功能的老哥,解決問題才是最重要的,不多評價....)
一直用ST-Link(當然是淘寶10元爆款)下載,偶然一次遇到需要串口下載的機會是那天臨時找不到ST-Link... 然后就想試試串口效果,反正PlatformIO也支持串口下載,萬萬沒想到事情還真沒那么簡單,下載失敗,留我在原地一臉懵逼。
排坑之路從度娘開始,首先看到這個:如何通過串口給STM32 燒錄HEX文件_百度經(jīng)驗

打開發(fā)現(xiàn) ... 是用的某第三方軟件mcuisp,而且好像只能手動選文件、搜索端口、然后好像也只能下載MDK生成的hex,對PlatformIO編譯出來的elf格式不是很友好,而且還是想在PlatformIO上一鍵(呸,三鍵)上傳更方便。
然后就看到了其實PlatformIO使用的是一個開源工具stm32flash,遂繼續(xù)百度,找到了項目開源地址 http://stm32flash.googlecode.com/
結果...emmm...

然后還有這個。
stm32flash download | SourceForge.net
貌似可以訪問,而且可以看到源碼,舒服了。。。
可是并沒有解決問題啊,我遇到的依然一直是這個結果:

然后再搜索錯誤代碼,找到這個[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來實現(xiàn)?
繼續(xù)挖坑。。。
進一步找到這個wiki:Blue Pill - STM32duino wiki
好像是跟我手里一模一樣的板子,原來還有個帥氣的代號Bule Pill

感嘆一下萬能的淘寶... 8元包郵可以用到老外們玩的科技產(chǎn)品 ... 不,這不是重點。然后又看到里面有這個鏈接: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 是一段特殊功能程序,而多數(shù)STM32F103的MCU沒有安裝bootloader,但是可以通過下載的方式添加。而添加的方式是:
- STLink等工具下載
- 使用STM32F103內置bootloader(native bootloader (built into the STM32F103)),用USART1串口下載???
其實看到這里我還是懵逼的,為啥沒有bootloader又有內置bootloader,到底是有還是沒有呢?
先繼續(xù)看解釋:
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
繼續(xù)看發(fā)現(xiàn)了一個似乎有些熟悉的東東,為啥這里下載步驟要修改BOOT0和BOOT1的跳線方式???不解,所以又去查了下STM32F103的芯片手冊。。。

官方手冊果然是比較硬核?似乎很厲害的樣子,但是除了定義啟動代碼位置以外,沒找到想要的東西啊。。。
底下還有一段蹩腳的機翻:

內嵌的自舉程序存放在系統(tǒng)存儲區(qū),由ST在生產(chǎn)線上寫入,用于通過可用的串行接口對閃存存儲器進行重新編程。
這段似乎就比較接近真相了... 繼續(xù)挖出里面提到的 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試試....

也成功了。。。
坑雖然多,走過的時候一個一個填起來,慢慢也發(fā)現(xiàn)其實也不是填不完嘛。。。雖然這個官方內置 bootloader 的簡介藏的是隱蔽了點... awsl ... o( ̄▽ ̄)o