Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ika_shouyu_poppoyaki by
Diff: main.cpp
- Revision:
- 22:bd98a782fba6
- Parent:
- 21:e149d0bdbf4a
- Child:
- 23:017f306cf3ca
--- a/main.cpp Fri Sep 13 03:09:09 2013 +0000
+++ b/main.cpp Fri Sep 13 03:34:43 2013 +0000
@@ -23,7 +23,10 @@
#include "mbed.h"
#include "target_table.h"
-#include "command_utilities.h"
+#include "serial_utilities.h"
+#include "command_interface.h"
+#include "writing.h"
+#include "uu_coding.h"
#include "ika.h"
@@ -39,7 +42,6 @@
#define ENTER_TO_ISP_MODE 0
#define NO_ISP_MODE 1
-#define STR_BUFF_SIZE 64
#define SOURCE_FILE "/local/bin"
@@ -59,30 +61,16 @@
int error_state = 0;
target_param *open_target( int baud_date );
-int write_flash( FILE *fp, target_param *tpp );
int verify_flash( FILE *fp, target_param *tpp );
int post_writing_process( target_param *tpp );
int file_size( FILE *fp );
void reset_target( int isp_pin_state );
-int try_and_check( char *command, char *expected_return_str, int mode );
-int try_and_check2( char *command, char *expected_return_str, int mode );
-void print_command( char *command );
-void print_result( int r );
char read_byte( void );
void erase_sectors( int last_sector );
-int write_uuencoded_data( FILE *fp, int ram_size, int sector_size, unsigned int );
-int write_binary_data( FILE *fp, int ram_size, int sector_size, unsigned int ram_start );
int verify_binary_data( FILE *fp );
int verify_uucoded_data( FILE *fp );
void get_binary_from_uucode_str( char *b, int size );
-int uudecode_a_line( char *b, char *s );
-void initialize_uud_table( void );
-void initialize_uue_table( void );
-long bin2uue( char *bin, char *str, int size );
-int get_flash_writing_size( int ram_size, unsigned int ram_start );
-void add_isp_checksum( char *b );
-void send_RAM_transfer_checksum( int checksum );
void success_indicator();
@@ -157,6 +145,35 @@
}
+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 )
+ try_and_check( "G 0 T\r\n", "0", 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;
+}
+
+
target_param *open_target( int baud_date )
{
target_param *tpp;
@@ -191,43 +208,6 @@
}
-int write_flash( FILE *fp, target_param *tpp )
-{
- if ( tpp->write_type == BINARY )
- write_binary_data( fp, tpp->ram_size, tpp->sector_size, tpp->ram_start_address );
- else // UUENCODE
- write_uuencoded_data( fp, tpp->ram_size, tpp->sector_size, tpp->ram_start_address );
-}
-
-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 )
- try_and_check( "G 0 T\r\n", "0", 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 reset_target( int isp_pin_state )
{
reset_pin = 1;
@@ -242,69 +222,6 @@
}
-int try_and_check( char *command, char *expected_return_str, int mode )
-{
- char rtn_str[ STR_BUFF_SIZE ];
- 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 );
-}
-
-
-int try_and_check2( char *command, char *expected_return_str, int mode )
-{
- char rtn_str[ STR_BUFF_SIZE ];
- 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 );
-}
-
-
-void print_command( char *command )
-{
- 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 );
-}
-
-
-void print_result( int r )
-{
- printf( "%s\r\n", r ? "Fail" : "Pass" );
-}
-
void erase_sectors( int last_sector )
{
@@ -317,124 +234,6 @@
try_and_check( command_str, "0", 0 );
}
-#define BYTES_PER_LINE 45
-char uu_table[ 0x60 + 1 ];
-
-int write_uuencoded_data( FILE *fp, int ram_size, int sector_size, unsigned int ram_start )
-{
- char command_str[ STR_BUFF_SIZE ];
- long checksum = 0;
- int total_size = 0;
- int size;
-
- int flash_writing_size;
- int lines_per_transfer;
- int transfer_size;
-
- char *b;
-
- initialize_uue_table();
-
- flash_writing_size = get_flash_writing_size( ram_size, ram_start );
- lines_per_transfer = ((flash_writing_size / BYTES_PER_LINE) + 1);
- transfer_size = (((flash_writing_size + 11) / 12) * 12);
-
- // char b[ transfer_size ]; // this can be done in mbed-compiler. but I should do it in common way
-
- 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_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, i == (lines_per_transfer - 1) ? (transfer_size % BYTES_PER_LINE) : BYTES_PER_LINE );
-
- // 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_start, flash_writing_size );
- try_and_check( command_str, "0", 0 );
-
- total_size += size;
- }
-
- free( b );
-
- return ( total_size );
-}
-
-
-int write_binary_data( FILE *fp, int ram_size, int sector_size, unsigned int ram_start )
-{
- char command_str[ STR_BUFF_SIZE ];
- int total_size = 0;
- int size;
- int flash_writing_size;
- char *b;
-
- flash_writing_size = 256;
-
- if ( NULL == (b = (char *)malloc( flash_writing_size * sizeof( char ) )) )
- error( "malloc error happened\r\n" );
-
- printf( "\r\n ==== flash writing ====\r\n" );
-
- 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_start, flash_writing_size );
- try_and_check( command_str, "0", 0 );
-
- put_binary( b, flash_writing_size );
- put_string( "\r\n" );
-
- 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_start, flash_writing_size );
- try_and_check( command_str, "0", 0 );
-
- total_size += size;
- //printf( " total %d bytes transferred\r", total_size );
-
- }
-
- free( b );
-
- return ( total_size );
-}
-
int verify_binary_data( FILE *fp )
{
@@ -526,6 +325,8 @@
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" );
@@ -585,7 +386,6 @@
int read_size = 0;
int retry_count = 3;
- initialize_uud_table();
while ( retry_count-- ) {
@@ -619,127 +419,7 @@
}
-int uudecode_a_line( char *b, char *s )
-{
- unsigned long v;
- int read_size;
-
- read_size = (*s++) - ' ';
-
- for ( int i = 0; i < read_size; i += 3 ) {
- v = uu_table[ *s++ ] << 18;
- v |= uu_table[ *s++ ] << 12;
- v |= uu_table[ *s++ ] << 6;
- v |= uu_table[ *s++ ] << 0;
-
- *b++ = (v >> 16) & 0xFF;
- *b++ = (v >> 8) & 0xFF;
- *b++ = (v >> 0) & 0xFF;
- }
-
- return ( read_size );
-}
-
-
-void initialize_uue_table( void )
-{
- int i;
-
- uu_table[ 0 ] = 0x60; // 0x20 is translated to 0x60 !
-
- for ( i = 1; i < 64; i++ ) {
- uu_table[ i ] = (char)(' ' + i);
- }
-}
-
-
-void initialize_uud_table( void )
-{
- int i;
-
- uu_table[ 0x60 ] = 0;
-
- for ( i = 0x21; i < 0x60; i++ ) {
- uu_table[ i ] = i - 0x20;
- }
-}
-
-
-long bin2uue( char *bin, char *str, int size )
-{
- unsigned long v;
- long checksum = 0;
- int strpos = 0;
-
- *(str + strpos++) = ' ' + size;
-
- for ( int i = 0; i < size; 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);
- *(str + strpos++) = uu_table[ (v >> 18) & 0x3F ];
- *(str + strpos++) = uu_table[ (v >> 12) & 0x3F ];
- *(str + strpos++) = uu_table[ (v >> 6) & 0x3F ];
- *(str + strpos++) = uu_table[ (v >> 0) & 0x3F ];
- }
- *(str + strpos++) = '\n';
- *(str + strpos++) = '\0';
-
- return checksum;
-}
-
-
-int get_flash_writing_size( int ram_size, unsigned int ram_start )
-{
- int flash_writing_size[] = {
- 4096,
- 1024,
- 512,
- 256
- };
- int available_size;
- int i;
-
- available_size = ram_size - (ram_start & 0xFFFF);
-
- for ( i = 0; i < sizeof( flash_writing_size ) / sizeof( int ); i++ ) {
- if ( flash_writing_size[ i ] < available_size )
- break;
- }
-
- return ( flash_writing_size[ i ] );
-}
-
-
-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 send_RAM_transfer_checksum( int checksum )
-{
- char command[ 16 ];
-
- sprintf( command, "%d\n", checksum );
- try_and_check( command, "OK", 0 );
-}
