Ika Shouyu Poppoyaki - LPC82x supported

Dependencies:   MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

Revision:
4:55f1977bd11a
Parent:
3:3c380e643e74
Child:
5:ff30f5b58617
--- a/main.cpp	Sat Aug 24 15:06:33 2013 +0000
+++ b/main.cpp	Sun Aug 25 00:41:56 2013 +0000
@@ -7,10 +7,20 @@
 LocalFileSystem local( "local" );
 
 #define     SOURCE_FILE         "/local/bin"
+//#define     BAUD_RATE           9600
+//#define     BAUD_RATE           57600
+#define     BAUD_RATE           115200
+
 #define     STR_BUFF_SIZE       64
 #define     RAM_START_ADDRESS   0x10000300L
+//#define     RAM_START_ADDRESS   0x10000100L
 #define     SECTOR_SIZE         4096
 
+#define     FLASH_WRITING_SIZE  1024            //  This value should be 256, 512, 1024 or 4096
+#define     LINES_PER_TRANSFER  (((FLASH_WRITING_SIZE / 45) + 3) & ~0x3)
+#define     TRANSFER_SIZE       (LINES_PER_TRANSFER * 45)
+
+
 enum {
     ENTER_TO_ISP_MODE,
     NO_ISP_MODE
@@ -148,51 +158,65 @@
     try_and_check( command_str, "0", 0 );
 }
 
-#define FLASH_WRITING_SIZE  1024
-#define TRANSFER_SIZE       (24 * 45)
+
+void send_RAM_transfer_checksum( int checksum )
+{
+    char    command[ 16 ];
+
+    sprintf( command, "%d\n", checksum );
+    try_and_check( command, "OK", 0 );
+}
+
 
 char    b[ TRANSFER_SIZE ];
 
 void write_binary_data( FILE *fp )
 {
     char    command_str[ STR_BUFF_SIZE ];
-    long    checksum   = 0;
+    long    checksum        = 0;
     int     transfer_count  = 0;
+    int     total_size      = 0;
     int     size;
 
     initialize_uue_table();
 
     for ( int i = FLASH_WRITING_SIZE; i < TRANSFER_SIZE; i++ )
-        b[ i ]  = 0;
+        b[ i ]  = 0;    //  this is not neccesary but just stuffing stuffing bytes
 
     while ( size    = fread( b, sizeof( char ), FLASH_WRITING_SIZE, fp ) ) {
 
-        if ( !transfer_count ) {
+        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_ADDRESS, 1080 );
+        sprintf( command_str, "W %ld %ld\r\n", RAM_START_ADDRESS, TRANSFER_SIZE );
         try_and_check( command_str, "0", 0 );
 
-        for ( int i = 0; i < 24; i++ ) {
+        for ( int i = 0; i < LINES_PER_TRANSFER; i++ ) {
             checksum   += bin2uue( b + (i * 45), command_str );
+
             printf( "%02d %s\r", i, command_str );
+
             put_string( command_str );
-            if ( (i == 19) || (i == 23) ) {
-                sprintf( command_str, "%ld\n", checksum );
-                printf( " %ld  %s\r\n", checksum, command_str );
-                printf( "\"%s\" %s\r\n", command_str, try_and_check( command_str, "OK", 0 ) ? "Fail" : "Pass" );
 
+            if ( !((i + 1) % 20) ) {
+                send_RAM_transfer_checksum( checksum );
                 checksum   = 0;
             }
         }
 
-        printf( "\"P 0 0\" %s\r\n", try_and_check( "P 0 0\r\n", "0", 0 ) ? "Fail" : "Pass" );
+        send_RAM_transfer_checksum( checksum );
+        checksum   = 0;
 
-        sprintf( command_str, "C %ld %ld %ld\r\n", FLASH_WRITING_SIZE * transfer_count++, 0x10000300L, FLASH_WRITING_SIZE );
+        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_ADDRESS, FLASH_WRITING_SIZE );
+        try_and_check( command_str, "0", 0 );
 
+        total_size  += size;
     }
     try_and_check( "G 0 T\r\n", "0", 0 );
 }
@@ -227,7 +251,7 @@
     int     data_size;
     int     last_sector;
 
-    target.baud( 9600 );
+    target.baud( BAUD_RATE );
 
     if ( NULL == (fp    = fopen( SOURCE_FILE, "rb" )) ) {
         error( "couldn't open source file" );