Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
23:365775929293
Parent:
22:f2b01e5e087e
--- a/main.cpp	Mon Sep 18 13:38:40 2017 +0000
+++ b/main.cpp	Mon Sep 18 14:47:43 2017 +0000
@@ -1,9 +1,12 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "d7a.h"
+#include "dbg.h"
+#include "cup.h"
 #include "DebouncedInterrupt.h"
-#include "modem_ref_helper.h"
-#include "modem_callbacks.h"
-#include "bin.h"
-#include "cup.h"
 
+// File IDs
+#define D7A_FID_FIRMWARE_VERSION        (2)
 
 // To udate your firmware:
 // - Specify your root key
@@ -17,9 +20,47 @@
 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 )
@@ -54,13 +95,13 @@
     PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
 }
 
-int32_t check_slack(uint32_t cup_max_size, uint32_t cup_data_size)
+int32_t check_slack(cup_param_t* cup, cup_cfg_t* cup_cfg)
 {
     //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_max_size - cup_data_size;
+    int32_t cup_slack = cup_cfg->key - cup->data_size;
     
     cup_slack = ((cup_slack/256)*256);
     
@@ -76,50 +117,13 @@
     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;
-    revision_t rev;
+    d7a_revision_t rev;
+    cup_cfg_t cup_cfg;
     cup_param_t* cup = (cup_param_t*)&cup_modem;
-    int32_t cup_slack = 0;
+    int32_t cup_slack;
     
     // Start & initialize
     DBG_OPEN(DEBUG_LED);
@@ -133,15 +137,12 @@
     user_interrupt.attach(button_push_isr, IRQ_FALL, 200, true);
 #endif
 
-    modem_helper_open(&callbacks);
-    
-    g_main_id = modem_get_id(my_main_callback);
+    d7a_open(&shield_config, D7A_PIN_RESET, &callbacks);
     
     do
     {
         // Check modem revision
-        modem_read_file(D7A_FID_FIRMWARE_VERSION, &rev, 0, sizeof(revision_t), g_main_id);
-        modem_ready.wait();
+        D7A_READ(&rev, D7A_FID_FIRMWARE_VERSION, 0, sizeof(d7a_revision_t), NULL);
         
         if (check_parameter("Manufacturer ID...       ", rev.manufacturer_id, cup->mfg_id))
         {
@@ -149,7 +150,7 @@
             break;
         }
         
-        if (check_parameter("Hardware version...      ", rev.hw_version, cup->hw_id))
+        if (check_parameter("Hardware version...      ", rev.hw_version&0x00FFFFFF, cup->hw_id&0x00FFFFFF))
         {
             print_check_hardware();
             break;
@@ -163,7 +164,10 @@
                   "/!\\ Step 2/2: Upgrading to full modem firmware. /!\\\r\n"
                   );
             
-            cup_slack = check_slack(rev.cup_max_size, cup->data_size);
+            // 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);
             
             if (cup_slack < 0)
             {
@@ -185,7 +189,7 @@
                 break;
             }
             
-            if (check_parameter("Firmware id...           ", rev.fw_version.id&0x7F, cup->fw_id))
+            if (check_parameter("Firmware id...           ", rev.fw_version.id, cup->fw_id))
             {
                 print_check_rev();
                 break;
@@ -212,7 +216,10 @@
                 break;
             }
             
-            cup_slack = check_slack(rev.cup_max_size, cup->data_size);
+            // 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);
             
             if (cup_slack < 0)
             {
@@ -221,8 +228,8 @@
                       "/!\\ Checking for bootloader firmware                /!\\\r\n"
                       "\r\n"
                 );
-                                
-                cup_slack = check_slack(rev.cup_max_size, ((cup_param_t*)&cup_bootloader)->data_size);
+                
+                cup_slack = check_slack((cup_param_t*)&cup_bootloader, &cup_cfg);
                 
                 if (cup_slack < 0)
                 {