Zephyr DFU over USB Not Swapping

I am using an NRF52840DK.

1. I erase the chip to start clean:

 nrfjprog --eraseall

2. I flash the MCUboot bootloader:

 ~/mcuboot/boot/zephyr$ west flash

The output from the console is OK as there is no image:

 *** Booting MCUboot v2.1.0-43-g50f35144e4e4 ***
 *** Using Zephyr OS build v3.6.0-596-g9fa4cd8226ce ***
 I: Starting bootloader
 I: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
 I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
 I: Boot source: none
 W: Failed reading image headers; Image=0
 E: Unable to find bootable image

3. I flash my first USB DFU enabled image:

 ~/zephyr/usb_dfu/build/zephyr$ nrfjprog --program build/zephyr/zephyr.signed.hex

After resetting, the console output is OK and the image is executed:

 *** Booting MCUboot v2.1.0-43-g50f35144e4e4 ***
 *** Using Zephyr OS build v3.6.0-596-g9fa4cd8226ce ***
 I: Starting bootloader
 I: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
 I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
 I: Boot source: none
 I: Image index: 0, Swap type: none
 I: Bootloader chainload address offset: 0xc000
 I: Jumping to the first image slot
 [00:00:00.000,213] <err> qspi_nor: JEDEC id [00 00 00] expect [c2 28 17]
 *** Booting Zephyr OS build v3.6.0-596-g9fa4cd8226ce ***
 [00:00:00.000,549] <inf> main: This device supports USB DFU class.

4. I check dfu-util:

 ~/zephyr/counter/build/zephyr# dfu-util -l
 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 sourceforge.net/.../

 Found Runtime: [2fe3:0005] ver=0306, devnum=117, cfg=1, intf=0, path="3-3.4.3", alt=0, name="UNKNOWN", serial="4327BC869F6C049C"

Notice there is only alt=0

5. I try to download the new image:

 ~/zephyr/counter/build/zephyr# dfu-util --download zephyr.signed.hex

The download fails but alt=1 appears:

 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 sourceforge.net/.../

 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 2fe3: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...
 Device will detach and reattach...
 dfu-util: More than one DFU capable USB device found! Try `--list' and specify the serial number or disconnect all but one device

6. I try to download the new image in the newly appeared slot:

 ~/zephyr/counter/build/zephyr# dfu-util --download zephyr.signed.hex --alt 1

This time dfu-util succeeds:

 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 sourceforge.net/.../

 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 2fe3:ffff
 Device DFU version 0110
 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 128
 Copying data from PC to DFU device
 Download [=========================] 100% 177796 bytes
 Download done.
 DFU state(2) = dfuIDLE, status(0) = No error condition is present
 Done!

But after I press reset, the board swaps but still boots the old image:

 *** Booting MCUboot v2.1.0-43-g50f35144e4e4 ***
 *** Using Zephyr OS build v3.6.0-596-g9fa4cd8226ce ***
 I: Starting bootloader
 I: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
 I: Secondary image: magic=good, swap_type=0x2, copy_done=0x3, image_ok=0x3
 I: Boot source: none
 I: Image index: 0, Swap type: test
 I: Bootloader chainload address offset: 0xc000
 I: Jumping to the first image slot
 [00:00:00.000,213] <err> qspi_nor: JEDEC id [00 00 00] expect [c2 28 17]
 *** Booting Zephyr OS build v3.6.0-596-g9fa4cd8226ce ***
 [00:00:00.000,549] <inf> main: This device supports USB DFU class.

What am I doing wrong?

Related