当前文档版本为 v0.5.0,您可以访问当前页面的 开发中 版本以获取最近可能的更新。

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线两条

    1. 用于供电和查看串口打印Log

    2. 用于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

  1. 以默认升级方式,下载mcuboot程序

  2. 将编译好的dfu程序,通过imgtool处理,生成签名好的文件并下载入slot0_partition,复位确认mcuboot可以正常跳转至dfu程序

  3. 参考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/

  1. 将运行的dfu程序板卡,通过usb与MAC电脑相连,在MAC的系统报告usb界面,正确识别到Zephyr DFU sample

    image

  2. 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.1 参考链接

dfu-util工具:http://dfu-util.sourceforge.net/

《USB DFU Spec》DFU Spec文档:DFU 1.1

5.2 代码实现

zephyr中代码位置:

zephyr\subsys\usb\class\dfu\usb_dfu.c

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传输64B

    • FIFO 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数据交互流程如下

      image

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%