SUBSYS: USB DFU¶
1 功能概述¶
usb hid dfu sample
演示了Zephyr USB DFU(Device Firmware Update)在PAN1080 SoC上的使用方法,主要包括:
结合
mcuboot
为固件分配区域位置通过usb升级工具
dfu-util
检测usb dfu device并为固件进行升级
2 环境准备¶
PAN1080A-AFLD (LQFP封装、1MB Flash、64 Pin)
Micro USB线两条
用于供电和查看串口打印Log
用于USB通信升级
硬件接线:
将PAN1080A-AFLD EVB板上的P02,P03通过跳线帽与USB DM和USB DP相连
PC软件: 串口调试助手(UartAssist)或终端工具(SecureCRT),波特率921600
USB Host环境:
dfu-util
运行在MAC OS
环境下,相关环境搭建参考http://dfu-util.sourceforge.net/
3 编译和烧录¶
例程位置:zephyr\samples_panchip\subsys\usb\dfu
使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍。
需要注意,编译后下载时需要将程序bin文件下载入固定程序区域,并烧录mcuboot
具体操作请参考04_dev_guides\zephyr_bootloader_guidance.md
4 演示说明¶
4.1 下载程序¶
参考04_dev_guides\zephyr_bootloader_guidance.md
以默认升级方式,下载
mcuboot
程序将编译好的
dfu
程序,通过imgtool
处理,生成签名好的文件并下载入slot0_partition
,复位确认mcuboot
可以正常跳转至dfu
程序参考
03_samples\basic\hello_world
准备hello_world_with_boot
作为待升级程序,通过imgtool
进行操作,生成signed1.0.bin
,大小为32k
python bootloader\mcuboot\scripts\imgtool.py sign --key bootloader\mcuboot\root-rsa-2048.pem --header-size 0x200 --align 8 --version 1.0 --slot-size 0x8000 --pad build\$(sample_name)\zephyr\zephyr.bin build\$(sample_name)\zephyr\signed1.0.bin
4.2 USB Host识别¶
参考http://dfu-util.sourceforge.net/
将运行的dfu程序板卡,通过usb与MAC电脑相连,在MAC的系统报告usb界面,正确识别到Zephyr DFU sample
MAC OS X操作系统下准备USB Host端的升级工具
Mac OS X users can also get dfu-util from Homebrew with “brew install dfu-util” or from MacPorts.
准备
Homebrew
(mac os下载开源库工具),下载按照以下命令在根目录按照bash
(命令行)运行/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
通过
homebrew
下载开源工具dfu-util
,参考以下命令下载并配置brew install dfu-util export HOMEBREW_NO_INSTALL_CLEANUP=TRUE
通过
dfu-tool
命令dfu-util -l
查看usb dfu
信息,log如下,usb dfu可以正常识别通信dfu-util 0.11 Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc. Copyright 2010-2021 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: [2fe4:ffff] ver=0207, devnum=21, cfg=1, intf=0, path="20-3", alt=1, name="image-1", serial="0123456789ABCDEF" Found DFU: [2fe4:ffff] ver=0207, devnum=21, cfg=1, intf=0, path="20-3", alt=0, name="image-0", serial="0123456789ABCDEF"
4.3 DFU升级¶
以上环境正确运行后,将准备好的signed1.0.bin
存在mac用户根目录,通过命令dfu-util --alt 1 --download signed1.0.bin
运行,开始进行升级
升级进行并成功后,显示如下log
dfu-util 0.11
Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2021 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: Warning: Invalid DFU suffix signature
dfu-util: A valid DFU suffix will be required in a future dfu-util release
Opening DFU capable USB device...
Device ID 2fe4:0005
Device DFU version 0110
Claiming USB DFU (Run-Time) Interface...
Setting Alternate Interface zero...
Determining device status...
DFU state(0) = appIDLE, status(0) = No error condition is present
Device really in Run-Time Mode, send DFU detach request...
Resetting USB...
Opening DFU USB Device...
Claiming USB DFU Interface...
Setting Alternate Interface #1 ...
Determining device status...
DFU state(2) = dfuIDLE, status(0) = No error condition is present
DFU mode device DFU version 0110
Device returned transfer size 64
Copying data from PC to DFU device
Download [=========================] 100% 32768 bytes
Download done.
4.4 LOG说明¶
为减少LOG对usb通信影响,USB DFU运行没有开启通信中的LOG,下载mcuboot运行后,LOG显示如下
*** Booting Zephyr OS build zephyr-v2.7.0-416-g5b5cf2f6d901 ***
I: Starting bootloader
I: Primary image: magic=good, swap_type=0x4, copy_done=0x1, image_ok=0x1
I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Boot source: none
I: Swap type: none
I: Bootloader chainload address offset: 0x10000
I: Jumping to the first image slot
*** Booting Zephyr OS build zephyr-v2.7.0-488-g22d426332c5f ***
USB DFU 在升级完成后,hello_world_with_boot
程序存储在slot1_partition
,以Swap type: test
模式进行运行,第一次复位芯片,hello_world_with_boot
程序运行
*** Booting Zephyr OS build zephyr-v2.7.0-416-g5b5cf2f6d901 ***
I: Starting bootloader
I: Primary image: magic=good, swap_type=0x4, copy_done=0x1, image_ok=0x1
I: Secondary image: magic=good, swap_type=0x2, copy_done=0x3, image_ok=0x3
I: Boot source: none
I: Swap type: test
I: Bootloader chainload address offset: 0x10000
I: Jumping to the first image slot
*** Booting Zephyr OS build zephyr-v2.7.0-472-g6a4336897fe6 ***
Hello World from Zephyr on pan108xxb5_evb with boot!
ih_magic 96f3b83d
ih_load_addr 0
ih_hdr_size 200
ih_protect_tlv_size 0
ih_img_size 3f6c
version 1.0.0.0
第二次复位芯片,芯片重新运行在dfu
程序中
*** Booting Zephyr OS build zephyr-v2.7.0-416-g5b5cf2f6d901 ***
I: Starting bootloader
I: Primary image: magic=good, swap_type=0x2, copy_done=0x1, image_ok=0x3
I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Boot source: none
I: Swap type: revert
I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
I: Bootloader chainload address offset: 0x10000
I: Jumping to the first image slot
*** Booting Zephyr OS build zephyr-v2.7.0-488-g22d426332c5f ***
5 开发说明¶
5.3 补充说明¶
dfu-util
暂时不支持在window环境下运行,即使通过Zadig
安装windows usb驱动程序,可以正常识别,并且dfu-util
可以正常检测到dfu dev
,仍有以下限制# This composite configuration may not work for Windows OS Host. # Windows OS does not send reset after DFU_DETACH request # (does not re-enumerates) and thus make it unable for the device # to restart in DFU mode.
mac os
环境下dfu-util
通信速率为64B/260ms,受限于主机发包速率,通信包效率通过逻辑分析仪抓包,约为1~2ms传输64BFIFO size:Endpoint0 64B(max)
Transfer Type:Control Transfer
one block传输通信:Get status—DFU download 64B—Get status
通过逻辑分析仪链接USB pin(P02—USB DM,P03—USB DP),可以抓取传输过程的一个USB download block数据交互流程如下
6 RAM/Flash资源使用情况¶
Memory region Used Size Region Size %age Used
FLASH: 31464 B 1020 KB 8.00%
SRAM: 7024 B 64 KB 10.72%