Zephyr USB DFU with ST Disco L475 IOT01 - joectchan/blog GitHub Wiki

Background

This page discribe a sample application showing how to use USB DFU to upgrade firmware.

  • Following
https://hub.mender.io/t/updating-device-firmware-using-dfu-in-zephyr-project-on-a-frdm-k64f-board/1618
https://www.methodpark.de/blog/a-setup-for-firmware-updates-over-the-air-part-3-wireless-sensor-nodes-mcuboot/
  • Adapting to this STM dev board:
ST Disco L475 IOT01 (B-L475E-IOT01A)
https://docs.zephyrproject.org/latest/boards/arm/disco_l475_iot1/doc/index.html
  • This is the copy of git repo where I build MCUBoot
cd ~/workspace/zephyrproject2/
  • This is the copy of git repo where I build MCUBoot
cd zephpyr
source zephyr-env.sh
cd ..
  • Try this. It should build for you:
west build -p auto -s bootloader/mcuboot/boot/zephyr -d build-mcuboot -b disco_l475_iot1 

But for me, I had tried a few things successfully and unsuccessfully. I need to use -p always to let the build system forget my previous status and just build again from beginning.

west build -p always -s bootloader/mcuboot/boot/zephyr -d build-mcuboot -b disco_l475_iot1 

This is a screenshot of what I typed and my result.

joseph@nuc:~/workspace/zephyrproject2$ west build -p auto -s bootloader/mcuboot/boot/zephyr -d build-mcuboot -b disco_l475_iot1 
ninja: no work to do.
joseph@nuc:~/workspace/zephyrproject2$ west build -p always -s bootloader/mcuboot/boot/zephyr -d build-mcuboot -b disco_l475_iot1 
-- west build: making build dir /home/joseph/workspace/zephyrproject2/build-mcuboot pristine
-- west build: generating a build system
-- Application: /home/joseph/workspace/zephyrproject2/bootloader/mcuboot/boot/zephyr
-- Zephyr version: 2.2.99 (/home/joseph/workspace/zephyrproject2/zephyr)
-- Found Python3: /usr/bin/python3.7 (found suitable exact version "3.7.5") found components:  Interpreter 
-- Board: disco_l475_iot1
-- Found west: /home/joseph/.local/bin/west (found suitable version "0.7.2", minimum required is "0.7.1")
-- Found toolchain: zephyr (/home/joseph/zephyr-sdk-0.11.2)
-- Found BOARD.dts: /home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1.dts
-- Found devicetree overlay: /home/joseph/workspace/zephyrproject2/bootloader/mcuboot/boot/zephyr/dts.overlay
-- Generated zephyr.dts: /home/joseph/workspace/zephyrproject2/build-mcuboot/zephyr/zephyr.dts
-- Generated devicetree_unfixed.h: /home/joseph/workspace/zephyrproject2/build-mcuboot/zephyr/include/generated/devicetree_unfixed.h
Parsing /home/joseph/workspace/zephyrproject2/bootloader/mcuboot/boot/zephyr/Kconfig
Loaded configuration '/home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1_defconfig'
Merged configuration '/home/joseph/workspace/zephyrproject2/bootloader/mcuboot/boot/zephyr/prj.conf'
Merged configuration '/home/joseph/workspace/zephyrproject2/bootloader/mcuboot/boot/zephyr/boards/disco_l475_iot1.conf'
Configuration saved to '/home/joseph/workspace/zephyrproject2/build-mcuboot/zephyr/.config'
Kconfig header saved to '/home/joseph/workspace/zephyrproject2/build-mcuboot/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/joseph/zephyr-sdk-0.11.2/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Cache files will be written to: /home/joseph/.cache/zephyr
-- Configuring done
-- Generating done
-- Build files have been written to: /home/joseph/workspace/zephyrproject2/build-mcuboot
-- west build: building application
[1/239] Preparing syscall dependency handling

[234/239] Linking C executable zephyr/zephyr_prebuilt.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       48080 B        64 KB     73.36%
            SRAM:       29596 B        96 KB     30.11%
        IDT_LIST:         168 B         2 KB      8.20%
[239/239] Linking C executable zephyr/zephyr.elf
joseph@nuc:~/workspace/zephyrproject2$ 

Flash MCUBoot elf to target

joseph@nuc:~/workspace/zephyrproject2$ west flash -d build-mcuboot
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner openocd
-- runners.openocd: Flashing file: /home/joseph/workspace/zephyrproject2/build-mcuboot/zephyr/zephyr.hex
Open On-Chip Debugger 0.10.0+dev-01340-ga0e8edc4e-dirty (2020-02-14-05:38)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 500 kHz
Info : STLINK V2J36M26 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.219435
Info : stm32l4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
    TargetName         Type       Endian TapName            State       
--  ------------------ ---------- ------ ------------------ ------------
 0* stm32l4x.cpu       hla_target little stm32l4x.cpu       running

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08000d4c msp: 0x20000648
Info : device idcode = 0x10076415 (STM32L47/L48xx - Rev: 4)
Info : flash size = 1024kbytes
Info : flash mode : dual-bank
Warn : Adding extra erase range, 0x0800bbd0 .. 0x0800bfff
auto erase enabled
wrote 48080 bytes from file /home/joseph/workspace/zephyrproject2/build-mcuboot/zephyr/zephyr.hex in 2.275331s (20.636 KiB/s)

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08002f24 msp: 0x200063e8
verified 48080 bytes in 1.460684s (32.145 KiB/s)

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
shutdown command invoked
joseph@nuc:~/workspace/zephyrproject2$ 

Screenshot of my PuTTY serial console

*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.005,000] <inf> mcuboot: Starting bootloader
[00:00:00.007,000] <inf> mcuboot: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Boot source: primary slot
[00:00:00.015,000] <inf> mcuboot: Swap type: none

Build USB DFU sample application

I do not follow the manual editing to make the sample application reboot.

This is a screenshot of what I typed and my result.

joseph@nuc:~/workspace/zephyrproject2$ west build -p auto -s zephyr/samples/subsys/usb/dfu -d build-dfu -b disco_l475_iot1
-- west build: generating a build system
Including boilerplate (Zephyr base): /home/joseph/workspace/zephyrproject2/zephyr/cmake/app/boilerplate.cmake
-- Application: /home/joseph/workspace/zephyrproject2/zephyr/samples/subsys/usb/dfu
-- Zephyr version: 2.2.99 (/home/joseph/workspace/zephyrproject2/zephyr)
-- Found Python3: /usr/bin/python3.7 (found suitable exact version "3.7.5") found components:  Interpreter 
-- Board: disco_l475_iot1
-- Found west: /home/joseph/.local/bin/west (found suitable version "0.7.2", minimum required is "0.7.1")
-- Found toolchain: zephyr (/home/joseph/zephyr-sdk-0.11.2)
-- Found BOARD.dts: /home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1.dts
-- Generated zephyr.dts: /home/joseph/workspace/zephyrproject2/build-dfu/zephyr/zephyr.dts
-- Generated devicetree_unfixed.h: /home/joseph/workspace/zephyrproject2/build-dfu/zephyr/include/generated/devicetree_unfixed.h
Parsing /home/joseph/workspace/zephyrproject2/zephyr/samples/subsys/usb/dfu/Kconfig
Loaded configuration '/home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1_defconfig'
Merged configuration '/home/joseph/workspace/zephyrproject2/zephyr/samples/subsys/usb/dfu/prj.conf'
Configuration saved to '/home/joseph/workspace/zephyrproject2/build-dfu/zephyr/.config'
Kconfig header saved to '/home/joseph/workspace/zephyrproject2/build-dfu/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/joseph/zephyr-sdk-0.11.2/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Cache files will be written to: /home/joseph/.cache/zephyr
CMake Warning at ../../../../subsys/usb/CMakeLists.txt:28 (message):
  CONFIG_USB_DEVICE_VID has default value 0x2FE3.

  This value is only for testing and MUST be configured for USB products.


-- Configuring done
-- Generating done
-- Build files have been written to: /home/joseph/workspace/zephyrproject2/build-dfu
-- west build: building application
[1/153] Preparing syscall dependency handling

[148/153] Linking C executable zephyr/zephyr_prebuilt.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       45440 B       432 KB     10.27%
            SRAM:       11380 B        96 KB     11.58%
        IDT_LIST:         184 B         2 KB      8.98%
[153/153] Linking C executable zephyr/zephyr.elf
joseph@nuc:~/workspace/zephyrproject2$ 

Sign this sample application.

joseph@nuc:~/workspace/zephyrproject2$ bootloader/mcuboot/scripts/imgtool.py sign \
>     --key bootloader/mcuboot/root-rsa-2048.pem \
>     --header-size 0x200 \
>     --align 8 \
>     --version 1.2 \
>     --slot-size 0x6C000 \
>     build-dfu/zephyr/zephyr.bin \
>     signed-dfu-app.bin
joseph@nuc:~/workspace/zephyrproject2$ 

I have to change my slot-size parameter to work for my board. All examples I read on the web shows slot-size of 0x60000. That did not work for my board. I need to search for the dts file of my board and read its flash layout definition.

joseph@nuc:~/workspace/zephyrproject2$ find . -name disco_l475_iot1.dts
./zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1.dts
joseph@nuc:~/workspace/zephyrproject2$ cat ./zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1.dts

...
                /*
                 * The flash starting at offset 0x10000 and ending at
                 * offset 0x1ffff is reserved for use by the application.
                 */

                slot0_partition: partition@20000 {
                        label = "image-0";
                        reg = <0x00020000 0x0006C000>;
                };
                slot1_partition: partition@8c000 {
                        label = "image-1";
                        reg = <0x0008C000 0x0006C000>;
                };
                scratch_partition: partition@f8000 {
                        label = "image-scratch";
                        reg = <0x000F8000 0x00006000>;
                };
...

Notice the two lines:

reg = <0x00020000 0x0006C000>;

reg = <0x0008C000 0x0006C000>;

The first number is the address of the partition. Second is the size. This tells me I need to use --slot-size 0x6C000

Flash signed binary using pyOCD to slot 0

The previous step created a signed binary. I need to flash it into slot0_partition: partition@20000.

joseph@nuc:~/workspace/zephyrproject2$ ls
bootloader  build-dfu  build-hello-world  build-mcuboot  modules  signed-dfu-app.bin  tools  zephyr

Zephyr toolchain already has pyOCD installed. It can connect to st-link and show the starting address of the flash on my board. First, use the list command to show the st-link probe and the target board type.

joseph@nuc:~/workspace/zephyrproject2$ pyocd list
  #   Probe                               Unique ID                 
--------------------------------------------------------------------
  0   DISCO-L475VG-IOT01A [stm32l475xg]   0670FF323535474B43132338  

Note that the board type is stm32l475xg. Then, connect using pyocd cmd and then display memmory map.

joseph@nuc:~/workspace/zephyrproject2$ pyocd cmd
Connected to STM32L475xG [Sleeping]: 0670FF323535474B43132338
>>> show map
 Region  Start       End         Size        Access  Sector      Page       
 flash   0x08000000  0x080fffff  0x00100000  rx      0x00000800  0x00000400 
 sram2   0x10000000  0x10007fff  0x00008000  rwx     -           -          
 sram1   0x20000000  0x20017fff  0x00018000  rwx     -           -          
>>> 

Quit pyocd with Ctrl-D.

Starting address of slot 0 is at offset 0x20000 of the flash, which is located at 0x08000000.

i.e. 0x08000000 + 0x20000 = 0x08020000

Use pyocd to flash the signed binary. The above steps give me the value for these parameters -a 0x08020000 and -t stm32l475xg

joseph@nuc:~/workspace/zephyrproject2$ pyocd flash -e sector -a 0x08020000 -t stm32l475xg signed-dfu-app.bin
[====================] 100%
0003908:INFO:loader:Erased 47104 bytes (23 sectors), programmed 47104 bytes (46 pages), skipped 0 bytes (0 pages) at 14.46 kB/s
joseph@nuc:~/workspace/zephyrproject2$ 

Observe bootloader booting signed application in slot 0

Screenshot of PuTTY. Note the first 6 lines are printed by the bootloader. The last two lines come from the signed application.

*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.005,000] <inf> mcuboot: Starting bootloader
[00:00:00.007,000] <inf> mcuboot: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Boot source: primary slot
[00:00:00.015,000] <inf> mcuboot: Swap type: none
*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.062,000] <inf> main: This device supports USB DFU class.

Build Blinky sample application, and enable DFU

joseph@nuc:~/workspace/zephyrproject2$ west build -p auto -s zephyr/samples/basic/blinky -d build-blinky -b disco_l475_iot1 -- -DCONFIG_BOOTLOADER_MCUBOOT=y
-- west build: generating a build system
Including boilerplate (Zephyr base (cached)): /home/joseph/workspace/zephyrproject2/zephyr/cmake/app/boilerplate.cmake
-- Application: /home/joseph/workspace/zephyrproject2/zephyr/samples/basic/blinky
-- Zephyr version: 2.2.99 (/home/joseph/workspace/zephyrproject2/zephyr)
-- Board: disco_l475_iot1
-- Found west: /home/joseph/.local/bin/west (found suitable version "0.7.2", minimum required is "0.7.1")
-- Found toolchain: zephyr (/home/joseph/zephyr-sdk-0.11.2)
-- Found BOARD.dts: /home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1.dts
-- Generated zephyr.dts: /home/joseph/workspace/zephyrproject2/build-blinky/zephyr/zephyr.dts
-- Generated devicetree_unfixed.h: /home/joseph/workspace/zephyrproject2/build-blinky/zephyr/include/generated/devicetree_unfixed.h
Parsing /home/joseph/workspace/zephyrproject2/zephyr/Kconfig
Loaded configuration '/home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1_defconfig'
Merged configuration '/home/joseph/workspace/zephyrproject2/zephyr/samples/basic/blinky/prj.conf'
Merged configuration '/home/joseph/workspace/zephyrproject2/build-blinky/zephyr/misc/generated/extra_kconfig_options.conf'
Configuration saved to '/home/joseph/workspace/zephyrproject2/build-blinky/zephyr/.config'
Kconfig header saved to '/home/joseph/workspace/zephyrproject2/build-blinky/zephyr/include/generated/autoconf.h'
-- Cache files will be written to: /home/joseph/.cache/zephyr
-- Configuring done
-- Generating done
-- Build files have been written to: /home/joseph/workspace/zephyrproject2/build-blinky
-- west build: building application
[119/124] Linking C executable zephyr/zephyr_prebuilt.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       14640 B       432 KB      3.31%
            SRAM:        4312 B        96 KB      4.39%
        IDT_LIST:         168 B         2 KB      8.20%
[124/124] Linking C executable zephyr/zephyr.elf
joseph@nuc:~/workspace/zephyrproject2$ 

Sign Blinky using West sign

This is actually the best way to sign an application for DFU. The above steps I used to sign the USB DFU sample application are the work done behind the scene by west.

joseph@nuc:~/workspace/zephyrproject2$ west sign -t imgtool -p bootloader/mcuboot/scripts/imgtool.py \
>     -d build-blinky \
>     -- --key bootloader/mcuboot/root-rsa-2048.pem
=== image configuration:
partition offset: 131072 (0x20000)
partition size: 442368 (0x6c000)
text section offset: 512 (0x200)
=== signed binaries:
bin: /home/joseph/workspace/zephyrproject2/build-blinky/zephyr/zephyr.signed.bin
hex: /home/joseph/workspace/zephyrproject2/build-blinky/zephyr/zephyr.signed.hex

Note that west put the signed binary at this location, build-blinky/zephyr/zephyr.signed.bin

Install dfu-util for Ubuntu (if you have not done so)

joseph@nuc:~/workspace/zephyrproject2$ apt-cache search dfu-util
dfu-util - Device firmware update (DFU) USB programmer

joseph@nuc:~/workspace/zephyrproject2$ sudo apt-get install dfu-util
Reading package lists... Done
Building dependency tree       
Reading state information... Done
dfu-util is already the newest version (0.9-1).
0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.
joseph@nuc:~/workspace/zephyrproject2$ 

Use dfu-util to download signed Blinky app to slot1

joseph@nuc:~/workspace/zephyrproject2$ sudo dfu-util --alt 1 --download build-blinky/zephyr/zephyr.signed.bin
[sudo] password for joseph: 
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 2fe3:0005
Run-time device DFU version 0110
Claiming USB DFU Runtime Interface...
Determining device status: state = appIDLE, status = 0
Device really in Runtime Mode, send DFU detach request...
Resetting USB...
Opening DFU USB Device...
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 128
Copying data from PC to DFU device
Download	[=========================] 100%        14976 bytes
Download done.
state(2) = dfuIDLE, status(0) = No error condition is present
Done!
joseph@nuc:~/workspace/zephyrproject2$ 

Reset target

Use pyocd to reset target manually, since we used dfu-util to download.

oseph@nuc:~/workspace/zephyrproject2$ pyocd cmd
Connected to STM32L475xG [Sleeping]: 0670FF323535474B43132338
>>> reset
Resetting target
>>> 

Screenshot of PuTTY

*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.005,000] <inf> mcuboot: Starting bootloader
[00:00:00.007,000] <inf> mcuboot: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Boot source: primary slot
[00:00:00.015,000] <inf> mcuboot: Swap type: test
*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***

Note that the 6 lines from bootloader is the same. There is only one line after them this time. Look at my board and there is a flashing LED.

Reset target and observe the application is reverted back to USB DFU application

Reset the target with pyocd and watch the target board reboot. Our blinky app does not have additional code to tell the bootloader this new image is good. Bottloader will revert to the previous image, which is USB DRU app.

joseph@nuc:~/workspace/zephyrproject2$ pyocd cmd
Connected to STM32L475xG [Sleeping]: 0670FF323535474B43132338
>>> reset
Resetting target
>>> 

Screenshot of PuTTY. Look at the board. LED does not blink.

*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.005,000] <inf> mcuboot: Starting bootloader
[00:00:00.007,000] <inf> mcuboot: Primary image: magic=good, swap_type=0x2, copy_done=0x1, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Boot source: none
[00:00:00.007,000] <inf> mcuboot: Swap type: revert
*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.062,000] <inf> main: This device supports USB DFU class.

Build threads sample application for DFU

We are now comfortable with building DFU enabled sample app and signing it using west commands. Let repeat that on yet another sample app, threads.

joseph@nuc:~/workspace/zephyrproject2$ west build -p auto -s zephyr/samples/basic/threads -d build-threads -b disco_l475_iot1 -- -DCONFIG_BOOTLOADER_MCUBOOT=y
-- west build: generating a build system
Including boilerplate (Zephyr base): /home/joseph/workspace/zephyrproject2/zephyr/cmake/app/boilerplate.cmake
-- Application: /home/joseph/workspace/zephyrproject2/zephyr/samples/basic/threads
-- Zephyr version: 2.2.99 (/home/joseph/workspace/zephyrproject2/zephyr)
-- Found Python3: /usr/bin/python3.7 (found suitable exact version "3.7.5") found components:  Interpreter 
-- Board: disco_l475_iot1
-- Found west: /home/joseph/.local/bin/west (found suitable version "0.7.2", minimum required is "0.7.1")
-- Found toolchain: zephyr (/home/joseph/zephyr-sdk-0.11.2)
-- Found BOARD.dts: /home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1.dts
-- Generated zephyr.dts: /home/joseph/workspace/zephyrproject2/build-threads/zephyr/zephyr.dts
-- Generated devicetree_unfixed.h: /home/joseph/workspace/zephyrproject2/build-threads/zephyr/include/generated/devicetree_unfixed.h
Parsing /home/joseph/workspace/zephyrproject2/zephyr/Kconfig
Loaded configuration '/home/joseph/workspace/zephyrproject2/zephyr/boards/arm/disco_l475_iot1/disco_l475_iot1_defconfig'
Merged configuration '/home/joseph/workspace/zephyrproject2/zephyr/samples/basic/threads/prj.conf'
Merged configuration '/home/joseph/workspace/zephyrproject2/build-threads/zephyr/misc/generated/extra_kconfig_options.conf'
Configuration saved to '/home/joseph/workspace/zephyrproject2/build-threads/zephyr/.config'
Kconfig header saved to '/home/joseph/workspace/zephyrproject2/build-threads/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/joseph/zephyr-sdk-0.11.2/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Cache files will be written to: /home/joseph/.cache/zephyr
CMake Warning at ../../../CMakeLists.txt:1368 (message):
  __ASSERT() statements are globally ENABLED


-- Configuring done
-- Generating done
-- Build files have been written to: /home/joseph/workspace/zephyrproject2/build-threads
-- west build: building application
[1/130] Preparing syscall dependency handling

[125/130] Linking C executable zephyr/zephyr_prebuilt.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       26560 B       432 KB      6.00%
            SRAM:        8320 B        96 KB      8.46%
        IDT_LIST:         168 B         2 KB      8.20%
[130/130] Linking C executable zephyr/zephyr.elf
joseph@nuc:~/workspace/zephyrproject2$ 

Sign threads application

joseph@nuc:~/workspace/zephyrproject2$ west sign -t imgtool -p bootloader/mcuboot/scripts/imgtool.py \
>     -d build-threads \
>     -- --key bootloader/mcuboot/root-rsa-2048.pem
=== image configuration:
partition offset: 131072 (0x20000)
partition size: 442368 (0x6c000)
text section offset: 512 (0x200)
=== signed binaries:
bin: /home/joseph/workspace/zephyrproject2/build-threads/zephyr/zephyr.signed.bin
hex: /home/joseph/workspace/zephyrproject2/build-threads/zephyr/zephyr.signed.hex
joseph@nuc:~/workspace/zephyrproject2$ 

Use dfu-util to download signed threds app to slot1

joseph@nuc:~/workspace/zephyrproject2$ sudo dfu-util --alt 1 --download build-threads/zephyr/zephyr.signed.bin
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 2fe3:0005
Run-time device DFU version 0110
Claiming USB DFU Runtime Interface...
Determining device status: state = appIDLE, status = 0
Device really in Runtime Mode, send DFU detach request...
Resetting USB...
Opening DFU USB Device...
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 128
Copying data from PC to DFU device
Download	[=========================] 100%        26896 bytes
Download done.
state(2) = dfuIDLE, status(0) = No error condition is present
Done!
joseph@nuc:~/workspace/zephyrproject2$ 

Reset, observe threads app output, reset, revert

joseph@nuc:~/workspace/zephyrproject2$ pyocd cmd
Connected to STM32L475xG [Sleeping]: 0670FF323535474B43132338
>>> reset
Resetting target

... watch for about 10 secs ...

>>> reset
Resetting target
>>> 

Screenshot of PuTTY

Note the line Swap type: test after the first reset changed to Swap type: revert after second reset.

*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.005,000] <inf> mcuboot: Starting bootloader
[00:00:00.007,000] <inf> mcuboot: Primary image: magic=good, swap_type=0x4, copy_done=0x1, image_ok=0x1
[00:00:00.007,000] <inf> mcuboot: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Boot source: none
[00:00:00.007,000] <inf> mcuboot: Swap type: test
*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
Toggle USR0 LED: Counter = 0
Toggle USR1 LED: Counter = 0
Toggle USR0 LED: Counter = 1
Toggle USR0 LED: Counter = 2
Toggle USR0 LED: Counter = 3
Toggle USR0 LED: Counter = 4
Toggle USR0 LED: Counter = 5
Toggle USR0 LED: Counter = 6
Toggle USR0 LED: Counter = 7
Toggle USR0 LED: Counter = 8
Toggle USR0 LED: Counter = 9
Toggle USR1 LED: Counter = 1
Toggle USR0 LED: Counter = 10
Toggle USR0 LED: Counter = 11
Toggle USR0 LED: Counter = 12
Toggle USR0 LED: Counter = 13
Toggle USR0 LED: Counter = 14
Toggle USR0 LED: Counter = 15
Toggle USR0 LED: Counter = 16
Toggle USR0 LED: Counter = 17
Toggle USR0 LED: Counter = 18
Toggle USR0 LED: Counter = 19
Toggle USR1 LED: Counter = 2
Toggle USR0 LED: Counter = 20
Toggle USR0 LED: Counter = 21

... waited 10 secs ...

Toggle USR0 LED: Counter = 88
Toggle USR0 LED: Counter = 89
Toggle USR1 LED: Counter = 9
Toggle USR0 LED: Counter = 90
Toggle USR0 LED: Counter = 91
Toggle USR0 LED: Counter = 92
Toggle USR0 LED: Counter = 93
Toggle USR0 LED: Counter = 94
*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.005,000] <inf> mcuboot: Starting bootloader
[00:00:00.007,000] <inf> mcuboot: Primary image: magic=good, swap_type=0x2, copy_done=0x1, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.007,000] <inf> mcuboot: Boot source: none
[00:00:00.007,000] <inf> mcuboot: Swap type: revert
*** Booting Zephyr OS build zephyr-v2.2.0-1791-g8d984b336ed4  ***
[00:00:00.062,000] <inf> main: This device supports USB DFU class.
⚠️ **GitHub.com Fallback** ⚠️