Sophie Dexter
/
Just4Trionic
Just4Trionic - CAN and BDM FLASH programmer for Saab cars
Diff: t7utils.cpp
- Revision:
- 4:682d96ff6d79
- Parent:
- 3:92dae9083c83
- Child:
- 5:1775b4b13232
--- a/t7utils.cpp Tue Jun 07 12:23:28 2011 +0000 +++ b/t7utils.cpp Wed Sep 11 11:55:51 2013 +0000 @@ -1,7 +1,7 @@ /******************************************************************************* t7utils.cpp -(c) 2011 by Sophie Dexter +(c) 2011, 2012 by Sophie Dexter portions (c) Tomi Liljemark (firstname.surname@gmail.com) This C++ module provides functions for communicating simple messages to and from @@ -151,7 +151,7 @@ timer.start(); received = 0; - printf(" %% complete.\r"); + printf(" 0.00 %% complete.\r"); while (received < T7FLASHSIZE) { // T7_dump_jumpa[7] = ((T7FLASHSIZE - received) < 0xEF) ? (T7FLASHSIZE - received) : 0xEF; T7_dump_jumpb[2] = (received >> 16) & 0xFF; @@ -371,7 +371,7 @@ return TRUE; } -bool t7_flash() { +bool t7_flash_raw() { char T7_flash_jumpa[] = T7FLAJP1A; char T7_flash_jumpb[] = T7FLAJP1B; char T7_flash_end[] = T7FLA_END; @@ -447,22 +447,22 @@ uint32_t address = 0; - printf(" %% complete.\r"); + printf(" 0.00 %% complete.\r"); while (address < T7FLASHSIZE) { - data[0] = 0x4A; // 0x40 send, | 0x0A (10) messages to follow +// data[0] = 0x4A; // 0x40 send, | 0x0A (10) messages to follow // data[0] = 0x42; // 0x40 send, | 0x02 (2) messages to follow -// data[0] = 0x40; // 0x40 send, | 0x00 (0) messages to follow + data[0] = 0x40; // 0x40 send, | 0x00 (0) messages to follow data[1] = 0xA1; - data[2] = 0x41; // length+1 (64 Bytes) +// data[2] = 0x41; // length+1 (64 Bytes) // data[2] = 0x11; // length+1 (16 Bytes) -// data[2] = 0x05; // length+1 (4 Bytes) + data[2] = 0x05; // length+1 (4 Bytes) data[3] = 0x36; // Data Transfer for ( k = 4; k < 8; k++ ) //data[k] = *(bin + bin_count++); if (!fread(&data[k],1,1,fp)) { fclose(fp); - printf("Error reading the BIN file MODIFIED.HEX"); + printf("Error reading the BIN file MODIFIED.HEX\r\n"); return FALSE; } // /* DEBUG info... @@ -475,35 +475,36 @@ fclose(fp); return FALSE; } -//* - for (i = 9; i>=0; i--) { -// for (i = 1; i>=0; i--) { - data[0] = i; - // data[1] = 0xA1; - for ( k = 2; k < 8; k++ ) - //data[k] = *(bin + bin_count++); - if (!fread(&data[k],1,1,fp)) { - fclose(fp); - printf("Error reading the BIN file MODIFIED.HEX"); - return FALSE; + /* + for (i = 9; i>=0; i--) { + // for (i = 1; i>=0; i--) { + data[0] = i; + // data[1] = 0xA1; + for ( k = 2; k < 8; k++ ) + //data[k] = *(bin + bin_count++); + if (!fread(&data[k],1,1,fp)) { + fclose(fp); + printf("Error reading the BIN file MODIFIED.HEX\r\n"); + return FALSE; + } + // /* DEBUG info... + // for (k = 0; k < 8; k++ ) printf("0x%02X ", data[k] ); + // for (k = 2; k < 8; k++ ) printf("%c ", data[k] ); + // printf(" data\r\n"); + + // printf("%6.2f\r", 100*(float)address/(float)T7FLASHSIZE ); + wait_ms(1); + // wait_ms(10); // 31/3/12 this longer wait might be needed for i-bus connections... + if (!can_send_timeout (T7SEC_ID, data, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } } -// /* DEBUG info... -// for (k = 0; k < 8; k++ ) printf("0x%02X ", data[k] ); -// for (k = 2; k < 8; k++ ) printf("%c ", data[k] ); -// printf(" data\r\n"); - -// printf("%6.2f\r", 100*(float)address/(float)T7FLASHSIZE ); - wait_ms(1); - if (!can_send_timeout (T7SEC_ID, data, 8, T7MESSAGETIMEOUT)) { - printf("err t7utils line: %d\r\n", __LINE__ ); - fclose(fp); - return FALSE; - } - } -//*/ - address += 0x40; + */ +// address += 0x40; // address += 0x10; -// address += 0x04; + address += 0x04; if (!can_wait_timeout(T7SEC_RX, data, 8, T7LONGERTIMEOUT)) { printf("err t7utils line: %d\r\n", __LINE__ ); fclose(fp); @@ -525,9 +526,115 @@ printf("%6.2f\r", 100*(float)address/(float)T7FLASHSIZE ); } printf("\n"); -/* - // Send "Request Data Transfer Exit" to Trionic - if (!can_send_timeout (T7SEC_ID, T7_flash_end, 8, T7MESSAGETIMEOUT)) { + /* + // Send "Request Data Transfer Exit" to Trionic + if (!can_send_timeout (T7SEC_ID, T7_flash_end, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!can_wait_timeout(T7SEC_RX, data, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + T7_flash_ack[3] = data[0] & 0xBF; + if (!can_send_timeout (T7ACK_ID, T7_flash_ack, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if ( data[3] != 0x77 ) { + printf("Cannot Update FLASH, message refused.\r\n"); + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + // Send "Request Data Transfer Exit" to Trionic + if (!can_send_timeout (T7SEC_ID, T7_flash_exit, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!can_wait_timeout(T7SEC_RX, data, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + T7_flash_ack[3] = data[0] & 0xBF; + if (!can_send_timeout (T7ACK_ID, T7_flash_ack, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if ( data[3] != 0x71 ) { + printf("Cannot Update FLASH, message refused.\r\n"); + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + */ + timer.stop(); + printf("SUCCESS! Programming the FLASH took %#.1f seconds.\r\n",timer.read()); + fclose(fp); + return TRUE; +} + + +bool t7_flash() { + char T7_flash_jumpa[] = T7FLABINA; + char T7_flash_jumpb[] = T7FLABINB; + char T7_flash_jumpc[] = T7FLAHDRA; + char T7_flash_jumpd[] = T7FLAHDRB; + char T7_flash_end[] = T7FLA_END; + char T7_flash_exit[] = T7FLAEXIT; + char T7_flash_ack[] = T7FLA_ACK; + char data[8]; + int i, k; + + // fopen modified.hex here? + // need lots of fcloses though + printf("Checking the FLASH BIN file...\r\n"); + FILE *fp = fopen("/local/modified.hex", "r"); // Open "modified.hex" on the local file system for reading + if (!fp) { + printf("Error: I could not find the BIN file MODIFIED.HEX\r\n");; + return TERM_ERR; + } + // obtain file size - it should match the size of the FLASH chips: + fseek (fp , 0 , SEEK_END); + uint32_t file_size = ftell (fp); + rewind (fp); + + // read the initial stack pointer value in the BIN file - it should match the value expected for the type of ECU + uint8_t stack_byte = 0; + uint32_t stack_long = 0; + if (!fread(&stack_byte,1,1,fp)) return TERM_ERR; + stack_long |= (stack_byte << 24); + if (!fread(&stack_byte,1,1,fp)) return TERM_ERR; + stack_long |= (stack_byte << 16); + if (!fread(&stack_byte,1,1,fp)) return TERM_ERR; + stack_long |= (stack_byte << 8); + if (!fread(&stack_byte,1,1,fp)) return TERM_ERR; + stack_long |= stack_byte; + rewind (fp); + + if (file_size != T7FLASHSIZE || stack_long != T7POINTER) { + fclose(fp); + printf("The BIN file does not appear to be for a T7 ECU :-(\r\n"); + printf("BIN file size: %#010x, FLASH chip size: %#010x, Pointer: %#010x.\r\n", file_size, T7FLASHSIZE, stack_long); + return TERM_ERR; + } + + timer.reset(); + timer.start(); + + // Send "Request Download - tool to module" to Trionic + if (!can_send_timeout (T7SEC_ID, T7_flash_jumpa, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!can_send_timeout (T7SEC_ID, T7_flash_jumpb, 8, T7MESSAGETIMEOUT)) { printf("err t7utils line: %d\r\n", __LINE__ ); fclose(fp); return FALSE; @@ -543,14 +650,62 @@ fclose(fp); return FALSE; } - if ( data[3] != 0x77 ) { + if ( data[3] != 0x74 ) { printf("Cannot Update FLASH, message refused.\r\n"); printf("err t7utils line: %d\r\n", __LINE__ ); fclose(fp); return FALSE; } - // Send "Request Data Transfer Exit" to Trionic - if (!can_send_timeout (T7SEC_ID, T7_flash_exit, 8, T7MESSAGETIMEOUT)) { + + uint32_t address = 0; + + printf(" 0.00 %% complete.\r"); +// FLASH main Binary image up to address 0x7B000 + while (address < 0x70000) { + data[0] = 0x40; // 0x40 send, | 0x00 (0) messages to follow + data[1] = 0xA1; + data[2] = 0x05; // length+1 (4 Bytes) + data[3] = 0x36; // Data Transfer + for ( k = 4; k < 8; k++ ) + if (!fread(&data[k],1,1,fp)) { + fclose(fp); + printf("Error reading the BIN file MODIFIED.HEX\r\n"); + return FALSE; + } + if (!can_send_timeout (T7SEC_ID, data, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + address += 0x04; + if (!can_wait_timeout(T7SEC_RX, data, 8, T7LONGERTIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + // Send acknowledgement + T7_flash_ack[3] = data[0] & 0xBF; + if (!can_send_timeout (T7ACK_ID, T7_flash_ack, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if ( data[3] != 0x76 ) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!(address % 0x80)) + printf("%6.2f\r", 100*(float)address/(float)T7FLASHSIZE ); + } + + // Send "Request Download - tool to module" to Trionic + if (!can_send_timeout (T7SEC_ID, T7_flash_jumpc, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!can_send_timeout (T7SEC_ID, T7_flash_jumpd, 8, T7MESSAGETIMEOUT)) { printf("err t7utils line: %d\r\n", __LINE__ ); fclose(fp); return FALSE; @@ -566,12 +721,106 @@ fclose(fp); return FALSE; } - if ( data[3] != 0x71 ) { + if ( data[3] != 0x74 ) { printf("Cannot Update FLASH, message refused.\r\n"); printf("err t7utils line: %d\r\n", __LINE__ ); fclose(fp); return FALSE; } + + address = 0x7FF00; + fseek (fp , 0x7FF00 , SEEK_SET); + +// FLASH BIN file Header + while (address < T7FLASHSIZE) { + data[0] = 0x40; // 0x40 send, | 0x00 (0) messages to follow + data[1] = 0xA1; + data[2] = 0x05; // length+1 (4 Bytes) + data[3] = 0x36; // Data Transfer + for ( k = 4; k < 8; k++ ) + if (!fread(&data[k],1,1,fp)) { + fclose(fp); + printf("Error reading the BIN file MODIFIED.HEX\r\n"); + return FALSE; + } + if (!can_send_timeout (T7SEC_ID, data, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + address += 0x04; + if (!can_wait_timeout(T7SEC_RX, data, 8, T7LONGERTIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + // Send acknowledgement + T7_flash_ack[3] = data[0] & 0xBF; + if (!can_send_timeout (T7ACK_ID, T7_flash_ack, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if ( data[3] != 0x76 ) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!(address % 0x80)) + printf("%6.2f\r", 100*(float)address/(float)T7FLASHSIZE ); + } + + + printf("\n"); + printf("Restarting T7 ECU"); +/* + // Send "Request Data Transfer Exit" to Trionic + if (!can_send_timeout (T7SEC_ID, T7_flash_end, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!can_wait_timeout(T7SEC_RX, data, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + T7_flash_ack[3] = data[0] & 0xBF; + if (!can_send_timeout (T7ACK_ID, T7_flash_ack, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if ( data[3] != 0x77 ) { + printf("Cannot Update FLASH, message refused.\r\n"); + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } +// + // Send "Request Data Transfer Exit" to Trionic + if (!can_send_timeout (T7SEC_ID, T7_flash_exit, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if (!can_wait_timeout(T7SEC_RX, data, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + T7_flash_ack[3] = data[0] & 0xBF; + if (!can_send_timeout (T7ACK_ID, T7_flash_ack, 8, T7MESSAGETIMEOUT)) { + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } + if ( data[3] != 0x71 ) { + printf("Cannot Update FLASH, message refused.\r\n"); + printf("err t7utils line: %d\r\n", __LINE__ ); + fclose(fp); + return FALSE; + } */ timer.stop(); printf("SUCCESS! Programming the FLASH took %#.1f seconds.\r\n",timer.read());