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