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:
- 28:689c3880e0e4
- Parent:
- 27:2b5c1eb39bb5
- Child:
- 29:96e28bc1bd99
diff -r 2b5c1eb39bb5 -r 689c3880e0e4 main.cpp --- a/main.cpp Thu Sep 19 01:08:20 2013 +0000 +++ b/main.cpp Fri Sep 20 00:40:08 2013 +0000 @@ -31,6 +31,7 @@ #include "verification.h" #include "_user_settings.h" #include "ika.h" +#include "error_code.h" BusOut leds( LED4, LED3, LED2, LED1 ); LocalFileSystem local( "local" ); @@ -38,7 +39,7 @@ int error_state = 0; -int post_writing_process( target_param *tpp ); +int isp_flash_write( char *file_name ); int file_size( FILE *fp ); char read_byte( void ); void success_indicator(); @@ -46,58 +47,19 @@ int main() { - FILE *fp; - target_param *tpp; - int data_size; - int last_sector; - - printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" ); - - if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) ) { - error( "couldn't open the taget" ); - return ( 1 ); - } - - 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( " opening file: \"%s\"\r\n", SOURCE_FILE ); - - if ( NULL == (fp = fopen( SOURCE_FILE, "rb" )) ) { - error( "couldn't open source file" ); - return ( 1 ); - } - - data_size = file_size( fp ); - last_sector = data_size / tpp->sector_size; - - printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 ); - printf( " resetting target\r\n" ); - - erase_sectors( last_sector ); - - printf( "\r\n ==== flash writing ====\r\n" ); - write_flash( fp, tpp ); + int err; - printf( "\r\n ==== flash reading and verifying ====\r\n" ); - verify_flash( fp, tpp ); - - fclose( fp ); - + err = isp_flash_write( SOURCE_FILE ); + printf( "\r\n %s\r\n\r\n", - error_state ? + err ? "** The data could not be written :(" : "** The data has been written successflly :)" ); - if ( error_state ) + if ( err ) error( " ** ISP failed\r\n" ); - post_writing_process( tpp ); - - #ifdef AUTO_PROGRAM_START set_target_baud_rate( TARGET_OPERATION_BAUD_RATE ); @@ -113,15 +75,57 @@ } -int post_writing_process( target_param *tpp ) +int isp_flash_write( char *file_name ) { - if ( tpp->write_type == UUENCODE ) - return ( try_and_check( "G 0 T\r\n", "0", 0 ) ); - else - return ( 0 ); + FILE *fp; + target_param *tpp; + int data_size; + int last_sector; + int err; + + if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) ) { + return ( ERROR_AT_TARGET_OPEN ); + } + + 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( " opening file: \"%s\"\r\n", file_name ); + + if ( NULL == (fp = fopen( file_name, "rb" )) ) { + return ( ERROR_AT_FILE_OPEN ); + } + + data_size = file_size( fp ); + last_sector = data_size / tpp->sector_size; + + printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 ); + printf( " resetting target\r\n" ); + + if ( erase_sectors( last_sector ) ) + return ( ERROR_AT_SECTOR_ERASE ); + + printf( "\r\n ==== flash writing ====\r\n" ); + + if ( err = write_flash( fp, tpp ) ) + return ( err ); + + printf( "\r\n ==== flash reading and verifying ====\r\n" ); + + if ( err = verify_flash( fp, tpp ) ) + return ( err ); + + fclose( fp ); + + post_writing_process( tpp ); + + return ( 0 ); } + int file_size( FILE *fp ) { int size; @@ -142,9 +146,9 @@ } -void set_leds( char v ) +void toggle_led( char v ) { - leds = v; + leds = leds ^ (0x1 << v); }