Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
22:f2b01e5e087e
Parent:
21:308466f40058
Child:
23:365775929293
Child:
24:3aa9b3b4a89c
--- a/main.cpp	Fri Sep 15 15:52:44 2017 +0000
+++ b/main.cpp	Mon Sep 18 13:38:40 2017 +0000
@@ -1,12 +1,9 @@
-#include "mbed.h"
-#include "rtos.h"
-#include "d7a.h"
-#include "dbg.h"
+#include "DebouncedInterrupt.h"
+#include "modem_ref_helper.h"
+#include "modem_callbacks.h"
+#include "bin.h"
 #include "cup.h"
-#include "DebouncedInterrupt.h"
 
-// File IDs
-#define D7A_FID_FIRMWARE_VERSION        (2)
 
 // To udate your firmware:
 // - Specify your root key
@@ -20,47 +17,9 @@
 uint8_t root_key[CUP_DEFAULT_KEY_SIZE] = CUP_DEFAULT_KEY;
 uint8_t root_key_size = CUP_DEFAULT_KEY_SIZE;
 
-// -----------------------------------------------
-// Hardware configuration
-// -----------------------------------------------
-#if defined(TARGET_STM32L152RE)
-    #define D7A_PIN_TX              (D10)
-    #define D7A_PIN_RX              (D2)
-    #define D7A_PIN_RTS             (D13)
-    #define D7A_PIN_CTS             (D9)
-    #define D7A_PIN_RESET           (A3)
-    #define DEBUG_LED               (NC)
-    #define DEBUG_BUTTON            (USER_BUTTON)
-#elif defined(TARGET_STM32L432KC)
-    #define D7A_PIN_TX              (D5)
-    #define D7A_PIN_RX              (D4)
-    #define D7A_PIN_RTS             (D11)
-    #define D7A_PIN_CTS             (D10)
-    #define D7A_PIN_RESET           (D12)
-    #define DEBUG_LED               (D13) // LED1
-    #define DEBUG_BUTTON            (D9)
-#else
-    #error "Please choose or add the right platform."
-#endif
-
-// callbacks structure
-const d7a_callbacks_t callbacks = {
-    .write_file = NULL,
-    .read_file = NULL,
-    .notif_done = NULL,
-    .unsolicited_msg = NULL,
-};
-
-// Com configuration for the DASH7 shield
-const d7a_com_config_t shield_config = {
-    .tx  = D7A_PIN_TX,
-    .rx  = D7A_PIN_RX,
-    .rts = D7A_PIN_RTS,
-    .cts = D7A_PIN_CTS,
-};
-
 // Semaphore for notifiying button presses
 Semaphore button_user(0);
+Semaphore modem_ready(0);
 
 // Interrupt Service Routine on button press.
 void button_push_isr( void )
@@ -95,13 +54,13 @@
     PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
 }
 
-int32_t check_slack(cup_param_t* cup, cup_cfg_t* cup_cfg)
+int32_t check_slack(uint32_t cup_max_size, uint32_t cup_data_size)
 {
     //PRINT("key: %d data: %d code: %d src: %d\r\n", cup_cfg->key, cup->data_size, cup->code_size, cup_cfg->src_offset);
     PRINT("Checking CUP Slack...              ");
     
     //int32_t data_size = (((cup->data_size/256)+1)*256);
-    int32_t cup_slack = cup_cfg->key - cup->data_size;
+    int32_t cup_slack = cup_max_size - cup_data_size;
     
     cup_slack = ((cup_slack/256)*256);
     
@@ -117,13 +76,50 @@
     return cup_slack;
 }
 
+modem_callbacks_t callbacks = {
+    .read       = my_read,
+    .write      = my_write,
+    .read_fprop = my_read_fprop,
+    .flush      = my_flush,
+    .remove     = my_delete,
+    .udata      = my_udata,
+    .lqual      = my_lqual,
+    .ldown      = my_ldown,
+    .reset      = my_reset,
+    .boot       = my_boot
+};
+
+// Callback for g_main_id User
+void my_main_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_ready.release();
+    }
+    else if (err)
+    {
+        PRINT("Got err %d\n", err);
+        FLUSH();
+        while(1);
+    }
+}
+
+uint8_t g_main_id;
+
 int main()
 {
     bool bootloader;
-    d7a_revision_t rev;
-    cup_cfg_t cup_cfg;
+    revision_t rev;
     cup_param_t* cup = (cup_param_t*)&cup_modem;
-    int32_t cup_slack;
+    int32_t cup_slack = 0;
     
     // Start & initialize
     DBG_OPEN(DEBUG_LED);
@@ -137,12 +133,15 @@
     user_interrupt.attach(button_push_isr, IRQ_FALL, 200, true);
 #endif
 
-    d7a_open(&shield_config, D7A_PIN_RESET, &callbacks);
+    modem_helper_open(&callbacks);
+    
+    g_main_id = modem_get_id(my_main_callback);
     
     do
     {
         // Check modem revision
-        D7A_READ(&rev, D7A_FID_FIRMWARE_VERSION, 0, sizeof(d7a_revision_t), NULL);
+        modem_read_file(D7A_FID_FIRMWARE_VERSION, &rev, 0, sizeof(revision_t), g_main_id);
+        modem_ready.wait();
         
         if (check_parameter("Manufacturer ID...       ", rev.manufacturer_id, cup->mfg_id))
         {
@@ -150,7 +149,7 @@
             break;
         }
         
-        if (check_parameter("Hardware version...      ", rev.hw_version&0x00FFFFFF, cup->hw_id))
+        if (check_parameter("Hardware version...      ", rev.hw_version, cup->hw_id))
         {
             print_check_hardware();
             break;
@@ -164,10 +163,7 @@
                   "/!\\ Step 2/2: Upgrading to full modem firmware. /!\\\r\n"
                   );
             
-            // Read CUP config with root permissions
-            D7A_READ(&cup_cfg, cup->cfg_fid, 0, sizeof(cup_cfg_t), root_key);
-            
-            cup_slack = check_slack(cup, &cup_cfg);
+            cup_slack = check_slack(rev.cup_max_size, cup->data_size);
             
             if (cup_slack < 0)
             {
@@ -189,7 +185,7 @@
                 break;
             }
             
-            if (check_parameter("Firmware id...           ", rev.fw_version.id, cup->fw_id))
+            if (check_parameter("Firmware id...           ", rev.fw_version.id&0x7F, cup->fw_id))
             {
                 print_check_rev();
                 break;
@@ -216,10 +212,7 @@
                 break;
             }
             
-            // Read CUP config with root permissions
-            D7A_READ(&cup_cfg, cup->cfg_fid, 0, sizeof(cup_cfg_t), root_key);
-            
-            cup_slack = check_slack(cup, &cup_cfg);
+            cup_slack = check_slack(rev.cup_max_size, cup->data_size);
             
             if (cup_slack < 0)
             {
@@ -228,8 +221,8 @@
                       "/!\\ Checking for bootloader firmware                /!\\\r\n"
                       "\r\n"
                 );
-                
-                cup_slack = check_slack((cup_param_t*)&cup_bootloader, &cup_cfg);
+                                
+                cup_slack = check_slack(rev.cup_max_size, ((cup_param_t*)&cup_bootloader)->data_size);
                 
                 if (cup_slack < 0)
                 {