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:
- 8:b220fadbb3d8
- Parent:
- 7:815366f003ee
- Child:
- 9:ca4c9a2ac8e1
--- a/main.cpp Sun Aug 25 03:30:28 2013 +0000 +++ b/main.cpp Sun Aug 25 06:22:33 2013 +0000 @@ -8,25 +8,22 @@ LocalFileSystem local( "local" ); #define SOURCE_FILE "/local/bin" -//#define BAUD_RATE 9600 +#define BAUD_RATE 115200 //#define BAUD_RATE 57600 -#define BAUD_RATE 115200 +//#define BAUD_RATE 9600 -#define STR_BUFF_SIZE 64 +#define SECTOR_SIZE 4096 #define RAM_START_ADDRESS 0x10000000 + #define RAM_WRITE_OFFSET 0x200 #define RAM_WRITE_START (RAM_START_ADDRESS + RAM_WRITE_OFFSET) -#define SECTOR_SIZE 4096 +#define BYTES_PER_LINE 45 +#define STR_BUFF_SIZE 64 -//#define FLASH_WRITING_SIZE 1024 // This value should be 256, 512, 1024 or 4096 -#define FLASH_WRITING_SIZE 512 // This value should be 256, 512, 1024 or 4096 -#define BYTES_PER_LINE 45 -#define LINES_PER_TRANSFER (((FLASH_WRITING_SIZE / BYTES_PER_LINE) + 3) & ~0x3) -#define TRANSFER_SIZE (LINES_PER_TRANSFER * BYTES_PER_LINE) +#pragma diag_suppress 1293 // surpressing a warning message of "assignment in condition" ;) -#pragma diag_suppress 1293 // surpressing a warning message of "assignment in condition" - -char uue_table[ 64 ]; +char uue_table[ 64 ]; +int error_state = 0; enum { ENTER_TO_ISP_MODE, @@ -59,11 +56,11 @@ int data_size; int last_sector; target_param *tpp; - + printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" ); target.baud( BAUD_RATE ); - + printf( " opening file: \"%s\"\r\n", SOURCE_FILE ); if ( NULL == (fp = fopen( SOURCE_FILE, "rb" )) ) { @@ -76,40 +73,46 @@ printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 ); printf( " resetting target\r\n" ); - + reset_target( ENTER_TO_ISP_MODE ); - + try_and_check( "?", "Synchronized", 0 ); - + try_and_check2( "Synchronized\r\n", "OK", 0 ); try_and_check2( "12000\r\n", "OK", 0 ); try_and_check2( "U 23130\r\n", "0", 0 ); try_and_check2( "A 0\r\n", "0", 0 ); - + try_and_check( "K\r\n", "0", 0 ); get_string( str_buf0 ); get_string( str_buf1 ); - + printf( " result of \"K\" = %s %s\r\n", str_buf0, str_buf1 ); - + try_and_check( "J\r\n", "0", 0 ); get_string( str_buf0 ); - + printf( " result of \"J\" = %s\r\n", str_buf0 ); - + tpp = find_target_param( str_buf0 ); - printf( "target device found : type = \"%s\"\r\n", tpp->type_name ); - printf( " ID = 0x%08X\r\n", tpp->id ); - printf( " RAM size = %10d bytes\r\n", tpp->ram_size ); - printf( " flash size = %10d bytes\r\n", tpp->flash_size ); - + printf( " target device found : type = \"%s\"\r\n", tpp->type_name ); + printf( " ID = 0x%08X\r\n", tpp->id ); + printf( " RAM size = %10d bytes\r\n", tpp->ram_size ); + printf( " flash size = %10d bytes\r\n", tpp->flash_size ); + erase_sectors( last_sector ); write_binary_data( fp, tpp->ram_size ); - + fclose( fp ); - + + printf( "\r\n %s\r\n\r\n", + error_state ? + "** The data could not be written :(" : + "** The data has been written successflly :)" + ); + int i = 0; - + while ( 1 ) { leds = 0x1 << (i++ & 0x3); wait( 0.1 ); @@ -120,11 +123,11 @@ 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; } @@ -144,14 +147,19 @@ int try_and_check( char *command, char *expected_return_str, int mode ) { char rtn_str[ STR_BUFF_SIZE ]; - int result; - + int result = 1; + print_command( command ); put_string( command ); - + get_string( rtn_str ); print_result( result = strcmp( expected_return_str, rtn_str ) ); - + + if ( result && !mode ) + error( "command failed\r\n" ); + + error_state |= result; + return ( result ); } @@ -159,15 +167,20 @@ int try_and_check2( char *command, char *expected_return_str, int mode ) { char rtn_str[ STR_BUFF_SIZE ]; - int result; + int result = 1; + print_command( command ); - put_string( command ); - + get_string( rtn_str ); // just readout echoback get_string( rtn_str ); print_result( result = strcmp( expected_return_str, rtn_str ) ); - + + if ( result && !mode ) + error( "command failed\r\n" ); + + error_state |= result; + return ( result ); } @@ -176,15 +189,15 @@ { char s[ STR_BUFF_SIZE ]; char *pos; - + strcpy( s, command ); - + if ( pos = strchr( s, '\r' ) ) *pos = '\0'; - + if ( pos = strchr( s, '\n' ) ) *pos = '\0'; - + printf( " command-\"%s\" : ", s ); } @@ -199,7 +212,7 @@ { while ( !target.readable() ) ; - + return ( target.getc() ); } @@ -207,10 +220,10 @@ void erase_sectors( int last_sector ) { char command_str[ STR_BUFF_SIZE ]; - + sprintf( command_str, "P 0 %d\r\n", last_sector ); try_and_check( command_str, "0", 0 ); - + *(command_str) = 'E'; try_and_check( command_str, "0", 0 ); } @@ -223,66 +236,66 @@ int transfer_count = 0; int total_size = 0; int size; - + int flash_writing_size; int lines_per_transfer; int transfer_size; - + initialize_uue_table(); - + flash_writing_size = get_flash_writing_size( ram_size ); lines_per_transfer=(((flash_writing_size / BYTES_PER_LINE) + 3) & ~0x3); transfer_size=(lines_per_transfer * BYTES_PER_LINE); - + // char b[ transfer_size ]; // this can be done in mbed-compiler. but I should do it in common way - + char *b; - + if ( NULL == (b = (char *)malloc( transfer_size * sizeof( char ) )) ) error( "malloc error happened\r\n" ); - + for ( int i = flash_writing_size; i < transfer_size; i++ ) b[ i ] = 0; // this is not neccesary but just stuffing stuffing bytes - + while ( size = fread( b, sizeof( char ), flash_writing_size, fp ) ) { - + if ( !total_size ) { // overwriting 4 bytes data for address=0x1C // there is a slot for checksum that is checked in (target's) boot process add_isp_checksum( b ); } - + sprintf( command_str, "W %ld %ld\r\n", RAM_WRITE_START, transfer_size ); try_and_check( command_str, "0", 0 ); - + for ( int i = 0; i < lines_per_transfer; i++ ) { checksum += bin2uue( b + (i * BYTES_PER_LINE), command_str ); - - printf( "%02d %s\r", i, command_str ); - + + printf( " data -- %02d %s\r", i, command_str ); + put_string( command_str ); - + if ( !((i + 1) % 20) ) { send_RAM_transfer_checksum( checksum ); checksum = 0; } } - + send_RAM_transfer_checksum( checksum ); checksum = 0; - + sprintf( command_str, "P %d %d\r\n", total_size / SECTOR_SIZE, total_size / SECTOR_SIZE ); try_and_check( command_str, "0", 0 ); - + sprintf( command_str, "C %d %d %d\r\n", total_size, RAM_WRITE_START, flash_writing_size ); try_and_check( command_str, "0", 0 ); - + total_size += size; } - + try_and_check( "G 0 T\r\n", "0", 0 ); free( b ); - + return ( transfer_count ); } @@ -290,9 +303,9 @@ void initialize_uue_table( void ) { int i; - + uue_table[0] = 0x60; // 0x20 is translated to 0x60 ! - + for (i = 1; i < 64; i++) { uue_table[i] = (char)(0x20 + i); } @@ -304,9 +317,9 @@ unsigned long v; long checksum = 0; int strpos = 0; - + *(str + strpos++) = ' ' + BYTES_PER_LINE; - + for ( int i = 0; i < BYTES_PER_LINE; i += 3 ) { checksum += *(bin + i + 0) + *(bin + i + 1) + *(bin + i + 2); v = (*(bin + i + 0) << 16) | (*(bin + i + 1) << 8) | (*(bin + i + 2) << 0); @@ -317,7 +330,7 @@ } *(str + strpos++) = '\n'; *(str + strpos++) = '\0'; - + return checksum; } @@ -332,14 +345,14 @@ }; int available_size; int i; - + available_size = ram_size - RAM_WRITE_OFFSET; - + for ( i = 0; i < sizeof( flash_writing_size ) / sizeof( int ); i++ ) { if ( flash_writing_size[ i ] < available_size ) break; } - + return ( flash_writing_size[ i ] ); } @@ -347,21 +360,21 @@ 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" ); } @@ -369,7 +382,7 @@ void send_RAM_transfer_checksum( int checksum ) { char command[ 16 ]; - + sprintf( command, "%d\n", checksum ); try_and_check( command, "OK", 0 ); } @@ -379,7 +392,7 @@ { char c; static int i = 0; - + while ( c = *s++ ) { target.putc( c ); leds = i++ & 0x1; @@ -391,20 +404,20 @@ { int i = 0; char c = 0; - + do { do { if ( target.readable() ) { c = target.getc(); - + if ( ( c == '\n') || (c == '\r') ) break; - + *s++ = c; i++; } } while ( 1 ); } while ( !i ); - + *s = '\0'; }