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: isp.cpp
- Revision:
- 30:e0d7524661ca
- Child:
- 32:3700d5df4e18
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/isp.cpp Fri Sep 20 02:21:58 2013 +0000 @@ -0,0 +1,112 @@ + +#include "mbed.h" +#include "target_table.h" +#include "serial_utilities.h" +#include "command_interface.h" +#include "writing.h" +#include "uu_coding.h" +#include "target_handling.h" +#include "verification.h" +#include "isp.h" +#include "_user_settings.h" + + +BusOut leds( LED4, LED3, LED2, LED1 ); +Ticker success; + + +int file_size( FILE *fp ); +void success_indicator(); + +int isp_flash_write( char *file_name ) +{ + FILE *fp; + target_param *tpp; + int data_size; + int last_sector; + int transferred_size; + 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, &transferred_size ) ) + return ( err ); + + printf( " -- %d bytes data are written\r\n", transferred_size ); + + + printf( "\r\n ==== flash reading and verifying ====\r\n" ); + + if ( err = verify_flash( fp, tpp, &transferred_size ) ) + return ( err ); + + printf( " -- %d bytes data are read and verified\r\n", transferred_size ); + + fclose( fp ); + + post_writing_process( tpp ); + + return ( 0 ); +} + + +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 start_target_in_normal_mode( int baud_rate ) +{ + set_target_baud_rate( baud_rate ); + reset_target( NO_ISP_MODE ); +} + +void start_success_indicator( void ) +{ + success.attach( &success_indicator, 0.1 ); +} + +void success_indicator() +{ + static int i = 0; + + leds = 0x1 << (i++ & 0x3); +} + + +void toggle_led( char v ) +{ + leds = leds ^ (0x1 << v); +} +