Just4Trionic - CAN and BDM FLASH programmer for Saab cars

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************************************
00002 
00003 Just4Trionic by Just4pLeisure
00004 *****************************
00005 (c) 2010 by Sophie Dexter
00006 
00007 Whilst I have written this program myself I could not have done it without
00008 a lot of help and the original ideas and programs written by:
00009 Dilemma - Author of the Trionic Suite software programs and general Guru
00010 http://trionic.mobixs.eu/ and http://www.ecuproject.com.
00011 General Failure - Author of the T5CANlib software and regular contributor at
00012 http://www.ecuproject.com.
00013 Tomi Liljemark - Lots of information and programs about the Saab CAN bus
00014 http://pikkupossu.1g.fi/tomi/projects/projects.html.
00015 Scott Howard - Author of the BDM software.
00016 Janis Silins - Valued contributor at http://www.ecuproject.com and creator of
00017 USBBDM. For sharing his BDM software (which also had a very useful method of
00018 entering commands)
00019 Plus inspiration and ideas from many others...
00020 
00021 Sophie x
00022 
00023 ********************************************************************************
00024 
00025 WARNING: Use at your own risk, sadly this software comes with no guarantees.
00026 This software is provided 'free' and in good faith, but the author does not
00027 accept liability for any damage arising from its use.
00028 
00029 ********************************************************************************
00030 
00031 Version 1.6 (04/2016)
00032 
00033 Fixed since Version 1.5:
00034     BDM
00035         Improvements to CPU register content display
00036         T8 erase timeout period to 200 seconds (was incorrectly only 20 seconds)
00037 
00038 Added since Version 1.5:
00039     T7 CAN functions:
00040         Separate FLASHing algorithms for I and P-BUS connections
00041         Completely safe, but slow, I-BUS algorithm
00042         Pseudo 'recovery' FLASHing function can be used to retry a failed
00043         FLASH with safe, slow algorithm
00044         
00045 Changes since Version 1.5:
00046     Additional debug messages
00047     T7 CAN functions:
00048         Code refactoring to make file handling simpler
00049 
00050 Version 1.5 (04/2015)
00051 
00052 Added since Version 1.4
00053     T8 CAN functions: DUMP, FLASH and RECOVERY
00054         Using modified T8Bootloaders that are slightly faster :-)
00055     Algorithms for different types of 5 volt replacement FLASH Chips in T5 ECUs
00056         AMD, ST and AMIC 29F010 types
00057         SST 39F010
00058         ATMEL 29C512 and 29C010
00059         
00060 Changes since Version 1.4
00061     BDM FLASHing uses BD32 like Target Resident Drivers.
00062         Based on my 'universal scripts'
00063         BDM FLASHing much faster now
00064     Always use slow BDM clock function (bdm_clk_slow) for reliability
00065         Even when using the slow method it only takes 8 seconds
00066         longer to FLASH a T8 ECU.
00067         mbed library and compiler changes make it difficult to
00068         maintain the faster bdm functions that rely on software
00069         delay loops for timing.
00070     The file extension of the file to be FLASHed is now the more usual 'BIN'.
00071         i.e. 'modified.bin' instead of 'modified.hex'
00072 
00073 ********************************************************************************
00074 
00075 Version 1.4 (07/2013)
00076 
00077 Added since Verion 1.3
00078     Progress indication shown as a percentage, slightly slower but more informative
00079     T8 BDM Functionality
00080 
00081 Changes since Verion 1.3
00082     T7 CAN FLASH algorithm changed to only send 4 Bytes at a time
00083         Slightly slower but more reliable especially with I-Bus connection
00084 
00085 ********************************************************************************
00086 
00087 Version 1.3 (06/2011) - Basic T7 CAN support and faster T5 CAN bootloader
00088 
00089 Changes since Verion 1.2
00090     New T7 CAN menu provides some very basic T7 CAN DUMP and FLASH functions
00091     T5 CAN uses Mybooty version 2 for 1 Mbps CAN speed
00092         Get MyBooty version 2.x from forum.ecuproject.com
00093         Does not work with version MyBooty 1.x
00094 
00095 ********************************************************************************
00096 
00097 Version 1.2 (12/2010) - Only a very small update
00098 
00099 Fixed since Version 1.1:
00100     My method of detecting the FLASH type didn't work for T7 ECUs
00101         Now that that I have corrected this bug it is possible to
00102         FLASH Trionic 7 ECUs using the BDM connection
00103         See the 'get_flash_id' function in bdmtrionic.cpp
00104 
00105 Changes since Verion 1.1
00106     I have removed everything to do with the BDM DS connection
00107     I have changed the mbed pin number for the BDM DSO connection
00108         Now all BDM connections are part of the same MBED 'port'
00109             Being part of one port makes it possible to change all
00110             connections simulataneously so speeding up BDM
00111         See 'interfaces.cpp' for details of how the mbed pins connect.
00112             Uglybug's wiring circuit has these connections (thanks Uglybug)
00113 
00114 ********************************************************************************
00115 
00116 Version 1.1 (09/2010) - Still very crude way of doing things
00117 
00118 Additions since Version 1:
00119     The BDM interface is now working
00120         Based on Janis Silin's BDM software with modifications and additions
00121             Detect which type of FLASH chip is fitted to work out type of ECU
00122             Modifications to FLASH algorithms - I think my algorithms are
00123             closer to the datasheet methods. Still to do:
00124                 Separate pulse counters for 28Fxxx erase
00125                 DQ7 and DQ5 checking method for 29Fxxx FLASH
00126         Works for T5.5 ECUs with 28F010 and 29F010 chips
00127         Probably works with T5.2 ECUs (chip detection method)
00128         MAY work with T7 ECUs ('prep' method may need changes - I can't test T7)
00129         NOTE: Some of Janis' original BDM commands may not work, or at least
00130         not as originally intended
00131     Lawicell CAN232 interface partially working
00132         Only a few Lawicell message types to open/close, set speed and write
00133     Trionic5 CAN functions
00134         All-in-one 'D' and 'F' commands to DUMP and FLASH BIN files
00135             Lots of checking for errors, either works or says it failed
00136             No need to interpret the cryptic CAN messages anymore
00137         Should now work for T5.2 and T5.5 ECUs
00138         Detects FLASH chip type and works out which ECU is connected T5.2/5.5
00139 
00140 ********************************************************************************
00141 
00142 Version 1 (04/2010)- The basic CAN functions are working
00143 
00144 I have decided to 'release' this software somewhat prematurely because the FLASH
00145 chips in my spare ECU have 'died' and I don't know when I will be able to do
00146 carry on improving and 'polishing' it. This way others will be able to use and
00147 enhance it without having to wait for me.
00148 
00149 For now, only option '5' Trionic ECU CAN interface is working. BDM and Lawicell
00150 CAN232 functions are dummies. The intention is to build a complete suite of CAN
00151 software for Trionic5, 7 and 8 ECU types as well as adding a BDM interface to
00152 make an 'all-in-one' USB programming tool.
00153 
00154 To make this you will need an mbed system and the CAN circuit from this page:
00155 http://mbed.org/projects/cookbook/wiki/CanBusExample1
00156 
00157 Some ideas for the truly creative and adventurous of you is to make a gizmo that
00158 doesn't even need to be connected to a laptop or PC to use, maybe even a self-
00159 contained vesion of Dilemma's CarPC using ideas from this pages:
00160 
00161 http://mbed.org/projects/cookbook/wiki/PS2Keyboard
00162 http://mbed.org/projects/cookbook/wiki/PS2Mouse
00163 http://mbed.org/projects/cookbook/wiki/MobileLCD
00164 http://mbed.org/projects/cookbook/wiki/SDCard
00165 
00166 *******************************************************************************/
00167 
00168 #include "mbed.h"
00169 //
00170 #include "common.h"
00171 #include "bdm.h"
00172 #include "can232.h"
00173 #include "t5can.h"
00174 #include "t7can.h"
00175 #include "t8can.h"
00176 
00177 // constants
00178 #define CMD_BUF_LENGTH      32              ///< command buffer size
00179 
00180 // static variables
00181 static char cmd_buffer[CMD_BUF_LENGTH];     ///< command string buffer
00182 
00183 // private functions
00184 uint8_t execute_just4trionic_cmd();
00185 void show_just4trionic_help();
00186 
00187 int main()
00188 {
00189     // fast serial speed
00190     //pc.baud(921600);
00191     pc.baud(115200);
00192 
00193     // the address of the function to be attached (leds_off) and the interval (0.1 seconds)
00194     // This 'ticker' turns off the activity LEDs so that they don't stay on if something has gone wrong
00195     ticker.attach(&leds_off, 0.1);
00196 
00197     // clear incoming buffer
00198     // sometimes TeraTerm gets 'confused'. johnc does this in his code
00199     // hopefully this will fix the problem
00200     // unfortunately it doesn't, but it seems like a good idea
00201     char rx_char;
00202     while (pc.readable())
00203         rx_char = pc.getc();
00204 
00205     show_just4trionic_help();
00206 
00207     // main loop
00208     *cmd_buffer = '\0';
00209     char ret;
00210     while (true) {
00211         // read chars from USB
00212         // send received messages to the pc over USB connection
00213         // This function displays any CAN messages that are 'missed' by the other functions
00214         // Can messages might be 'missed' because they are received after a 'timeout' period
00215         // or because they weren't expected, e.g. if the T5 ECU resets for some reason
00216         t5_can_show_can_message();
00217         if (pc.readable()) {
00218             // turn Error LED off for next command
00219             led4 = 0;
00220             rx_char = pc.getc();
00221             switch (rx_char) {
00222                     // end-of-command reached
00223                 case TERM_OK :
00224                     // execute command and return flag via USB
00225                     timer.reset();
00226                     timer.start();
00227                     ret = execute_just4trionic_cmd();
00228                     show_just4trionic_help();
00229                     pc.putc(ret);
00230                     // reset command buffer
00231                     *cmd_buffer = '\0';
00232                     // light up LED
00233 //                    ret == TERM_OK ? led_on(LED_ACT) : led_on(LED_ERR);
00234                     ret == TERM_OK ? led3 = 1 : led4 = 1;
00235                     break;
00236                     // another command char
00237                 default:
00238                     // store in buffer if space permits
00239                     if (StrLen(cmd_buffer) < CMD_BUF_LENGTH - 1) {
00240                         StrAddc(cmd_buffer, rx_char);
00241                     }
00242                     break;
00243             }
00244         }
00245     }
00246 }
00247 
00248 //-----------------------------------------------------------------------------
00249 /**
00250     Executes a command and returns result flag (does not transmit the flag
00251     itself).
00252 
00253     @return                    command flag (success / failure)
00254 */
00255 uint8_t execute_just4trionic_cmd()
00256 {
00257 
00258 
00259 //    uint8_t cmd_length = strlen(cmd_buffer);
00260     // command groups
00261     switch (*cmd_buffer) {
00262 //            CHECK_ARGLENGTH(0);
00263         case 'b':
00264         case 'B':
00265             bdm();
00266             return TERM_OK;
00267         case 'o':
00268         case 'O':
00269             can232();
00270             return TERM_OK;
00271         case '5':
00272             t5_can();
00273             return TERM_OK;
00274         case '7':
00275             t7_can();
00276             return TERM_OK;
00277         case '8':
00278             t8_can();
00279             return TERM_OK;
00280         case 'h':
00281         case 'H':
00282             return TERM_OK;
00283         default:
00284             break;
00285     }
00286 
00287 // unknown command
00288     return TERM_ERR;
00289 }
00290 
00291 void show_just4trionic_help()
00292 {
00293 #ifdef DEBUG
00294     printf("*************************\r\n");
00295     printf("** D E B U G B U I L D **\r\n");
00296     printf("*************************\r\n");
00297 #endif
00298     printf("=========================\r\n");
00299     printf("Just4Trionic Release %d.%d\r\n", FW_VERSION_MAJOR, FW_VERSION_MINOR);
00300     printf("=========================\r\n");
00301     printf("Modes Menu\r\n");
00302     printf("=========================\r\n");
00303     printf("b/B - Enter BDM mode\r\n");
00304     printf("o/O - Enter Lawicel CAN mode\r\n");
00305     printf("5   - Enter Trionic5 CAN mode\r\n");
00306     printf("7   - Enter Trionic7 CAN mode\r\n");
00307     printf("8   - Enter Trionic8 CAN mode\r\n");
00308     printf("\r\n");
00309     printf("h/H - show this help menu\r\n");
00310     printf("\r\n");
00311     return;
00312 }