Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
13:c3324b26d473
Parent:
12:beabd59e0c35
Child:
15:24434827c575
--- a/main.cpp	Fri Jan 06 14:16:49 2017 +0000
+++ b/main.cpp	Wed Jan 11 11:44:05 2017 +0000
@@ -90,9 +90,10 @@
     
     // Start & initialize
     DBG_OPEN(DEBUG_LED);
-    PRINT("\r\n-----------------------------------\r\n");
-    PRINT("---------- D7A WM Updater ---------\r\n");
-    PRINT("-----------------------------------\r\n");
+    PRINT("\r\n"
+          "-----------------------------------\r\n"
+          "---------- D7A WM Updater ---------\r\n"
+          "-----------------------------------\r\n");
 
 #ifdef DEBUG_BUTTON
     DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
@@ -100,74 +101,77 @@
 #endif
 
     d7a_open(&shield_config, D7A_PIN_RESET, &callbacks);
-
-    // Check modem revision
-    D7A_READ(&rev, D7A_FID_FIRMWARE_VERSION, 0, sizeof(d7a_revision_t), NULL);
     
-    // Read CUP config with root permissions
-    D7A_READ(&cup_cfg, CUP_CFG_FID, 0, sizeof(cup_cfg_t), root_key);
-    
-    uint8_t hw_errors = 0;
-    uint8_t fw_errors = 0;
-    uint8_t errors = 0;
-    
-    uint32_t version_old = (rev.fw_version.major << 24) | (rev.fw_version.minor << 16) | rev.fw_version.patch;
-    uint32_t version_new = (CUP_FW_MAJOR << 24) | (CUP_FW_MINOR << 16) | CUP_FW_PATCH;
-    
-    hw_errors += check_parameter("Manufacturer ID...       ", rev.manufacturer_id, CUP_MFG_ID);
-    hw_errors += check_parameter("Device ID...             ", rev.device_id, CUP_DEV_ID);
-    hw_errors += check_parameter("Hardware version...      ", rev.hw_version, CUP_HW_ID);
-    fw_errors += check_parameter("Firmware id...           ", rev.fw_version.id, CUP_FW_ID);
-    
-    PRINT("Checking CUP Slack...              ");
-    cup_offset = (CUP_CODE_SIZE > cup_cfg.src_offset)? CUP_CODE_SIZE - cup_cfg.src_offset : 0;
-    int32_t cup_slack = cup_cfg.key - cup_offset - CUP_DATA_SIZE;
-    if (cup_slack < 0)
-    {
-        PRINT("Failed. (%d bytes short)\r\n", -cup_slack);
-        fw_errors++;
-    }
-    else
-    {
-        PRINT("OK. (%d bytes)\r\n", cup_slack);
-    }
-    
-    if (version_old != version_new)
+    do
     {
-        fw_errors += check_parameter("Firmware version major...", rev.fw_version.major, CUP_TARGET_FW_MAJOR);
-        fw_errors += check_parameter("Firmware version minor...", rev.fw_version.minor, CUP_TARGET_FW_MINOR);
-    }
-    
-    PRINT("\r\n");
-            
-    if (hw_errors)
-    {
-        PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
-    }
+        // Check modem revision
+        D7A_READ(&rev, D7A_FID_FIRMWARE_VERSION, 0, sizeof(d7a_revision_t), NULL);
+        
+        uint8_t hw_errors = 0;
+        uint8_t fw_errors = 0;
+        
+        uint32_t version_old = (rev.fw_version.major << 24) | (rev.fw_version.minor << 16) | rev.fw_version.patch;
+        uint32_t version_new = (CUP_FW_MAJOR << 24) | (CUP_FW_MINOR << 16) | CUP_FW_PATCH;
+        
+        hw_errors += check_parameter("Manufacturer ID...       ", rev.manufacturer_id, CUP_MFG_ID);
+        hw_errors += check_parameter("Device ID...             ", rev.device_id, CUP_DEV_ID);
+        hw_errors += check_parameter("Hardware version...      ", rev.hw_version, CUP_HW_ID);
+        fw_errors += check_parameter("Firmware id...           ", rev.fw_version.id, CUP_FW_ID);
+        
+        if (version_old != version_new)
+        {
+            fw_errors += check_parameter("Firmware version major...", rev.fw_version.major, CUP_TARGET_FW_MAJOR);
+            fw_errors += check_parameter("Firmware version minor...", rev.fw_version.minor, CUP_TARGET_FW_MINOR);
+        }    
+        else
+        {
+            PRINT("\r\nYour modem is up to date! (v%d.%d.%d)\r\n",
+                    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
+            break;
+        }
+        
+        // Read CUP config with root permissions
+        D7A_READ(&cup_cfg, CUP_CFG_FID, 0, sizeof(cup_cfg_t), root_key);
+        
+        PRINT("Checking CUP Slack...              ");
+        cup_offset = (CUP_CODE_SIZE > cup_cfg.src_offset)? CUP_CODE_SIZE - cup_cfg.src_offset : 0;
+        int32_t cup_slack = cup_cfg.key - cup_offset - CUP_DATA_SIZE;
+        if (cup_slack < 0)
+        {
+            PRINT("Failed. (%d bytes short)\r\n", -cup_slack);
+            fw_errors++;
+        }
+        else
+        {
+            PRINT("OK. (%d bytes)\r\n", cup_slack);
+        }
+                
+        if (hw_errors)
+        {
+            PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
+            break;
+        }
+        
+        if (fw_errors)
+        {
+            PRINT("Please, check that you chose the right Revision in mbed revision tree.\r\n"
+                  "You are trying to upgrade from v%d.%d.%d to v%d.%d.%d.\r\n"
+                  "You can only upgrade to another patch %d.%d.x --> %d.%d.y\r\n"
+                  "or to the next minor %d.%d.x --> %d.%d.0\r\n",
+                    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
+                    CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH,
+                    rev.fw_version.major, rev.fw_version.minor,
+                    rev.fw_version.major, rev.fw_version.minor,
+                    rev.fw_version.major, rev.fw_version.minor,
+                    rev.fw_version.major, rev.fw_version.minor+1);
+        }
+                
+        if (hw_errors || fw_errors)
+        {
+            PRINT("\r\nSelect the right parameters and recompile.\r\n");
+            break;
+        }
     
-    if (fw_errors)
-    {
-        PRINT("Please, check that you chose the right Revision in mbed revision tree.\r\n"
-              "You are trying to upgrade from v%d.%d.%d to v%d.%d.%d.\r\n",
-                rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
-                CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH);
-        PRINT("You can only upgrade to another patch %d.%d.x --> %d.%d.y\r\n"
-              "or to the next minor %d.%d.x --> %d.%d.0\r\n",
-                rev.fw_version.major, rev.fw_version.minor,
-                rev.fw_version.major, rev.fw_version.minor,
-                rev.fw_version.major, rev.fw_version.minor,
-                rev.fw_version.major, rev.fw_version.minor+1);
-    }
-    
-    errors = hw_errors + fw_errors;
-    
-    if (errors)
-    {
-        PRINT("\r\nSelect the right parameters and recompile.\r\n");
-    }
-
-    else
-    {
         if (version_old > version_new)
         {
             PRINT("/!\\ Your modem is at a more recent version (v%d.%d.%d)/!\\\r\n"
@@ -183,30 +187,21 @@
             PRINT("PRESS USER BUTTON TO START DOWNGRADE...\r\n");
             button_user.wait();
 #endif
-            cup_start_update(cup_offset);
         }
-        else if (version_old == version_new)
+        else if (version_old != version_new)
         {
-            PRINT("Your modem is up to date! (v%d.%d.%d)\r\n", 
-                    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
-#ifdef DEBUG_BUTTON
-            PRINT("PRESS USER BUTTON TO RETRY UPGRADE...\r\n");
-            button_user.wait();
-            cup_start_update(cup_offset);
-#endif
-        }
-        else
-        {
-            PRINT("Upgrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n", 
+            PRINT("\r\nUpgrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n", 
                     rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
                     CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH);
 #ifdef DEBUG_BUTTON
             PRINT("PRESS USER BUTTON TO START UPGRADE...\r\n");
             button_user.wait();
 #endif
-            cup_start_update(cup_offset);
         }
-    }
+    
+        cup_start_update(cup_offset);
+    
+    } while (0);
     
     // Set main task to lowest priority
     osThreadSetPriority(osThreadGetId(), osPriorityIdle);