Sophie Dexter
/
Just4Trionic
Just4Trionic - CAN and BDM FLASH programmer for Saab cars
t5utils.cpp@3:92dae9083c83, 2011-06-07 (annotated)
- Committer:
- Just4pLeisure
- Date:
- Tue Jun 07 12:23:28 2011 +0000
- Revision:
- 3:92dae9083c83
- Parent:
- 1:d5452e398b76
Basic T7 CAN DUMP and FLASH for P-BUS connection only
Requires MyBooty V2.x for T5 CAN FLASHing at 1 Mbps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Just4pLeisure | 1:d5452e398b76 | 1 | /******************************************************************************* |
Just4pLeisure | 1:d5452e398b76 | 2 | |
Just4pLeisure | 1:d5452e398b76 | 3 | t5utils.cpp |
Just4pLeisure | 1:d5452e398b76 | 4 | (c) 2010 by Sophie Dexter |
Just4pLeisure | 1:d5452e398b76 | 5 | |
Just4pLeisure | 1:d5452e398b76 | 6 | This C++ module provides functions for communicating simple messages to and from |
Just4pLeisure | 1:d5452e398b76 | 7 | the T5 ECU |
Just4pLeisure | 1:d5452e398b76 | 8 | |
Just4pLeisure | 1:d5452e398b76 | 9 | ******************************************************************************** |
Just4pLeisure | 1:d5452e398b76 | 10 | |
Just4pLeisure | 1:d5452e398b76 | 11 | WARNING: Use at your own risk, sadly this software comes with no guarantees. |
Just4pLeisure | 1:d5452e398b76 | 12 | This software is provided 'free' and in good faith, but the author does not |
Just4pLeisure | 1:d5452e398b76 | 13 | accept liability for any damage arising from its use. |
Just4pLeisure | 1:d5452e398b76 | 14 | |
Just4pLeisure | 1:d5452e398b76 | 15 | *******************************************************************************/ |
Just4pLeisure | 1:d5452e398b76 | 16 | |
Just4pLeisure | 1:d5452e398b76 | 17 | #include "t5utils.h" |
Just4pLeisure | 1:d5452e398b76 | 18 | |
Just4pLeisure | 1:d5452e398b76 | 19 | // |
Just4pLeisure | 1:d5452e398b76 | 20 | // T5Open enables the CAN chip and sets the CAN speed to 615 kbits. |
Just4pLeisure | 1:d5452e398b76 | 21 | // |
Just4pLeisure | 1:d5452e398b76 | 22 | // This function is a 'quick fix' for now. |
Just4pLeisure | 1:d5452e398b76 | 23 | // |
Just4pLeisure | 1:d5452e398b76 | 24 | // inputs: none |
Just4pLeisure | 1:d5452e398b76 | 25 | // return: bool TRUE if all went OK, |
Just4pLeisure | 1:d5452e398b76 | 26 | // |
Just4pLeisure | 1:d5452e398b76 | 27 | bool T5Open() { |
Just4pLeisure | 1:d5452e398b76 | 28 | can_open (); |
Just4pLeisure | 1:d5452e398b76 | 29 | return TRUE; |
Just4pLeisure | 1:d5452e398b76 | 30 | } |
Just4pLeisure | 1:d5452e398b76 | 31 | |
Just4pLeisure | 1:d5452e398b76 | 32 | // |
Just4pLeisure | 1:d5452e398b76 | 33 | // T5Close disables the CAN chip. |
Just4pLeisure | 1:d5452e398b76 | 34 | // |
Just4pLeisure | 1:d5452e398b76 | 35 | // This function is a 'quick fix' for now. |
Just4pLeisure | 1:d5452e398b76 | 36 | // |
Just4pLeisure | 1:d5452e398b76 | 37 | // inputs: none |
Just4pLeisure | 1:d5452e398b76 | 38 | // return: bool TRUE if all went OK, |
Just4pLeisure | 1:d5452e398b76 | 39 | // |
Just4pLeisure | 1:d5452e398b76 | 40 | bool T5Close() { |
Just4pLeisure | 1:d5452e398b76 | 41 | can_close (); |
Just4pLeisure | 1:d5452e398b76 | 42 | return TRUE; |
Just4pLeisure | 1:d5452e398b76 | 43 | } |
Just4pLeisure | 1:d5452e398b76 | 44 | |
Just4pLeisure | 1:d5452e398b76 | 45 | // |
Just4pLeisure | 1:d5452e398b76 | 46 | // T5WaitResponse |
Just4pLeisure | 1:d5452e398b76 | 47 | // |
Just4pLeisure | 1:d5452e398b76 | 48 | // Waits for a response message with the correct message id from the T5 ECU. |
Just4pLeisure | 1:d5452e398b76 | 49 | // The response is a single ascii character from the symboltable. |
Just4pLeisure | 1:d5452e398b76 | 50 | // |
Just4pLeisure | 1:d5452e398b76 | 51 | // Returns an error ('BELL' character) if: |
Just4pLeisure | 1:d5452e398b76 | 52 | // a response is not received before the timeout |
Just4pLeisure | 1:d5452e398b76 | 53 | // the message length is less than 8 bytes (all messages should be 8 bytes) |
Just4pLeisure | 1:d5452e398b76 | 54 | // the message type is incorrect |
Just4pLeisure | 1:d5452e398b76 | 55 | // |
Just4pLeisure | 1:d5452e398b76 | 56 | // inputs: none |
Just4pLeisure | 1:d5452e398b76 | 57 | // return: a single char that is the response from the T5 ECU |
Just4pLeisure | 1:d5452e398b76 | 58 | // |
Just4pLeisure | 1:d5452e398b76 | 59 | char T5WaitResponse() { |
Just4pLeisure | 1:d5452e398b76 | 60 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 61 | if (!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) return '\a'; |
Just4pLeisure | 1:d5452e398b76 | 62 | if (T5RxMsg[0] != 0xC6) return '\a'; |
Just4pLeisure | 1:d5452e398b76 | 63 | return T5RxMsg[2]; |
Just4pLeisure | 1:d5452e398b76 | 64 | } |
Just4pLeisure | 1:d5452e398b76 | 65 | |
Just4pLeisure | 1:d5452e398b76 | 66 | // |
Just4pLeisure | 1:d5452e398b76 | 67 | // |
Just4pLeisure | 1:d5452e398b76 | 68 | // T5WaitResponsePrint |
Just4pLeisure | 1:d5452e398b76 | 69 | // |
Just4pLeisure | 1:d5452e398b76 | 70 | // Waits for a response message with the correct message id from the T5 ECU and displays the whole message: |
Just4pLeisure | 1:d5452e398b76 | 71 | // |
Just4pLeisure | 1:d5452e398b76 | 72 | // wiiildddddddddddddddd |
Just4pLeisure | 1:d5452e398b76 | 73 | // iii - is the CAN message id |
Just4pLeisure | 1:d5452e398b76 | 74 | // l - is the message length, should always be 8 for T5 messages |
Just4pLeisure | 1:d5452e398b76 | 75 | // dd,dd,dd,dd,dd,dd,dd,dd are the CAN message data bytes |
Just4pLeisure | 1:d5452e398b76 | 76 | // |
Just4pLeisure | 1:d5452e398b76 | 77 | // Returns an error if: |
Just4pLeisure | 1:d5452e398b76 | 78 | // a response is not received before the timeout |
Just4pLeisure | 1:d5452e398b76 | 79 | // the message length is less than 8 bytes (all messages should be 8 bytes) |
Just4pLeisure | 1:d5452e398b76 | 80 | // the message type is incorrect |
Just4pLeisure | 1:d5452e398b76 | 81 | // |
Just4pLeisure | 1:d5452e398b76 | 82 | // Inputs: none |
Just4pLeisure | 1:d5452e398b76 | 83 | // return: bool TRUE if a qualifying message was received |
Just4pLeisure | 1:d5452e398b76 | 84 | // FALSE if a message wasn't received in time, had the wrong id etc... |
Just4pLeisure | 1:d5452e398b76 | 85 | // |
Just4pLeisure | 1:d5452e398b76 | 86 | bool T5WaitResponsePrint() { |
Just4pLeisure | 1:d5452e398b76 | 87 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 88 | if (!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 89 | printf("w%03x8", RESPID); |
Just4pLeisure | 1:d5452e398b76 | 90 | for (char i=0; i<8; i++) { |
Just4pLeisure | 1:d5452e398b76 | 91 | printf("%02x", T5RxMsg[i]); |
Just4pLeisure | 1:d5452e398b76 | 92 | } |
Just4pLeisure | 1:d5452e398b76 | 93 | printf("\n\r"); |
Just4pLeisure | 1:d5452e398b76 | 94 | return TRUE; |
Just4pLeisure | 1:d5452e398b76 | 95 | } |
Just4pLeisure | 1:d5452e398b76 | 96 | |
Just4pLeisure | 1:d5452e398b76 | 97 | // T5GetSymbol |
Just4pLeisure | 1:d5452e398b76 | 98 | // |
Just4pLeisure | 1:d5452e398b76 | 99 | // Reads a single symbol name (in the symbol table) from the T5 ECU. |
Just4pLeisure | 1:d5452e398b76 | 100 | // T5GetSymbol sends ACK messages to the T5 ECU and stores the |
Just4pLeisure | 1:d5452e398b76 | 101 | // reply characters in a string until a '\n' character is received |
Just4pLeisure | 1:d5452e398b76 | 102 | // when the function returns with the pointer to that string. |
Just4pLeisure | 1:d5452e398b76 | 103 | // |
Just4pLeisure | 1:d5452e398b76 | 104 | // inputs: a pointer to the string used to store the symbol name. |
Just4pLeisure | 1:d5452e398b76 | 105 | // return: a pointer to the string used to store the symbol name. |
Just4pLeisure | 1:d5452e398b76 | 106 | // |
Just4pLeisure | 1:d5452e398b76 | 107 | char *T5GetSymbol(char *s) { |
Just4pLeisure | 1:d5452e398b76 | 108 | do { |
Just4pLeisure | 1:d5452e398b76 | 109 | if (T5Ack()) { |
Just4pLeisure | 1:d5452e398b76 | 110 | } |
Just4pLeisure | 1:d5452e398b76 | 111 | *s = T5WaitResponse(); |
Just4pLeisure | 1:d5452e398b76 | 112 | } while (*s++ != '\n'); |
Just4pLeisure | 1:d5452e398b76 | 113 | *s = '\0'; |
Just4pLeisure | 1:d5452e398b76 | 114 | return s; |
Just4pLeisure | 1:d5452e398b76 | 115 | } |
Just4pLeisure | 1:d5452e398b76 | 116 | |
Just4pLeisure | 1:d5452e398b76 | 117 | // T5ReadCmnd |
Just4pLeisure | 1:d5452e398b76 | 118 | // |
Just4pLeisure | 1:d5452e398b76 | 119 | // Sends a 'read' type command to the T5 ECU. 'read' commands are used to read something from the T5 ECU. |
Just4pLeisure | 1:d5452e398b76 | 120 | // |
Just4pLeisure | 1:d5452e398b76 | 121 | // inputs: a 'char' which is the type of 'read' requested (e.g. symbol table, version etc). |
Just4pLeisure | 1:d5452e398b76 | 122 | // return: bool TRUE if the message was sent within the timeout period, |
Just4pLeisure | 1:d5452e398b76 | 123 | // FALSE if the message couldn't be sent. |
Just4pLeisure | 1:d5452e398b76 | 124 | // |
Just4pLeisure | 1:d5452e398b76 | 125 | bool T5ReadCmnd(char c) { |
Just4pLeisure | 1:d5452e398b76 | 126 | char T5TxMsg[] = T5_RDCMND; |
Just4pLeisure | 1:d5452e398b76 | 127 | T5TxMsg[1] = c; |
Just4pLeisure | 1:d5452e398b76 | 128 | return (can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)); |
Just4pLeisure | 1:d5452e398b76 | 129 | } |
Just4pLeisure | 1:d5452e398b76 | 130 | |
Just4pLeisure | 1:d5452e398b76 | 131 | // |
Just4pLeisure | 1:d5452e398b76 | 132 | // T5WriteCmnd is unfinished |
Just4pLeisure | 1:d5452e398b76 | 133 | // |
Just4pLeisure | 1:d5452e398b76 | 134 | bool T5WriteCmnd(unsigned int addr, char data) { |
Just4pLeisure | 1:d5452e398b76 | 135 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 136 | } |
Just4pLeisure | 1:d5452e398b76 | 137 | |
Just4pLeisure | 1:d5452e398b76 | 138 | |
Just4pLeisure | 1:d5452e398b76 | 139 | |
Just4pLeisure | 1:d5452e398b76 | 140 | // t5_can_read_data |
Just4pLeisure | 1:d5452e398b76 | 141 | // |
Just4pLeisure | 1:d5452e398b76 | 142 | // Reads 6 bytes of the data from the SRAM or FLASH in the T5 ECU. |
Just4pLeisure | 1:d5452e398b76 | 143 | // |
Just4pLeisure | 1:d5452e398b76 | 144 | // There is something back-to-front about the way this works, but it does :-) |
Just4pLeisure | 1:d5452e398b76 | 145 | // |
Just4pLeisure | 1:d5452e398b76 | 146 | // inputs: a pointer to an array of 6 bytes for storing the data read from the T5 ECU. |
Just4pLeisure | 1:d5452e398b76 | 147 | // the address to read SRAM data from in the T5 ECU |
Just4pLeisure | 1:d5452e398b76 | 148 | // return: a pointer to an array of 6 bytes for storing the data read from the T5 ECU. |
Just4pLeisure | 1:d5452e398b76 | 149 | // bool TRUE if the message was sent within the timeout period, |
Just4pLeisure | 1:d5452e398b76 | 150 | // FALSE if the message couldn't be sent. |
Just4pLeisure | 1:d5452e398b76 | 151 | // |
Just4pLeisure | 1:d5452e398b76 | 152 | bool t5_can_read_data(char *data, uint32_t addr) { |
Just4pLeisure | 1:d5452e398b76 | 153 | |
Just4pLeisure | 1:d5452e398b76 | 154 | // send a can message to the T5 with the address we want to read from |
Just4pLeisure | 1:d5452e398b76 | 155 | char T5TxMsg[] = T5RAMCMND; |
Just4pLeisure | 1:d5452e398b76 | 156 | T5TxMsg[1] = ((uint8_t)(addr >> 24)); // high high byte of address |
Just4pLeisure | 1:d5452e398b76 | 157 | T5TxMsg[2] = ((uint8_t)(addr >> 16)); // high low byte of address |
Just4pLeisure | 1:d5452e398b76 | 158 | T5TxMsg[3] = ((uint8_t)(addr >> 8)); // low high byte of address |
Just4pLeisure | 1:d5452e398b76 | 159 | T5TxMsg[4] = ((uint8_t)(addr)); // low low byte of address |
Just4pLeisure | 1:d5452e398b76 | 160 | // if the message cannot be sent then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 161 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 162 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 163 | // wait for the T5 to reply with some data |
Just4pLeisure | 1:d5452e398b76 | 164 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 165 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 166 | if ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 1:d5452e398b76 | 167 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 168 | for (int i=2; i<8; i++) |
Just4pLeisure | 1:d5452e398b76 | 169 | data[7-i] = T5RxMsg[i]; |
Just4pLeisure | 1:d5452e398b76 | 170 | return TRUE; |
Just4pLeisure | 1:d5452e398b76 | 171 | } |
Just4pLeisure | 1:d5452e398b76 | 172 | |
Just4pLeisure | 1:d5452e398b76 | 173 | // T5Ack |
Just4pLeisure | 1:d5452e398b76 | 174 | // |
Just4pLeisure | 1:d5452e398b76 | 175 | // Sends an 'ACK' message to the T5 ECU to prompt the T5 to send the next |
Just4pLeisure | 1:d5452e398b76 | 176 | // ascii character from the symboltable |
Just4pLeisure | 1:d5452e398b76 | 177 | // |
Just4pLeisure | 1:d5452e398b76 | 178 | // Returns an error if: |
Just4pLeisure | 1:d5452e398b76 | 179 | // the 'ACK' message cannot be sent before the timeout |
Just4pLeisure | 1:d5452e398b76 | 180 | // |
Just4pLeisure | 1:d5452e398b76 | 181 | // inputs: none |
Just4pLeisure | 1:d5452e398b76 | 182 | // return: bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 183 | // FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 184 | |
Just4pLeisure | 1:d5452e398b76 | 185 | bool T5Ack() { |
Just4pLeisure | 1:d5452e398b76 | 186 | char T5TxMsg[] = T5ACKCMND; |
Just4pLeisure | 1:d5452e398b76 | 187 | return (can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)); |
Just4pLeisure | 1:d5452e398b76 | 188 | } |
Just4pLeisure | 1:d5452e398b76 | 189 | |
Just4pLeisure | 1:d5452e398b76 | 190 | // |
Just4pLeisure | 1:d5452e398b76 | 191 | // t5_can_send_boot_address |
Just4pLeisure | 1:d5452e398b76 | 192 | // |
Just4pLeisure | 1:d5452e398b76 | 193 | // Send an address where 'bootloader' or FLASH CAN messages are uploaded to. |
Just4pLeisure | 1:d5452e398b76 | 194 | // The number of bytes (up to 0x7F) that will be sent in the following CAN messages is also sent. |
Just4pLeisure | 1:d5452e398b76 | 195 | // |
Just4pLeisure | 1:d5452e398b76 | 196 | // inputs: an unsigned int, the address where messages should be sent. |
Just4pLeisure | 1:d5452e398b76 | 197 | // an int, the number of bytes (up to 0x7F) that will be sent in the following CAN messages. |
Just4pLeisure | 1:d5452e398b76 | 198 | // return: bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 199 | // FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 200 | // |
Just4pLeisure | 1:d5452e398b76 | 201 | bool t5_can_send_boot_address(uint32_t addr, uint8_t len) { |
Just4pLeisure | 1:d5452e398b76 | 202 | // send a can message to the T5 with the address and number of bytes we are going to send |
Just4pLeisure | 1:d5452e398b76 | 203 | char T5TxMsg[] = T5_BTCMND; |
Just4pLeisure | 1:d5452e398b76 | 204 | T5TxMsg[1] = ((uint8_t)(addr >> 24)); // high high byte of address |
Just4pLeisure | 1:d5452e398b76 | 205 | T5TxMsg[2] = ((uint8_t)(addr >> 16)); // high low byte of address |
Just4pLeisure | 1:d5452e398b76 | 206 | T5TxMsg[3] = ((uint8_t)(addr >> 8)); // low high byte of address |
Just4pLeisure | 1:d5452e398b76 | 207 | T5TxMsg[4] = ((uint8_t)(addr)); // low low byte of address |
Just4pLeisure | 1:d5452e398b76 | 208 | T5TxMsg[5] = (len); // number of bytes to upload (sent in following messages) |
Just4pLeisure | 1:d5452e398b76 | 209 | // if the message cannot be sent then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 210 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 211 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 212 | // wait for the T5 to reply |
Just4pLeisure | 1:d5452e398b76 | 213 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 214 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 215 | return ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 1:d5452e398b76 | 216 | ? FALSE : TRUE; |
Just4pLeisure | 1:d5452e398b76 | 217 | } |
Just4pLeisure | 1:d5452e398b76 | 218 | |
Just4pLeisure | 1:d5452e398b76 | 219 | // |
Just4pLeisure | 1:d5452e398b76 | 220 | // t5_can_send_boot_frame |
Just4pLeisure | 1:d5452e398b76 | 221 | // |
Just4pLeisure | 1:d5452e398b76 | 222 | // Upload 'bootloader' or new FLASH byts to T5 ECU in CAN messages |
Just4pLeisure | 1:d5452e398b76 | 223 | // |
Just4pLeisure | 1:d5452e398b76 | 224 | // The CAN messages are made up of 1 byte which is added to the address given in the T5SendBootAddress meaassge |
Just4pLeisure | 1:d5452e398b76 | 225 | // followed by 7 bytes of data. |
Just4pLeisure | 1:d5452e398b76 | 226 | // The first byte normally starts at 0x00 and increases by 7 in following messages until another T5SendBootAddress message is sent e.g. |
Just4pLeisure | 1:d5452e398b76 | 227 | // 00,dd,dd,dd,dd,dd,dd,dd,dd 'dd' bytes are the data bytes, 7 in each CAN message except the last one which may have less. |
Just4pLeisure | 1:d5452e398b76 | 228 | // 07,dd,dd,dd,dd,dd,dd,dd,dd |
Just4pLeisure | 1:d5452e398b76 | 229 | // 0E,dd,dd,dd,dd,dd,dd,dd,dd (0x0E is 14) |
Just4pLeisure | 1:d5452e398b76 | 230 | // 15,dd,dd,dd,dd,dd,dd,dd,dd (0x15 is 21) |
Just4pLeisure | 1:d5452e398b76 | 231 | // etc. |
Just4pLeisure | 1:d5452e398b76 | 232 | // |
Just4pLeisure | 1:d5452e398b76 | 233 | // inputs: a pointer to an array of 8 bytes to be sent to the T5 ECU SRAM or FLASH. |
Just4pLeisure | 1:d5452e398b76 | 234 | // return: bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 235 | // FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 236 | // |
Just4pLeisure | 1:d5452e398b76 | 237 | bool t5_can_send_boot_frame (char *T5TxMsg) { |
Just4pLeisure | 1:d5452e398b76 | 238 | // send a can message to the T5 with the bytes we are sending |
Just4pLeisure | 1:d5452e398b76 | 239 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 240 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 241 | // wait for the T5 to reply |
Just4pLeisure | 1:d5452e398b76 | 242 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 243 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 244 | return ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 1:d5452e398b76 | 245 | ? FALSE : TRUE; |
Just4pLeisure | 1:d5452e398b76 | 246 | } |
Just4pLeisure | 1:d5452e398b76 | 247 | |
Just4pLeisure | 1:d5452e398b76 | 248 | // |
Just4pLeisure | 1:d5452e398b76 | 249 | // T5StartBootLoader |
Just4pLeisure | 1:d5452e398b76 | 250 | // |
Just4pLeisure | 1:d5452e398b76 | 251 | // Send the jump to address to the T5 ECU for starting the 'bootloader'. |
Just4pLeisure | 1:d5452e398b76 | 252 | // The jump address must be somewhere in RAM (addresses 0x0000 to 0x8000). |
Just4pLeisure | 1:d5452e398b76 | 253 | // (The start address comes from the S7/8/9 line in the S19 file.) |
Just4pLeisure | 1:d5452e398b76 | 254 | // |
Just4pLeisure | 1:d5452e398b76 | 255 | // inputs: an unsigned int, the address to jump to start of 'bootloader'. |
Just4pLeisure | 1:d5452e398b76 | 256 | // return: bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 257 | // FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 258 | // |
Just4pLeisure | 1:d5452e398b76 | 259 | bool T5StartBootLoader (uint32_t addr) { |
Just4pLeisure | 1:d5452e398b76 | 260 | char T5TxMsg[] = T5JMPCMND; |
Just4pLeisure | 1:d5452e398b76 | 261 | T5TxMsg[3] = ((uint8_t)(addr >> 8)); // low byte of address |
Just4pLeisure | 1:d5452e398b76 | 262 | T5TxMsg[4] = ((uint8_t)(addr)); // high byte of address |
Just4pLeisure | 1:d5452e398b76 | 263 | return (can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)); |
Just4pLeisure | 1:d5452e398b76 | 264 | } |
Just4pLeisure | 1:d5452e398b76 | 265 | |
Just4pLeisure | 1:d5452e398b76 | 266 | // |
Just4pLeisure | 1:d5452e398b76 | 267 | // t5_boot_checksum_command |
Just4pLeisure | 1:d5452e398b76 | 268 | // |
Just4pLeisure | 1:d5452e398b76 | 269 | // Send the checksum command to Bootloader |
Just4pLeisure | 1:d5452e398b76 | 270 | // |
Just4pLeisure | 1:d5452e398b76 | 271 | // inputs: none |
Just4pLeisure | 1:d5452e398b76 | 272 | // return: bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 273 | // FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 274 | // |
Just4pLeisure | 1:d5452e398b76 | 275 | bool t5_boot_checksum_command(uint32_t* result) { |
Just4pLeisure | 1:d5452e398b76 | 276 | *result = 0; |
Just4pLeisure | 1:d5452e398b76 | 277 | // send a can message to the T5 requesting that it calculates the BIN file checksum |
Just4pLeisure | 1:d5452e398b76 | 278 | char T5TxMsg[] = T5SUMCMND; |
Just4pLeisure | 1:d5452e398b76 | 279 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 280 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 281 | // wait for the T5 to reply |
Just4pLeisure | 1:d5452e398b76 | 282 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 283 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 284 | if ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5CHECKSUMTIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 1:d5452e398b76 | 285 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 286 | // get the checksum |
Just4pLeisure | 1:d5452e398b76 | 287 | for (uint8_t i=0; i<4; i++) { |
Just4pLeisure | 1:d5452e398b76 | 288 | *result <<= 8; |
Just4pLeisure | 1:d5452e398b76 | 289 | *result |= T5RxMsg[i+2]; |
Just4pLeisure | 1:d5452e398b76 | 290 | } |
Just4pLeisure | 1:d5452e398b76 | 291 | return TRUE; |
Just4pLeisure | 1:d5452e398b76 | 292 | } |
Just4pLeisure | 1:d5452e398b76 | 293 | |
Just4pLeisure | 1:d5452e398b76 | 294 | // |
Just4pLeisure | 1:d5452e398b76 | 295 | // t5_boot_reset_command |
Just4pLeisure | 1:d5452e398b76 | 296 | // |
Just4pLeisure | 1:d5452e398b76 | 297 | // Send 'exit and restart T5' command to the Bootloader |
Just4pLeisure | 1:d5452e398b76 | 298 | // |
Just4pLeisure | 1:d5452e398b76 | 299 | // inputs: none |
Just4pLeisure | 1:d5452e398b76 | 300 | // return: bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 301 | // FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 302 | // |
Just4pLeisure | 1:d5452e398b76 | 303 | bool t5_boot_reset_command() { |
Just4pLeisure | 1:d5452e398b76 | 304 | // send a can message to the T5 telling it to reset the ECU |
Just4pLeisure | 1:d5452e398b76 | 305 | char T5TxMsg[] = T5RSTCMND; |
Just4pLeisure | 1:d5452e398b76 | 306 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 307 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 308 | // wait for the T5 to reply |
Just4pLeisure | 1:d5452e398b76 | 309 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 310 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 3:92dae9083c83 | 311 | return ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 3:92dae9083c83 | 312 | ? FALSE : TRUE; |
Just4pLeisure | 3:92dae9083c83 | 313 | // if ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 3:92dae9083c83 | 314 | // return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 315 | // wait for the T5 to reset |
Just4pLeisure | 1:d5452e398b76 | 316 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 3:92dae9083c83 | 317 | // return (!can_wait_timeout(RSETID, T5RxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 3:92dae9083c83 | 318 | // ? FALSE : TRUE; |
Just4pLeisure | 1:d5452e398b76 | 319 | } |
Just4pLeisure | 1:d5452e398b76 | 320 | |
Just4pLeisure | 1:d5452e398b76 | 321 | //----------------------------------------------------------------------------- |
Just4pLeisure | 1:d5452e398b76 | 322 | /** |
Just4pLeisure | 1:d5452e398b76 | 323 | t5_boot_get_flash_type |
Just4pLeisure | 1:d5452e398b76 | 324 | Send 'get FLASH chip types' command to the Bootloader |
Just4pLeisure | 1:d5452e398b76 | 325 | |
Just4pLeisure | 1:d5452e398b76 | 326 | @param result start address of T5.x FLASH |
Just4pLeisure | 1:d5452e398b76 | 327 | make FLASH chip manufacturer code |
Just4pLeisure | 1:d5452e398b76 | 328 | type FLASH chip type code |
Just4pLeisure | 1:d5452e398b76 | 329 | |
Just4pLeisure | 1:d5452e398b76 | 330 | @return bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 331 | FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 332 | */ |
Just4pLeisure | 1:d5452e398b76 | 333 | bool t5_boot_get_flash_type(uint32_t* result, uint8_t* make, uint8_t* type) { |
Just4pLeisure | 1:d5452e398b76 | 334 | *result = 0; |
Just4pLeisure | 1:d5452e398b76 | 335 | *make = 0; |
Just4pLeisure | 1:d5452e398b76 | 336 | *type = 0; |
Just4pLeisure | 1:d5452e398b76 | 337 | // send a can message to the T5 requesting that it tells us the type of FLASH chips |
Just4pLeisure | 1:d5452e398b76 | 338 | char T5TxMsg[] = T5TYPCMND; |
Just4pLeisure | 1:d5452e398b76 | 339 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 340 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 341 | // wait for the T5 to reply |
Just4pLeisure | 1:d5452e398b76 | 342 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 343 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 344 | if ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 1:d5452e398b76 | 345 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 346 | // get the start address |
Just4pLeisure | 1:d5452e398b76 | 347 | for (uint8_t i=0; i<4; i++) { |
Just4pLeisure | 1:d5452e398b76 | 348 | *result <<= 8; |
Just4pLeisure | 1:d5452e398b76 | 349 | *result |= T5RxMsg[i+2]; |
Just4pLeisure | 1:d5452e398b76 | 350 | } |
Just4pLeisure | 1:d5452e398b76 | 351 | // get the manufacture code |
Just4pLeisure | 1:d5452e398b76 | 352 | *make = T5RxMsg[6]; |
Just4pLeisure | 1:d5452e398b76 | 353 | // get the FLASH type code |
Just4pLeisure | 1:d5452e398b76 | 354 | *type = T5RxMsg[7]; |
Just4pLeisure | 1:d5452e398b76 | 355 | return TRUE; |
Just4pLeisure | 1:d5452e398b76 | 356 | } |
Just4pLeisure | 1:d5452e398b76 | 357 | |
Just4pLeisure | 1:d5452e398b76 | 358 | //----------------------------------------------------------------------------- |
Just4pLeisure | 1:d5452e398b76 | 359 | /** |
Just4pLeisure | 1:d5452e398b76 | 360 | t5_boot_erase_command |
Just4pLeisure | 1:d5452e398b76 | 361 | Send 'erase FLASH chip types' command to the Bootloader |
Just4pLeisure | 1:d5452e398b76 | 362 | |
Just4pLeisure | 1:d5452e398b76 | 363 | @param none |
Just4pLeisure | 1:d5452e398b76 | 364 | |
Just4pLeisure | 1:d5452e398b76 | 365 | @return bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 366 | FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 367 | */ |
Just4pLeisure | 1:d5452e398b76 | 368 | bool t5_boot_erase_command() { |
Just4pLeisure | 1:d5452e398b76 | 369 | // send a can message to the T5 telling it to erase the FLASH chips |
Just4pLeisure | 1:d5452e398b76 | 370 | char T5TxMsg[] = T5ERACMND; |
Just4pLeisure | 1:d5452e398b76 | 371 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 372 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 373 | // wait for the T5 to reply |
Just4pLeisure | 1:d5452e398b76 | 374 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 375 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 376 | return ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5ERASETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 1:d5452e398b76 | 377 | ? FALSE : TRUE; |
Just4pLeisure | 1:d5452e398b76 | 378 | } |
Just4pLeisure | 1:d5452e398b76 | 379 | |
Just4pLeisure | 1:d5452e398b76 | 380 | //----------------------------------------------------------------------------- |
Just4pLeisure | 1:d5452e398b76 | 381 | /** |
Just4pLeisure | 1:d5452e398b76 | 382 | t5_boot_c3_command |
Just4pLeisure | 1:d5452e398b76 | 383 | Send 'C3 - Get last address' command to the Bootloader |
Just4pLeisure | 1:d5452e398b76 | 384 | |
Just4pLeisure | 1:d5452e398b76 | 385 | @param result last address of T5 FLASH |
Just4pLeisure | 1:d5452e398b76 | 386 | mode might indicate if bootloader is loaded |
Just4pLeisure | 1:d5452e398b76 | 387 | |
Just4pLeisure | 1:d5452e398b76 | 388 | @return bool TRUE if message was sent OK, |
Just4pLeisure | 1:d5452e398b76 | 389 | FALSE if message could not be sent. |
Just4pLeisure | 1:d5452e398b76 | 390 | */ |
Just4pLeisure | 1:d5452e398b76 | 391 | bool t5_boot_c3_command(uint32_t* result, uint16_t* mode) { |
Just4pLeisure | 1:d5452e398b76 | 392 | *result = 0; |
Just4pLeisure | 1:d5452e398b76 | 393 | *mode = 0; |
Just4pLeisure | 1:d5452e398b76 | 394 | // send a can message to the T5 requesting that it tells us the last address of FLASH |
Just4pLeisure | 1:d5452e398b76 | 395 | char T5TxMsg[] = T5EOFCMND; |
Just4pLeisure | 1:d5452e398b76 | 396 | if (!can_send_timeout (CMNDID, T5TxMsg, 8, T5MESSAGETIMEOUT)) |
Just4pLeisure | 1:d5452e398b76 | 397 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 398 | // wait for the T5 to reply |
Just4pLeisure | 1:d5452e398b76 | 399 | char T5RxMsg[8]; |
Just4pLeisure | 1:d5452e398b76 | 400 | // if a message is not received, has the wrong type or indicates an error then there is a problem |
Just4pLeisure | 1:d5452e398b76 | 401 | if ((!can_wait_timeout(RESPID, T5RxMsg, 8, T5MESSAGETIMEOUT)) || (T5RxMsg[0] != T5TxMsg[0]) || (T5RxMsg[1] != 0x00)) |
Just4pLeisure | 1:d5452e398b76 | 402 | return FALSE; |
Just4pLeisure | 1:d5452e398b76 | 403 | // get the checksum |
Just4pLeisure | 1:d5452e398b76 | 404 | for (uint8_t i=0; i<4; i++) { |
Just4pLeisure | 1:d5452e398b76 | 405 | *result <<= 8; |
Just4pLeisure | 1:d5452e398b76 | 406 | *result |= T5RxMsg[i+2]; |
Just4pLeisure | 1:d5452e398b76 | 407 | } |
Just4pLeisure | 1:d5452e398b76 | 408 | for (uint8_t i=0; i<2; i++) { |
Just4pLeisure | 1:d5452e398b76 | 409 | *mode <<= 8; |
Just4pLeisure | 1:d5452e398b76 | 410 | *mode |= T5RxMsg[i+6]; |
Just4pLeisure | 1:d5452e398b76 | 411 | } |
Just4pLeisure | 1:d5452e398b76 | 412 | return TRUE; |
Just4pLeisure | 1:d5452e398b76 | 413 | } |