Mbed OS and Pelion Device Management hands-on workshop - connect and firmware update service

Committer:
MACRUM
Date:
Tue Oct 30 15:08:15 2018 +0900
Revision:
0:202fb3cf8be8
Add DISCO_L475VG_IOT01A with QSPIF

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:202fb3cf8be8 1 # Simple Pelion Device Management Client - template application
MACRUM 0:202fb3cf8be8 2
MACRUM 0:202fb3cf8be8 3 (aka Simple Mbed Cloud Client template)
MACRUM 0:202fb3cf8be8 4
MACRUM 0:202fb3cf8be8 5 ## Overview
MACRUM 0:202fb3cf8be8 6
MACRUM 0:202fb3cf8be8 7 This is a template application for platform vendors. It demonstrates how to create a simple application that can connect to the Pelion IoT Platform service, register resources and get ready to receive a firmware update.
MACRUM 0:202fb3cf8be8 8
MACRUM 0:202fb3cf8be8 9 It's intended to be forked and customized to add platform-specific features (such as sensors and actuators) and configure the connectivity and storage to work **out-of-the-box**. The template application works in **developer mode** by default.
MACRUM 0:202fb3cf8be8 10
MACRUM 0:202fb3cf8be8 11 There is a mirror version of the stable (master) template application on [this location](https://os.mbed.com/teams/mbed-os-examples/code/mbed-cloud-example) to facilitate the fork and publish on os.mbed.com.
MACRUM 0:202fb3cf8be8 12
MACRUM 0:202fb3cf8be8 13 ## Table of Contents
MACRUM 0:202fb3cf8be8 14
MACRUM 0:202fb3cf8be8 15 1. [Getting started with the application](#getting-started-with-the-application)
MACRUM 0:202fb3cf8be8 16 2. [Porting to a new platform](#porting-to-a-new-platform)
MACRUM 0:202fb3cf8be8 17 3. [Enabling firmware updates](#enabling-firmware-updates)
MACRUM 0:202fb3cf8be8 18 4. [Automated testing](#automated-testing)
MACRUM 0:202fb3cf8be8 19 5. [Known issues](#known-issues)
MACRUM 0:202fb3cf8be8 20
MACRUM 0:202fb3cf8be8 21 ## Getting started with the application
MACRUM 0:202fb3cf8be8 22
MACRUM 0:202fb3cf8be8 23 This is a summary of the process for developers to get started and get a device connected to Pelion Device Management Client.
MACRUM 0:202fb3cf8be8 24
MACRUM 0:202fb3cf8be8 25 ### Mbed Online IDE
MACRUM 0:202fb3cf8be8 26
MACRUM 0:202fb3cf8be8 27 1. Import the application into the Online IDE.
MACRUM 0:202fb3cf8be8 28 2. Add the API key to establish connection.
MACRUM 0:202fb3cf8be8 29 3. Install the developer certificate.
MACRUM 0:202fb3cf8be8 30 4. Compile and program.
MACRUM 0:202fb3cf8be8 31
MACRUM 0:202fb3cf8be8 32 ### Mbed CLI tools
MACRUM 0:202fb3cf8be8 33
MACRUM 0:202fb3cf8be8 34 1. Import the application into your desktop:
MACRUM 0:202fb3cf8be8 35
MACRUM 0:202fb3cf8be8 36 ```
MACRUM 0:202fb3cf8be8 37 mbed import https://os.mbed.com/teams/mbed-os-examples/code/mbed-cloud-example
MACRUM 0:202fb3cf8be8 38 cd mbed-cloud-example
MACRUM 0:202fb3cf8be8 39 ```
MACRUM 0:202fb3cf8be8 40
MACRUM 0:202fb3cf8be8 41 2. Download the developer certificate from the Pelion Device Management [portal](https://portal.mbedcloud.com).
MACRUM 0:202fb3cf8be8 42 3. Compile and program:
MACRUM 0:202fb3cf8be8 43
MACRUM 0:202fb3cf8be8 44 ```
MACRUM 0:202fb3cf8be8 45 mbed compile -t <toolchain> -m <target> -f
MACRUM 0:202fb3cf8be8 46 ```
MACRUM 0:202fb3cf8be8 47
MACRUM 0:202fb3cf8be8 48 ## Porting to a new platform
MACRUM 0:202fb3cf8be8 49
MACRUM 0:202fb3cf8be8 50 ### Requirements
MACRUM 0:202fb3cf8be8 51
MACRUM 0:202fb3cf8be8 52 The hardware requirements for Mbed OS platforms to support Pelion Client are [here](https://cloud.mbed.com/docs/current/cloud-requirements/index.html).
MACRUM 0:202fb3cf8be8 53
MACRUM 0:202fb3cf8be8 54 In general, to start creating a secure connected product, you need a microcontroller that has the following features:
MACRUM 0:202fb3cf8be8 55
MACRUM 0:202fb3cf8be8 56 * RAM: 96K or more
MACRUM 0:202fb3cf8be8 57 * Flash: 512K or more
MACRUM 0:202fb3cf8be8 58 * True Random Number Generator (TRNG)
MACRUM 0:202fb3cf8be8 59 * Real Time Clock (RTC)
MACRUM 0:202fb3cf8be8 60
MACRUM 0:202fb3cf8be8 61 Additionally, to use the Device Management Client, the microcontroller needs to support the following in Mbed OS (latest version preferred) or in a compatible driver library:
MACRUM 0:202fb3cf8be8 62
MACRUM 0:202fb3cf8be8 63 * A storage device (SDcard, SPI Flash, Data Flash)
MACRUM 0:202fb3cf8be8 64 * IP connectivity (Ethernet, Wi-Fi, Cellular, 6LoWPAN, Thread)
MACRUM 0:202fb3cf8be8 65
MACRUM 0:202fb3cf8be8 66 For the Firmware update over the air (FOTA), you need the following:
MACRUM 0:202fb3cf8be8 67
MACRUM 0:202fb3cf8be8 68 * [FlashIAP](https://github.com/ARMmbed/mbed-os/blob/master/drivers/FlashIAP.h) - Flash In-Application Programming (IAP).
MACRUM 0:202fb3cf8be8 69 * [Mbed Bootloader](https://github.com/ARMmbed/mbed-bootloader) or a compatible bootloader.
MACRUM 0:202fb3cf8be8 70 * TCP connection - the current Firmware Download client only supports HTTP download over TCP (this will be resolved in a future release, so that CoAP blockwise transfer will be used for UDP connections).
MACRUM 0:202fb3cf8be8 71
MACRUM 0:202fb3cf8be8 72 ### References
MACRUM 0:202fb3cf8be8 73
MACRUM 0:202fb3cf8be8 74 * Check which Mbed OS platforms are supported in the [Pelion Device Management quick-start guide](https://cloud.mbed.com/quick-start).
MACRUM 0:202fb3cf8be8 75 * Check which storage options are available [here](https://os.mbed.com/docs/latest/reference/storage.html).
MACRUM 0:202fb3cf8be8 76 * Check which network options are available [here](https://os.mbed.com/docs/latest/reference/network-socket.html).
MACRUM 0:202fb3cf8be8 77
MACRUM 0:202fb3cf8be8 78
MACRUM 0:202fb3cf8be8 79 ### Porting steps
MACRUM 0:202fb3cf8be8 80
MACRUM 0:202fb3cf8be8 81 Supporting a new derivative platform requires the following steps:
MACRUM 0:202fb3cf8be8 82
MACRUM 0:202fb3cf8be8 83 * Fork the template and create an example application for your platform in https://os.mbed.com/teams/your-team
MACRUM 0:202fb3cf8be8 84 * Change the connectivity interface. Ethernet is the default - see `main.cpp`.
MACRUM 0:202fb3cf8be8 85 * Change the filesystem and/or the block device for storage. FAT filesystem over SD card is the default. See `main.cpp`.
MACRUM 0:202fb3cf8be8 86 * (Optional) Make minor changes in `mbed_app.json` to support multiple platforms with same connectivity and storage.
MACRUM 0:202fb3cf8be8 87 * (Recommended) Remove information and files not related to the platform you're porting.
MACRUM 0:202fb3cf8be8 88
MACRUM 0:202fb3cf8be8 89 <span class="notes">**Note:** Make sure that the application works out-of-the-box and no changes are required in the `main.cpp` file nor `mbed_app.json`. The goal is to deliver a great UX to our developers.</span>
MACRUM 0:202fb3cf8be8 90
MACRUM 0:202fb3cf8be8 91 ### Porting example
MACRUM 0:202fb3cf8be8 92
MACRUM 0:202fb3cf8be8 93 In this example, an app with an SD card and on-chip Ethernet is taken to a custom board that has an MCU + Wi-Fi module.
MACRUM 0:202fb3cf8be8 94
MACRUM 0:202fb3cf8be8 95 #### Changing the storage option
MACRUM 0:202fb3cf8be8 96
MACRUM 0:202fb3cf8be8 97 <span class="notes">**Note:** From Mbed OS 5.10+, block device drivers have been moved to `mbed-os/components/storage/blockdevice` and many platforms have a default block device interface. </span>
MACRUM 0:202fb3cf8be8 98
MACRUM 0:202fb3cf8be8 99 ##### Non-default storage configuration
MACRUM 0:202fb3cf8be8 100
MACRUM 0:202fb3cf8be8 101 If you wish to override the default storage configuration or add support for storage, you can add the configuration into the `mbed_app.json` file. For example:
MACRUM 0:202fb3cf8be8 102
MACRUM 0:202fb3cf8be8 103 ```json
MACRUM 0:202fb3cf8be8 104 "NUCLEO_F429ZI": {
MACRUM 0:202fb3cf8be8 105 "target.features_add" : ["STORAGE"],
MACRUM 0:202fb3cf8be8 106 "target.components_add": ["SD"],
MACRUM 0:202fb3cf8be8 107 "sd.SPI_MOSI" : "PE_6",
MACRUM 0:202fb3cf8be8 108 "sd.SPI_MISO" : "PE_5",
MACRUM 0:202fb3cf8be8 109 "sd.SPI_CLK" : "PE_2",
MACRUM 0:202fb3cf8be8 110 "sd.SPI_CS" : "PE_4"
MACRUM 0:202fb3cf8be8 111 }
MACRUM 0:202fb3cf8be8 112 ```
MACRUM 0:202fb3cf8be8 113
MACRUM 0:202fb3cf8be8 114 ##### Example of default storage configuration using Mbed OS 5.10+
MACRUM 0:202fb3cf8be8 115
MACRUM 0:202fb3cf8be8 116 1. Include the header files for the FAT file system:
MACRUM 0:202fb3cf8be8 117
MACRUM 0:202fb3cf8be8 118 ```cpp
MACRUM 0:202fb3cf8be8 119 #include "FATFileSystem.h"
MACRUM 0:202fb3cf8be8 120 ```
MACRUM 0:202fb3cf8be8 121
MACRUM 0:202fb3cf8be8 122 2. Declare the global object for the default block device driver:
MACRUM 0:202fb3cf8be8 123
MACRUM 0:202fb3cf8be8 124 ```cpp
MACRUM 0:202fb3cf8be8 125 BlockDevice* bd = BlockDevice::get_default_instance();
MACRUM 0:202fb3cf8be8 126 ```
MACRUM 0:202fb3cf8be8 127
MACRUM 0:202fb3cf8be8 128 3. Declare the global objects for the file system:
MACRUM 0:202fb3cf8be8 129
MACRUM 0:202fb3cf8be8 130 ```cpp
MACRUM 0:202fb3cf8be8 131 FATFileSystem fs("sd", bd);
MACRUM 0:202fb3cf8be8 132 ```
MACRUM 0:202fb3cf8be8 133
MACRUM 0:202fb3cf8be8 134 ##### Example of SD card configuration using Mbed OS 5.9 and older
MACRUM 0:202fb3cf8be8 135
MACRUM 0:202fb3cf8be8 136 1. Add the SD card driver (`sd-driver.lib`) if it is not already added. On the command line:
MACRUM 0:202fb3cf8be8 137
MACRUM 0:202fb3cf8be8 138 ```
MACRUM 0:202fb3cf8be8 139 mbed add https://github.com/armmbed/sd-driver
MACRUM 0:202fb3cf8be8 140 ```
MACRUM 0:202fb3cf8be8 141
MACRUM 0:202fb3cf8be8 142 2. Include the header files for the SD driver and FAT file system:
MACRUM 0:202fb3cf8be8 143
MACRUM 0:202fb3cf8be8 144 ```cpp
MACRUM 0:202fb3cf8be8 145 #include "SDBlockDevice.h"
MACRUM 0:202fb3cf8be8 146 #include "FATFileSystem.h"
MACRUM 0:202fb3cf8be8 147 ```
MACRUM 0:202fb3cf8be8 148
MACRUM 0:202fb3cf8be8 149 3. Declare the global objects for the SD card and file system:
MACRUM 0:202fb3cf8be8 150
MACRUM 0:202fb3cf8be8 151 ```cpp
MACRUM 0:202fb3cf8be8 152 SDBlockDevice bd(SPI_MOSI, SPI_MISO, SPI_CLK, SPI_CS);
MACRUM 0:202fb3cf8be8 153 FATFileSystem fs("sd", &sd);
MACRUM 0:202fb3cf8be8 154 ```
MACRUM 0:202fb3cf8be8 155
MACRUM 0:202fb3cf8be8 156 <span class="notes">**Note:** The `SPI_*` macros represent the pin names. The names can be defined in a variety of places including the sd-driver, your project’s configuration file (`mbed_app.json`) or the `pinnames.h` file for the target that defines the default pin names. You can use other pin names depending on the platform and the connections.</span>
MACRUM 0:202fb3cf8be8 157
MACRUM 0:202fb3cf8be8 158 For example, if the SPI signals for the SD card interface are connected on an Arduino compatible shield, you may define them like this:
MACRUM 0:202fb3cf8be8 159
MACRUM 0:202fb3cf8be8 160 ```cpp
MACRUM 0:202fb3cf8be8 161 SDBlockDevice sd(D11, D12, D13, D10);
MACRUM 0:202fb3cf8be8 162 ```
MACRUM 0:202fb3cf8be8 163
MACRUM 0:202fb3cf8be8 164 <span class="notes">**Note:** The default mounting point is `sd`. This can be overridden in the `mbed_app.json` file using `PAL_FS_MOUNT_POINT_PRIMARY` and `PAL_FS_MOUNT_POINT_SECONDARY`. See [documentation](https://cloud.mbed.com/docs/current/porting/port-filesystem.html).</span>
MACRUM 0:202fb3cf8be8 165
MACRUM 0:202fb3cf8be8 166 ##### For SPI Flash (devices that support SFDP)
MACRUM 0:202fb3cf8be8 167
MACRUM 0:202fb3cf8be8 168 <Please note that this section of the document is under construction. More information is needed.>
MACRUM 0:202fb3cf8be8 169
MACRUM 0:202fb3cf8be8 170 1. Add the SPI Flash driver (`spif-driver`) if it is not already added:
MACRUM 0:202fb3cf8be8 171
MACRUM 0:202fb3cf8be8 172 ```
MACRUM 0:202fb3cf8be8 173 mbed add https://github.com/armmbed/spif-driver
MACRUM 0:202fb3cf8be8 174 ```
MACRUM 0:202fb3cf8be8 175
MACRUM 0:202fb3cf8be8 176 2. Include the header files for the SPI Flash driver and LitteFS file system. For SPI Flash, we recommend LittleFS file system which supports wear leveling:
MACRUM 0:202fb3cf8be8 177
MACRUM 0:202fb3cf8be8 178 ```cpp
MACRUM 0:202fb3cf8be8 179 #include "SPIFBlockDevice.h"
MACRUM 0:202fb3cf8be8 180 #include "LittleFileSystem.h"
MACRUM 0:202fb3cf8be8 181 ```
MACRUM 0:202fb3cf8be8 182
MACRUM 0:202fb3cf8be8 183 3. Declare the global objects for the SD card and file system:
MACRUM 0:202fb3cf8be8 184
MACRUM 0:202fb3cf8be8 185 ```cpp
MACRUM 0:202fb3cf8be8 186 SPIFBlockDevice spif(SPI_MOSI, SPI_MISO, SPI_CLK, SPI_CS);
MACRUM 0:202fb3cf8be8 187 LittleFileSystem fs("fs", &spif);
MACRUM 0:202fb3cf8be8 188 ```
MACRUM 0:202fb3cf8be8 189
MACRUM 0:202fb3cf8be8 190 4. Update the construction of the `SimpleMbedCloudClient` object to pass in the file system and block device:
MACRUM 0:202fb3cf8be8 191
MACRUM 0:202fb3cf8be8 192 ```cpp
MACRUM 0:202fb3cf8be8 193 SimpleMbedCloudClient client(&net, &spif, &fs);
MACRUM 0:202fb3cf8be8 194 ```
MACRUM 0:202fb3cf8be8 195
MACRUM 0:202fb3cf8be8 196 #### Changing the network interface
MACRUM 0:202fb3cf8be8 197
MACRUM 0:202fb3cf8be8 198 <span class="notes">**Note:** From Mbed OS 5.10, platforms have a default network interface defined in `mbed-os/targets/targets.json`. If you wish to override the default configuration, you can add the configuration into the `mbed_app.json` file.</span>
MACRUM 0:202fb3cf8be8 199
MACRUM 0:202fb3cf8be8 200 ##### Non-default network configuration
MACRUM 0:202fb3cf8be8 201
MACRUM 0:202fb3cf8be8 202 If you wish to override the default network configuration, you can add the configuration into the `mbed_app.json` file. For example:
MACRUM 0:202fb3cf8be8 203
MACRUM 0:202fb3cf8be8 204 ```json
MACRUM 0:202fb3cf8be8 205 "NUCLEO_F429ZI": {
MACRUM 0:202fb3cf8be8 206 "target.network-default-interface-type" : "WIFI",
MACRUM 0:202fb3cf8be8 207 "esp8266.rx" : "D0",
MACRUM 0:202fb3cf8be8 208 "esp8266.tx" : "D1",
MACRUM 0:202fb3cf8be8 209 "esp8266.provide-default" : true,
MACRUM 0:202fb3cf8be8 210 "nsapi.default-wifi-security" : "WPA_WPA2",
MACRUM 0:202fb3cf8be8 211 "nsapi.default-wifi-ssid" : "\"SSID\"",
MACRUM 0:202fb3cf8be8 212 "nsapi.default-wifi-password" : "\"Password\""
MACRUM 0:202fb3cf8be8 213 }
MACRUM 0:202fb3cf8be8 214 ```
MACRUM 0:202fb3cf8be8 215
MACRUM 0:202fb3cf8be8 216 ##### Example of network initialization for Ethernet using Mbed OS 5.10+
MACRUM 0:202fb3cf8be8 217
MACRUM 0:202fb3cf8be8 218 1. Declare the network interface object:
MACRUM 0:202fb3cf8be8 219
MACRUM 0:202fb3cf8be8 220 ```
MACRUM 0:202fb3cf8be8 221 EthernetInterface * net = NetworkInterface::get_default_instance();
MACRUM 0:202fb3cf8be8 222 ```
MACRUM 0:202fb3cf8be8 223
MACRUM 0:202fb3cf8be8 224 2. Connect the interface:
MACRUM 0:202fb3cf8be8 225
MACRUM 0:202fb3cf8be8 226 ```
MACRUM 0:202fb3cf8be8 227 status = net->connect();
MACRUM 0:202fb3cf8be8 228 ```
MACRUM 0:202fb3cf8be8 229
MACRUM 0:202fb3cf8be8 230 3. When the Client is started, pass the network interface:
MACRUM 0:202fb3cf8be8 231 ```
MACRUM 0:202fb3cf8be8 232 SimpleMbedCloudClient client(net, &sd, &fs);
MACRUM 0:202fb3cf8be8 233 ```
MACRUM 0:202fb3cf8be8 234
MACRUM 0:202fb3cf8be8 235 ##### Example of network initialization for Wi-Fi using Mbed OS 5.10+
MACRUM 0:202fb3cf8be8 236
MACRUM 0:202fb3cf8be8 237 1. Declare the network interface object:
MACRUM 0:202fb3cf8be8 238
MACRUM 0:202fb3cf8be8 239 ```
MACRUM 0:202fb3cf8be8 240 WiFiInterface *net = WiFiInterface::get_default_instance();
MACRUM 0:202fb3cf8be8 241 ```
MACRUM 0:202fb3cf8be8 242
MACRUM 0:202fb3cf8be8 243 2. Connect the interface:
MACRUM 0:202fb3cf8be8 244
MACRUM 0:202fb3cf8be8 245 ```
MACRUM 0:202fb3cf8be8 246 status = net->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
MACRUM 0:202fb3cf8be8 247 ```
MACRUM 0:202fb3cf8be8 248
MACRUM 0:202fb3cf8be8 249 3. When the Client is started, pass the network interface:
MACRUM 0:202fb3cf8be8 250
MACRUM 0:202fb3cf8be8 251 ```
MACRUM 0:202fb3cf8be8 252 SimpleMbedCloudClient client(net, &sd, &fs);
MACRUM 0:202fb3cf8be8 253 ```
MACRUM 0:202fb3cf8be8 254
MACRUM 0:202fb3cf8be8 255
MACRUM 0:202fb3cf8be8 256 ##### Example of network initialization for Ethernet using Mbed OS 5.9 and older versions
MACRUM 0:202fb3cf8be8 257
MACRUM 0:202fb3cf8be8 258 The Ethernet interface is included within Mbed OS, so you do not need to add a library.
MACRUM 0:202fb3cf8be8 259
MACRUM 0:202fb3cf8be8 260 1. Include the header file for the interface:
MACRUM 0:202fb3cf8be8 261
MACRUM 0:202fb3cf8be8 262 ```
MACRUM 0:202fb3cf8be8 263 #include "EthernetInterface.h"
MACRUM 0:202fb3cf8be8 264 ```
MACRUM 0:202fb3cf8be8 265
MACRUM 0:202fb3cf8be8 266 2. Declare the network interface object:
MACRUM 0:202fb3cf8be8 267
MACRUM 0:202fb3cf8be8 268 ```
MACRUM 0:202fb3cf8be8 269 EthernetInterface net;
MACRUM 0:202fb3cf8be8 270 ```
MACRUM 0:202fb3cf8be8 271
MACRUM 0:202fb3cf8be8 272 3. Connect the interface:
MACRUM 0:202fb3cf8be8 273
MACRUM 0:202fb3cf8be8 274 ```
MACRUM 0:202fb3cf8be8 275 status = net.connect();
MACRUM 0:202fb3cf8be8 276 ```
MACRUM 0:202fb3cf8be8 277
MACRUM 0:202fb3cf8be8 278 4. When the Client is started, pass the network interface:
MACRUM 0:202fb3cf8be8 279
MACRUM 0:202fb3cf8be8 280 ```
MACRUM 0:202fb3cf8be8 281 SimpleMbedCloudClient client(&net, &sd, &fs);
MACRUM 0:202fb3cf8be8 282 ```
MACRUM 0:202fb3cf8be8 283
MACRUM 0:202fb3cf8be8 284 ##### Example of network initialization for Wi-Fi using Mbed OS 5.9 and older versions
MACRUM 0:202fb3cf8be8 285
MACRUM 0:202fb3cf8be8 286 This example references the ESP8266 Wi-Fi module, but the instructions are applicable to other modules.
MACRUM 0:202fb3cf8be8 287
MACRUM 0:202fb3cf8be8 288 1. Add the ESP8266 Wi-Fi interface driver (esp8266-driver) if it is not already added:
MACRUM 0:202fb3cf8be8 289
MACRUM 0:202fb3cf8be8 290 ```
MACRUM 0:202fb3cf8be8 291 mbed add https://github.com/ARMmbed/esp8266-driver
MACRUM 0:202fb3cf8be8 292 ```
MACRUM 0:202fb3cf8be8 293
MACRUM 0:202fb3cf8be8 294 <span class="notes">**Note:** You may have to update the firmware inside the ESP8266 module.</span>
MACRUM 0:202fb3cf8be8 295
MACRUM 0:202fb3cf8be8 296 2. Include the header file for the interface:
MACRUM 0:202fb3cf8be8 297
MACRUM 0:202fb3cf8be8 298 ```cpp
MACRUM 0:202fb3cf8be8 299 #include "ESP8266Interface.h"
MACRUM 0:202fb3cf8be8 300 ```
MACRUM 0:202fb3cf8be8 301
MACRUM 0:202fb3cf8be8 302 3. Declare the network interface object:
MACRUM 0:202fb3cf8be8 303
MACRUM 0:202fb3cf8be8 304 ```cpp
MACRUM 0:202fb3cf8be8 305 ESP8266Interface net(D1, D0);
MACRUM 0:202fb3cf8be8 306 ```
MACRUM 0:202fb3cf8be8 307
MACRUM 0:202fb3cf8be8 308 4. Connect the interface:
MACRUM 0:202fb3cf8be8 309
MACRUM 0:202fb3cf8be8 310 ```cpp
MACRUM 0:202fb3cf8be8 311 nsapi_error_t status = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
MACRUM 0:202fb3cf8be8 312 ```
MACRUM 0:202fb3cf8be8 313
MACRUM 0:202fb3cf8be8 314 5. When the Client is started, pass the network interface:
MACRUM 0:202fb3cf8be8 315
MACRUM 0:202fb3cf8be8 316 ```cpp
MACRUM 0:202fb3cf8be8 317 SimpleMbedCloudClient client(&net, &sd, &fs);
MACRUM 0:202fb3cf8be8 318 ```
MACRUM 0:202fb3cf8be8 319
MACRUM 0:202fb3cf8be8 320 6. Add the Wi-Fi credentials information in `mbed_app.json` (located at the top level of the example project):
MACRUM 0:202fb3cf8be8 321
MACRUM 0:202fb3cf8be8 322 ```json
MACRUM 0:202fb3cf8be8 323 "config": {
MACRUM 0:202fb3cf8be8 324 "wifi-ssid": {
MACRUM 0:202fb3cf8be8 325 "help": "WiFi SSID",
MACRUM 0:202fb3cf8be8 326 "value": "\"SSID\""
MACRUM 0:202fb3cf8be8 327 },
MACRUM 0:202fb3cf8be8 328 "wifi-password": {
MACRUM 0:202fb3cf8be8 329 "help": "WiFi Password",
MACRUM 0:202fb3cf8be8 330 "value": "\"PASSWORD\""
MACRUM 0:202fb3cf8be8 331 }
MACRUM 0:202fb3cf8be8 332 }
MACRUM 0:202fb3cf8be8 333 ```
MACRUM 0:202fb3cf8be8 334
MACRUM 0:202fb3cf8be8 335 #### Changing the target MCU
MACRUM 0:202fb3cf8be8 336
MACRUM 0:202fb3cf8be8 337 To change the target board to another board that is supported by Mbed OS, simply change the target name.
MACRUM 0:202fb3cf8be8 338
MACRUM 0:202fb3cf8be8 339 ##### Using the command line
MACRUM 0:202fb3cf8be8 340
MACRUM 0:202fb3cf8be8 341 Use the -m option. For example:
MACRUM 0:202fb3cf8be8 342
MACRUM 0:202fb3cf8be8 343 ```
MACRUM 0:202fb3cf8be8 344 mbed compile -m Hexiwear -t GCC_ARM
MACRUM 0:202fb3cf8be8 345 ```
MACRUM 0:202fb3cf8be8 346
MACRUM 0:202fb3cf8be8 347 ##### Using the online compiler
MACRUM 0:202fb3cf8be8 348
MACRUM 0:202fb3cf8be8 349 Click the platform name on the top right corner, then select another platform.
MACRUM 0:202fb3cf8be8 350
MACRUM 0:202fb3cf8be8 351 ##### Using an IDE
MACRUM 0:202fb3cf8be8 352
MACRUM 0:202fb3cf8be8 353 First re-export (create project files) for the target with the command line. For example:
MACRUM 0:202fb3cf8be8 354
MACRUM 0:202fb3cf8be8 355 ```
MACRUM 0:202fb3cf8be8 356 mbed export -m Hexiwear -i uvision
MACRUM 0:202fb3cf8be8 357 ```
MACRUM 0:202fb3cf8be8 358
MACRUM 0:202fb3cf8be8 359 #### Creating a custom target board
MACRUM 0:202fb3cf8be8 360
MACRUM 0:202fb3cf8be8 361 Read the Mbed OS [Contributing](https://os.mbed.com/docs/latest/reference/porting-targets.html) documentation on how to add a new target.
MACRUM 0:202fb3cf8be8 362
MACRUM 0:202fb3cf8be8 363 #### Update the application logic
MACRUM 0:202fb3cf8be8 364
MACRUM 0:202fb3cf8be8 365 The template example uses a ticker object to periodically fire a software interrupt to simulate button presses. Let’s say you want to make an actual button press.
MACRUM 0:202fb3cf8be8 366
MACRUM 0:202fb3cf8be8 367 By default, there is a Ticker object, which fires every five seconds and invokes a callback function:
MACRUM 0:202fb3cf8be8 368
MACRUM 0:202fb3cf8be8 369 ```cpp
MACRUM 0:202fb3cf8be8 370 Ticker timer;
MACRUM 0:202fb3cf8be8 371 timer.attach(eventQueue.event(&fake_button_press), 5.0);
MACRUM 0:202fb3cf8be8 372 ```
MACRUM 0:202fb3cf8be8 373
MACRUM 0:202fb3cf8be8 374 This callback function changes the `button_res` resource:
MACRUM 0:202fb3cf8be8 375
MACRUM 0:202fb3cf8be8 376 ```cpp
MACRUM 0:202fb3cf8be8 377 void fake_button_press() {
MACRUM 0:202fb3cf8be8 378 int v = button_res->get_value_int() + 1;
MACRUM 0:202fb3cf8be8 379
MACRUM 0:202fb3cf8be8 380 button_res->set_value(v);
MACRUM 0:202fb3cf8be8 381
MACRUM 0:202fb3cf8be8 382 printf("Simulated button clicked %d times\n", v);
MACRUM 0:202fb3cf8be8 383 }
MACRUM 0:202fb3cf8be8 384 ```
MACRUM 0:202fb3cf8be8 385
MACRUM 0:202fb3cf8be8 386 If you want to change this to an actual button, here is how to do it:
MACRUM 0:202fb3cf8be8 387
MACRUM 0:202fb3cf8be8 388 1. Remove:
MACRUM 0:202fb3cf8be8 389
MACRUM 0:202fb3cf8be8 390 ```cpp
MACRUM 0:202fb3cf8be8 391 Ticker timer;
MACRUM 0:202fb3cf8be8 392 timer.attach(eventQueue.event(&fake_button_press), 5.0);
MACRUM 0:202fb3cf8be8 393 ```
MACRUM 0:202fb3cf8be8 394
MACRUM 0:202fb3cf8be8 395 2. Declare an `InterruptIn` object on the button, and attach the callback function to the `fall` handler:
MACRUM 0:202fb3cf8be8 396
MACRUM 0:202fb3cf8be8 397 ```cpp
MACRUM 0:202fb3cf8be8 398 InterruptIn btn(BUTTON1);
MACRUM 0:202fb3cf8be8 399 btn.fall(eventQueue.event(&fake_button_press), 5.0);
MACRUM 0:202fb3cf8be8 400 ```
MACRUM 0:202fb3cf8be8 401
MACRUM 0:202fb3cf8be8 402 3. Rename `fake_button_press` to `real_button_press`.
MACRUM 0:202fb3cf8be8 403
MACRUM 0:202fb3cf8be8 404
MACRUM 0:202fb3cf8be8 405 #### Pelion Client v1.3.x SOTP-specific changes
MACRUM 0:202fb3cf8be8 406
MACRUM 0:202fb3cf8be8 407 The version v1.3+ introduces a new feature called Software One-Time Programming (SOTP) that makes use of the internal flash of the MCU as an One-Time-Programmable section. It stores the keys required to decrypt the credentials stored in the persistent storage. Read more on this in the [porting documentation](https://cloud.mbed.com/docs/current/porting/changing-a-customized-porting-layer.html#rtos-module) under the RTOS module section.
MACRUM 0:202fb3cf8be8 408
MACRUM 0:202fb3cf8be8 409 The flash must be divided into two sections (default 2, maximum 2) for your target. You need to modify the `mbed_app.json` file as follows:
MACRUM 0:202fb3cf8be8 410
MACRUM 0:202fb3cf8be8 411 1. Add a section to the `target_overrides` with SOTP addresses and sizes.
MACRUM 0:202fb3cf8be8 412
MACRUM 0:202fb3cf8be8 413 You can find the memory map information in the reference manual of your MCU. Note the sectors should be placed at the last two sectors of the flash, so the SOTP region is preserved during drag and drop programming of binaries. This is an example for the NUCLEO_F429ZI board:
MACRUM 0:202fb3cf8be8 414
MACRUM 0:202fb3cf8be8 415 ```json
MACRUM 0:202fb3cf8be8 416 "NUCLEO_F429ZI": {
MACRUM 0:202fb3cf8be8 417 "app.sotp-section-1-address": "(0x081C0000)",
MACRUM 0:202fb3cf8be8 418 "app.sotp-section-1-size" : "(128*1024)",
MACRUM 0:202fb3cf8be8 419 "app.sotp-section-2-address": "(0x081E0000)",
MACRUM 0:202fb3cf8be8 420 "app.sotp-section-2-size" : "(128*1024)"
MACRUM 0:202fb3cf8be8 421 }
MACRUM 0:202fb3cf8be8 422 ```
MACRUM 0:202fb3cf8be8 423
MACRUM 0:202fb3cf8be8 424 2. Add the macro definition to the "config" section. Note that the address and size macros are already provided. You only need to add the macro for the number of sections:
MACRUM 0:202fb3cf8be8 425
MACRUM 0:202fb3cf8be8 426 ```json
MACRUM 0:202fb3cf8be8 427 "sotp-num-sections": {
MACRUM 0:202fb3cf8be8 428 "help": "Number of SOTP sections",
MACRUM 0:202fb3cf8be8 429 "macro_name": "PAL_INT_FLASH_NUM_SECTIONS",
MACRUM 0:202fb3cf8be8 430 "value": null
MACRUM 0:202fb3cf8be8 431 }
MACRUM 0:202fb3cf8be8 432 ```
MACRUM 0:202fb3cf8be8 433
MACRUM 0:202fb3cf8be8 434 ## Enabling firmware updates
MACRUM 0:202fb3cf8be8 435
MACRUM 0:202fb3cf8be8 436 Mbed OS 5.10 and Mbed CLI 1.8 simplifies the process to enable and perform Firmware Updates. Here is a summary on how to configure the device and verify its correct behaviour.
MACRUM 0:202fb3cf8be8 437
MACRUM 0:202fb3cf8be8 438 For full documentation about bootloaders and firmware update, read the following documents:
MACRUM 0:202fb3cf8be8 439
MACRUM 0:202fb3cf8be8 440 - [Introduccion to bootloaders](https://os.mbed.com/docs/latest/porting/bootloader.html)
MACRUM 0:202fb3cf8be8 441 - [Creating and using a bootloader](https://os.mbed.com/docs/latest/tutorials/bootloader.html)
MACRUM 0:202fb3cf8be8 442 - [Bootloader configuration in Mbed OS](https://os.mbed.com/docs/latest/tools/configuring-tools.html)
MACRUM 0:202fb3cf8be8 443 - [Mbed Bootloader for Pelion Device Management Client](https://github.com/ARMmbed/mbed-bootloader)
MACRUM 0:202fb3cf8be8 444 - [Updating devices with Arm Mbed CLI](https://os.mbed.com/docs/latest/tools/cli-update.html)
MACRUM 0:202fb3cf8be8 445
MACRUM 0:202fb3cf8be8 446 This is a summary to use Arm Mbed OS managed bootloaders.
MACRUM 0:202fb3cf8be8 447
MACRUM 0:202fb3cf8be8 448 #### Preparing a bootloader
MACRUM 0:202fb3cf8be8 449
MACRUM 0:202fb3cf8be8 450 If Mbed OS contains a default pre-built bootloader in `mbed-os/feature/FEATURE_BOOTLOADER`, then you can skip this section.
MACRUM 0:202fb3cf8be8 451
MACRUM 0:202fb3cf8be8 452 Otherwise, you'll need to compile the [mbed-bootloader](https://github.com/armmbed/mbed-bootloader) and add it to your application. Once it's done, we recommend to send a Pull-Requests to [Mbed OS](https://github.com/ARMmbed/mbed-os) to contribute with a default bootloader for your Mbed Enabled platform.
MACRUM 0:202fb3cf8be8 453
MACRUM 0:202fb3cf8be8 454 You can see an example of bootloader configuration for the `NUCLEO_F429ZI` in `bootloader/mbed_app.json`. This is compatible with the `mbed_app.json` configuration in this template application.
MACRUM 0:202fb3cf8be8 455
MACRUM 0:202fb3cf8be8 456 <span class="notes">**Note:** Make sure the configuration for the bootloader (`mbed_app.json`) corresponds with the configuration of your application's `mbed_app.json`, otherwise the bootloader may not be able to find an application or apply the new firmware.</span>
MACRUM 0:202fb3cf8be8 457
MACRUM 0:202fb3cf8be8 458 #### Enabling the application to use a bootloader
MACRUM 0:202fb3cf8be8 459
MACRUM 0:202fb3cf8be8 460 ##### Option 1: default & prebuilt bootloader
MACRUM 0:202fb3cf8be8 461
MACRUM 0:202fb3cf8be8 462 If Mbed OS contains a prebuilt bootloader for the target, then you can indicate to use it in the `mbed_app.json`. For example:
MACRUM 0:202fb3cf8be8 463
MACRUM 0:202fb3cf8be8 464 ```
MACRUM 0:202fb3cf8be8 465 {
MACRUM 0:202fb3cf8be8 466 "target_overrides": {
MACRUM 0:202fb3cf8be8 467 "K64F": {
MACRUM 0:202fb3cf8be8 468 "target.features_add": ["BOOTLOADER"]
MACRUM 0:202fb3cf8be8 469 }
MACRUM 0:202fb3cf8be8 470 }
MACRUM 0:202fb3cf8be8 471 }
MACRUM 0:202fb3cf8be8 472 ```
MACRUM 0:202fb3cf8be8 473
MACRUM 0:202fb3cf8be8 474 ##### Option 2: custom bootloader
MACRUM 0:202fb3cf8be8 475
MACRUM 0:202fb3cf8be8 476 If you'd like to overide a default bootloader or use a custom one available in the application, then indicate the path to the booloader, `app_offset` and `header_offset` parameters in `mbed_app.json`. For example:
MACRUM 0:202fb3cf8be8 477
MACRUM 0:202fb3cf8be8 478 ```
MACRUM 0:202fb3cf8be8 479 "target_overrides": {
MACRUM 0:202fb3cf8be8 480 "K64F": {
MACRUM 0:202fb3cf8be8 481 "target.app_offset": "0xa400",
MACRUM 0:202fb3cf8be8 482 "target.header_offset": "0xa000",
MACRUM 0:202fb3cf8be8 483 "target.bootloader_img": "bootloader/my_bootloader.bin"
MACRUM 0:202fb3cf8be8 484 }
MACRUM 0:202fb3cf8be8 485 }
MACRUM 0:202fb3cf8be8 486 ```
MACRUM 0:202fb3cf8be8 487
MACRUM 0:202fb3cf8be8 488 You may need to specify `header_format` as well. You could include the default header format from [Mbed OS](https://github.com/ARMmbed/mbed-os/blob/master/features/FEATURE_BOOTLOADER/mbed_lib.json) by adding `"target.features_add": ["BOOTLOADER"]`.
MACRUM 0:202fb3cf8be8 489
MACRUM 0:202fb3cf8be8 490 #### Verifying that firmware update works
MACRUM 0:202fb3cf8be8 491
MACRUM 0:202fb3cf8be8 492 Follow these steps to generate a manifest, compile and perform a firmware update of your device:
MACRUM 0:202fb3cf8be8 493
MACRUM 0:202fb3cf8be8 494 1. Configure the API key for your Pelion account.
MACRUM 0:202fb3cf8be8 495
MACRUM 0:202fb3cf8be8 496 If you don't have an API key available, then login in [Pelion IoT Platform portal](https://portal.mbedcloud.com/), navigate to 'Access Management', 'API keys' and create a new one. Then specify the API key as global `mbed` configuration:
MACRUM 0:202fb3cf8be8 497
MACRUM 0:202fb3cf8be8 498 ```
MACRUM 0:202fb3cf8be8 499 mbed config -G CLOUD_SDK_API_KEY <your-api-key>
MACRUM 0:202fb3cf8be8 500 ```
MACRUM 0:202fb3cf8be8 501
MACRUM 0:202fb3cf8be8 502 2. Initialize the device management feature:
MACRUM 0:202fb3cf8be8 503
MACRUM 0:202fb3cf8be8 504 ```
MACRUM 0:202fb3cf8be8 505 mbed dm init -d "company.com" --model-name "product-model" -q --force
MACRUM 0:202fb3cf8be8 506 ```
MACRUM 0:202fb3cf8be8 507
MACRUM 0:202fb3cf8be8 508 3. Compile the application, include the firware update credentials generated before, merge with the bootloader and program the device:
MACRUM 0:202fb3cf8be8 509
MACRUM 0:202fb3cf8be8 510 ```
MACRUM 0:202fb3cf8be8 511 mbed compile -t <toolchain> -m <target> -c -f
MACRUM 0:202fb3cf8be8 512 ```
MACRUM 0:202fb3cf8be8 513
MACRUM 0:202fb3cf8be8 514 4. Open a serial terminal, verify the application boots and is able to register to the Device Management service. Write down the `<endpoint ID>`, as it's required to identify the device to perform a firmware update.
MACRUM 0:202fb3cf8be8 515
MACRUM 0:202fb3cf8be8 516 5. Update the firmware of the device through Mbed CLI:
MACRUM 0:202fb3cf8be8 517
MACRUM 0:202fb3cf8be8 518 ```
MACRUM 0:202fb3cf8be8 519 mbed dm update device -D <device ID> -t <toolchain> -m <target>
MACRUM 0:202fb3cf8be8 520 ```
MACRUM 0:202fb3cf8be8 521
MACRUM 0:202fb3cf8be8 522 Inspect the logs on the device to see the update progress. It should look similar to:
MACRUM 0:202fb3cf8be8 523
MACRUM 0:202fb3cf8be8 524 ```
MACRUM 0:202fb3cf8be8 525 Firmware download requested
MACRUM 0:202fb3cf8be8 526 Authorization granted
MACRUM 0:202fb3cf8be8 527 Downloading: [+++- ] 6 %
MACRUM 0:202fb3cf8be8 528 ```
MACRUM 0:202fb3cf8be8 529
MACRUM 0:202fb3cf8be8 530 When the download completes, the firmware is verified. If everything is OK, the firmware update is applied, the device reboots and attemps to connect to the Device Management service again. The `<endpoint ID>` should be preserved.
MACRUM 0:202fb3cf8be8 531
MACRUM 0:202fb3cf8be8 532 ## Automated testing
MACRUM 0:202fb3cf8be8 533
MACRUM 0:202fb3cf8be8 534 The Simple Pelion Client provides Greentea tests to confirm your platform works as expected. The network and storage configuration is already defined in Mbed OS 5.10, but you may want to override the configuration in `mbed_app.json`.
MACRUM 0:202fb3cf8be8 535
MACRUM 0:202fb3cf8be8 536 For details on Simple Pelion Client testing, refer to the documentation [here](https://github.com/ARMmbed/simple-mbed-cloud-client#testing).
MACRUM 0:202fb3cf8be8 537
MACRUM 0:202fb3cf8be8 538 This template application contains a working application and tests passing for the `K64F` and `K66F` platforms.
MACRUM 0:202fb3cf8be8 539
MACRUM 0:202fb3cf8be8 540 ## Known issues
MACRUM 0:202fb3cf8be8 541
MACRUM 0:202fb3cf8be8 542 Please check the issues reported on github.