STM32: Maple bootloader介绍 – 在自制ST-LINK/v2-1调试器上运行

Maple系列开发板是LeafLabs公司出品,硬件与软件都开源。可惜的是,从官方主页(http://www.leaflabs.com/about-maple/)上可以看到Maple产品线从2015年3月官方将不再支持:

As of March 2015, the LeafLabs Maple line and the libmaple library are end-of-life and no longer supported by LeafLabs. The design files for Maple and Maple Mini will remain available on GitHub, under a CC-BY-SA 2.0 license, for anyone who wants to recreate or reimagine these boards. libmaple will also stay on GitHub, and we will continue to take community patches.

You can still read the docs. The forums will remain active until August 2016, but at that point they will be converted to a static archive. Consider checking out the resources and community at www.stm32duino.com instead.

这里我们需要用到它的bootloader,  以便使我手中的这块ST-LINK/V2-1开发板能够正常加载JLINK和ST-LINK/V2-1固件,看看在没有原生ST-LINK/V2-1 bootloader的情况下,JLINK与ST-LINK/V2-1能否正常工作。

Maple-Mini开发板个人还是非常喜欢的:

2016_07_18_maple-mini_top

2016_07_18_maple-mini_bottom

(原图片来自:http://www.alpha-crucis.com/en/microcontroleurs/3814-maple-mini-3700386112805.html)

  • 为什么是maple-bootloader

使用Maple bootloader的主要是因为它的代码非常清晰,基本没有使用到ST官方的库(官方的库代码实在是太大了)。当然,最主要的还是因为它实现了Device Firmware Upgrade (DFU) 1.1协议,有了它的支持,我们可以通过USB接口上传或者是下载固件。具体请看官方介分绍(http://dfu-util.sourceforge.net/):

dfu-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum. DFU is intended to download and upload firmware to/from devices connected over USB. It ranges from small devices like micro-controller boards to mobile phones. Using dfu-util you can download firmware to your DFU-enabled device or upload firmware from it. dfu-util has been tested with the Openmoko Neo1973 and Freerunner and many other devices.

  • 相关代码下载

Maple bootloader的代码可以从github.com上下载:

$ git clone https://github.com/leaflabs/maple-bootloader.git

NOTE:

这里我们其实可以看到maple-bootloader有多个分支: audio-boot, maple-ret6, master, mini-boot, native-boot, newboot和serial-boot, 每个分支可能对应的是不同的开发板? 这里我们使用的是master分支上的代码。

由于自制的ST-LINK/V2-1调试器与maple开发板所使用的硬件资源有少许的不同,所以还需要修改一下代码。

主要修改的地方有:

- LED指示灯所占用的引脚为GPIOA PIN9

-控制USB端口枚举的引脚为GPIOA PIN15,由于PIN15默认作为JTAG的JTDI引脚,需要进行mapping, 同时将引脚设为Open Drain Ouput

-Button修改:maple上有一个button, 开机时按下(hold)这个button, 可以进入DFU模式,进行固件更新,在ST-LINK/V2-1上没有这个button, 需要去除相关的代码。

- User program在Flash中的位置,maple定义的位置是在0x08005000上,由于之后我们打算去测试提取的JLINK与ST-LINK/V2-1的固件(->@<-)能否正常工作,所以将位置修改为0x08004000上。

修改后的代码可以从这里下载:2016_07_18_maple-bootloader-for-stlink-v2_1.tar.gz

  • 代码编译

下载完代码后,我们可以看到有Makefile文件,表明它使用的是make编译系统,同时,从Makefile文件我们可以看到它所使用的编译工具为arm-none-eabi工具链,所以在编译代码之前,我们需要准备好编译环境。

代码编译只需要执行make命令就好:

B85PRO:maple-bootloader hzak$ make
mkdir -p build/stm32_lib
mkdir -p build/usb_lib
--
-------- begin --------
build/usb.o build/usb_callbacks.o build/usb_descriptor.o build/main.o build/hardware.o build/dfu.o build/usb_lib/usb_regs.o build/usb_lib/usb_int.o build/usb_lib/usb_init.o build/usb_lib/usb_core.o build/usb_lib/usb_mem.o
arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.9.3 20150303 (release) [ARM/embedded-4_9-branch revision 221220]
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Compiling C: usb.c
arm-none-eabi-gcc -c -mthumb -mcpu=cortex-m3 -mthumb-interwork -I.  -O0 -ffunction-sections -fdata-sections -Wall -Wimplicit -Wcast-align -Wpointer-arith -Wswitch -Wredundant-decls -Wreturn-type -Wshadow -Wunused -Wa,-adhlns=build/usb.lst -I./stm32_lib -I./usb_lib -MD -MP -MF .dep/usb.o.d usb.c -o build/usb.o

...

Linking: build/maple_boot.elf
arm-none-eabi-gcc -mthumb -mcpu=cortex-m3 -mthumb-interwork -I.  -O0 -ffunction-sections -fdata-sections -Wall -Wimplicit -Wcast-align -Wpointer-arith -Wswitch -Wredundant-decls -Wreturn-type -Wshadow -Wunused -Wa,-adhlns=build/build/usb.lst -I./stm32_lib -I./usb_lib -MD -MP -MF .dep/maple_boot.elf.d  build/stm32_lib/c_only_startup.o  build/stm32_lib/cortexm3_macro.o  build/usb.o  build/usb_callbacks.o  build/usb_descriptor.o  build/main.o  build/hardware.o  build/dfu.o  build/usb_lib/usb_regs.o  build/usb_lib/usb_int.o  build/usb_lib/usb_init.o  build/usb_lib/usb_core.o  build/usb_lib/usb_mem.o --output build/maple_boot.elf -nostartfiles -Wl,-Map=build/maple_boot.map,--cref,--gc-sections -lc -lgcc -Tstm32_lib/c_only_md.ld

Creating load file for Flash: build/maple_boot.bin
arm-none-eabi-objcopy -O binary build/maple_boot.elf build/maple_boot.bin

Creating Extended Listing: build/maple_boot.lss
arm-none-eabi-objdump -h -S -D build/maple_boot.elf > build/maple_boot.lss

Creating Symbol Table: build/maple_boot.sym
arm-none-eabi-nm -n build/maple_boot.elf > build/maple_boot.sym

Size after:
build/maple_boot.elf  :
section                        size        addr
.isr_vector                     240   134217728
.flashtext                        0   134217968
.text                         11452   134217968
.data                           472   536870912
.bss                             68   536871384
.bss.bDeviceState                 4   536871452
.bss.bIntPackSOF                  1   536871456
.bss.dfuAppStatus                 8   536871460
.bss.userFlash                    1   536871468
.bss.dfuBusy                      1   536871469
.bss.recvBuffer                1024   536871472
.bss.userFirmwareLen              4   536872496
.bss.thisBlockLen                 2   536872500
.bss.uploadBlockLen               2   536872502
.bss.Data_Mul_MaxPacketSize       1   536872504
._usrstack                      259   536872505
.comment                        112           0
.ARM.attributes                  47           0
Total                         13698

Errors: none
-------- end --------

最后,会在build目录下生成我们所需要的bin文件:maple_boot.bin。

  • 使用openocd命令写入固件

使用ST-LINK/V2调试器通过如下命令:

$ /usr/local/bin/openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c init -c 'reset halt' -c 'flash write_image erase build/maple_boot.bin 0x08000000 bin' -c 'verify_image build/maple_boot.bin 0x08000000 bin' -c 'reset run' -c shutdown
Open On-Chip Debugger 0.10.0-dev-00288-g060e9c3 (2016-05-14-22:52)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v16 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 2.876905
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
stm32f1x.cpu: target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08004180 msp: 0x20001000
auto erase enabled
Info : device id = 0x20036410
Info : flash size = 64kbytes
stm32f1x.cpu: target state: halted
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x2000003a msp: 0x20001000
wrote 12288 bytes from file build/maple_boot.bin in 0.692859s (17.320 KiB/s)
stm32f1x.cpu: target state: halted
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x2000002e msp: 0x20001000
verified 12164 bytes in 0.187511s (63.350 KiB/s)
shutdown command invoked
  • 使用dfu-util工具下载固件

从dfu-util网官上我们可以下载到dfu-util的源代码:

$ git clone git://git.code.sf.net/p/dfu-util/dfu-util

自行编译之后,就可以在src/目录下生成我们所需要的dfu-util命令,再通过如下命令,我们可以知道设备的相关信息:

$ dfu-util/src/dfu-util -l
dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Found DFU: [1eaf:0003] ver=0201, devnum=5, cfg=1, intf=0, path="20-2", alt=1, name="DFU Program FLASH 0x08004000", serial="LLM 003"
Found DFU: [1eaf:0003] ver=0201, devnum=5, cfg=1, intf=0, path="20-2", alt=0, name="DFU Program RAM 0x20000C00", serial="LLM 003"

这里,我们需要将固件下载到flash中运行,alt选择1,通过如下命令下载固件:

$ dfu-util/src/dfu-util -a 1 -D build/ch.bin -R
dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

dfu-util: Invalid DFU suffix signature
dfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
Opening DFU capable USB device...
ID 1eaf:0003
Run-time device DFU version 0110
Claiming USB DFU Interface...
Setting Alternate Setting #1 ...
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 0110
Device returned transfer size 1024
Copying data from PC to DFU device
Download	[=========================] 100%        42768 bytes
Download done.
state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present
Done!
Resetting USB to switch back to runtime mode
  • 相关的参考文档
  1. http://www.leaflabs.com/about-maple/
  2. https://github.com/leaflabs/maple-bootloader
  3. http://dfu-util.sourceforge.net/

发表评论

电子邮件地址不会被公开。 必填项已用*标注