Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
23:365775929293
Parent:
22:f2b01e5e087e
--- a/cup.cpp	Mon Sep 18 13:38:40 2017 +0000
+++ b/cup.cpp	Mon Sep 18 14:47:43 2017 +0000
@@ -1,10 +1,13 @@
+#include "mbed.h"
 #include "cup.h"
 #include "bin.h"
+#include "crc.h"
+#include "d7a.h"
+#include "dbg.h"
 
 
 uint8_t const modem_data[CUP_DATA_SIZE] = CUP_DATA;
 uint8_t const bootloader_data[BOOTLOADER_DATA_SIZE] = BOOTLOADER_DATA;
-Semaphore modem_cup_ready(0);
 
 cup_param_t const cup_modem = {
     .data            = (uint8_t*)modem_data,
@@ -50,28 +53,6 @@
     .target_fw_minor = BOOTLOADER_TARGET_FW_MINOR,
 };
 
-void my_cup_callback(uint8_t terminal, int8_t err, uint8_t id)
-{
-    (void)id;
-    
-    if (terminal)
-    {
-        if (err)
-        {
-            PRINT("Done err %d\n", err);
-            FLUSH();
-            while(1);
-        }
-        modem_cup_ready.release();
-    }
-    else if (err)
-    {
-        PRINT("Got err %d\n", err);
-        FLUSH();
-        while(1);
-    }
-}
-
 void cup_start_update(uint32_t offset, bool bootloader)
 {
     cup_cfg_t cfg = {
@@ -87,8 +68,6 @@
     Timer tim;
     int32_t rem;
     
-    uint8_t id = modem_get_id(my_cup_callback);
-    
     if (bootloader)
     {
         PRINT("Uploading Bootloader\r\n");
@@ -101,10 +80,11 @@
     }
 
     rem = cup->data_size;
-        
+    
+    //d7a_stop();
+    
     // Start CUP
-    modem_write_file_root(cup->cfg_fid, (uint8_t*)&cfg, 0, 4, root_key, id);
-    modem_cup_ready.wait();
+    D7A_WRITE((uint8_t*)&cfg, cup->cfg_fid, 0, 4, root_key);
         
     // Upload file
     PRINT("Uploading %d bytes to CUP file. (offset %d)\r\n", cup->data_size, offset);
@@ -114,8 +94,7 @@
     while (rem > 0)
     {
         int32_t chunk = (rem > cup->local_mtu)? cup->local_mtu : rem;
-        modem_write_file(cup->code_fid, &(cup->data[fof]), fof + offset, chunk, id);
-        modem_cup_ready.wait();
+        D7A_WRITE(&(cup->data[fof]), cup->code_fid, fof + offset, chunk, NULL);
         rem -= chunk;
         fof += chunk;
         
@@ -134,8 +113,7 @@
     PRINT("CUP: %d bytes written in %.2f sec (%.2f kB/s)\r\n", cup->data_size, time_s, (cup->data_size/time_s)/1024.0);
         
     // Force PFLASH-cache flushing
-    modem_flush_file_root(cup->code_fid, root_key, id);
-    modem_cup_ready.wait();
+    D7A_FLUSH(cup->code_fid, root_key);
         
     // Send Upgrade command
     cfg.cmd = 0xC0D5;
@@ -143,9 +121,25 @@
     cfg.src_offset = offset;
     cfg.signature = cup->signature;
         
-    modem_write_file_root(cup->cfg_fid, (uint8_t*)&cfg, 0, 12, root_key, id);
-    modem_cup_ready.wait();
+    D7A_WRITE((uint8_t*)&cfg, cup->cfg_fid, 0, 12, root_key);
     
     PRINT("Waiting self reboot...\r\n");
+    
+    d7a_wait_ready();
+    
+    
+    D7A_READ(&cfg, cup->cfg_fid, 0, 2, root_key);
+    
+    if (cfg.cmd)
+    {
+        PRINT("/!\\ CUP Error 0x%04X /!\\\r\n", cfg.cmd);
+    }
+    else
+    
+    {
+        PRINT("CUP OK\r\nResetting...\r\n\r\n");
+        FLUSH();
+        NVIC_SystemReset();
+    }
 }
     
\ No newline at end of file