Just4Trionic - CAN and BDM FLASH programmer for Saab cars

Dependencies:   mbed

t7can.cpp

Committer:
Just4pLeisure
Date:
2011-06-07
Revision:
3:92dae9083c83
Child:
4:682d96ff6d79

File content as of revision 3:92dae9083c83:

/*******************************************************************************

trionic7.cpp - CAN Bus functions for Just4Trionic by Just4pLeisure
(c) 2011 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).

Some functions need an additional 'bootloader' program to be sent to the T5 ECU
before they can be used. These functions are: Identifying the T5 ECU type and
FLASH chips, dumping the FLASH chips, erasing the FLASH chips, writing to the
FLASH chips and calculating the FLASH chips' checksum.

My version of the bootloader, BOOTY.S19, includes some features not in other
bootloaders; identifying the ECU and FLASH chip types, a 'safer' way of dumping
the FLASH chips and the ability to program AMD 29F010 type FLASH chips

********************************************************************************

WARNING: Use at your own risk, sadly this software comes with no guarantees.
This software is provided 'free' and in good faith, but the author does not
accept liability for any damage arising from its use.

*******************************************************************************/

#include "t7can.h"

// constants
#define CMD_BUF_LENGTH      32              ///< command buffer size

// static variables
static char cmd_buffer[CMD_BUF_LENGTH];     ///< command string buffer

//static uint32_t cmd_addr;                   ///< address (optional)
//static uint32_t cmd_value;                  ///< value    (optional)
//static uint32_t cmd_result;                 ///< result

//static uint32_t flash_start = 0;

// private functions
uint8_t execute_t7_cmd();
void t7_can_show_help();
void t7_can_show_full_help();

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);

    t7_can_show_help();

    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");

    // main loop
    *cmd_buffer = '\0';
    char ret;
    char rx_char;
    while (true) {
        // read chars from USB
        // send received messages to the pc over USB connection
        // This function displays any CAN messages that are 'missed' by the other functions
        // Can messages might be 'missed' because they are received after a 'timeout' period
        // or because they weren't expected, e.g. if the T5 ECU resets for some reason
//        t7_show_can_message();
        silent_can_message();
        if (pc.readable()) {
            // turn Error LED off for next command
            led4 = 0;
            rx_char = pc.getc();
            switch (rx_char) {
                    // 'ESC' key to go back to mbed Just4Trionic 'home' menu
                case '\e':
                    can_close();
                    return;
                    // end-of-command reached
                case TERM_OK :
                    // execute command and return flag via USB
                    timer.reset();
                    timer.start();
                    ret = execute_t7_cmd();
                    pc.putc(ret);
                    printf("Completed in %.3f seconds.\r\n", timer.read());
                    // reset command buffer
                    *cmd_buffer = '\0';
                    // light up LED
                    //                    ret == TERM_OK ? led_on(LED_ACT) : led_on(LED_ERR);
                    ret == TERM_OK ? led3 = 1 : led4 = 1;
                    break;
                    // another command char
                default:
                    // store in buffer if space permits
                    if (StrLen(cmd_buffer) < CMD_BUF_LENGTH - 1) {
                        StrAddc(cmd_buffer, rx_char);
                    }
                    break;
            }
        }
    }
}

//-----------------------------------------------------------------------------
/**
    Executes a command and returns result flag (does not transmit the flag
    itself).

    @return                    command flag (success / failure)
*/
uint8_t execute_t7_cmd() {


//    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;
        case 'a' :
        case 'A' :
            return t7_authenticate()
                   ? TERM_OK : TERM_ERR;

            // 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
        case 'd':
        case 'D':
            return t7_dump()
                   ? TERM_OK : TERM_ERR;
            // Send a FLASH update file to the T5 ECU
        case 'f':
        case 'F':
            return t7_flash()
                   ? TERM_OK : TERM_ERR;

        case '3':

            // Print help
        case 'h':
            t7_can_show_help();
            return TERM_OK;
        case 'H':
            t7_can_show_full_help();
            return TERM_OK;
        default:
            t7_can_show_help();
            break;
    }
    // unknown command
    return TERM_ERR;
}

//
// Trionic7ShowHelp
//
// Displays a list of things that can be done with the T5 ECU.
//
// inputs:    none
// return:    none
//
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("\r\n");
    printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
    printf("\r\n");
    printf("h  - Show this help menu\r\n");
    printf("\r\n");
    return;
}
//
// t7_can_show_full_help
//
// Displays a complete list of things that can be done with the T5 ECU.
//
// inputs:    none
// return:    none
//
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("\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("\r\n");
    printf("'ESC' - Return to Just4Trionic Main Menu\r\n");
    printf("\r\n");
    printf("H  - Show this help menu\r\n");
    printf("\r\n");
    return;
}