Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

PSoC 6 Device Firmware Update (DFU) Supporting 2 Loadable Applications

PSoC 6 Device Firmware Update (DFU) Supporting 2 Loadable Applications

LeiS_01
Employee
Employee
5 sign-ins First like given First question asked

Version: **

This KBA provided step-by-step application guide on ModusToolbox PSoC 6 Device Firmware Update (DFU) Supporting 2 Loadable Applications.

1 Device Firmware Update (DFU)


1.1 What Is Device Firmware Update (DFU)?

1. At the factory, initial programming of firmware into a product is typically done at PCB assembly time, using the MCU’s JTAG or SWD interface.
2. These interfaces are not usually available in the field, and therefore are generally not used for firmware updates.

3. A better way to update firmware in the field is to use an existing connection between the product and the outside world.

4. The connection may be a standard communication port such as I2C, USB, or UART, a wireless channel such as BLE, or a custom protocol.

IFX_Publisher2_0-1689577353878.png


1.2 Terms and Definitions

1. The system that provides the data to update the flash is called the host.
2. the system being updated is called the target.
3. The act of transferring data from the host to the target flash is called DFU (Device Firmware Update).
4. The data that received from Host and is placed in flash is called the application or firmware image.

 
1.3  Device Firmware Update (DFU) Middleware Library


The purpose of the DFU middleware library is to provide an SDK for updating firmware images. The middleware allows creating two types of projects:

1. An application loader receives the program and switch to the new application.
2. A loadable application to be transferred and programmed.

The DFU SDK has the following features:

1. Read firmware images from a host through a number of transport interfaces, e.g. BLE, USB, UART, I2C, SPI.
2. Program a firmware image to the specified address in internal flash, XIP region, or any external memory that supports the DFU API.
3. Copy & Validate applications.
4. Switches applications. Passes parameters in RAM when switching applications.
5. Supports many application images, the number of which is limited only by the metadata size. Each image can be an application loader. For example, 512-byte metadata supports up to 63 applications.
6. Supports customization.
7. Supports the CRC-32 checksum to validate data.

2 Create a basic DFU loader and a loadable application


2.1 STEP 0: Prepare project template

1. Create project for DFU loader application by clicking “New Application” from ModusToolBox, choose BSP “CY8CPROTO-062-4343W”, use “Hello World” code example and name it DFU_UART_APP0.
2.Create a project for the DFU loadable application in the same way and name it DFU_UART_APP1.
3. Include the DFU middleware into each project using the ModusToolbox Library Manager. It is recommended to check Shared checkbox against DFU middleware, so it will be downloaded once and then shared between all applications in the workspace.

IFX_Publisher2_2-1689577353892.png

4. Include a DFU header in main.c of each project to get access to DFU API:

#include "cycfg.h"

#include "cy_dfu.h"

2.2 STEP 1: Setup Loader Application DFU_UART_APP0

  1. Copy the configuration files

dfu_user.c from ..\mtb_shared\dfu\[VERSION]\config\CAT1A

dfu_user.h from ..\mtb_shared\dfu\[VERSION]\config folder

and put them at the same directory of main.c.

  1. Copy the transport files from the  ..\mtb_shared\dfu\[VERSION]\config\CAT1A directory and put them at the same directory of main.c.

In our example, UART requires transport_uart.c and transport_uart.h.

  1. Copy the app0 linker script file from

..\mtb_shared\dfu\[VERSION]\linker_scripts\CAT1A\TOOLCHAIN_<COMPILER>\dfu_cm4_app0.[ext]

and put it at the same directory of main.c.

  1. Open APP0 Device Configurator, configure

SCB5 as UART, name it DFU_UART, Baud Rate 115200, RX P5.0, TX P5.1

and save the configuration.

IFX_Publisher2_3-1689577353896.png

 

  1. Continue at APP0 Device Configurator,

Enable P13.7, name it PIN_LED, set Drive Mode to Strong Drive. Input buffer off and Initial Drive State High (1).

  1. Modify APP0 Makefile:

LINKER_SCRIPT=dfu_cm4_app0.ld

POSTBUILD="$(CY_MCUELFTOOL_DIR)/bin/cymcuelftool.exe" --sign $(CY_CONFIG_DIR)/$(APPNAME).elf --hex $(CY_CONFIG_DIR)/$(APPNAME).hex

  1. Add DFU related source code in main.c, we can copy and replace the main.c by using sample code.
  2. Build and program into the evaluation board.

2.3 STEP 2: Setup Loadable Application DFU_UART_APP1

  1. Copy the configuration file

dfu_user.h from ..\mtb_shared\dfu\[VERSION]\config folder

and put it at the same directory of main.c. [Please note that dfu_user.c is not needed for APP1.]

  1. Copy the app1 linker script file from

..\mtb_shared\dfu\[VERSION]\linker_scripts\CAT1A\TOOLCHAIN_<COMPILER>\dfu_cm4_app1.[ext]

and put it at the same directory of main.c.

  1. In APP1, open Device Configurator,

Enable P0.4, name it PIN_SW, set Drive Mode to Resistive Pull-Up. Input buffer on and Initial Drive State High (1).

  1. Modify APP1 Makefile:

LINKER_SCRIPT=dfu_cm4_app1.ld

POSTBUILD="$(CY_MCUELFTOOL_DIR)/bin/cymcuelftool.exe" --sign $(CY_CONFIG_DIR)/$(APPNAME).elf CRC --output $(APPNAME)_crc.elf && \"$(CY_MCUELFTOOL_DIR)/bin/cymcuelftool.exe" -P $(APPNAME)_crc.elf --output $(APPNAME)_crc.cyacd2

We can copy above content from the sample code Makefile to avoid format related build errors.

  1. Modify APP1 main.c. We can copy and replace the main.c by using sample code.
  2. Build APP1 to generate *.cyacd2 file that can be used by DFU Host Tool.

3 .cyacd2 File Format and Metadata Structure


3.1 .cyacd2 File Format

 

IFX_Publisher2_4-1689577353901.png

1. The file data is in the form of ASCII hex numbers. For example, a byte 0x1E is represented by the characters 0x31 (ASCII ‘1’) followed by 0x45 (ASCII ‘E’).
2. All multi-byte fields are little-endian.
3. Header: A header row has the structure as below:

IFX_Publisher2_5-1689577353902.png

4. @APPINFO:[__cy_app_verify_start],[__cy_app_verify_length].
5. Data: A data row has the structure shown as below:

IFX_Publisher2_6-1689577353903.png6.Last 4 bytes is CRC.IFX_Publisher2_7-1689577353904.png


3.2 Metadata Structure

IFX_Publisher2_8-1689577353907.png

1. Application metadata has eight bytes of data per application, followed by four bytes for checksum.

IFX_Publisher2_9-1689577353911.png

2. You can set the number of applications N in the dfu_user.h file:

#define CY_DFU_MAX_APPS (N)

The default value of N is 2.

3. Each application start address must be aligned to a flash row or NVM page boundary. The application length must be a multiple of the flash row or NVM page length.

4 Create a DFU loader to support two loadable applications

4.1 Steps to create and configure the application projects


1. We can start by using the DFU_UART_APP0 and DFU_UART_APP1 created previously. Just copy them to a new workspace and import the projects.

2. Create one additional loadable application project. Click “New Application” from ModusToolBox, choose BSP “CY8CPROTO-062-4343W”, use “Hello World” code example and name it DFU_UART_APP2.

3. Include the DFU middleware into DFU_UART_APP2 using the ModusToolbox Library Manager.

4. Copy the configuration file

dfu_user.h from ..\mtb_shared\dfu\[VERSION]\config folder

and put it in the same directory of main. c for DFU_UART_APP2. [Please note that dfu_user.c is not needed.]

5. Copy the linker script file for DFU_UART_APP2, put it at the same directory of main.c, rename to dfu_cm4_app2.ld from

..\mtb_shared\dfu\[VERSION]\linker_scripts\CAT1A\TOOLCHAIN_<COMPILER>\dfu_cm4_app1..[ext]

and set __cy_app_id symbol  to 2 in dfu_cm4_app2.ld file.

6. Modify the link files from APP0, APP1 and APP2, add flash and RAM sections to the 3rd (APP2) application. Name them flash_app2, ram_app2. You can adjust the ORIGIN and LENGTH based on the application size.

IFX_Publisher2_10-1689577353913.png

7. Update the region aliases for flash and RAM to use flash_app2 and ram_app2 in dfu_cm4_app2.ld:

IFX_Publisher2_11-1689577353915.png

8. Add symbols __cy_app2_verify_start and __cy_app2_verify_length for metadata initialization in the application 2 link file dfu_cm4_app2.ld and also add them in the same way as that of application 0 and 1.

IFX_Publisher2_12-1689577353917.png

9. Add macros to the dfu_user.h

CY_DFU_APP2_VERIFY_START

CY_DFU_APP2_VERIFY_LENGTH

for application 2 in the same way as that of application 0 and 1.

IFX_Publisher2_13-1689577353918.png

 

IFX_Publisher2_14-1689577353919.png

10. Modify dfu_user.h 

#define CY_DFU_MAX_APPS            (3U)

for app2 in the same way as that of application 0 and 1.

IFX_Publisher2_15-1689577353920.png

11. Add to the cy_dfu_metadata array of the dfu_user.c

CY_DFU_APP2_VERIFY_START

CY_DFU_APP2_VERIFY_LENGTH

 in application 0 (loader) to update the metadata with the 3rd application.

IFX_Publisher2_16-1689577353923.png

12. In APP2, open Device Configurator,

Enable P0.4, name it PIN_SW, set Drive Mode to Resistive Pull-Up. Input buffer on and Initial Drive State High (1).

13. Modify APP2 Makefile:

LINKER_SCRIPT=dfu_cm4_app2.ld

POSTBUILD="$(CY_MCUELFTOOL_DIR)/bin/cymcuelftool.exe" --sign $(CY_CONFIG_DIR)/$(APPNAME).elf CRC --output $(APPNAME)_crc.elf && \"$(CY_MCUELFTOOL_DIR)/bin/cymcuelftool.exe" -P $(APPNAME)_crc.elf --output $(APPNAME)_crc.cyacd2

We can copy above content from the sample code Makefile to avoid format related build errors.

14. Modify APP2 main.c. We can copy and replace the main.c by using sample code.

15. Build APP2 to generate *.cyacd2 file for boot.

16. Update APP0 (loader) main.c, add variables and function to support two applications (loadable). We can copy and replace the main.c by using sample code.
17. Use Cypress Programmer to erase the board, use ModusToolbox to build and download APP0, then use DFU Host Tool to download *.cyacd2 of APP1 and APP2.

18. As in the APP0 main.c, we define as below:

const uint32_t PreferredAppID = 1u;

const uint32_t NextAppID = 2u;

After downloading the APP0 by ModusToolbox, we can use DFU Host Tool to download APP2 first via UART. After downloading finished, we can see the LED blinking fast confirming that APP2 is running. As APP1 is not downloaded to the board, after we press the reset button, APP2 will start to run.

19. When APP2 is running, we can press the user button to jump back to APP0 and use DFU Host Tool to download APP1 via UART. After APP1 downloading finished, we can see the LED blinking slow confirming that APP1 is running. As APP1 is the preferred APP in APP0, after pressing the reset button again, APP1 will start to run.

411 Views