Add to 11U68 11E68

Dependencies:   DirectoryList MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

Files at this revision

API Documentation at this revision

Comitter:
okano
Date:
Fri Sep 13 03:34:43 2013 +0000
Parent:
21:e149d0bdbf4a
Child:
23:017f306cf3ca
Commit message:
dividing code into modules

Changed in this revision

command_interface.cpp Show annotated file Show diff for this revision Revisions of this file
command_interface.h Show annotated file Show diff for this revision Revisions of this file
command_utilities.cpp Show diff for this revision Revisions of this file
command_utilities.h Show diff for this revision Revisions of this file
ika.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
serial_utilities.cpp Show annotated file Show diff for this revision Revisions of this file
serial_utilities.h Show annotated file Show diff for this revision Revisions of this file
target_table.h Show annotated file Show diff for this revision Revisions of this file
uu_coding.cpp Show annotated file Show diff for this revision Revisions of this file
uu_coding.h Show annotated file Show diff for this revision Revisions of this file
writing.cpp Show annotated file Show diff for this revision Revisions of this file
writing.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/command_interface.cpp	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,80 @@
+#include    "mbed.h"
+#include    "command_interface.h"
+#include    "serial_utilities.h"
+#include    "ika.h"
+
+
+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 send_RAM_transfer_checksum( int checksum )
+{
+    char    command[ 16 ];
+
+    sprintf( command, "%d\n", checksum );
+    try_and_check( command, "OK", 0 );
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/command_interface.h	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,6 @@
+#define     STR_BUFF_SIZE       64
+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 );
+void    send_RAM_transfer_checksum( int checksum );
--- a/command_utilities.cpp	Fri Sep 13 03:09:09 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,122 +0,0 @@
-#include    "mbed.h"
-#include    "command_utilities.h"
-#include    "ika.h"
-
-Serial          pc ( USBTX,USBRX );
-
-#if 0
-Serial          target( p28, p27 );
-#else
-#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
-#define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 512
-#include "MODSERIAL.h"
-MODSERIAL       target( p28, p27 ); //
-#endif
-
-void set_target_baud_rate( int baud_rate )
-{
-    target.baud( baud_rate );
-}
-
-
-void usb_serial_bridge_operation( void )
-{
-    while (1) {
-
-        if ( pc.readable() ) {
-            target.putc( pc.getc() );
-        }
-
-        if ( target.readable() ) {
-            pc.putc( target.getc() );
-        }
-    }
-}
-
-
-void put_string( char *s )
-{
-    char            c;
-    static int      i   = 0;
-
-    while ( c = *s++ ) {
-        target.putc( c );
-        set_leds( i++ & 0x1 );
-    }
-}
-
-
-void put_binary( char *b, int size )
-{
-    for ( int i = 0; i < size; i++ )
-        target.putc( *b++ );
-}
-
-
-Timeout timeout;
-
-int timeout_flag    = 0;
-
-void set_flag()
-{
-    timeout_flag    = 1;
-}
-
-
-void get_string( char *s )
-{
-    int     i   = 0;
-    char    c   = 0;
-    timeout_flag    = 0;
-
-    timeout.attach( &set_flag, 1 );
-
-    do {
-        do {
-            if ( target.readable() ) {
-                c  = target.getc();
-
-                if ( ( c == '\n') || (c == '\r') )
-                    break;
-
-                *s++    = c;
-                i++;
-            }
-
-            if ( timeout_flag )
-                return;
-        } while ( 1 );
-    } while ( !i );
-
-    *s  = '\0';
-}
-
-
-int get_binary( char *b, int length )
-{
-    int i;
-
-    timeout_flag    = 0;
-    timeout.attach( &set_flag, 1 );
-
-    for ( i = 0; i < length; i++ ) {
-        if ( target.readable() )
-            *b++    = target.getc();
-
-        if ( timeout_flag )
-            return ( i );
-    }
-
-    return ( i );
-}
-
-
-char read_byte( void )
-{
-    while ( !target.readable() )
-        ;
-
-    return ( target.getc() );
-}
-
-
--- a/command_utilities.h	Fri Sep 13 03:09:09 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-
-void    set_target_baud_rate( int baud_rate );
-void    usb_serial_bridge_operation( void );
-
-void    put_string( char *s );
-void    put_binary( char *b, int size );
-void    get_string( char *s );
-int     get_binary( char *b, int length );
-char    read_byte( void );
--- a/ika.h	Fri Sep 13 03:09:09 2013 +0000
+++ b/ika.h	Fri Sep 13 03:34:43 2013 +0000
@@ -2,3 +2,6 @@
 #pragma diag_suppress 1293  //  surpressing a warning message of "assignment in condition" ;)
 
 void set_leds( char v );
+
+
+extern int     error_state;
--- 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 );
-}
 
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial_utilities.cpp	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,122 @@
+#include    "mbed.h"
+#include    "serial_utilities.h"
+#include    "ika.h"
+
+Serial          pc ( USBTX,USBRX );
+
+#if 0
+Serial          target( p28, p27 );
+#else
+#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
+#define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 512
+#include "MODSERIAL.h"
+MODSERIAL       target( p28, p27 ); //
+#endif
+
+void set_target_baud_rate( int baud_rate )
+{
+    target.baud( baud_rate );
+}
+
+
+void usb_serial_bridge_operation( void )
+{
+    while (1) {
+
+        if ( pc.readable() ) {
+            target.putc( pc.getc() );
+        }
+
+        if ( target.readable() ) {
+            pc.putc( target.getc() );
+        }
+    }
+}
+
+
+void put_string( char *s )
+{
+    char            c;
+    static int      i   = 0;
+
+    while ( c = *s++ ) {
+        target.putc( c );
+        set_leds( i++ & 0x1 );
+    }
+}
+
+
+void put_binary( char *b, int size )
+{
+    for ( int i = 0; i < size; i++ )
+        target.putc( *b++ );
+}
+
+
+Timeout timeout;
+
+int timeout_flag    = 0;
+
+void set_flag()
+{
+    timeout_flag    = 1;
+}
+
+
+void get_string( char *s )
+{
+    int     i   = 0;
+    char    c   = 0;
+    timeout_flag    = 0;
+
+    timeout.attach( &set_flag, 1 );
+
+    do {
+        do {
+            if ( target.readable() ) {
+                c  = target.getc();
+
+                if ( ( c == '\n') || (c == '\r') )
+                    break;
+
+                *s++    = c;
+                i++;
+            }
+
+            if ( timeout_flag )
+                return;
+        } while ( 1 );
+    } while ( !i );
+
+    *s  = '\0';
+}
+
+
+int get_binary( char *b, int length )
+{
+    int i;
+
+    timeout_flag    = 0;
+    timeout.attach( &set_flag, 1 );
+
+    for ( i = 0; i < length; i++ ) {
+        if ( target.readable() )
+            *b++    = target.getc();
+
+        if ( timeout_flag )
+            return ( i );
+    }
+
+    return ( i );
+}
+
+
+char read_byte( void )
+{
+    while ( !target.readable() )
+        ;
+
+    return ( target.getc() );
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial_utilities.h	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,9 @@
+
+void    set_target_baud_rate( int baud_rate );
+void    usb_serial_bridge_operation( void );
+
+void    put_string( char *s );
+void    put_binary( char *b, int size );
+void    get_string( char *s );
+int     get_binary( char *b, int length );
+char    read_byte( void );
--- a/target_table.h	Fri Sep 13 03:09:09 2013 +0000
+++ b/target_table.h	Fri Sep 13 03:34:43 2013 +0000
@@ -1,3 +1,7 @@
+#ifndef MBED_ISP__TARGET_TABLE__
+#define MBED_ISP__TARGET_TABLE__
+
+
 
 #define     NOT_FOUND   0
 
@@ -20,3 +24,4 @@
 
 target_param *find_target_param( char *device_id_string );
 
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uu_coding.cpp	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,71 @@
+char    uu_table[ 0x60 + 1 ];
+
+
+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;
+    }
+}
+
+
+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 );
+}
+
+
+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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uu_coding.h	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,7 @@
+
+void    initialize_uue_table( void );
+void    initialize_uud_table( void );
+long    bin2uue( char *bin, char *str, int size );
+int     uudecode_a_line( char *b, char *s );
+
+extern char    uu_table[ 0x60 + 1 ];
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/writing.cpp	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,182 @@
+
+#include    "mbed.h"
+#include    "writing.h"
+#include    "command_interface.h"
+#include    "uu_coding.h"
+#include    "serial_utilities.h"
+
+
+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     get_flash_writing_size( int ram_size, unsigned int ram_start );
+
+
+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 write_uuencoded_data( FILE *fp, int ram_size, int sector_size, unsigned int ram_start )
+{
+#define     BYTES_PER_LINE      45
+
+    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 );
+}
+
+
+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" );
+}
+
+
+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 ] );
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/writing.h	Fri Sep 13 03:34:43 2013 +0000
@@ -0,0 +1,7 @@
+#include    "target_table.h"
+
+
+int     write_flash( FILE *fp, target_param *tpp );
+void    add_isp_checksum( char *b );
+
+