Just4Trionic - CAN and BDM FLASH programmer for Saab cars

Dependencies:   mbed

Revision:
6:2fbcbebed28c
Parent:
5:1775b4b13232
--- a/t7can.cpp	Sat Apr 25 17:07:08 2015 +0000
+++ b/t7can.cpp	Sat Apr 23 18:31:40 2016 +0000
@@ -42,18 +42,26 @@
 void t7_can_show_help();
 void t7_can_show_full_help();
 
-void t7_can() {
+// private variables
+bool ibus = false;
+bool pbus = false;
+
+void t7_can()
+{
     // Start the CAN bus system
     // Note that at the moment this is only for T7 ECUs at 500 kbits
     t7_can_show_help();
 
     char data[8];
+    ibus = false;
+    pbus = false;
     printf("Trying to listen to CAN I-Bus (47619 Bit/s)...\r\n");
     can_configure(2, 47619, 1);
     if (can_wait_timeout(T7ANYMSG, data, 8, T7MESSAGETIMEOUT)) {
         printf("Connected to Saab I-Bus\r\n");
         printf("Switching to I-Bus active mode\r\n");
         can_configure(2, 47619, 0);
+        ibus = true;
     } else {
         printf("I did not receive any I-Bus messages\r\n");
         printf("Trying to listen to  CAN P-Bus (500 kBit/s)...\r\n");
@@ -62,12 +70,14 @@
             printf("Connected to Saab P-Bus\r\n");
             printf("Switching to P-Bus active mode\r\n");
             can_configure(2, 500000, 0);
+            pbus = true;
         } else {
             printf("I did not receive any P-Bus messages\r\n");
             printf("Switching to P-Bus active mode\r\n");
             can_configure(2, 500000, 0);
             if (can_wait_timeout(T7ANYMSG, data, 8, T7CONNECTTIMEOUT)) {
                 printf("Connected to Saab P-Bus\r\n");
+                pbus = true;
                 //can_active();
             } else {
                 printf("FAILED to connect!\r\n");
@@ -153,108 +163,156 @@
 
     @return                    command flag (success / failure)
 */
-uint8_t execute_t7_cmd() {
+uint8_t execute_t7_cmd()
+{
 
-    char data[8];
 //    uint8_t cmd_length = strlen(cmd_buffer);
     // command groups
     switch (*cmd_buffer) {
 //            CHECK_ARGLENGTH(0);
-            // Get the Symbol Table
+        // Initialise a T7 CAN session
         case 'i' :
             if (t7_initialise()) {
-                printf("Trionic 7 Connection OK\r\n");
+                printf("SUCCESS! Trionic 7 CAN Connection OK.\r\n");
                 return TERM_OK;
             } else {
-                printf("Trionic 7 Connection Failed\r\n");
+                printf("FAILURE! No CAN connection to Trionic 7.\\r\n");
                 return TERM_ERR;
             }
-//            return t7_initialise()
-//                   ? TERM_OK : TERM_ERR;
+        // Get security clearance for T7 CAN session
         case 'a' :
-        case 'A' :
             if (t7_authenticate()) {
-                printf("Security Key Accepted\r\n");
+                printf("SUCCESS! Security Key Accepted.\r\n");
                 return TERM_OK;
             } else {
-                printf("Security Key Failed\r\n");
+                printf("FAILURE! Unable to obtain a Security Key.\\r\n");
                 return TERM_ERR;
             }
-//            return t7_authenticate()
-//                   ? TERM_OK : TERM_ERR;
-
-// Erase the FLASH chips
+        // Erase the FLASH chips
         case 'e':
-        case 'E':
             return t7_erase()
                    ? TERM_OK : TERM_ERR;
-// DUMP the T5 ECU BIN file stored in the FLASH chips
+        // DUMP the T5 ECU BIN file stored in the FLASH chips
         case 'D':
             if (!t7_authenticate()) {
                 if (!t7_initialise()) {
-                    printf("Trionic 7 Connection Failed\r\n");
+                    printf("FAILURE! No CAN connection to Trionic 7.\r\n");
                     return TERM_ERR;
                 }
                 if (!t7_authenticate()) {
-                    printf("Security Key Failed\r\n");
+                    printf("FAILURE! Unable to obtain a Security Key.\r\n");
                     return TERM_ERR;
                 }
             }
         case 'd':
-            return t7_dump()
+            return t7_dump(pbus)
                    ? TERM_OK : TERM_ERR;
 // Send a FLASH update file to the T5 ECU
-        case 'F':
+        case 'F': {
+            FILE *fp = t7_file_open("/local/modified.bin");    // Open "modified.bin" on the local file system
+            if (!fp) {
+                printf("FAILURE! Unable to find the BIN file \"MODIFIED.BIN\"\r\n");
+                return TERM_ERR;
+            }
             if (!t7_authenticate()) {
                 if (!t7_initialise()) {
-                    printf("Trionic 7 Connection Failed\r\n");
+                    printf("FAILURE! No CAN connection to Trionic 7.\r\n");
+                    fclose(fp);
                     return TERM_ERR;
                 }
                 if (!t7_authenticate()) {
-                    printf("Security Key Failed\r\n");
+                    printf("FAILURE! Unable to obtain a Security Key.\r\n");
+                    fclose(fp);
                     return TERM_ERR;
                 }
             }
             if (!t7_erase()) {
-                printf("Could not Erase FLASH!\r\n");
+                printf("FAILURE: Unable to Erase FLASH!\r\n");
+                fclose(fp);
+                return TERM_ERR;
+            }
+            bool result = t7_flash(fp, pbus);
+            fclose(fp);
+            return result ? TERM_OK : TERM_ERR;
+        }
+        case 'f': {
+            FILE *fp = t7_file_open("/local/modified.bin");    // Open "modified.bin" on the local file system
+            if (!fp) {
+                printf("FAILURE! Unable to find the BIN file \"MODIFIED.BIN\"\r\n");
+                return TERM_ERR;
+            }
+            bool result = t7_flash(fp, pbus);
+            fclose(fp);
+            return result ? TERM_OK : TERM_ERR;
+        }
+// Recovery FLASHes the entire BIN file and using 'safe' but slow 4 byte at a time transfers
+        case 'R': {
+            FILE *fp = t7_file_open("/local/modified.bin");    // Open "modified.bin" on the local file system
+            if (!fp) {
+                printf("FAILURE! Unable to find the BIN file \"MODIFIED.BIN\"\r\n");
                 return TERM_ERR;
             }
-        case 'f':
-            return t7_flash()
-                   ? TERM_OK : TERM_ERR;
+            if (!t7_authenticate()) {
+                if (!t7_initialise()) {
+                    printf("FAILURE! No CAN connection to Trionic 7.\r\n");
+                    fclose(fp);
+                    return TERM_ERR;
+                }
+                if (!t7_authenticate()) {
+                    printf("FAILURE! Unable to obtain a Security Key.\r\n");
+                    fclose(fp);
+                    return TERM_ERR;
+                }
+            }
+            if (!t7_erase()) {
+                printf("FAILURE: Unable to Erase FLASH!\r\n");
+                fclose(fp);
+                return TERM_ERR;
+            }
+            bool result = t7_recover(fp);
+            fclose(fp);
+            return result ? TERM_OK : TERM_ERR;
+        }
 // Try to connect to CAN I-BUS
-        case 'I' :
+        case 'I' : {
+            char data[8];
             printf("Trying to open CAN I-Bus (47619 Bit/s)...\r\n");
+            ibus = true;
+            pbus = false;
             can_close();
             //can_monitor();
             can_set_speed(47619);
             can_open();
             if (can_wait_timeout(T7ANYMSG, data, 8, T7CONNECTTIMEOUT)) {
-                printf("Connected to Saab I-Bus\r\n");
+                printf("SUCCESS! Connected to Saab I-Bus.\r\n");
                 //can_active();
                 return TERM_OK;
             } else {
                 printf("I did not receive any I-Bus messages\r\n");
-                printf("FAILED to connect!\r\n");
+                printf("FAILURE! Unable to connect Saab I-Bus.\r\n");
                 return TERM_ERR;
             }
+        }
 // Try to connect to CAN P-BUS
-        case 'P' :
+        case 'P' : {
+            char data[8];
             printf("Trying to open CAN P-Bus (500 kBit/s)...\r\n");
+            ibus = false;
+            pbus = true;
             can_close();
             //can_monitor();
             can_set_speed(500000);
             can_open();
             if (can_wait_timeout(T7ANYMSG, data, 8, T7CONNECTTIMEOUT)) {
-                printf("Connected to Saab P-Bus\r\n");
+                printf("SUCCESS! Connected to Saab P-Bus.\r\n");
                 //can_active();
                 return TERM_OK;
             } else {
                 printf("I did not receive any P-Bus messages\r\n");
-                printf("FAILED to connect!\r\n");
+                printf("FAILURE! Unable to connect Saab P-Bus.\r\n");
                 return TERM_ERR;
             }
-
+        }
 // Print help
         case 'h':
             t7_can_show_help();
@@ -278,11 +336,13 @@
 // inputs:    none
 // return:    none
 //
-void t7_can_show_help() {
+void t7_can_show_help()
+{
     printf("Trionic 7 Command Menu\r\n");
     printf("======================\r\n");
     printf("D - DUMP the T7 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
     printf("F - FLASH the update file 'MODIFIED.BIN' to the T7\r\n");
+    printf("R - Recovery FLASH T7 with update file 'MODIFIED.BIN'\r\n");
     printf("\r\n");
     printf("I - Try to open CAN I-Bus (47619 Bit/s)\r\n");
     printf("P - Try to open CAN P-Bus (500 kBit/s)\r\n");
@@ -301,11 +361,13 @@
 // inputs:    none
 // return:    none
 //
-void t7_can_show_full_help() {
+void t7_can_show_full_help()
+{
     printf("Trionic 7 Command Menu\r\n");
     printf("======================\r\n");
     printf("D - DUMP the T7 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
     printf("F - FLASH the update file 'MODIFIED.BIN' to the T7\r\n");
+    printf("R - Recovery FLASH T7 with update file 'MODIFIED.BIN'\r\n");
     printf("\r\n");
     printf("I - Try to open CAN I-Bus (47619 Bit/s)\r\n");
     printf("P - Try to open CAN P-Bus (500 kBit/s)\r\n");