Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

Committer:
Jeej
Date:
Tue Mar 14 15:18:05 2017 +0000
Revision:
17:05c9ec970a6d
Parent:
16:5a6168dbe7bb
Child:
18:a8a640941018
Release 4.7.232 (For modems 4.7.x only. For 4.6.x use the previous Release)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeej 0:82a60d86ab2e 1 #include "mbed.h"
Jeej 0:82a60d86ab2e 2 #include "rtos.h"
Jeej 0:82a60d86ab2e 3 #include "d7a.h"
Jeej 0:82a60d86ab2e 4 #include "dbg.h"
Jeej 0:82a60d86ab2e 5 #include "cup.h"
Jeej 0:82a60d86ab2e 6 #include "DebouncedInterrupt.h"
Jeej 0:82a60d86ab2e 7
Jeej 0:82a60d86ab2e 8 // File IDs
Jeej 0:82a60d86ab2e 9 #define D7A_FID_FIRMWARE_VERSION (2)
Jeej 0:82a60d86ab2e 10
Jeej 2:e0cdfa7d2a8b 11 // To udate your firmware:
Jeej 2:e0cdfa7d2a8b 12 // - Specify your root key
Jeej 2:e0cdfa7d2a8b 13 // - Choose your Hardware in bin.h
Jeej 2:e0cdfa7d2a8b 14 // - Program your NUCLEO with your modem stacked
Jeej 2:e0cdfa7d2a8b 15 // - Follow the instructions printed on the debug port
Jeej 2:e0cdfa7d2a8b 16
Jeej 2:e0cdfa7d2a8b 17
Jeej 2:e0cdfa7d2a8b 18 // This is the default root key
Jeej 2:e0cdfa7d2a8b 19 // if you have changed this key, please specify it here
Jeej 12:beabd59e0c35 20 uint8_t root_key[CUP_DEFAULT_KEY_SIZE] = CUP_DEFAULT_KEY;
Jeej 12:beabd59e0c35 21 uint8_t root_key_size = CUP_DEFAULT_KEY_SIZE;
Jeej 12:beabd59e0c35 22
Jeej 12:beabd59e0c35 23 // -----------------------------------------------
Jeej 12:beabd59e0c35 24 // Hardware configuration
Jeej 12:beabd59e0c35 25 // -----------------------------------------------
Jeej 12:beabd59e0c35 26 #if defined(TARGET_STM32L152RE)
Jeej 12:beabd59e0c35 27 #define D7A_PIN_TX (D10)
Jeej 12:beabd59e0c35 28 #define D7A_PIN_RX (D2)
Jeej 12:beabd59e0c35 29 #define D7A_PIN_RTS (D13)
Jeej 12:beabd59e0c35 30 #define D7A_PIN_CTS (D9)
Jeej 12:beabd59e0c35 31 #define D7A_PIN_RESET (A3)
Jeej 12:beabd59e0c35 32 #define DEBUG_LED (NC)
Jeej 12:beabd59e0c35 33 #define DEBUG_BUTTON (USER_BUTTON)
Jeej 12:beabd59e0c35 34 #elif defined(TARGET_STM32L432KC)
Jeej 12:beabd59e0c35 35 #define D7A_PIN_TX (D5)
Jeej 12:beabd59e0c35 36 #define D7A_PIN_RX (D4)
Jeej 12:beabd59e0c35 37 #define D7A_PIN_RTS (D11)
Jeej 12:beabd59e0c35 38 #define D7A_PIN_CTS (D10)
Jeej 12:beabd59e0c35 39 #define D7A_PIN_RESET (D12)
Jeej 12:beabd59e0c35 40 #define DEBUG_LED (D13) // LED1
Jeej 12:beabd59e0c35 41 #else
Jeej 12:beabd59e0c35 42 #error "Please choose or add the right platform."
Jeej 12:beabd59e0c35 43 #endif
Jeej 0:82a60d86ab2e 44
Jeej 0:82a60d86ab2e 45 // callbacks structure
Jeej 0:82a60d86ab2e 46 const d7a_callbacks_t callbacks = {
Jeej 0:82a60d86ab2e 47 .write_file = NULL,
Jeej 0:82a60d86ab2e 48 .read_file = NULL,
Jeej 0:82a60d86ab2e 49 .notif_done = NULL,
Jeej 0:82a60d86ab2e 50 .unsolicited_msg = NULL,
Jeej 0:82a60d86ab2e 51 };
Jeej 0:82a60d86ab2e 52
Jeej 0:82a60d86ab2e 53 // Com configuration for the DASH7 shield
Jeej 0:82a60d86ab2e 54 const d7a_com_config_t shield_config = {
Jeej 12:beabd59e0c35 55 .tx = D7A_PIN_TX,
Jeej 12:beabd59e0c35 56 .rx = D7A_PIN_RX,
Jeej 12:beabd59e0c35 57 .rts = D7A_PIN_RTS,
Jeej 12:beabd59e0c35 58 .cts = D7A_PIN_CTS,
Jeej 0:82a60d86ab2e 59 };
Jeej 0:82a60d86ab2e 60
Jeej 0:82a60d86ab2e 61 // Semaphore for notifiying button presses
Jeej 0:82a60d86ab2e 62 Semaphore button_user(0);
Jeej 0:82a60d86ab2e 63
Jeej 0:82a60d86ab2e 64 // Interrupt Service Routine on button press.
Jeej 0:82a60d86ab2e 65 void button_push_isr( void )
Jeej 0:82a60d86ab2e 66 {
Jeej 0:82a60d86ab2e 67 button_user.release();
Jeej 0:82a60d86ab2e 68 }
Jeej 0:82a60d86ab2e 69
Jeej 7:5b8648784381 70 uint8_t check_parameter(const char* str, uint32_t param1, uint32_t param2)
Jeej 7:5b8648784381 71 {
Jeej 7:5b8648784381 72 PRINT("Checking %s ", str);
Jeej 7:5b8648784381 73 if (param1 != param2)
Jeej 7:5b8648784381 74 {
Jeej 7:5b8648784381 75 PRINT("Failed. (0x%08X != 0x%08X)\r\n", param1, param2);
Jeej 7:5b8648784381 76 return 1;
Jeej 7:5b8648784381 77 }
Jeej 7:5b8648784381 78 else
Jeej 7:5b8648784381 79 {
Jeej 7:5b8648784381 80 PRINT("OK. (0x%08X)\r\n", param1);
Jeej 7:5b8648784381 81 return 0;
Jeej 7:5b8648784381 82 }
Jeej 7:5b8648784381 83 }
Jeej 7:5b8648784381 84
Jeej 15:24434827c575 85 void print_check_rev(void)
Jeej 15:24434827c575 86 {
Jeej 17:05c9ec970a6d 87 PRINT("\r\n"
Jeej 17:05c9ec970a6d 88 "/!\\ Please, check that you are at the right commit in the mbed revision tree /!\\\r\n"
Jeej 17:05c9ec970a6d 89 );
Jeej 15:24434827c575 90 }
Jeej 15:24434827c575 91
Jeej 15:24434827c575 92 void print_check_hardware(void)
Jeej 15:24434827c575 93 {
Jeej 15:24434827c575 94 PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
Jeej 15:24434827c575 95 }
Jeej 15:24434827c575 96
Jeej 15:24434827c575 97 int32_t check_slack(cup_param_t* cup, cup_cfg_t* cup_cfg)
Jeej 15:24434827c575 98 {
Jeej 16:5a6168dbe7bb 99 //PRINT("key: %d data: %d code: %d src: %d\r\n", cup_cfg->key, cup->data_size, cup->code_size, cup_cfg->src_offset);
Jeej 15:24434827c575 100 PRINT("Checking CUP Slack... ");
Jeej 16:5a6168dbe7bb 101
Jeej 16:5a6168dbe7bb 102 int32_t data_size = (((cup->data_size/256)+1)*256);
Jeej 16:5a6168dbe7bb 103 int32_t cup_slack = cup_cfg->key - data_size;
Jeej 15:24434827c575 104
Jeej 15:24434827c575 105 if (cup_slack < 0)
Jeej 15:24434827c575 106 {
Jeej 15:24434827c575 107 PRINT("Failed. (%d bytes short)\r\n", -cup_slack);
Jeej 15:24434827c575 108 }
Jeej 15:24434827c575 109 else
Jeej 15:24434827c575 110 {
Jeej 15:24434827c575 111 PRINT("OK. (%d bytes)\r\n", cup_slack);
Jeej 15:24434827c575 112 }
Jeej 15:24434827c575 113
Jeej 15:24434827c575 114 return cup_slack;
Jeej 15:24434827c575 115 }
Jeej 15:24434827c575 116
Jeej 0:82a60d86ab2e 117 int main()
Jeej 0:82a60d86ab2e 118 {
Jeej 15:24434827c575 119 bool bootloader;
Jeej 0:82a60d86ab2e 120 d7a_revision_t rev;
Jeej 7:5b8648784381 121 cup_cfg_t cup_cfg;
Jeej 15:24434827c575 122 cup_param_t* cup = (cup_param_t*)&cup_modem;
Jeej 15:24434827c575 123 int32_t cup_slack;
Jeej 0:82a60d86ab2e 124
Jeej 0:82a60d86ab2e 125 // Start & initialize
Jeej 12:beabd59e0c35 126 DBG_OPEN(DEBUG_LED);
Jeej 13:c3324b26d473 127 PRINT("\r\n"
Jeej 13:c3324b26d473 128 "-----------------------------------\r\n"
Jeej 13:c3324b26d473 129 "---------- D7A WM Updater ---------\r\n"
Jeej 13:c3324b26d473 130 "-----------------------------------\r\n");
Jeej 0:82a60d86ab2e 131
Jeej 12:beabd59e0c35 132 #ifdef DEBUG_BUTTON
Jeej 12:beabd59e0c35 133 DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
Jeej 0:82a60d86ab2e 134 user_interrupt.attach(button_push_isr, IRQ_FALL, 200, true);
Jeej 12:beabd59e0c35 135 #endif
Jeej 0:82a60d86ab2e 136
Jeej 12:beabd59e0c35 137 d7a_open(&shield_config, D7A_PIN_RESET, &callbacks);
Jeej 0:82a60d86ab2e 138
Jeej 13:c3324b26d473 139 do
Jeej 0:82a60d86ab2e 140 {
Jeej 13:c3324b26d473 141 // Check modem revision
Jeej 13:c3324b26d473 142 D7A_READ(&rev, D7A_FID_FIRMWARE_VERSION, 0, sizeof(d7a_revision_t), NULL);
Jeej 13:c3324b26d473 143
Jeej 15:24434827c575 144 if (check_parameter("Manufacturer ID... ", rev.manufacturer_id, cup->mfg_id))
Jeej 13:c3324b26d473 145 {
Jeej 15:24434827c575 146 print_check_rev();
Jeej 13:c3324b26d473 147 break;
Jeej 13:c3324b26d473 148 }
Jeej 13:c3324b26d473 149
Jeej 15:24434827c575 150 if (check_parameter("Hardware version... ", rev.hw_version, cup->hw_id))
Jeej 13:c3324b26d473 151 {
Jeej 15:24434827c575 152 print_check_hardware();
Jeej 13:c3324b26d473 153 break;
Jeej 13:c3324b26d473 154 }
Jeej 13:c3324b26d473 155
Jeej 15:24434827c575 156 if (rev.device_id == BOOTLOADER_DEV_ID)
Jeej 13:c3324b26d473 157 {
Jeej 15:24434827c575 158 // Update bootloader
Jeej 16:5a6168dbe7bb 159 PRINT("\r\n"
Jeej 16:5a6168dbe7bb 160 "/!\\ This modem has a 4.7.x bootloader firmware. /!\\\r\n"
Jeej 16:5a6168dbe7bb 161 "/!\\ Step 2/2: Upgrading to full modem firmware. /!\\\r\n"
Jeej 16:5a6168dbe7bb 162 );
Jeej 15:24434827c575 163
Jeej 15:24434827c575 164 // Read CUP config with root permissions
Jeej 15:24434827c575 165 D7A_READ(&cup_cfg, cup->cfg_fid, 0, sizeof(cup_cfg_t), root_key);
Jeej 15:24434827c575 166
Jeej 15:24434827c575 167 cup_slack = check_slack(cup, &cup_cfg);
Jeej 15:24434827c575 168
Jeej 15:24434827c575 169 if (cup_slack < 0)
Jeej 15:24434827c575 170 {
Jeej 15:24434827c575 171 PRINT("/!\\ Not enough space for bootloader /!\\\r\n");
Jeej 15:24434827c575 172 break;
Jeej 15:24434827c575 173 }
Jeej 16:5a6168dbe7bb 174
Jeej 16:5a6168dbe7bb 175 cup_start_update(cup_slack);
Jeej 13:c3324b26d473 176 }
Jeej 15:24434827c575 177 else
Jeej 0:82a60d86ab2e 178 {
Jeej 15:24434827c575 179 uint32_t version_old = (rev.fw_version.major << 24) | (rev.fw_version.minor << 16) | rev.fw_version.patch;
Jeej 15:24434827c575 180 uint32_t version_new = (cup->fw_major << 24) | (cup->fw_minor << 16) | cup->fw_patch;
Jeej 15:24434827c575 181
Jeej 15:24434827c575 182 // Update modem
Jeej 15:24434827c575 183 if (check_parameter("Device ID... ", rev.device_id, cup->dev_id))
Jeej 15:24434827c575 184 {
Jeej 15:24434827c575 185 print_check_rev();
Jeej 15:24434827c575 186 break;
Jeej 15:24434827c575 187 }
Jeej 15:24434827c575 188
Jeej 15:24434827c575 189 if (check_parameter("Firmware id... ", rev.fw_version.id, cup->fw_id))
Jeej 15:24434827c575 190 {
Jeej 15:24434827c575 191 print_check_rev();
Jeej 15:24434827c575 192 break;
Jeej 15:24434827c575 193 }
Jeej 15:24434827c575 194
Jeej 15:24434827c575 195 if (version_old != version_new)
Jeej 15:24434827c575 196 {
Jeej 15:24434827c575 197 if (check_parameter("Firmware version major...", rev.fw_version.major, cup->target_fw_major))
Jeej 15:24434827c575 198 {
Jeej 15:24434827c575 199 print_check_rev();
Jeej 15:24434827c575 200 break;
Jeej 15:24434827c575 201 }
Jeej 17:05c9ec970a6d 202
Jeej 15:24434827c575 203 if (check_parameter("Firmware version minor...", rev.fw_version.minor, cup->target_fw_minor))
Jeej 15:24434827c575 204 {
Jeej 15:24434827c575 205 print_check_rev();
Jeej 15:24434827c575 206 break;
Jeej 17:05c9ec970a6d 207 }
Jeej 15:24434827c575 208 }
Jeej 15:24434827c575 209 else
Jeej 15:24434827c575 210 {
Jeej 15:24434827c575 211 PRINT("\r\nYour modem is up to date! (v%d.%d.%d)\r\n",
Jeej 15:24434827c575 212 rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
Jeej 15:24434827c575 213 break;
Jeej 15:24434827c575 214 }
Jeej 15:24434827c575 215
Jeej 15:24434827c575 216 // Read CUP config with root permissions
Jeej 15:24434827c575 217 D7A_READ(&cup_cfg, cup->cfg_fid, 0, sizeof(cup_cfg_t), root_key);
Jeej 15:24434827c575 218
Jeej 15:24434827c575 219 cup_slack = check_slack(cup, &cup_cfg);
Jeej 15:24434827c575 220
Jeej 15:24434827c575 221 if (cup_slack < 0)
Jeej 15:24434827c575 222 {
Jeej 16:5a6168dbe7bb 223 PRINT("\r\n"
Jeej 16:5a6168dbe7bb 224 "/!\\ Not enough space for full modem firmware binary /!\\\r\n"
Jeej 16:5a6168dbe7bb 225 "/!\\ Checking for bootloader firmware /!\\\r\n"
Jeej 16:5a6168dbe7bb 226 "\r\n"
Jeej 16:5a6168dbe7bb 227 );
Jeej 15:24434827c575 228
Jeej 15:24434827c575 229 cup_slack = check_slack((cup_param_t*)&cup_bootloader, &cup_cfg);
Jeej 15:24434827c575 230
Jeej 15:24434827c575 231 if (cup_slack < 0)
Jeej 15:24434827c575 232 {
Jeej 15:24434827c575 233 PRINT("/!\\ Not enough space for bootloader /!\\\r\n");
Jeej 15:24434827c575 234 break;
Jeej 15:24434827c575 235 }
Jeej 15:24434827c575 236
Jeej 15:24434827c575 237 bootloader = true;
Jeej 16:5a6168dbe7bb 238
Jeej 16:5a6168dbe7bb 239 PRINT("\r\n"
Jeej 16:5a6168dbe7bb 240 "/!\\ This update will be done in 2 steps. /!\\\r\n"
Jeej 16:5a6168dbe7bb 241 "/!\\ Step 1/2: Upgrading modem to bootloader. /!\\\r\n"
Jeej 16:5a6168dbe7bb 242 "/!\\ Do no push the reset button or turn off the board during upgrade. /!\\\r\n"
Jeej 16:5a6168dbe7bb 243 );
Jeej 15:24434827c575 244 }
Jeej 15:24434827c575 245 else
Jeej 15:24434827c575 246 {
Jeej 15:24434827c575 247 bootloader = false;
Jeej 15:24434827c575 248 }
Jeej 15:24434827c575 249
Jeej 15:24434827c575 250 if (version_old > version_new)
Jeej 15:24434827c575 251 {
Jeej 15:24434827c575 252 PRINT("/!\\ Your modem is at a more recent version (v%d.%d.%d)/!\\\r\n"
Jeej 15:24434827c575 253 "/!\\ Are you sure you want to downgrade to v%d.%d.%d ? /!\\\r\n",
Jeej 15:24434827c575 254 rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
Jeej 15:24434827c575 255 cup->fw_major, cup->fw_minor, cup->fw_patch);
Jeej 15:24434827c575 256 PRINT("PRESS USER BUTTON TO CONFIRM...\r\n");
Jeej 15:24434827c575 257 button_user.wait();
Jeej 15:24434827c575 258 PRINT("\r\nDowngrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n",
Jeej 15:24434827c575 259 rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
Jeej 15:24434827c575 260 cup->fw_major, cup->fw_minor, cup->fw_patch);
Jeej 15:24434827c575 261 #ifdef DEBUG_BUTTON
Jeej 15:24434827c575 262 PRINT("PRESS USER BUTTON TO START DOWNGRADE...\r\n");
Jeej 15:24434827c575 263 button_user.wait();
Jeej 15:24434827c575 264 #endif
Jeej 15:24434827c575 265 }
Jeej 15:24434827c575 266 else if (version_old != version_new)
Jeej 15:24434827c575 267 {
Jeej 15:24434827c575 268 PRINT("\r\nUpgrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n",
Jeej 15:24434827c575 269 rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
Jeej 15:24434827c575 270 cup->fw_major, cup->fw_minor, cup->fw_patch);
Jeej 15:24434827c575 271 #ifdef DEBUG_BUTTON
Jeej 15:24434827c575 272 PRINT("PRESS USER BUTTON TO START UPGRADE...\r\n");
Jeej 15:24434827c575 273 button_user.wait();
Jeej 15:24434827c575 274 #endif
Jeej 15:24434827c575 275 }
Jeej 15:24434827c575 276
Jeej 16:5a6168dbe7bb 277 cup_start_update(cup_slack, bootloader);
Jeej 12:beabd59e0c35 278 }
Jeej 13:c3324b26d473 279
Jeej 13:c3324b26d473 280 } while (0);
Jeej 0:82a60d86ab2e 281
Jeej 0:82a60d86ab2e 282 // Set main task to lowest priority
Jeej 0:82a60d86ab2e 283 osThreadSetPriority(osThreadGetId(), osPriorityIdle);
Jeej 0:82a60d86ab2e 284 while(true)
Jeej 0:82a60d86ab2e 285 {
Jeej 0:82a60d86ab2e 286 // Wait to avoid beeing stuck in loop
Jeej 0:82a60d86ab2e 287 Thread::wait(200);
Jeej 0:82a60d86ab2e 288 }
Jeej 0:82a60d86ab2e 289 }