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:
- 24:9830b4f1207b
- Parent:
- 23:017f306cf3ca
- Child:
- 25:33cb5ad8ae24
--- a/main.cpp Fri Sep 13 03:41:11 2013 +0000 +++ b/main.cpp Fri Sep 13 03:49:11 2013 +0000 @@ -28,6 +28,7 @@ #include "writing.h" #include "uu_coding.h" #include "target_handling.h" +#include "verification.h" #include "ika.h" @@ -56,15 +57,11 @@ int error_state = 0; -int verify_flash( FILE *fp, target_param *tpp ); int post_writing_process( target_param *tpp ); int file_size( FILE *fp ); char read_byte( void ); void erase_sectors( int last_sector ); -int verify_binary_data( FILE *fp ); -int verify_uucoded_data( FILE *fp ); -void get_binary_from_uucode_str( char *b, int size ); void success_indicator(); @@ -139,15 +136,6 @@ } -int verify_flash( FILE *fp, target_param *tpp ) -{ - if ( tpp->write_type == BINARY ) - verify_binary_data( fp ); - else - verify_uucoded_data( fp ); -} - - int post_writing_process( target_param *tpp ) { if ( tpp->write_type == UUENCODE ) @@ -182,189 +170,6 @@ } -int verify_binary_data( FILE *fp ) -{ - char command_str[ STR_BUFF_SIZE ]; - int read_size = 0; - int size; - int flash_reading_size; - char *bf; - char *br; - int error_flag = 0; - unsigned long checksum = 0; - unsigned long checksum_count = 0; - - fseek( fp, 0, SEEK_SET ); // seek back to beginning of file - - flash_reading_size = 128; - - if ( NULL == (bf = (char *)malloc( flash_reading_size * sizeof( char ) )) ) - error( "malloc error happened (in verify process, file data buffer)\r\n" ); - - if ( NULL == (br = (char *)malloc( flash_reading_size * sizeof( char ) )) ) - error( "malloc error happened (in verify process, read data buffer)\r\n" ); - - - printf( "\r\n ==== flash reading and verifying ====\r\n" ); - - while ( size = fread( bf, sizeof( char ), flash_reading_size, fp ) ) { - - if ( read_size < 0x20 ) { - for ( int i = 0; i < flash_reading_size; i += 4 ) { - - if ( checksum_count == 7 ) { - checksum = 0xFFFFFFFF - checksum + 1; - *((unsigned int *)(bf + i)) = checksum; - //printf( "\r\n\r\n -- calculated checksum : 0x%08X\r\n", checksum ); - } else { - checksum += *((unsigned int *)(bf + i)); - } - - checksum_count++; - } - } - - - sprintf( command_str, "R %ld %ld\r\n", read_size, size ); -// try_and_check( command_str, "0", MODE_SILENT ); - try_and_check( command_str, "0", 0 ); - - get_binary( br, 1 ); - get_binary( br, size ); - - for ( int i = 0; i < size; i++ ) { -// printf( " %s 0x%02X --- 0x%02X\r\n", (*(bf + i) != *(br + i)) ? "***" : " ", *(bf + i), *(br + i) ); - if ( (*(bf + i) != *(br + i)) ) { -// printf( " %s 0x%02X --- 0x%02X\r\n", (*(bf + i) != *(br + i)) ? "***" : " ", *(bf + i), *(br + i) ); - error_flag++; - } - } - - if ( error_flag ) - break; - - read_size += size; - -// printf( " total %d bytes read\r\n", read_size ); - } - - error_state |= error_flag; - - printf( " total %d bytes read\r", read_size ); - printf( " verification result : \"%s\"\r\n", error_flag ? "Fail" : "Pass" ); - - free( bf ); - free( br ); - - return ( read_size ); -} - - -int verify_uucoded_data( FILE *fp ) -{ - char command_str[ STR_BUFF_SIZE ]; - int read_size = 0; - int size; - int flash_reading_size; - char *bf; - char *br; - int error_flag = 0; - - flash_reading_size = 176; - - initialize_uud_table(); - - if ( NULL == (bf = (char *)malloc( flash_reading_size * sizeof( char ) )) ) - error( "malloc error happened (in verify process, file data buffer)\r\n" ); - - if ( NULL == (br = (char *)malloc( flash_reading_size * sizeof( char ) )) ) - error( "malloc error happened (in verify process, read data buffer)\r\n" ); - - fseek( fp, 0, SEEK_SET ); // seek back to beginning of file - - while ( size = fread( bf, sizeof( char ), flash_reading_size, fp ) ) { - - if ( !read_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( bf ); - } - - sprintf( command_str, "R %ld %ld\r\n", read_size, size ); - try_and_check( command_str, "0", 0 ); - - get_binary_from_uucode_str( br, size ); - - for ( int i = 0; i < size; i++ ) { -// printf( " %s 0x%02X --- 0x%02X\r\n", (*(bf + i) != *(br + i)) ? "***" : " ", *(bf + i), *(br + i) ); - if ( (*(bf + i) != *(br + i)) ) { - printf( " %s 0x%02X --- 0x%02X\r\n", (*(bf + i) != *(br + i)) ? "***" : " ", *(bf + i), *(br + i) ); - error_flag++; - } - } - - if ( error_flag ) - break; - - read_size += size; -// printf( " total %d bytes read\r", read_size ); - } - - error_state |= error_flag; - - printf( " total %d bytes read\r", read_size ); - printf( " verification result : \"%s\"\r\n", error_flag ? "Fail" : "Pass" ); - - free( bf ); - free( br ); - - return ( read_size ); -} - - -void get_binary_from_uucode_str( char *b, int size ) -{ -#define N 4 - - char s[ N ][ STR_BUFF_SIZE ]; - char ss[ STR_BUFF_SIZE ]; - long checksum = 0; - int line_count = 0; - int read_size = 0; - int retry_count = 3; - - - while ( retry_count-- ) { - - for ( int i = 0; i < N; i++ ) - get_string( s[ i ] ); - - get_string( ss ); - - - while ( size ) { - read_size = uudecode_a_line( b, s[ line_count ] ); - - for ( int i = 0; i < read_size; i++ ) - checksum += *b++; - - size -= read_size; - line_count++; - } - -// printf( " checksum -- %s (internal = %ld)\r\n", ss, checksum ); - - if ( checksum == atol( ss ) ) { - put_string( "OK\r\n" ); - return; -// printf( " checksum OK\r\n" ); - } else { - printf( " checksum RESEND\r\n" ); - put_string( "RESEND\r\n" ); - } - } -} -