James Randall / Mbed 2 deprecated Just4Trionic

Dependencies:   mbed

Fork of Just4Trionic by Sophie Dexter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers t7can.cpp Source File

t7can.cpp

00001 /*******************************************************************************
00002 
00003 trionic7.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 Trionic7 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 T5 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 My version of the bootloader, BOOTY.S19, includes some features not in other
00015 bootloaders; identifying the ECU and FLASH chip types, a 'safer' way of dumping
00016 the FLASH chips and the ability to program AMD 29F010 type FLASH chips
00017 
00018 ********************************************************************************
00019 
00020 WARNING: Use at your own risk, sadly this software comes with no guarantees.
00021 This software is provided 'free' and in good faith, but the author does not
00022 accept liability for any damage arising from its use.
00023 
00024 *******************************************************************************/
00025 
00026 #include "t7can.h"
00027 
00028 // constants
00029 #define CMD_BUF_LENGTH      32              ///< command buffer size
00030 
00031 // static variables
00032 static char cmd_buffer[CMD_BUF_LENGTH];     ///< command string buffer
00033 
00034 //static uint32_t cmd_addr;                   ///< address (optional)
00035 //static uint32_t cmd_value;                  ///< value    (optional)
00036 //static uint32_t cmd_result;                 ///< result
00037 
00038 //static uint32_t flash_start = 0;
00039 
00040 // private functions
00041 uint8_t execute_t7_cmd();
00042 void t7_can_show_help();
00043 void t7_can_show_full_help();
00044 
00045 void t7_can() {
00046     // Start the CAN bus system
00047     // Note that at the moment this is only for T7 ECUs at 500 kbits
00048     t7_can_show_help();
00049 
00050     char data[8];
00051     printf("Trying to listen to CAN I-Bus (47619 Bit/s)...\r\n");
00052     can_configure(2, 47619, 1);
00053     if (can_wait_timeout(T7ANYMSG, data, 8, T7MESSAGETIMEOUT)) {
00054         printf("Connected to Saab I-Bus\r\n");
00055         printf("Switching to I-Bus active mode\r\n");
00056         can_configure(2, 47619, 0);
00057     } else {
00058         printf("I did not receive any I-Bus messages\r\n");
00059         printf("Trying to listen to  CAN P-Bus (500 kBit/s)...\r\n");
00060         can_configure(2, 500000, 1);
00061         if (can_wait_timeout(T7ANYMSG, data, 8, T7MESSAGETIMEOUT)) {
00062             printf("Connected to Saab P-Bus\r\n");
00063             printf("Switching to P-Bus active mode\r\n");
00064             can_configure(2, 500000, 0);
00065         } else {
00066             printf("I did not receive any P-Bus messages\r\n");
00067             printf("Switching to P-Bus active mode\r\n");
00068             can_configure(2, 500000, 0);
00069             if (can_wait_timeout(T7ANYMSG, data, 8, T7CONNECTTIMEOUT)) {
00070                 printf("Connected to Saab P-Bus\r\n");
00071                 //can_active();
00072             } else {
00073                 printf("FAILED to connect!\r\n");
00074                 led4 = 1;
00075             }
00076         }
00077     }
00078 
00079 //    if (t7_initialise())
00080 //        printf("Connected to Saab I-Bus\r\n");
00081 //    else {
00082 //        printf("Opening CAN channel to Saab P-Bus (500 kBit/s)...\r\n");
00083 //        can_set_speed(500000);
00084 //        if (t7_initialise())
00085 //            printf("Connected to Saab P-Bus\r\n");
00086 //        else {
00087 //            printf("FAILED to connect!\r\n");
00088 //            led4 = 1;
00089 //        }
00090 //    }
00091 
00092 //    if (t7_initialise())
00093 //        printf("Trionic 7 Connection OK\r\n");
00094 //    else
00095 //        printf("Trionic 7 Connection Failed\r\n");
00096 //    t7_authenticate();
00097 //    if (t7_authenticate())
00098 //        printf("Security Key Accepted\r\n");
00099 //    else
00100 //        printf("Security Key Failed\r\n");
00101 
00102 // main loop
00103     *cmd_buffer = '\0';
00104     char ret;
00105     char rx_char;
00106     while (true) {
00107         // read chars from USB
00108         // send received messages to the pc over USB connection
00109         // This function displays any CAN messages that are 'missed' by the other functions
00110         // Can messages might be 'missed' because they are received after a 'timeout' period
00111         // or because they weren't expected, e.g. if the T5 ECU resets for some reason
00112 //        t7_show_can_message();
00113         silent_can_message();
00114         if (pc.readable()) {
00115             // turn Error LED off for next command
00116             led4 = 0;
00117             rx_char = pc.getc();
00118             switch (rx_char) {
00119                     // 'ESC' key to go back to mbed Just4Trionic 'home' menu
00120                 case '\e':
00121                     can_close();
00122                     return;
00123                     // end-of-command reached
00124                 case TERM_OK :
00125                     // execute command and return flag via USB
00126                     timer.reset();
00127                     timer.start();
00128                     ret = execute_t7_cmd();
00129                     pc.putc(ret);
00130                     printf("Completed in %.3f seconds.\r\n", timer.read());
00131                     // reset command buffer
00132                     *cmd_buffer = '\0';
00133                     // light up LED
00134                     //                    ret == TERM_OK ? led_on(LED_ACT) : led_on(LED_ERR);
00135                     ret == TERM_OK ? led3 = 1 : led4 = 1;
00136                     break;
00137                     // another command char
00138                 default:
00139                     // store in buffer if space permits
00140                     if (StrLen(cmd_buffer) < CMD_BUF_LENGTH - 1) {
00141                         StrAddc(cmd_buffer, rx_char);
00142                     }
00143                     break;
00144             }
00145         }
00146     }
00147 }
00148 
00149 //-----------------------------------------------------------------------------
00150 /**
00151     Executes a command and returns result flag (does not transmit the flag
00152     itself).
00153 
00154     @return                    command flag (success / failure)
00155 */
00156 uint8_t execute_t7_cmd() {
00157 
00158     char data[8];
00159 //    uint8_t cmd_length = strlen(cmd_buffer);
00160     // command groups
00161     switch (*cmd_buffer) {
00162 //            CHECK_ARGLENGTH(0);
00163             // Get the Symbol Table
00164         case 'i' :
00165             if (t7_initialise()) {
00166                 printf("Trionic 7 Connection OK\r\n");
00167                 return TERM_OK;
00168             } else {
00169                 printf("Trionic 7 Connection Failed\r\n");
00170                 return TERM_ERR;
00171             }
00172 //            return t7_initialise()
00173 //                   ? TERM_OK : TERM_ERR;
00174         case 'a' :
00175         case 'A' :
00176             if (t7_authenticate()) {
00177                 printf("Security Key Accepted\r\n");
00178                 return TERM_OK;
00179             } else {
00180                 printf("Security Key Failed\r\n");
00181                 return TERM_ERR;
00182             }
00183 //            return t7_authenticate()
00184 //                   ? TERM_OK : TERM_ERR;
00185 
00186 // Erase the FLASH chips
00187         case 'e':
00188         case 'E':
00189             return t7_erase()
00190                    ? TERM_OK : TERM_ERR;
00191 // DUMP the T5 ECU BIN file stored in the FLASH chips
00192         case 'D':
00193             if (!t7_authenticate()) {
00194                 if (!t7_initialise()) {
00195                     printf("Trionic 7 Connection Failed\r\n");
00196                     return TERM_ERR;
00197                 }
00198                 if (!t7_authenticate()) {
00199                     printf("Security Key Failed\r\n");
00200                     return TERM_ERR;
00201                 }
00202             }
00203         case 'd':
00204             return t7_dump()
00205                    ? TERM_OK : TERM_ERR;
00206 // Send a FLASH update file to the T5 ECU
00207         case 'F':
00208             if (!t7_authenticate()) {
00209                 if (!t7_initialise()) {
00210                     printf("Trionic 7 Connection Failed\r\n");
00211                     return TERM_ERR;
00212                 }
00213                 if (!t7_authenticate()) {
00214                     printf("Security Key Failed\r\n");
00215                     return TERM_ERR;
00216                 }
00217             }
00218             if (!t7_erase()) {
00219                 printf("Could not Erase FLASH!\r\n");
00220                 return TERM_ERR;
00221             }
00222         case 'f':
00223             return t7_flash()
00224                    ? TERM_OK : TERM_ERR;
00225 // Try to connect to CAN I-BUS
00226         case 'I' :
00227             printf("Trying to open CAN I-Bus (47619 Bit/s)...\r\n");
00228             can_close();
00229             //can_monitor();
00230             can_set_speed(47619);
00231             can_open();
00232             if (can_wait_timeout(T7ANYMSG, data, 8, T7CONNECTTIMEOUT)) {
00233                 printf("Connected to Saab I-Bus\r\n");
00234                 //can_active();
00235                 return TERM_OK;
00236             } else {
00237                 printf("I did not receive any I-Bus messages\r\n");
00238                 printf("FAILED to connect!\r\n");
00239                 return TERM_ERR;
00240             }
00241 // Try to connect to CAN P-BUS
00242         case 'P' :
00243             printf("Trying to open CAN P-Bus (500 kBit/s)...\r\n");
00244             can_close();
00245             //can_monitor();
00246             can_set_speed(500000);
00247             can_open();
00248             if (can_wait_timeout(T7ANYMSG, data, 8, T7CONNECTTIMEOUT)) {
00249                 printf("Connected to Saab P-Bus\r\n");
00250                 //can_active();
00251                 return TERM_OK;
00252             } else {
00253                 printf("I did not receive any P-Bus messages\r\n");
00254                 printf("FAILED to connect!\r\n");
00255                 return TERM_ERR;
00256             }
00257 
00258 // Print help
00259         case 'h':
00260             t7_can_show_help();
00261             return TERM_OK;
00262         case 'H':
00263             t7_can_show_full_help();
00264             return TERM_OK;
00265         default:
00266             t7_can_show_help();
00267             break;
00268     }
00269 // unknown command
00270     return TERM_ERR;
00271 }
00272 
00273 //
00274 // Trionic7ShowHelp
00275 //
00276 // Displays a list of things that can be done with the T5 ECU.
00277 //
00278 // inputs:    none
00279 // return:    none
00280 //
00281 void t7_can_show_help() {
00282     printf("Trionic 7 Command Menu\r\n");
00283     printf("======================\r\n");
00284     printf("D - DUMP the T7 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
00285     printf("F - FLASH the update file 'MODIFIED.BIN' to the T7\r\n");
00286     printf("\r\n");
00287     printf("I - Try to open CAN I-Bus (47619 Bit/s)\r\n");
00288     printf("P - Try to open CAN P-Bus (500 kBit/s)\r\n");
00289     printf("\r\n");
00290     printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
00291     printf("\r\n");
00292     printf("h  - Show this help menu\r\n");
00293     printf("\r\n");
00294     return;
00295 }
00296 //
00297 // t7_can_show_full_help
00298 //
00299 // Displays a complete list of things that can be done with the T5 ECU.
00300 //
00301 // inputs:    none
00302 // return:    none
00303 //
00304 void t7_can_show_full_help() {
00305     printf("Trionic 7 Command Menu\r\n");
00306     printf("======================\r\n");
00307     printf("D - DUMP the T7 ECU FLASH to a file 'ORIGINAL.BIN'\r\n");
00308     printf("F - FLASH the update file 'MODIFIED.BIN' to the T7\r\n");
00309     printf("\r\n");
00310     printf("I - Try to open CAN I-Bus (47619 Bit/s)\r\n");
00311     printf("P - Try to open CAN P-Bus (500 kBit/s)\r\n");
00312     printf("\r\n");
00313     printf("\r\n");
00314     printf("i - Send initialisation message to T7\r\n");
00315     printf("a - Send Authentication key to T7\r\n");
00316     printf("d - Dump T7 Bin file 'ORIGINAL.BIN'\r\n");
00317     printf("e - Erase the FLASH in the T7 ECU\r\n");
00318     printf("f - FLASH the file 'MODIFIED.BIN' to the T7 ECU\r\n");
00319     printf("\r\n");
00320     printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
00321     printf("\r\n");
00322     printf("H  - Show this help menu\r\n");
00323     printf("\r\n");
00324     return;
00325 }
00326 
00327