Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame^] | 1 | ### Blinking LED Test Application For Mcubootapp Bootloading Application |
| 2 | |
| 3 | ### Description |
| 4 | |
| 5 | Implements simple Blinky LED CM4 application to demonstrate MCUBootApp bootloading application operation in terms of boot and upgrade processes. |
| 6 | |
| 7 | It is validated and started by MCUBootApp which is running on CM0p core of PSoC 6 device. |
| 8 | |
| 9 | Functionality: |
| 10 | |
| 11 | * Blinks RED led with 2 different rates, depending on type of image - BOOT or UPGRADE. |
| 12 | * Prints debug info and version of itself to terminal at 115200 baud rate. |
| 13 | * Manages watchdog timer started in MCUBootApp as one of confirmation mechanisms. |
| 14 | * Sets special bit in flash to confirm image is operable (UPGRADE image). |
| 15 | * Can be built for BOOT slot or UPGRADE slot of bootloader. |
| 16 | * Can be used to evaluate `swap` and `overwrite only` upgrade modes. |
| 17 | |
| 18 | ### Hardware Limitations |
| 19 | |
| 20 | Since this application is created to demonstrate MCUBoot library features and not as reference examples some considerations are taken. |
| 21 | |
| 22 | 1. Port/pin `P5_0` and `P5_1` used to configure serial port for debug prints. These pins are the most commonly used for serial port connection among available Cypress PSoC 6 kits. If you try to use custom hardware with this application - change definitions of `CY_DEBUG_UART_TX` and `CY_DEBUG_UART_RX` in `main.c` of BlinkyApp to port/pin pairs corresponding to your design. |
| 23 | 2. Port `GPIO_PRT13` pin `7U` used to define user connection LED. This pin is the most commonly used for USER_LED connection among available Cypress PSoC 6 kits. If you try to use custom hardware with this application - change definitions of `LED_PORT` and `LED_PIN` in `main.c` of BlinkyApp to port/pin pairs corresponding to your design. |
| 24 | |
| 25 | ### Pre-build Action |
| 26 | |
| 27 | Pre-build action is implemented to define start address and size of flash, as well as RAM start address and size for BlinkyApp. |
| 28 | |
| 29 | These values are set by specifing following macros (default values shown): |
| 30 | `SLOT_SIZE ?= 0x10000` - for slot located in internal flash |
| 31 | `SLOT_SIZE ?= 0x40200` - for slot located in external flash |
| 32 | |
| 33 | For PSoC 6 2M devices: |
| 34 | `DEFINES_APP += -DRAM_START=0x08040000` |
| 35 | `DEFINES_APP += -DRAM_SIZE=0x10000` |
| 36 | |
| 37 | For PSoC 6 1M and 512K devices: |
| 38 | `DEFINES_APP += -DRAM_START=0x08020000` |
| 39 | `DEFINES_APP += -DRAM_SIZE=0x10000` |
| 40 | |
| 41 | For all devices: |
| 42 | `DEFINES_APP += -DUSER_APP_START=0x10018000` |
| 43 | |
| 44 | in `boot/cypress/BlinkyApp.mk`. |
| 45 | |
| 46 | Pre-build action calls GCC preprocessor which replaces defines to particular values in `BlinkyApp_template.ld`. |
| 47 | |
| 48 | **Important**: make sure RAM areas of CM4-based BlinkyApp and CM0p-based MCUBootApp bootloader do not overlap. |
| 49 | |
| 50 | Memory (stack) corruption of CM0p application can cause failure if SystemCall-served operations invoked from CM4. |
| 51 | |
| 52 | ### Building An Application |
| 53 | |
| 54 | Supported platforms: |
| 55 | |
| 56 | * PSOC_062_2M |
| 57 | * PSOC_062_1M |
| 58 | * PSOC_062_512K |
| 59 | |
| 60 | Root directory for build is **boot/cypress.** |
| 61 | |
| 62 | **Single image** |
| 63 | |
| 64 | The following command will build regular HEX file of a BlinkyApp for primary (BOOT) slot: |
| 65 | |
| 66 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT |
| 67 | |
| 68 | _Note: HEADER_OFFSET=%SLOT_SIZE%_ |
| 69 | |
| 70 | To build image for secondary (UPGRADE) slot to use in `swap` upgrade: |
| 71 | |
| 72 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x10000 SWAP_UPGRADE=1 |
| 73 | |
| 74 | To build image for secondary (UPGRADE) slot to use in `overwrite only` upgrade: |
| 75 | |
| 76 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x10000 SWAP_UPGRADE=0 |
| 77 | |
| 78 | To build image for primary (BOOT) image for custom slot size `0x70000`: |
| 79 | |
| 80 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT SLOT_SIZE=0x70000 |
| 81 | |
| 82 | To build image for secondary (UPGRADE) image for custom slot size `0x70000` to use in `swap` upgrade: |
| 83 | |
| 84 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE SLOT_SIZE=0x70000 HEADER_OFFSET=0x70000 SWAP_UPGRADE=1 |
| 85 | |
| 86 | **Multi image** |
| 87 | |
| 88 | `BlinkyApp` can be built to use in multi image bootloader configuration. |
| 89 | |
| 90 | To obtain appropriate hex files to use with multi image MCUBootApp, makefile flag `HEADER_OFFSET=` can be used. |
| 91 | |
| 92 | Example usage: |
| 93 | |
| 94 | Considering default config: |
| 95 | |
| 96 | * first image BOOT (PRIMARY) slot starts `0x10018000` |
| 97 | * slot size `0x10000` |
| 98 | * second image BOOT (PRIMARY) slot starts at `0x10038000` |
| 99 | |
| 100 | To obtain appropriate artifact for second image PRIMARY slot run this command: |
| 101 | |
| 102 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT HEADER_OFFSET=0x20000 |
| 103 | |
| 104 | *Note:* only 2 images are supported at the moment. |
| 105 | |
| 106 | **Upgrade mode dependency** |
| 107 | |
| 108 | `MCUBootApp` can upgrade image either by overwriting an image from a secondary slot to a primary slot or by swapping the two images. |
| 109 | |
| 110 | To build `BlinkyApp` for different upgrade mode `SWAP_UPGRADE` flag is used. |
| 111 | |
| 112 | `SWAP_UPGRADE=0` - for overwrite mode. |
| 113 | `SWAP_UPGRADE=1` - for swap upgrade mode (default). |
| 114 | |
| 115 | **Upgrade image for external memory** |
| 116 | |
| 117 | To prepare MCUBootApp for work with external memory refer to `MCUBootApp/ExternalMemory.md`. |
| 118 | |
| 119 | To build `BlinkyApp` upgrade image for external memory use command: |
| 120 | |
| 121 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x7FE8000 ERASED_VALUE=0xff USE_EXTERNAL_FLASH=1 |
| 122 | |
| 123 | `HEADER_OFFSET` defines the offset from original boot image address. This one in line above suggests secondary slot will start from `0x18000000`, which is a start of external memory related addreses on PSoC 6 devices. |
| 124 | |
| 125 | `ERASED_VALUE` defines the memory cell contents in erased state. It is `0x00` for PSoC 6 internal Flash and `0xff` for S25FL512S. |
| 126 | |
| 127 | In case of using muti-image configuration, upgrade image for second application can be built using next command: |
| 128 | |
| 129 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x8228000 ERASED_VALUE=0xff USE_EXTERNAL_FLASH=1 |
| 130 | |
| 131 | Note: for S25FL512S block address should be mutiple of 0x40000. |
| 132 | |
| 133 | **Encrypted upgrade image** |
| 134 | |
| 135 | To prepare MCUBootApp for work with encrypted upgrade image please refer to `MCUBootApp/Readme.md`. |
| 136 | |
| 137 | To obtain encrypted upgrade image of BlinkyApp extra flag ENC_IMG=1 should be passed in command line, for example: |
| 138 | |
| 139 | make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x20000 ENC_IMG=1 |
| 140 | |
| 141 | This also suggests user already placed corresponing *.pem key in \keys folder. The key variables are defined in root Makefile as SIGN_KEY_FILE and ENC_KEY_FILE |
| 142 | |
| 143 | ### Complete Build Flags Description |
| 144 | - `BUILDCFG` - configuration type |
| 145 | - Release |
| 146 | - Debug |
| 147 | - `MAKEINFO` - build verbosity level |
| 148 | - 0 (default) - less build info |
| 149 | - 1 - verbose output of compilation |
| 150 | - `PLATFORM` |
| 151 | - `PSOC_062_2M` - only supported now |
| 152 | - `SLOT_SIZE` - size of primary/secondary slot of MCUBootApp this app will be used with |
| 153 | - 0x%VALUE% |
| 154 | - `HEADER_OFFSET` - shift start address of image by value |
| 155 | - 0 (default) - no offset of output hex file |
| 156 | - 0x%VALUE% - offset for output hex file |
| 157 | - `IMG_TYPE` - for which slot of MCUBootApp image is build |
| 158 | - `BOOT` (default) - build image for primary (BOOT) slot |
| 159 | - `UPGRADE` - build image for secondary (UPGRADE) slot |
| 160 | - `SWAP_UPGRADE` - define upgrade mode type on `MCUBootApp` this app will be used with |
| 161 | - `0` - for overwrite mode. |
| 162 | - `1` - (default) for swap upgrade mode |
| 163 | - `ERASED_VALUE` - define memory cell contents in erased state |
| 164 | - `0x0` - internal memory |
| 165 | - `0xff` - external memory |
| 166 | - `TOOLCHAIN_PATH` - path to gcc compiler to use for build |
| 167 | - Example: TOOLCHAIN_PATH=/home/fw-security/ModusToolbox/tools_2.0/gcc-7.2.1 |
| 168 | - Example: TOOLCHAIN_PATH=C:\gcc |
| 169 | |
| 170 | ### Post-Build |
| 171 | |
| 172 | Post build action is executed at compile time for `BlinkyApp`. In case of build for `PSOC_062_2M`, `PSOC_062_1M`, `PSOC_062_512K` platforms it calls `imgtool` from `MCUBoot` scripts and adds signature to compiled image. |
| 173 | |
| 174 | Flags passed to `imgtool` for signature are defined in `SIGN_ARGS` variable in BlinkyApp.mk. |
| 175 | |
| 176 | ### How To Program An Application |
| 177 | |
| 178 | There are couple ways of programming BlinkyApp firmware. Following instructions assume usage of one of Cypress development kits, for example `CY8CPROTO_062_4343W`. |
| 179 | |
| 180 | 1. Direct usage of OpenOCD. |
| 181 | |
| 182 | OpenOCD package is supplied with ModusToolbox IDE and can be found in installation folder under `./tools_2.1/openocd`. |
| 183 | |
| 184 | Open terminal application - and execute following command after substitution `PATH_TO_APPLICATION.hex` and `OPENOCD` paths. |
| 185 | |
| 186 | Connect a board to your computer. Switch Kitprog3 to DAP-BULK mode by pressing `SW3 MODE` button until `LED2 STATUS` constantly shines. |
| 187 | |
| 188 | export OPENOCD=/Applications/ModusToolbox/tools_2.1/openocd |
| 189 | |
| 190 | ${OPENOCD}/bin/openocd -s ${OPENOCD}/scripts \ |
| 191 | -f ${OPENOCD}/scripts/interface/kitprog3.cfg \ |
| 192 | -f ${OPENOCD}/scripts/target/psoc6_2m.cfg \ |
| 193 | -c "init; reset init; program PATH_TO_APPLICATION.hex" \ |
| 194 | -c "resume; reset; exit" |
| 195 | |
| 196 | 2. Using GUI tool `Cypress Programmer` |
| 197 | |
| 198 | Follow [link](https://www.cypress.com/products/psoc-programming-solutions) to download. |
| 199 | |
| 200 | Connect board to your computer. Switch Kitprog3 to DAP-BULK mode by pressing `SW3 MODE` button until `LED2 STATUS` constantly shines. Open `Cypress Programmer` and click `Connect`, then choose hex file: `MCUBootApp.hex` or `BlinkyApp.hex` and click `Program`. Check log to ensure programming success. Reset board. |
| 201 | |
| 202 | 3. Using `DAPLINK`. |
| 203 | |
| 204 | Connect board to your computer. Switch embeded Kitprog3 to `DAPLINK` mode by pressing `SW3 MODE` button until `LED2 STATUS` blinks fast and mass storage device appeared in OS. Drag and drop `hex` files you wish to program to `DAPLINK` drive in your OS. |
| 205 | |
| 206 | **Hex file names to use for programming** |
| 207 | |
| 208 | `BlinkyApp` always produce build artifacts in 2 separate folders: `BlinkyApp/out/PSOC_062_2M/Debug/boot` and `BlinkyApp/out/PSOC_062_2M/Debug/upgrade`. |
| 209 | |
| 210 | These files are ready to be flashed to the board: |
| 211 | |
| 212 | * **BlinkyApp.hex** from `boot` folder |
| 213 | * **BlinkyApp_upgrade.hex** from `upgrade` folder |
| 214 | |
| 215 | `BlinkyApp_unsigned.hex` hex file is also preserved in both cases for possible troubleshooting. |
| 216 | |
| 217 | **Important: When swap status upgrade mode used** |
| 218 | |
| 219 | In case of using this application in a system with `swap` type of upgrade refer first to `MCUBootApp.md` section **SWAP/Expected lifecycle**. |
| 220 | |
| 221 | **BlinkyApp.hex** should be programmed to a device once. All firmware upgrades should be delivered using secondary (UPGRADE) slot thus **BlinkyApp_upgrade.hex** image. |
| 222 | |
| 223 | If user for some reason tries to program **BlinkyApp.hex** to primary slot directly second time - **system state should be reset**. |
| 224 | |
| 225 | To reset system state at least `swap status partition` area in flash should be erased - see addresses in `MCUBootApp.md` paragraph **Memory maps**. |
| 226 | |
| 227 | To erase swap status partition area in MCUBootApp with a single image configuration with default memory map using `OpenOCD` execute command: |
| 228 | |
| 229 | $OPENOCD_PATH/bin/openocd -s "$OPENOCD_PATH/scripts" -f "$OPENOCD_PATH/ scripts/interface/kitprog3.cfg" -f "$OPENOCD_PATH/scripts/target/psoc6_2m.cfg" -c "init; reset init" -c "flash erase_address 0x10038000 0x1000" -c "reset; shutdown" |
| 230 | |
| 231 | To erase swap status partition area in MCUBootApp with a multi image configuration with default memory map using `OpenOCD` execute command: |
| 232 | |
| 233 | $OPENOCD_PATH/bin/openocd -s "$OPENOCD_PATH/scripts" -f "$OPENOCD_PATH/ scripts/interface/kitprog3.cfg" -f "$OPENOCD_PATH/scripts/target/psoc6_2m.cfg" -c "init; reset init" -c "flash erase_address 0x10078000 0x2000" -c "reset; shutdown" |
| 234 | |
| 235 | In both cases it is easier to erase all device flash or all flash after MCUBootApp. This command erases all flash after MCUBootApp including primary, secondary and swap status partiton. |
| 236 | |
| 237 | $OPENOCD_PATH/bin/openocd -s "$OPENOCD_PATH/scripts" -f "$OPENOCD_PATH/ scripts/interface/kitprog3.cfg" -f "$OPENOCD_PATH/scripts/target/psoc6_2m.cfg" -c "init; reset init" -c "flash erase_address 0x10018000" -c "reset; shutdown" |
| 238 | |
| 239 | ### Example Terminal Output |
| 240 | |
| 241 | When user application programmed in BOOT slot: |
| 242 | |
| 243 | =========================== |
| 244 | [BlinkyApp] BlinkyApp v1.0 [CM4] |
| 245 | =========================== |
| 246 | [BlinkyApp] GPIO initialized |
| 247 | [BlinkyApp] UART initialized |
| 248 | [BlinkyApp] Retarget I/O set to 115200 baudrate |
| 249 | [BlinkyApp] Red led blinks with 1 sec period |
| 250 | [BlinkyApp] Update watchdog timer started in MCUBootApp to mark sucessful start of user app |
| 251 | [BlinkyApp] Turn off watchdog timer |
| 252 | |
| 253 | When user application programmed in UPRADE slot and upgrade procedure was successful: |
| 254 | |
| 255 | =========================== |
| 256 | [BlinkyApp] BlinkyApp v2.0 [+] |
| 257 | =========================== |
| 258 | [BlinkyApp] GPIO initialized |
| 259 | [BlinkyApp] UART initialized |
| 260 | [BlinkyApp] Retarget I/O set to 115200 baudrate |
| 261 | [BlinkyApp] Red led blinks with 0.25 sec period |
| 262 | [BlinkyApp] Update watchdog timer started in MCUBootApp to mark sucessful start of user app |
| 263 | [BlinkyApp] Turn off watchdog timer |
| 264 | [BlinkyApp] Try to set img_ok to confirm upgrade image |
| 265 | [BlinkyApp] SWAP Status : Image OK was set at 0x10027fe8. |