Skip to content

Commit

Permalink
subsys: usb: rework USB DFU class driver
Browse files Browse the repository at this point in the history
This patch moves USB DFU class driver to subsys/usb/class.

For the first the USB DFU class driver depends on DFU image
manager and partition layout and is limited to use as an
application for the bootloader. The driver fetches the
information about the flash, erase block size, write block
size and partitions offset from the DT now. The driver has
two interfaces associated with the two partitions "SLOT-0"
and "SLOT-1". The "SLOT-0" can only be read.

In the following work the class driver can be extended so
that it can be used from the bootloader and update a flash
region directly from the bootloader.

Signed-off-by: Johann Fischer <j.fischer@phytec.de>
  • Loading branch information
jfischer-no authored and Anas Nashif committed Apr 30, 2018
1 parent 46700ea commit b2ca5ee
Show file tree
Hide file tree
Showing 11 changed files with 869 additions and 848 deletions.
110 changes: 110 additions & 0 deletions samples/subsys/usb/dfu/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
.. _usb_dfu:

USB DFU Sample Application
##########################

Overview
********

This sample app demonstrates use of a USB DFU Class driver provided
by the Zephyr project.

Requirements
************

This project requires an USB device driver. Currently, the USB DFU
class provided by the Zephyr project depends on DFU image manager and
partition layout. Refer to :ref:`flash_partitions` for details about
partition layout. You SoC must run MCUboot as the stage 1 bootloader.
This sample is built as an application for the MCUboot bootloader.

Building and Testing
********************

Building and signing the application
====================================

This sample can be built in the usual way (see :ref:`build_an_application`
for more details) and flashed with regular flash tools, but will need
to be loaded at the offset of SLOT-0.

Application images (such as this sample) must be signed.
Use the ``scripts/imagetool.py`` script from the `MCUboot GitHub repo`_
to sign the image. (See the `Using MCUboot with Zephyr`_ documentation for
details.)

.. code-block:: console
~/src/mcuboot/scripts/imgtool.py sign \
--key ~/src/mcuboot/root-rsa-2048.pem \
--header-size 0x200 \
--align 8 \
--version 1.2 \
--included-header \
./zephyr/zephyr.bin \
signed-zephyr.bin
Build and flash MCUboot bootloader for Zephyr project as it is described in
the `Using MCUboot with Zephyr`_ documentation. Then build, sign and flash
the USB DFU sample at the offset of SLOT-0.

Build and sign a second application image e.g. :ref:`hello_world`,
which will be used as an image for the update.
Do not forget to enable the required MCUboot Kconfig option (as described
in :ref:`mcuboot`) by adding the following line to
:file:`samples/hello_world/prj.conf`:

.. code-block:: console
CONFIG_BOOTLOADER_MCUBOOT=y
Testing
=======

The Linux ``dfu-util`` tool can be used to backup or update the application
image.

Use the following command to backup the SLOT-0 image:

.. code-block:: console
dfu-util --alt 0 --upload slot0_backup.bin
Use the following command to update the application:

.. code-block:: console
dfu-util --alt 1 --download signed-hello.bin
Reset the SoC. MCUboot boot will swap the images and boot the new application,
showing this output to the console:

.. code-block:: console
***** Booting Zephyr OS v1.1.0-65-g4ec7f76 *****
[MCUBOOT] [INF] main: Starting bootloader
[MCUBOOT] [INF] boot_status_source: Image 0: magic=unset, copy_done=0xff, image_ok=0xff
[MCUBOOT] [INF] boot_status_source: Scratch: magic=unset, copy_done=0xe, image_ok=0xff
[MCUBOOT] [INF] boot_status_source: Boot source: slot 0
[MCUBOOT] [INF] boot_swap_type: Swap type: test
[MCUBOOT] [INF] main: Bootloader chainload address offset: 0x20000
[MCUBOOT] [INF] main: Jumping to the first image slot0
***** Booting Zephyr OS v1.11.0-830-g9df01813c4 *****
Hello World! arm
Reset the SoC again and MCUboot should revert the images and boot
USB DFU sample, showing this output to the console:

.. code-block:: console
***** Booting Zephyr OS v1.1.0-65-g4ec7f76 *****
[MCUBOOT] [INF] main: Starting bootloader
[MCUBOOT] [INF] boot_status_source: Image 0: magic=good, copy_done=0x1, image_ok=0xff
[MCUBOOT] [INF] boot_status_source: Scratch: magic=unset, copy_done=0xe, image_ok=0xff
[MCUBOOT] [INF] boot_status_source: Boot source: none
[MCUBOOT] [INF] boot_swap_type: Swap type: revert
[MCUBOOT] [INF] main: Bootloader chainload address offset: 0x20000
***** Booting Zephyr OS v1.11.0-830-g9df01813c4 *****
.. _MCUboot GitHub repo: https://github.com/runtimeco/mcuboot
.. _Using MCUboot with Zephyr: https://mcuboot.com/mcuboot/readme-zephyr.html
5 changes: 4 additions & 1 deletion samples/subsys/usb/dfu/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ CONFIG_ARC_INIT=n
CONFIG_USB=y
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PRODUCT="Zephyr DFU sample"
CONFIG_USB_DFU_CLASS=y
CONFIG_FLASH=y
CONFIG_SOC_FLASH_QMSI=y
CONFIG_IMG_MANAGER=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_SYS_LOG=y
CONFIG_SYS_LOG_USB_DRIVER_LEVEL=1
CONFIG_SYS_LOG_USB_DEVICE_LEVEL=1
CONFIG_BOOTLOADER_MCUBOOT=y
6 changes: 3 additions & 3 deletions samples/subsys/usb/dfu/sample.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sample:
name: DFU over USB
name: USB DFU sample
tests:
test_x86:
test:
platform_whitelist: nrf52840_pca10056
depends_on: usb_device
platform_whitelist: quark_se_c1000_devboard
tags: usb
46 changes: 7 additions & 39 deletions samples/subsys/usb/dfu/src/main.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,19 @@
/*
* Copyright (c) 2016 Intel Corporation
* Copyright (c) 2017 Phytec Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief Sample app for DFU class driver
*
* This sample app implements a DFU class driver. It does not perform an actual
* firmware upgrade instead it allows user to upload a file at a predetermined
* flash address or to download the content from that flash address.
*/
/* Sample app for USB DFU class driver. */

#include <string.h>
#include <zephyr.h>
#include "usb_dfu.h"
#include <stdio.h>

#ifdef CONFIG_SOC_QUARK_SE_C1000
#define DFU_FLASH_DEVICE "QUARK_FLASH"
/* Unused flash area to test DFU class driver */
#define DFU_FLASH_TEST_ADDR (0x40030000 + 0x10000)
#define DFU_FLASH_PAGE_SIZE (2048)
#define DFU_FLASH_UPLOAD_SIZE (0x6000)
#else
#error "Unsupported board"
#endif
#include <logging/sys_log.h>

void main(void)
{
struct device *dev = NULL;

printf("DFU Test Application\n");

dev = device_get_binding(DFU_FLASH_DEVICE);
if (!dev) {
printf("Flash device not found\n");
return;
}

dfu_start(dev,
DFU_FLASH_TEST_ADDR,
DFU_FLASH_PAGE_SIZE,
DFU_FLASH_UPLOAD_SIZE);

while (1) {
/* do nothing */
}
/* Nothing to be done other than the selecting appropriate build
* config options. Use dfu-util to update the device.
*/
SYS_LOG_INF("This device supports USB DFU class.\n");
}
Loading

0 comments on commit b2ca5ee

Please sign in to comment.