Just4Trionic - CAN and BDM FLASH programmer for Saab cars

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers t8can.cpp Source File

t8can.cpp

00001 /*******************************************************************************
00002 
00003 trionic8.cpp - CAN Bus functions for Just4Trionic by Just4pLeisure
00004 (c) 2011, 2012 by Sophie Dexter
00005 
00006 This C++ module provides functions for reading and writing the FLASH chips and
00007 SRAM in Trionic8 ECUs. (Writing the adaption data back to SRAM not done yet).
00008 
00009 Some functions need an additional 'bootloader' program to be sent to the T8 ECU
00010 before they can be used. These functions are: Identifying the T5 ECU type and
00011 FLASH chips, dumping the FLASH chips, erasing the FLASH chips, writing to the
00012 FLASH chips and calculating the FLASH chips' checksum.
00013 
00014 ********************************************************************************
00015 
00016 WARNING: Use at your own risk, sadly this software comes with no guarantees.
00017 This software is provided 'free' and in good faith, but the author does not
00018 accept liability for any damage arising from its use.
00019 
00020 *******************************************************************************/
00021 
00022 #include "t8can.h"
00023 
00024 // constants
00025 #define CMD_BUF_LENGTH      32              ///< command buffer size
00026 
00027 // static variables
00028 static char cmd_buffer[CMD_BUF_LENGTH];     ///< command string buffer
00029 
00030 //static uint32_t cmd_addr;                   ///< address (optional)
00031 //static uint32_t cmd_value;                  ///< value    (optional)
00032 //static uint32_t cmd_result;                 ///< result
00033 
00034 //static uint32_t flash_start = 0;
00035 
00036 // private functions
00037 uint8_t execute_t8_cmd();
00038 void t8_can_show_help();
00039 void t8_can_show_full_help();
00040 
00041 void t8_can()
00042 {
00043     // Start the CAN bus system
00044     // Note that at the moment this is only for T8 ECUs at 500 kbits
00045     t8_can_show_help();
00046 
00047     char data[8];
00048     printf("Trying to listen to  CAN P-Bus (500 kBit/s)...\r\n");
00049     can_configure(2, 500000, 1);
00050     if (can_wait_timeout(T8ANYMSG, data, 8, T8MESSAGETIMEOUT)) {
00051         printf("Connected to Saab P-Bus\r\n");
00052         printf("Switching to P-Bus active mode\r\n");
00053         can_configure(2, 500000, 0);
00054     } else {
00055         printf("I did not receive any P-Bus messages\r\n");
00056         printf("Switching to P-Bus active mode\r\n");
00057         can_configure(2, 500000, 0);
00058         if (can_wait_timeout(T8ANYMSG, data, 8, T8CONNECTTIMEOUT)) {
00059             printf("Connected to Saab P-Bus\r\n");
00060             //can_active();
00061         } else {
00062             printf("FAILED to connect!\r\n");
00063             led4 = 1;
00064         }
00065     }
00066 
00067     can_reset_filters();
00068     can_add_filter(2, 0x645);         //645h - CIM
00069     can_add_filter(2, 0x7E0);         //7E0h -
00070     can_add_filter(2, 0x7E8);         //7E8h -
00071     can_add_filter(2, 0x311);         //311h -
00072     can_add_filter(2, 0x5E8);         //5E8h -
00073     //can_add_filter(2, 0x101);         //101h -
00074 
00075 
00076 // main loop
00077     *cmd_buffer = '\0';
00078     char ret;
00079     char rx_char;
00080     while (true) {
00081         // read chars from USB
00082         // send received messages to the pc over USB connection
00083         // This function displays any CAN messages that are 'missed' by the other functions
00084         // Can messages might be 'missed' because they are received after a 'timeout' period
00085         // or because they weren't expected, e.g. if the T5 ECU resets for some reason
00086 //        t7_show_can_message();
00087         silent_can_message();
00088         if (pc.readable()) {
00089             // turn Error LED off for next command
00090             led4 = 0;
00091             rx_char = pc.getc();
00092             switch (rx_char) {
00093                     // 'ESC' key to go back to mbed Just4Trionic 'home' menu
00094                 case '\e':
00095                     can_close();
00096                     return;
00097                     // end-of-command reached
00098                 case TERM_OK :
00099                     // execute command and return flag via USB
00100                     timer.reset();
00101                     timer.start();
00102                     ret = execute_t8_cmd();
00103                     pc.putc(ret);
00104                     printf("Completed in %.3f seconds.\r\n", timer.read());
00105                     // reset command buffer
00106                     *cmd_buffer = '\0';
00107                     // light up LED
00108                     //                    ret == TERM_OK ? led_on(LED_ACT) : led_on(LED_ERR);
00109                     ret == TERM_OK ? led3 = 1 : led4 = 1;
00110                     break;
00111                     // another command char
00112                 default:
00113                     // store in buffer if space permits
00114                     if (StrLen(cmd_buffer) < CMD_BUF_LENGTH - 1) {
00115                         StrAddc(cmd_buffer, rx_char);
00116                     }
00117                     break;
00118             }
00119         }
00120     }
00121 }
00122 
00123 //-----------------------------------------------------------------------------
00124 /**
00125     Executes a command and returns result flag (does not transmit the flag
00126     itself).
00127 
00128     @return                    command flag (success / failure)
00129 */
00130 uint8_t execute_t8_cmd()
00131 {
00132 
00133     char data[8];
00134 //    uint8_t cmd_length = strlen(cmd_buffer);
00135     // command groups
00136     switch (*cmd_buffer) {
00137 //            CHECK_ARGLENGTH(0);
00138             // Get the Symbol Table
00139         case 'i' :
00140             if (t8_initialise()) {
00141                 printf("Trionic 8 Connection OK\r\n");
00142                 return TERM_OK;
00143             } else {
00144                 printf("Trionic 8 Connection Failed\r\n");
00145                 return TERM_ERR;
00146             }
00147 //            return t7_initialise()
00148 //                   ? TERM_OK : TERM_ERR;
00149         case 'a' :
00150         case 'A' :
00151             if (t8_authenticate(T8REQID, T8RESPID, 0x01)) {
00152                 printf("Security Key Accepted\r\n");
00153                 return TERM_OK;
00154             } else {
00155                 printf("Security Key Failed\r\n");
00156                 return TERM_ERR;
00157             }
00158 //            return t7_authenticate()
00159 //                   ? TERM_OK : TERM_ERR;
00160 
00161 // DUMP the T8 ECU BIN file stored in the FLASH chips
00162         case 'D':
00163         case 'd':
00164             return t8_dump()
00165                    ? TERM_OK : TERM_ERR;
00166 // Send a FLASH update file to the T8 ECU
00167         case 'F':
00168         case 'f':
00169             return t8_flash()
00170                    ? TERM_OK : TERM_ERR;
00171 // Erase the FLASH chips
00172         case 'r':
00173         case 'R':
00174             return t8_recover()
00175                    ? TERM_OK : TERM_ERR;
00176 // Try to connect to CAN I-BUS
00177         case 'I' :
00178             printf("Trying to open CAN I-Bus (47619 Bit/s)...\r\n");
00179             can_close();
00180             //can_monitor();
00181             can_set_speed(47619);
00182             can_open();
00183             if (can_wait_timeout(T8ANYMSG, data, 8, T8CONNECTTIMEOUT)) {
00184                 printf("Connected to Saab I-Bus\r\n");
00185                 //can_active();
00186                 return TERM_OK;
00187             } else {
00188                 printf("I did not receive any I-Bus messages\r\n");
00189                 printf("FAILED to connect!\r\n");
00190                 return TERM_ERR;
00191             }
00192 // Try to connect to CAN P-BUS
00193         case 'P' :
00194             printf("Trying to open CAN P-Bus (500 kBit/s)...\r\n");
00195             can_close();
00196             //can_monitor();
00197             can_set_speed(500000);
00198             can_open();
00199             if (can_wait_timeout(T8ANYMSG, data, 8, T8CONNECTTIMEOUT)) {
00200                 printf("Connected to Saab P-Bus\r\n");
00201                 //can_active();
00202                 return TERM_OK;
00203             } else {
00204                 printf("I did not receive any P-Bus messages\r\n");
00205                 printf("FAILED to connect!\r\n");
00206                 return TERM_ERR;
00207             }
00208 // Show the VIN code
00209         case 'v':
00210             return t8_show_VIN()
00211                    ? TERM_OK : TERM_ERR;
00212         case 'V':
00213             return t8_write_VIN()
00214                    ? TERM_OK : TERM_ERR;
00215 // Print help
00216         case 'h':
00217             t8_can_show_help();
00218             return TERM_OK;
00219         case 'H':
00220             t8_can_show_full_help();
00221             return TERM_OK;
00222         default:
00223             t8_can_show_help();
00224             break;
00225     }
00226 // unknown command
00227     return TERM_ERR;
00228 }
00229 
00230 //
00231 // Trionic7ShowHelp
00232 //
00233 // Displays a list of things that can be done with the T5 ECU.
00234 //
00235 // inputs:    none
00236 // return:    none
00237 //
00238 void t8_can_show_help()
00239 {
00240     printf("Trionic 8 Command Menu\r\n");
00241     printf("======================\r\n");
00242     printf("D - DUMP the T8 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
00243     printf("F - FLASH the update file 'MODIFIED.BIN' to the T8\r\n");
00244     printf("R - Recover your T8 ECU FLASH with 'MODIFIED.BIN' file\r\n");
00245     printf("\r\n");
00246     printf("I - Try to open CAN I-Bus (47619 Bit/s)\r\n");
00247     printf("P - Try to open CAN P-Bus (500 kBit/s)\r\n");
00248     printf("\r\n");
00249     printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
00250     printf("\r\n");
00251     printf("h  - Show this help menu\r\n");
00252     printf("\r\n");
00253     return;
00254 }
00255 //
00256 // t7_can_show_full_help
00257 //
00258 // Displays a complete list of things that can be done with the T5 ECU.
00259 //
00260 // inputs:    none
00261 // return:    none
00262 //
00263 void t8_can_show_full_help()
00264 {
00265     printf("Trionic 8 Command Menu\r\n");
00266     printf("======================\r\n");
00267     printf("D - DUMP the T8 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
00268     printf("F - FLASH the update file 'MODIFIED.BIN' to the T8\r\n");
00269     printf("R - Recover your T8 ECU FLASH with 'MODIFIED.BIN' file\r\n");
00270     printf("\r\n");
00271     printf("I - Try to open CAN I-Bus (47619 Bit/s)\r\n");
00272     printf("P - Try to open CAN P-Bus (500 kBit/s)\r\n");
00273     printf("\r\n");
00274     printf("\r\n");
00275     printf("i - Send initialisation message to T8\r\n");
00276     printf("a - Send Authentication key to T8\r\n");
00277     printf("d - Dump T8 Bin file 'ORIGINAL.BIN'\r\n");
00278     printf("e - Erase the FLASH in the T8 ECU\r\n");
00279     printf("f - FLASH the file 'MODIFIED.BIN' to the T8 ECU\r\n");
00280     printf("v - Display the current VIN code stored in the T8 ECU\r\n");
00281     printf("V - FLASH the file 'MODIFIED.BIN' to the T8 ECU\r\n");
00282     printf("\r\n");
00283     printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
00284     printf("\r\n");
00285     printf("H  - Show this help menu\r\n");
00286     printf("\r\n");
00287     return;
00288 }
00289 
00290