Code to load a LPC1114 over tx/rx. I have only tested with a 1114 chip but it should work with other LPC uControllers
Dependencies: DirectoryList MODSERIAL mbed
Fork of ika_shouyu_poppoyaki by
Diff: main.cpp
- Revision:
- 1:54e619428ae6
- Parent:
- 0:6baefda2e511
- Child:
- 2:8d75eb0ecd20
--- a/main.cpp Sat Aug 24 06:57:50 2013 +0000 +++ b/main.cpp Sat Aug 24 13:08:37 2013 +0000 @@ -10,9 +10,11 @@ Serial target (p28,p27); LocalFileSystem local("local"); -#define SOURCE_FILE "/local/bin" -#define STR_BUFF_SIZE 128 +#define SOURCE_FILE "/local/bin" +#define STR_BUFF_SIZE 64 +#define RAM_START_ADDRESS 0x10000300L +#define SECTOR_SIZE 4096 void put_string( char *s ); void get_string( char *s ); @@ -35,9 +37,7 @@ put_string( command ); get_string( rtn_str ); // just readout echoback - printf( " RTN0=%s\r\n", rtn_str ); get_string( rtn_str ); - printf( " RTN1=%s\r\n", rtn_str ); return ( strcmp( expected_return_str, rtn_str ) ); } @@ -65,13 +65,13 @@ long bin2uue( char *bin, char *str ) { unsigned long v; - long check_sum = 0; + long checksum = 0; int strpos = 0; *(str + strpos++) = ' ' + 45; for ( int i = 0; i < 45; i += 3 ) { - check_sum += *(bin + i + 0) + *(bin + i + 1) + *(bin + i + 2); + checksum += *(bin + i + 0) + *(bin + i + 1) + *(bin + i + 2); v = (*(bin + i + 0) << 16) | (*(bin + i + 1) << 8) | (*(bin + i + 2) << 0); *(str + strpos++) = uue_table[ (v >> 18) & 0x3F ]; *(str + strpos++) = uue_table[ (v >> 12) & 0x3F ]; @@ -81,19 +81,53 @@ *(str + strpos++) = '\n'; *(str + strpos++) = '\0'; - return check_sum; + return checksum; } +void add_isp_checksum( char *b ) +{ + // see http://www.lpcware.com/content/nxpfile/lpc177x8x-checksum-insertion-program + + unsigned int *p; + unsigned int cksum = 0; + + p = (unsigned int *)b; + + for ( int i = 0; i < 7; i++ ) { + cksum += *p++; + } + + printf( " -- value at checksum slot : 0x%08X\r\n", *p ); + + *p = 0xFFFFFFFF - cksum + 1; + printf( " -- calculated checksum : 0x%08X\r\n", *p ); + + printf( " new checksum will be used to program flash\r\n" ); +} + + + +void erase_sectors( int last_sector ) +{ + char command_str[ STR_BUFF_SIZE ]; + + sprintf( command_str, "P 0 %d\r\n", last_sector ); + + printf( "\"%s\" %s\r\n", command_str, try_and_check( command_str, "0", 0 ) ? "Fail" : "Pass" ); + + *(command_str) = 'E'; + printf( "\"%s\" %s\r\n", command_str, try_and_check( command_str, "0", 0 ) ? "Fail" : "Pass" ); +} #define FLASH_WRITING_SIZE 1024 #define TRANSFER_SIZE (24 * 45) char b[ TRANSFER_SIZE ]; -void sending_data( FILE *fp ) +void write_binary_data( FILE *fp ) { char command_str[ STR_BUFF_SIZE ]; - long check_sum = 0; + long checksum = 0; int transfer_count = 0; int size; @@ -104,19 +138,23 @@ while ( size = fread( b, sizeof( char ), FLASH_WRITING_SIZE, fp ) ) { - sprintf( command_str, "W %ld %ld\r\n", 0x10000300L, 1080 ); + if ( !transfer_count ) { + add_isp_checksum( b ); + } + + sprintf( command_str, "W %ld %ld\r\n", RAM_START_ADDRESS, 1080 ); printf( "\"%s\" %s\r\n", command_str, try_and_check( command_str, "0", 0 ) ? "Fail" : "Pass" ); for ( int i = 0; i < 24; i++ ) { - check_sum += bin2uue( b + (i * 45), command_str ); + checksum += bin2uue( b + (i * 45), command_str ); printf( "%02d %s\r\n", i, command_str ); put_string( command_str ); if ( (i == 19) || (i == 23) ) { - sprintf( command_str, "%ld\n", check_sum ); - printf( " %ld %s\r\n", check_sum, command_str ); + sprintf( command_str, "%ld\n", checksum ); + printf( " %ld %s\r\n", checksum, command_str ); printf( "\"%s\" %s\r\n", command_str, try_and_check( command_str, "OK", 0 ) ? "Fail" : "Pass" ); - check_sum = 0; + checksum = 0; } } @@ -130,11 +168,35 @@ printf( "\"G 0 T\" %s\r\n", try_and_check( "G 0 T\r\n", "0", 0 ) ? "Fail" : "Pass" ); } +int file_size( FILE *fp ) +{ + int size; + + fseek( fp, 0, SEEK_END ); // seek to end of file + size = ftell( fp ); // get current file pointer + fseek( fp, 0, SEEK_SET ); // seek back to beginning of file + + return size; +} + +void reset_target( int isp_pin_state ) +{ + reset_pin = 1; + isp_pin = 0; + wait_ms( 100 ); + reset_pin = 0; + wait_ms( 100 ); + reset_pin = 1; + wait_ms( 100 ); +} + int main() { FILE *fp; char str_buf0[ STR_BUFF_SIZE ]; char str_buf1[ STR_BUFF_SIZE ]; + int data_size; + int last_sector; pc.baud(9600); target.baud(9600); @@ -144,15 +206,12 @@ return ( 1 ); } + data_size = file_size( fp ); + last_sector = data_size / SECTOR_SIZE; printf( "\r\n\r\ntarget RESET\r\n" ); + printf( "data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 ); - reset_pin = 1; - isp_pin = 0; - wait_ms( 100 ); - reset_pin = 0; - wait_ms( 100 ); - reset_pin = 1; - wait_ms( 100 ); + reset_target( 0 ); printf( "\"?\" >> \"Synchronized\" %s\r\n", try_and_check( "?", "Synchronized", 0 ) ? "Fail" : "Pass" ); printf( "\"Synchronized\\r\\n\" %s\r\n", try_and_check2( "Synchronized\r\n", "OK", 0 ) ? "Fail" : "Pass" ); @@ -180,10 +239,10 @@ get_string( str_buf0 ); printf( " result of \"N\" = %s %lX\r\n", str_buf0, atol( str_buf0 ) ); #endif - printf( "\"P 0 0\" %s\r\n", try_and_check( "P 0 0\r\n", "0", 0 ) ? "Fail" : "Pass" ); - printf( "\"E 0 0\" %s\r\n", try_and_check( "E 0 0\r\n", "0", 0 ) ? "Fail" : "Pass" ); - sending_data( fp ); + erase_sectors( last_sector ); + + write_binary_data( fp ); fclose( fp ); while ( 1 )