Dash7Board Code Upgrade Protocol demonstration code.
Dependencies: modem_ref_helper CRC
main.cpp@6:1bdff7d6689c, 2018-10-16 (annotated)
- Committer:
- Jeej
- Date:
- Tue Oct 16 15:24:09 2018 +0000
- Revision:
- 6:1bdff7d6689c
- Parent:
- 4:c97df2bc1731
- Child:
- 7:bfe920ee44f2
Activate all stack options.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jeej | 0:5589104abba0 | 1 | // @autor: jeremie@wizzilab.com |
Jeej | 0:5589104abba0 | 2 | // @date: 2017-12-14 |
Jeej | 0:5589104abba0 | 3 | |
Jeej | 0:5589104abba0 | 4 | #include "modem_ref_helper.h" |
Jeej | 0:5589104abba0 | 5 | #include "modem_callbacks.h" |
Jeej | 0:5589104abba0 | 6 | #include "files.h" |
Jeej | 0:5589104abba0 | 7 | #include "crc.h" |
Jeej | 0:5589104abba0 | 8 | |
Jeej | 0:5589104abba0 | 9 | #define CHUNK_SIZE (128) |
Jeej | 0:5589104abba0 | 10 | |
Jeej | 0:5589104abba0 | 11 | Semaphore modem_ready(0); |
Jeej | 0:5589104abba0 | 12 | Queue<touch_t, 8> g_file_modified; |
Jeej | 0:5589104abba0 | 13 | |
Jeej | 0:5589104abba0 | 14 | // CRC calculated on the incoming data stream |
Jeej | 0:5589104abba0 | 15 | extern uint32_t stream_crc; |
mikl_andre | 3:cfe26c2bb78b | 16 | extern int32_t last_end; |
Jeej | 0:5589104abba0 | 17 | |
Jeej | 4:c97df2bc1731 | 18 | const uint8_t default_root_key[] = DEFAULT_ROOT_KEY; |
Jeej | 0:5589104abba0 | 19 | |
Jeej | 0:5589104abba0 | 20 | modem_callbacks_t callbacks = { |
Jeej | 0:5589104abba0 | 21 | .read = my_read, |
Jeej | 0:5589104abba0 | 22 | .write = my_write, |
Jeej | 0:5589104abba0 | 23 | .read_fprop = my_read_fprop, |
Jeej | 0:5589104abba0 | 24 | .flush = my_flush, |
Jeej | 0:5589104abba0 | 25 | .remove = my_delete, |
Jeej | 0:5589104abba0 | 26 | .udata = my_udata, |
Jeej | 0:5589104abba0 | 27 | .lqual = my_lqual, |
Jeej | 0:5589104abba0 | 28 | .ldown = my_ldown, |
Jeej | 0:5589104abba0 | 29 | .reset = my_reset, |
Jeej | 1:dd4e18b267a1 | 30 | .boot = my_boot, |
Jeej | 1:dd4e18b267a1 | 31 | .busy = my_busy |
Jeej | 0:5589104abba0 | 32 | }; |
Jeej | 0:5589104abba0 | 33 | |
Jeej | 0:5589104abba0 | 34 | // Callback |
Jeej | 0:5589104abba0 | 35 | void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) |
Jeej | 0:5589104abba0 | 36 | { |
Jeej | 0:5589104abba0 | 37 | (void)id; |
Jeej | 0:5589104abba0 | 38 | |
Jeej | 4:c97df2bc1731 | 39 | if (ALP_ERR_NONE != err) |
Jeej | 4:c97df2bc1731 | 40 | { |
Jeej | 4:c97df2bc1731 | 41 | modem_print_error(ALP_ITF_TYPE_D7A, err); |
Jeej | 4:c97df2bc1731 | 42 | } |
Jeej | 0:5589104abba0 | 43 | |
Jeej | 0:5589104abba0 | 44 | if (terminal) |
Jeej | 0:5589104abba0 | 45 | { |
Jeej | 0:5589104abba0 | 46 | modem_ready.release(); |
Jeej | 0:5589104abba0 | 47 | } |
Jeej | 0:5589104abba0 | 48 | } |
Jeej | 0:5589104abba0 | 49 | |
Jeej | 0:5589104abba0 | 50 | void thread_file_modified() |
Jeej | 0:5589104abba0 | 51 | { |
Jeej | 0:5589104abba0 | 52 | touch_t* touch; |
Jeej | 0:5589104abba0 | 53 | osEvent evt; |
Jeej | 0:5589104abba0 | 54 | Timer tim; |
Jeej | 0:5589104abba0 | 55 | uint32_t total = 0; |
Jeej | 0:5589104abba0 | 56 | uint32_t next_chunk = 0; |
mikl_andre | 3:cfe26c2bb78b | 57 | uint32_t chunks=0; |
Jeej | 0:5589104abba0 | 58 | |
Jeej | 0:5589104abba0 | 59 | uint8_t id = modem_get_id(my_main_callback); |
Jeej | 0:5589104abba0 | 60 | |
Jeej | 0:5589104abba0 | 61 | PRINT("Ready\n"); |
Jeej | 0:5589104abba0 | 62 | |
Jeej | 0:5589104abba0 | 63 | while (true) |
Jeej | 0:5589104abba0 | 64 | { |
Jeej | 0:5589104abba0 | 65 | evt = g_file_modified.get(); |
Jeej | 0:5589104abba0 | 66 | touch = (evt.status == osEventMessage)? (touch_t*)evt.value.p : NULL; |
Jeej | 0:5589104abba0 | 67 | ASSERT(touch != NULL, "NULL touch pointer!\n"); |
Jeej | 0:5589104abba0 | 68 | |
Jeej | 0:5589104abba0 | 69 | switch (touch->fid) |
Jeej | 0:5589104abba0 | 70 | { |
Jeej | 0:5589104abba0 | 71 | case FID_APP_CUP_CFG: |
Jeej | 0:5589104abba0 | 72 | cup_cfg_t cup_cfg; |
Jeej | 0:5589104abba0 | 73 | |
Jeej | 0:5589104abba0 | 74 | ram_fs_read(FID_APP_CUP_CFG, 0, sizeof(cup_cfg_t), (uint8_t*)&cup_cfg); |
Jeej | 0:5589104abba0 | 75 | |
Jeej | 0:5589104abba0 | 76 | if (CUP_CMD_UPGRADE_FILE_START == cup_cfg.cmd) |
Jeej | 0:5589104abba0 | 77 | { |
Jeej | 0:5589104abba0 | 78 | PRINT("Enter upload mode. Watchdog %ds\n", cup_cfg.arch_nb); |
Jeej | 0:5589104abba0 | 79 | |
Jeej | 2:49acddddec25 | 80 | tim.stop(); |
Jeej | 0:5589104abba0 | 81 | tim.reset(); |
Jeej | 4:c97df2bc1731 | 82 | total = 0; |
Jeej | 4:c97df2bc1731 | 83 | next_chunk = 0; |
Jeej | 4:c97df2bc1731 | 84 | stream_crc = 0; |
Jeej | 4:c97df2bc1731 | 85 | chunks = 0; |
Jeej | 0:5589104abba0 | 86 | |
Jeej | 0:5589104abba0 | 87 | // Write to the modem CUP config file (as root) |
Jeej | 0:5589104abba0 | 88 | // to enter upload mode |
Jeej | 0:5589104abba0 | 89 | cup_cfg.cmd = CUP_CMD_UPGRADE_UPLOAD; |
Jeej | 0:5589104abba0 | 90 | modem_write_file_root(FID_CUP_CFG, (uint8_t*)&cup_cfg, 0, 4, (uint8_t*)default_root_key, id); |
Jeej | 0:5589104abba0 | 91 | modem_ready.wait(); |
Jeej | 0:5589104abba0 | 92 | } |
Jeej | 0:5589104abba0 | 93 | else if (CUP_CMD_UPGRADE_FILE_END == cup_cfg.cmd) |
Jeej | 0:5589104abba0 | 94 | { |
Jeej | 0:5589104abba0 | 95 | float time_s = tim.read(); |
Jeej | 0:5589104abba0 | 96 | uint32_t expected_size = cup_cfg.src_offset; |
Jeej | 0:5589104abba0 | 97 | uint32_t expected_crc = cup_cfg.signature; |
mikl_andre | 3:cfe26c2bb78b | 98 | |
mikl_andre | 3:cfe26c2bb78b | 99 | PRINT("\nDone. %d/%d bytes received in %d chunks in %.2f sec (%.2f kb/s)\r\n" |
Jeej | 0:5589104abba0 | 100 | "Expected CRC 0x%08X. Stream CRC 0x%08X.\r\n", |
mikl_andre | 3:cfe26c2bb78b | 101 | total, expected_size, chunks, time_s, (double)(total/time_s)/1024.0, |
Jeej | 0:5589104abba0 | 102 | expected_crc, stream_crc); |
Jeej | 0:5589104abba0 | 103 | |
Jeej | 0:5589104abba0 | 104 | tim.stop(); |
Jeej | 0:5589104abba0 | 105 | |
Jeej | 0:5589104abba0 | 106 | // Compare expected crc with received data crc |
Jeej | 0:5589104abba0 | 107 | if (stream_crc == expected_crc) |
Jeej | 0:5589104abba0 | 108 | { |
Jeej | 0:5589104abba0 | 109 | PRINT("Received complete binary.\n"); |
Jeej | 0:5589104abba0 | 110 | } |
Jeej | 0:5589104abba0 | 111 | else |
Jeej | 0:5589104abba0 | 112 | { |
Jeej | 0:5589104abba0 | 113 | PRINT("Missed or corrupted data in this session.\n"); |
Jeej | 0:5589104abba0 | 114 | } |
Jeej | 0:5589104abba0 | 115 | |
Jeej | 0:5589104abba0 | 116 | |
Jeej | 0:5589104abba0 | 117 | // Actual check is done here since we could have received some of the missed data in a previous session. |
Jeej | 0:5589104abba0 | 118 | |
Jeej | 0:5589104abba0 | 119 | /******************************/ |
Jeej | 0:5589104abba0 | 120 | /* Compute CRC on saved data. */ |
Jeej | 0:5589104abba0 | 121 | /******************************/ |
Jeej | 0:5589104abba0 | 122 | |
Jeej | 0:5589104abba0 | 123 | // If you have the full binary data, |
Jeej | 0:5589104abba0 | 124 | // You can use this function to compute CRC |
Jeej | 0:5589104abba0 | 125 | //binary_crc = crc32((char*)binary_data, expected_size); |
Jeej | 0:5589104abba0 | 126 | |
Jeej | 0:5589104abba0 | 127 | // Else just do a loop and a fast CRC on data chunks like in my_write |
Jeej | 0:5589104abba0 | 128 | } |
Jeej | 0:5589104abba0 | 129 | else |
Jeej | 0:5589104abba0 | 130 | { |
Jeej | 0:5589104abba0 | 131 | PRINT("CUP command 0x%04X\n", cup_cfg.cmd); |
Jeej | 0:5589104abba0 | 132 | } |
Jeej | 0:5589104abba0 | 133 | |
Jeej | 0:5589104abba0 | 134 | break; |
Jeej | 0:5589104abba0 | 135 | case FID_APP_CUP_CODE: |
Jeej | 0:5589104abba0 | 136 | // Chunk data should be saved in my_write callback |
mikl_andre | 3:cfe26c2bb78b | 137 | //PRINT("Got CUP code chunk length %d offset %d\n", touch->length, touch->offset); |
mikl_andre | 3:cfe26c2bb78b | 138 | chunks++; |
Jeej | 0:5589104abba0 | 139 | |
Jeej | 2:49acddddec25 | 140 | if (!next_chunk) |
Jeej | 2:49acddddec25 | 141 | { |
Jeej | 2:49acddddec25 | 142 | // First chunk, start timer |
Jeej | 2:49acddddec25 | 143 | tim.start(); |
mikl_andre | 3:cfe26c2bb78b | 144 | PRINT("Receiving CUP code chunks-> "); |
Jeej | 2:49acddddec25 | 145 | } |
Jeej | 2:49acddddec25 | 146 | |
mikl_andre | 3:cfe26c2bb78b | 147 | if (next_chunk < touch->offset) |
mikl_andre | 3:cfe26c2bb78b | 148 | { |
mikl_andre | 3:cfe26c2bb78b | 149 | PRINT("\nMissed %d chunk(s) at offset %d (got offset %d)\n", (touch->offset - next_chunk) / CHUNK_SIZE, next_chunk, touch->offset); |
mikl_andre | 3:cfe26c2bb78b | 150 | next_chunk = touch->offset + touch->length; |
mikl_andre | 3:cfe26c2bb78b | 151 | total += touch->length; |
mikl_andre | 3:cfe26c2bb78b | 152 | } |
mikl_andre | 3:cfe26c2bb78b | 153 | else if (next_chunk == touch->offset) |
Jeej | 0:5589104abba0 | 154 | { |
mikl_andre | 3:cfe26c2bb78b | 155 | next_chunk = touch->offset + touch->length; |
mikl_andre | 3:cfe26c2bb78b | 156 | total += touch->length; |
mikl_andre | 3:cfe26c2bb78b | 157 | PRINT("."); |
Jeej | 0:5589104abba0 | 158 | } |
mikl_andre | 3:cfe26c2bb78b | 159 | else |
mikl_andre | 3:cfe26c2bb78b | 160 | { |
mikl_andre | 3:cfe26c2bb78b | 161 | //PRINT("Repeat chunk(s) at offset %d\n", touch->offset); |
mikl_andre | 3:cfe26c2bb78b | 162 | } |
Jeej | 0:5589104abba0 | 163 | |
Jeej | 0:5589104abba0 | 164 | // Notify modem code file to reset upload watchdog |
Jeej | 0:5589104abba0 | 165 | // else the device will exit upload mode after the watchdog timeout |
Jeej | 0:5589104abba0 | 166 | modem_notify_file(FID_CUP_CODE, 0, 1, id); |
Jeej | 0:5589104abba0 | 167 | modem_ready.wait(); |
Jeej | 0:5589104abba0 | 168 | |
Jeej | 0:5589104abba0 | 169 | break; |
Jeej | 0:5589104abba0 | 170 | default: |
Jeej | 0:5589104abba0 | 171 | PRINT("TOUCH FID %d OFF %d LEN %d\n", touch->fid, touch->offset, touch->length); |
Jeej | 0:5589104abba0 | 172 | break; |
Jeej | 0:5589104abba0 | 173 | } |
Jeej | 0:5589104abba0 | 174 | |
Jeej | 0:5589104abba0 | 175 | FREE(touch); |
Jeej | 0:5589104abba0 | 176 | } |
Jeej | 0:5589104abba0 | 177 | } |
Jeej | 0:5589104abba0 | 178 | |
Jeej | 0:5589104abba0 | 179 | /*** Main function ------------------------------------------------------------- ***/ |
Jeej | 0:5589104abba0 | 180 | int main() { |
Jeej | 0:5589104abba0 | 181 | // Start & initialize |
Jeej | 0:5589104abba0 | 182 | #ifdef DEBUG_LED |
Jeej | 0:5589104abba0 | 183 | DBG_OPEN(DEBUG_LED); |
Jeej | 0:5589104abba0 | 184 | #else |
Jeej | 0:5589104abba0 | 185 | DBG_OPEN(NC); |
Jeej | 0:5589104abba0 | 186 | #endif |
Jeej | 0:5589104abba0 | 187 | PRINT("\n" |
Jeej | 0:5589104abba0 | 188 | "-----------------------------------------\n" |
Jeej | 0:5589104abba0 | 189 | "---------------- Demo CUP ---------------\n" |
Jeej | 0:5589104abba0 | 190 | "-----------------------------------------\n"); |
Jeej | 0:5589104abba0 | 191 | |
Jeej | 0:5589104abba0 | 192 | modem_helper_open(&callbacks); |
Jeej | 0:5589104abba0 | 193 | |
Jeej | 0:5589104abba0 | 194 | PRINT("--------------- APP infos ---------------\r\n"); |
Jeej | 0:5589104abba0 | 195 | PRINT(" - Manufacturer ID: %08X\r\n", f_rev.manufacturer_id); |
Jeej | 0:5589104abba0 | 196 | PRINT(" - Device ID: %08X\r\n", f_rev.device_id); |
Jeej | 0:5589104abba0 | 197 | PRINT(" - Hardware version: %08X\r\n", f_rev.hw_version); |
Jeej | 0:5589104abba0 | 198 | PRINT(" - Firmware version: v%d.%d.%d [%02X]\r\n", f_rev.fw_version.major, f_rev.fw_version.minor, f_rev.fw_version.patch, f_rev.fw_version.id); |
Jeej | 0:5589104abba0 | 199 | PRINT(" - CUP max size: %d\r\n", f_rev.cup_max_size); |
Jeej | 0:5589104abba0 | 200 | PRINT("-----------------------------------------\r\n"); |
Jeej | 0:5589104abba0 | 201 | |
Jeej | 0:5589104abba0 | 202 | uint8_t id = modem_get_id(my_main_callback); |
Jeej | 0:5589104abba0 | 203 | |
Jeej | 0:5589104abba0 | 204 | PRINT("Register Files\n"); |
Jeej | 0:5589104abba0 | 205 | modem_update_file(FID_HOST_REV, (alp_file_header_t*)&h_rev, (uint8_t*)&f_rev); |
Jeej | 0:5589104abba0 | 206 | modem_update_file(FID_APP_CUP_CFG, (alp_file_header_t*)&h_cup_cfg, (uint8_t*)&f_cup_cfg); |
Jeej | 0:5589104abba0 | 207 | |
Jeej | 0:5589104abba0 | 208 | // Declare the cup code file |
Jeej | 0:5589104abba0 | 209 | // It needs a special handling of its data in my_write callback since we want to keep them. |
Jeej | 0:5589104abba0 | 210 | // (can't be done with the current RAM file system) |
Jeej | 0:5589104abba0 | 211 | modem_declare_file(FID_APP_CUP_CODE, (alp_file_header_t*)&h_cup_code, true, id); |
Jeej | 0:5589104abba0 | 212 | modem_ready.wait(); |
Jeej | 0:5589104abba0 | 213 | |
Jeej | 1:dd4e18b267a1 | 214 | PRINT("Setup URCs\n"); |
Jeej | 1:dd4e18b267a1 | 215 | // Busy URC to know if modem enters CUP mode |
Jeej | 1:dd4e18b267a1 | 216 | modem_enable_urc(ALP_URC_TYPE_BUSY, 0, 0, true, id); |
Jeej | 1:dd4e18b267a1 | 217 | modem_ready.wait(); |
Jeej | 1:dd4e18b267a1 | 218 | |
Jeej | 0:5589104abba0 | 219 | PRINT("Start D7A Stack\n"); |
Jeej | 6:1bdff7d6689c | 220 | modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS | ALP_D7A_ISTAT_EOP, true, id); |
Jeej | 0:5589104abba0 | 221 | modem_ready.wait(); |
Jeej | 0:5589104abba0 | 222 | |
Jeej | 0:5589104abba0 | 223 | PRINT("Notify Modem Version\n"); |
Jeej | 0:5589104abba0 | 224 | modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id); |
Jeej | 0:5589104abba0 | 225 | modem_ready.wait(); |
Jeej | 0:5589104abba0 | 226 | |
Jeej | 0:5589104abba0 | 227 | PRINT("Notify Host Version\n"); |
Jeej | 4:c97df2bc1731 | 228 | modem_notify_host_rev(&f_rev, &h_rev, (uint8_t*)default_root_key); |
Jeej | 0:5589104abba0 | 229 | |
Jeej | 0:5589104abba0 | 230 | // id no longer needed |
Jeej | 0:5589104abba0 | 231 | modem_free_id(id); |
Jeej | 0:5589104abba0 | 232 | |
Jeej | 0:5589104abba0 | 233 | // Start file modified thread |
Jeej | 0:5589104abba0 | 234 | Thread th_file_modified(osPriorityNormal, 1024, NULL); |
Jeej | 0:5589104abba0 | 235 | osStatus status = th_file_modified.start(thread_file_modified); |
Jeej | 0:5589104abba0 | 236 | ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status); |
Jeej | 0:5589104abba0 | 237 | |
Jeej | 0:5589104abba0 | 238 | #ifdef DEBUG_LED |
Jeej | 0:5589104abba0 | 239 | DigitalOut my_led(DEBUG_LED); |
Jeej | 0:5589104abba0 | 240 | #endif |
Jeej | 0:5589104abba0 | 241 | |
Jeej | 0:5589104abba0 | 242 | // Set main task to lowest priority |
Jeej | 0:5589104abba0 | 243 | osThreadSetPriority(osThreadGetId(), osPriorityIdle); |
Jeej | 0:5589104abba0 | 244 | while(true) |
Jeej | 0:5589104abba0 | 245 | { |
Jeej | 0:5589104abba0 | 246 | Thread::wait(500); |
Jeej | 0:5589104abba0 | 247 | #ifdef DEBUG_LED |
Jeej | 0:5589104abba0 | 248 | my_led = !my_led; |
Jeej | 0:5589104abba0 | 249 | #endif |
Jeej | 0:5589104abba0 | 250 | } |
Jeej | 0:5589104abba0 | 251 | } |