Dash7Board Code Upgrade Protocol demonstration code.
Dependencies: modem_ref_helper CRC
main.cpp@7:bfe920ee44f2, 2019-08-20 (annotated)
- Committer:
- Jeej
- Date:
- Tue Aug 20 13:55:45 2019 +0000
- Revision:
- 7:bfe920ee44f2
- Parent:
- 6:1bdff7d6689c
- Child:
- 8:6b7d38139b43
WizziCom now uses DMA instead of interrupts.
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 | 7:bfe920ee44f2 | 71 | case FID_APP_CUP_CFG_BCAST: |
Jeej | 7:bfe920ee44f2 | 72 | cup_cfg_bcast_header_t cup_cfg; |
Jeej | 0:5589104abba0 | 73 | |
Jeej | 7:bfe920ee44f2 | 74 | ram_fs_read(FID_APP_CUP_CFG_BCAST, 0, sizeof(cup_cfg_bcast_header_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 | 7:bfe920ee44f2 | 78 | PRINT("Enter upload mode. Watchdog %ds\n", cup_cfg.to); |
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 | 7:bfe920ee44f2 | 89 | cup_cfg_t cup_start = { |
Jeej | 7:bfe920ee44f2 | 90 | .cmd = CUP_CMD_UPGRADE_UPLOAD, |
Jeej | 7:bfe920ee44f2 | 91 | .arch_nb = cup_cfg.to, |
Jeej | 7:bfe920ee44f2 | 92 | }; |
Jeej | 7:bfe920ee44f2 | 93 | modem_write_file_root(FID_CUP_CFG, (uint8_t*)&cup_start, 0, 4, (uint8_t*)default_root_key, id); |
Jeej | 7:bfe920ee44f2 | 94 | modem_ready.acquire(); |
Jeej | 0:5589104abba0 | 95 | } |
Jeej | 0:5589104abba0 | 96 | else if (CUP_CMD_UPGRADE_FILE_END == cup_cfg.cmd) |
Jeej | 0:5589104abba0 | 97 | { |
Jeej | 0:5589104abba0 | 98 | float time_s = tim.read(); |
Jeej | 7:bfe920ee44f2 | 99 | uint32_t expected_size = cup_cfg.len; |
Jeej | 7:bfe920ee44f2 | 100 | uint32_t expected_crc = cup_cfg.sig_new; |
mikl_andre | 3:cfe26c2bb78b | 101 | |
mikl_andre | 3:cfe26c2bb78b | 102 | PRINT("\nDone. %d/%d bytes received in %d chunks in %.2f sec (%.2f kb/s)\r\n" |
Jeej | 0:5589104abba0 | 103 | "Expected CRC 0x%08X. Stream CRC 0x%08X.\r\n", |
mikl_andre | 3:cfe26c2bb78b | 104 | total, expected_size, chunks, time_s, (double)(total/time_s)/1024.0, |
Jeej | 0:5589104abba0 | 105 | expected_crc, stream_crc); |
Jeej | 0:5589104abba0 | 106 | |
Jeej | 0:5589104abba0 | 107 | tim.stop(); |
Jeej | 0:5589104abba0 | 108 | |
Jeej | 0:5589104abba0 | 109 | // Compare expected crc with received data crc |
Jeej | 0:5589104abba0 | 110 | if (stream_crc == expected_crc) |
Jeej | 0:5589104abba0 | 111 | { |
Jeej | 0:5589104abba0 | 112 | PRINT("Received complete binary.\n"); |
Jeej | 0:5589104abba0 | 113 | } |
Jeej | 0:5589104abba0 | 114 | else |
Jeej | 0:5589104abba0 | 115 | { |
Jeej | 0:5589104abba0 | 116 | PRINT("Missed or corrupted data in this session.\n"); |
Jeej | 0:5589104abba0 | 117 | } |
Jeej | 0:5589104abba0 | 118 | |
Jeej | 0:5589104abba0 | 119 | |
Jeej | 0:5589104abba0 | 120 | // Actual check is done here since we could have received some of the missed data in a previous session. |
Jeej | 0:5589104abba0 | 121 | |
Jeej | 0:5589104abba0 | 122 | /******************************/ |
Jeej | 0:5589104abba0 | 123 | /* Compute CRC on saved data. */ |
Jeej | 0:5589104abba0 | 124 | /******************************/ |
Jeej | 0:5589104abba0 | 125 | |
Jeej | 0:5589104abba0 | 126 | // If you have the full binary data, |
Jeej | 0:5589104abba0 | 127 | // You can use this function to compute CRC |
Jeej | 0:5589104abba0 | 128 | //binary_crc = crc32((char*)binary_data, expected_size); |
Jeej | 0:5589104abba0 | 129 | |
Jeej | 0:5589104abba0 | 130 | // Else just do a loop and a fast CRC on data chunks like in my_write |
Jeej | 0:5589104abba0 | 131 | } |
Jeej | 0:5589104abba0 | 132 | else |
Jeej | 0:5589104abba0 | 133 | { |
Jeej | 0:5589104abba0 | 134 | PRINT("CUP command 0x%04X\n", cup_cfg.cmd); |
Jeej | 0:5589104abba0 | 135 | } |
Jeej | 0:5589104abba0 | 136 | |
Jeej | 0:5589104abba0 | 137 | break; |
Jeej | 0:5589104abba0 | 138 | case FID_APP_CUP_CODE: |
Jeej | 0:5589104abba0 | 139 | // Chunk data should be saved in my_write callback |
mikl_andre | 3:cfe26c2bb78b | 140 | //PRINT("Got CUP code chunk length %d offset %d\n", touch->length, touch->offset); |
mikl_andre | 3:cfe26c2bb78b | 141 | chunks++; |
Jeej | 0:5589104abba0 | 142 | |
Jeej | 2:49acddddec25 | 143 | if (!next_chunk) |
Jeej | 2:49acddddec25 | 144 | { |
Jeej | 2:49acddddec25 | 145 | // First chunk, start timer |
Jeej | 2:49acddddec25 | 146 | tim.start(); |
mikl_andre | 3:cfe26c2bb78b | 147 | PRINT("Receiving CUP code chunks-> "); |
Jeej | 2:49acddddec25 | 148 | } |
Jeej | 2:49acddddec25 | 149 | |
mikl_andre | 3:cfe26c2bb78b | 150 | if (next_chunk < touch->offset) |
mikl_andre | 3:cfe26c2bb78b | 151 | { |
mikl_andre | 3:cfe26c2bb78b | 152 | 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 | 153 | next_chunk = touch->offset + touch->length; |
mikl_andre | 3:cfe26c2bb78b | 154 | total += touch->length; |
mikl_andre | 3:cfe26c2bb78b | 155 | } |
mikl_andre | 3:cfe26c2bb78b | 156 | else if (next_chunk == touch->offset) |
Jeej | 0:5589104abba0 | 157 | { |
mikl_andre | 3:cfe26c2bb78b | 158 | next_chunk = touch->offset + touch->length; |
mikl_andre | 3:cfe26c2bb78b | 159 | total += touch->length; |
mikl_andre | 3:cfe26c2bb78b | 160 | PRINT("."); |
Jeej | 0:5589104abba0 | 161 | } |
mikl_andre | 3:cfe26c2bb78b | 162 | else |
mikl_andre | 3:cfe26c2bb78b | 163 | { |
mikl_andre | 3:cfe26c2bb78b | 164 | //PRINT("Repeat chunk(s) at offset %d\n", touch->offset); |
mikl_andre | 3:cfe26c2bb78b | 165 | } |
Jeej | 0:5589104abba0 | 166 | |
Jeej | 0:5589104abba0 | 167 | // Notify modem code file to reset upload watchdog |
Jeej | 0:5589104abba0 | 168 | // else the device will exit upload mode after the watchdog timeout |
Jeej | 0:5589104abba0 | 169 | modem_notify_file(FID_CUP_CODE, 0, 1, id); |
Jeej | 7:bfe920ee44f2 | 170 | modem_ready.acquire(); |
Jeej | 0:5589104abba0 | 171 | |
Jeej | 0:5589104abba0 | 172 | break; |
Jeej | 0:5589104abba0 | 173 | default: |
Jeej | 0:5589104abba0 | 174 | PRINT("TOUCH FID %d OFF %d LEN %d\n", touch->fid, touch->offset, touch->length); |
Jeej | 0:5589104abba0 | 175 | break; |
Jeej | 0:5589104abba0 | 176 | } |
Jeej | 0:5589104abba0 | 177 | |
Jeej | 0:5589104abba0 | 178 | FREE(touch); |
Jeej | 0:5589104abba0 | 179 | } |
Jeej | 0:5589104abba0 | 180 | } |
Jeej | 0:5589104abba0 | 181 | |
Jeej | 0:5589104abba0 | 182 | /*** Main function ------------------------------------------------------------- ***/ |
Jeej | 0:5589104abba0 | 183 | int main() { |
Jeej | 0:5589104abba0 | 184 | // Start & initialize |
Jeej | 0:5589104abba0 | 185 | #ifdef DEBUG_LED |
Jeej | 0:5589104abba0 | 186 | DBG_OPEN(DEBUG_LED); |
Jeej | 0:5589104abba0 | 187 | #else |
Jeej | 0:5589104abba0 | 188 | DBG_OPEN(NC); |
Jeej | 0:5589104abba0 | 189 | #endif |
Jeej | 0:5589104abba0 | 190 | PRINT("\n" |
Jeej | 0:5589104abba0 | 191 | "-----------------------------------------\n" |
Jeej | 0:5589104abba0 | 192 | "---------------- Demo CUP ---------------\n" |
Jeej | 0:5589104abba0 | 193 | "-----------------------------------------\n"); |
Jeej | 0:5589104abba0 | 194 | |
Jeej | 0:5589104abba0 | 195 | modem_helper_open(&callbacks); |
Jeej | 0:5589104abba0 | 196 | |
Jeej | 0:5589104abba0 | 197 | PRINT("--------------- APP infos ---------------\r\n"); |
Jeej | 0:5589104abba0 | 198 | PRINT(" - Manufacturer ID: %08X\r\n", f_rev.manufacturer_id); |
Jeej | 0:5589104abba0 | 199 | PRINT(" - Device ID: %08X\r\n", f_rev.device_id); |
Jeej | 0:5589104abba0 | 200 | PRINT(" - Hardware version: %08X\r\n", f_rev.hw_version); |
Jeej | 0:5589104abba0 | 201 | 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 | 202 | PRINT(" - CUP max size: %d\r\n", f_rev.cup_max_size); |
Jeej | 0:5589104abba0 | 203 | PRINT("-----------------------------------------\r\n"); |
Jeej | 0:5589104abba0 | 204 | |
Jeej | 0:5589104abba0 | 205 | uint8_t id = modem_get_id(my_main_callback); |
Jeej | 0:5589104abba0 | 206 | |
Jeej | 0:5589104abba0 | 207 | PRINT("Register Files\n"); |
Jeej | 0:5589104abba0 | 208 | modem_update_file(FID_HOST_REV, (alp_file_header_t*)&h_rev, (uint8_t*)&f_rev); |
Jeej | 7:bfe920ee44f2 | 209 | modem_update_file(FID_APP_CUP_CFG_BCAST, (alp_file_header_t*)&h_cup_cfg_bcast, (uint8_t*)&f_cup_cfg_bcast); |
Jeej | 0:5589104abba0 | 210 | |
Jeej | 0:5589104abba0 | 211 | // Declare the cup code file |
Jeej | 0:5589104abba0 | 212 | // It needs a special handling of its data in my_write callback since we want to keep them. |
Jeej | 0:5589104abba0 | 213 | // (can't be done with the current RAM file system) |
Jeej | 0:5589104abba0 | 214 | modem_declare_file(FID_APP_CUP_CODE, (alp_file_header_t*)&h_cup_code, true, id); |
Jeej | 7:bfe920ee44f2 | 215 | modem_ready.acquire(); |
Jeej | 0:5589104abba0 | 216 | |
Jeej | 1:dd4e18b267a1 | 217 | PRINT("Setup URCs\n"); |
Jeej | 1:dd4e18b267a1 | 218 | // Busy URC to know if modem enters CUP mode |
Jeej | 1:dd4e18b267a1 | 219 | modem_enable_urc(ALP_URC_TYPE_BUSY, 0, 0, true, id); |
Jeej | 7:bfe920ee44f2 | 220 | modem_ready.acquire(); |
Jeej | 1:dd4e18b267a1 | 221 | |
Jeej | 0:5589104abba0 | 222 | PRINT("Start D7A Stack\n"); |
Jeej | 6:1bdff7d6689c | 223 | modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS | ALP_D7A_ISTAT_EOP, true, id); |
Jeej | 7:bfe920ee44f2 | 224 | modem_ready.acquire(); |
Jeej | 0:5589104abba0 | 225 | |
Jeej | 0:5589104abba0 | 226 | PRINT("Notify Modem Version\n"); |
Jeej | 0:5589104abba0 | 227 | modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id); |
Jeej | 7:bfe920ee44f2 | 228 | modem_ready.acquire(); |
Jeej | 0:5589104abba0 | 229 | |
Jeej | 0:5589104abba0 | 230 | PRINT("Notify Host Version\n"); |
Jeej | 4:c97df2bc1731 | 231 | modem_notify_host_rev(&f_rev, &h_rev, (uint8_t*)default_root_key); |
Jeej | 0:5589104abba0 | 232 | |
Jeej | 0:5589104abba0 | 233 | // id no longer needed |
Jeej | 0:5589104abba0 | 234 | modem_free_id(id); |
Jeej | 0:5589104abba0 | 235 | |
Jeej | 0:5589104abba0 | 236 | // Start file modified thread |
Jeej | 0:5589104abba0 | 237 | Thread th_file_modified(osPriorityNormal, 1024, NULL); |
Jeej | 0:5589104abba0 | 238 | osStatus status = th_file_modified.start(thread_file_modified); |
Jeej | 0:5589104abba0 | 239 | ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status); |
Jeej | 0:5589104abba0 | 240 | |
Jeej | 0:5589104abba0 | 241 | #ifdef DEBUG_LED |
Jeej | 0:5589104abba0 | 242 | DigitalOut my_led(DEBUG_LED); |
Jeej | 0:5589104abba0 | 243 | #endif |
Jeej | 0:5589104abba0 | 244 | |
Jeej | 0:5589104abba0 | 245 | // Set main task to lowest priority |
Jeej | 7:bfe920ee44f2 | 246 | osThreadSetPriority(osThreadGetId(), osPriorityLow); |
Jeej | 0:5589104abba0 | 247 | while(true) |
Jeej | 0:5589104abba0 | 248 | { |
Jeej | 7:bfe920ee44f2 | 249 | ThisThread::sleep_for(500); |
Jeej | 0:5589104abba0 | 250 | #ifdef DEBUG_LED |
Jeej | 0:5589104abba0 | 251 | my_led = !my_led; |
Jeej | 0:5589104abba0 | 252 | #endif |
Jeej | 0:5589104abba0 | 253 | } |
Jeej | 0:5589104abba0 | 254 | } |