Just4Trionic - CAN and BDM FLASH programmer for Saab cars

Dependencies:   mbed

Revision:
4:682d96ff6d79
Parent:
3:92dae9083c83
Child:
5:1775b4b13232
--- a/t7can.cpp	Tue Jun 07 12:23:28 2011 +0000
+++ b/t7can.cpp	Wed Sep 11 11:55:51 2013 +0000
@@ -1,7 +1,7 @@
 /*******************************************************************************
 
 trionic7.cpp - CAN Bus functions for Just4Trionic by Just4pLeisure
-(c) 2011 by Sophie Dexter
+(c) 2011, 2012 by Sophie Dexter
 
 This C++ module provides functions for reading and writing the FLASH chips and
 SRAM in Trionic7 ECUs. (Writing the adaption data back to SRAM not done yet).
@@ -44,23 +44,62 @@
 
 void t7_can() {
     // Start the CAN bus system
-    // Note that at the moment this is only for T5 ECUs at 615 kbits
-    can_open();
-    can_set_speed(500000);
-
+    // Note that at the moment this is only for T7 ECUs at 500 kbits
     t7_can_show_help();
 
-    if (t7_initialise())
-        printf("Trionic 7 Connection OK\r\n");
-    else
-        printf("Trionic 7 Connection Failed\r\n");
+    char data[8];
+    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);
+    } 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");
+        can_configure(2, 500000, 1);
+        if (can_wait_timeout(T7ANYMSG, data, 8, T7MESSAGETIMEOUT)) {
+            printf("Connected to Saab P-Bus\r\n");
+            printf("Switching to P-Bus active mode\r\n");
+            can_configure(2, 500000, 0);
+        } 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");
+                //can_active();
+            } else {
+                printf("FAILED to connect!\r\n");
+                led4 = 1;
+            }
+        }
+    }
+
+//    if (t7_initialise())
+//        printf("Connected to Saab I-Bus\r\n");
+//    else {
+//        printf("Opening CAN channel to Saab P-Bus (500 kBit/s)...\r\n");
+//        can_set_speed(500000);
+//        if (t7_initialise())
+//            printf("Connected to Saab P-Bus\r\n");
+//        else {
+//            printf("FAILED to connect!\r\n");
+//            led4 = 1;
+//        }
+//    }
+
+//    if (t7_initialise())
+//        printf("Trionic 7 Connection OK\r\n");
+//    else
+//        printf("Trionic 7 Connection Failed\r\n");
 //    t7_authenticate();
-    if (t7_authenticate())
-        printf("Security Key Accepted\r\n");
-    else
-        printf("Security Key Failed\r\n");
+//    if (t7_authenticate())
+//        printf("Security Key Accepted\r\n");
+//    else
+//        printf("Security Key Failed\r\n");
 
-    // main loop
+// main loop
     *cmd_buffer = '\0';
     char ret;
     char rx_char;
@@ -116,40 +155,107 @@
 */
 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
         case 'i' :
-        case 'I' :
-            return t7_initialise()
-                   ? TERM_OK : TERM_ERR;
+            if (t7_initialise()) {
+                printf("Trionic 7 Connection OK\r\n");
+                return TERM_OK;
+            } else {
+                printf("Trionic 7 Connection Failed\r\n");
+                return TERM_ERR;
+            }
+//            return t7_initialise()
+//                   ? TERM_OK : TERM_ERR;
         case 'a' :
         case 'A' :
-            return t7_authenticate()
-                   ? TERM_OK : TERM_ERR;
+            if (t7_authenticate()) {
+                printf("Security Key Accepted\r\n");
+                return TERM_OK;
+            } else {
+                printf("Security Key Failed\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");
+                    return TERM_ERR;
+                }
+                if (!t7_authenticate()) {
+                    printf("Security Key Failed\r\n");
+                    return TERM_ERR;
+                }
+            }
         case 'd':
-        case 'D':
             return t7_dump()
                    ? TERM_OK : TERM_ERR;
-            // Send a FLASH update file to the T5 ECU
+// Send a FLASH update file to the T5 ECU
+        case 'F':
+            if (!t7_authenticate()) {
+                if (!t7_initialise()) {
+                    printf("Trionic 7 Connection Failed\r\n");
+                    return TERM_ERR;
+                }
+                if (!t7_authenticate()) {
+                    printf("Security Key Failed\r\n");
+                    return TERM_ERR;
+                }
+            }
+            if (!t7_erase()) {
+                printf("Could not Erase FLASH!\r\n");
+                return TERM_ERR;
+            }
         case 'f':
-        case 'F':
             return t7_flash()
                    ? TERM_OK : TERM_ERR;
+// Try to connect to CAN I-BUS
+        case 'I' :
+            printf("Trying to open CAN I-Bus (47619 Bit/s)...\r\n");
+            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");
+                //can_active();
+                return TERM_OK;
+            } else {
+                printf("I did not receive any I-Bus messages\r\n");
+                printf("FAILED to connect!\r\n");
+                return TERM_ERR;
+            }
+// Try to connect to CAN P-BUS
+        case 'P' :
+            printf("Trying to open CAN P-Bus (500 kBit/s)...\r\n");
+            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");
+                //can_active();
+                return TERM_OK;
+            } else {
+                printf("I did not receive any P-Bus messages\r\n");
+                printf("FAILED to connect!\r\n");
+                return TERM_ERR;
+            }
 
-        case '3':
-
-            // Print help
+// Print help
         case 'h':
             t7_can_show_help();
             return TERM_OK;
@@ -160,7 +266,7 @@
             t7_can_show_help();
             break;
     }
-    // unknown command
+// unknown command
     return TERM_ERR;
 }
 
@@ -175,8 +281,11 @@
 void t7_can_show_help() {
     printf("Trionic 7 Command Menu\r\n");
     printf("======================\r\n");
-    printf("D - Read and DUMP T7 FLASH BIN file\r\n");
-    printf("F - FLASH the update file to the T7\r\n");
+    printf("D - DUMP the T7 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
+    printf("F - FLASH the update file 'MODIFIED.HEX' to the T7\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");
     printf("\r\n");
     printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
     printf("\r\n");
@@ -195,13 +304,18 @@
 void t7_can_show_full_help() {
     printf("Trionic 7 Command Menu\r\n");
     printf("======================\r\n");
-    printf("D - Read and DUMP T7 FLASH BIN file\r\n");
-    printf("F - FLASH the update file to the T7\r\n");
+    printf("D - DUMP the T7 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
+    printf("F - FLASH the update file 'MODIFIED.HEX' to the T7\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");
     printf("\r\n");
     printf("\r\n");
     printf("i - Send initialisation message to T7\r\n");
     printf("a - Send Authentication key to T7\r\n");
-    printf("d - Dump T7 Bin file\r\n");
+    printf("d - Dump T7 Bin file 'ORIGINAL.BIN'\r\n");
+    printf("e - Erase the FLASH in the T7 ECU\r\n");
+    printf("f - FLASH the file 'MODIFIED.BIN' to the T7 ECU\r\n");
     printf("\r\n");
     printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
     printf("\r\n");