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 20 00:40:08 2013 +0000
Parent:
27:2b5c1eb39bb5
Child:
29:96e28bc1bd99
Commit message:
made function returns error. LED1 and LED2 assigned to toggle by TX and RX

Changed in this revision

error_code.h Show annotated file 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
target_handling.cpp Show annotated file Show diff for this revision Revisions of this file
target_table.cpp Show annotated file Show diff for this revision Revisions of this file
verification.cpp 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/error_code.h	Fri Sep 20 00:40:08 2013 +0000
@@ -0,0 +1,24 @@
+#ifndef MBED_ISP__ERROR_CODE__
+#define MBED_ISP__ERROR_CODE__
+
+enum {
+    NO_ERROR                            = 0x00,
+
+    ERROR_AT_TARGET_OPEN,
+    ERROR_AT_FILE_OPEN,
+    ERROR_AT_SECTOR_ERASE,
+
+    ERROR_AT_MALLOC_FOR_WRITE_BUFF,
+    ERROR_AT_WRITE_COMMAND,
+    ERROR_AT_PREPARE_COMMAND,
+    ERROR_AT_COPY_COMMAND,
+    ERROR_AT_SENDING_CHECKSUM,
+
+    ERROR_AT_MALLOC_FOR_VERIFY_FILE_BUFF,
+    ERROR_AT_MALLOC_FOR_VERIFY_DATA_BUFF,
+    ERROR_AT_READ_COMMAND,
+
+    ERROR_DATA_DOES_NOT_MATCH
+};
+
+#endif
--- a/ika.h	Thu Sep 19 01:08:20 2013 +0000
+++ b/ika.h	Fri Sep 20 00:40:08 2013 +0000
@@ -1,7 +1,7 @@
 
 #pragma diag_suppress 1293  //  surpressing a warning message of "assignment in condition" ;)
 
-void set_leds( char v );
+void toggle_led( char v );
 
 extern int     error_state;
 
--- a/main.cpp	Thu Sep 19 01:08:20 2013 +0000
+++ b/main.cpp	Fri Sep 20 00:40:08 2013 +0000
@@ -31,6 +31,7 @@
 #include    "verification.h"
 #include    "_user_settings.h"
 #include    "ika.h"
+#include    "error_code.h"
 
 BusOut          leds( LED4, LED3, LED2, LED1 );
 LocalFileSystem local( "local" );
@@ -38,7 +39,7 @@
 
 int     error_state         = 0;
 
-int     post_writing_process( target_param *tpp );
+int     isp_flash_write( char *file_name );
 int     file_size( FILE *fp );
 char    read_byte( void );
 void    success_indicator();
@@ -46,58 +47,19 @@
 
 int main()
 {
-    FILE            *fp;
-    target_param    *tpp;
-    int             data_size;
-    int             last_sector;
-
-    printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" );
-
-    if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) ) {
-        error( "couldn't open the taget" );
-        return ( 1 );
-    }
-
-    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", SOURCE_FILE );
-
-    if ( NULL == (fp    = fopen( SOURCE_FILE, "rb" )) ) {
-        error( "couldn't open source file" );
-        return ( 1 );
-    }
-
-    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" );
-
-    erase_sectors( last_sector );
-
-    printf( "\r\n  ==== flash writing ====\r\n" );
-    write_flash( fp, tpp );
+    int     err;
     
-    printf( "\r\n  ==== flash reading and verifying ====\r\n" );
-    verify_flash( fp, tpp );
-
-    fclose( fp );
-
+    err     = isp_flash_write( SOURCE_FILE );
+    
     printf( "\r\n  %s\r\n\r\n",
-            error_state ?
+            err ?
             "** The data could not be written :(" :
             "** The data has been written successflly :)"
           );
 
-    if ( error_state )
+    if ( err )
         error( "  ** ISP failed\r\n" );
 
-    post_writing_process( tpp );
-
-
 #ifdef  AUTO_PROGRAM_START
     set_target_baud_rate( TARGET_OPERATION_BAUD_RATE );
 
@@ -113,15 +75,57 @@
 }
 
 
-int post_writing_process( target_param *tpp )
+int isp_flash_write( char *file_name )
 {
-    if ( tpp->write_type == UUENCODE )
-        return ( try_and_check( "G 0 T\r\n", "0", 0 ) );
-    else
-        return ( 0 );
+    FILE            *fp;
+    target_param    *tpp;
+    int             data_size;
+    int             last_sector;
+    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 ) )
+        return ( err );
+    
+    printf( "\r\n  ==== flash reading and verifying ====\r\n" );
+    
+    if ( err    = verify_flash( fp, tpp ) )
+        return ( err );
+
+    fclose( fp );
+
+    post_writing_process( tpp );
+    
+    return ( 0 );
 }
 
 
+
 int file_size( FILE *fp )
 {
     int     size;
@@ -142,9 +146,9 @@
 }
 
 
-void set_leds( char v )
+void toggle_led( char v )
 {
-    leds    = v;
+    leds    = leds ^ (0x1 << v);
 }
 
 
--- a/serial_utilities.cpp	Thu Sep 19 01:08:20 2013 +0000
+++ b/serial_utilities.cpp	Fri Sep 20 00:40:08 2013 +0000
@@ -1,6 +1,7 @@
 #include    "mbed.h"
 #include    "serial_utilities.h"
 #include    "ika.h"
+#include    "error_code.h"
 
 Serial          pc ( USBTX,USBRX );
 
@@ -42,22 +43,22 @@
 void put_string( char *s )
 {
     char            c;
-    static int      i   = 0;
 
     while ( c = *s++ ) {
         target.putc( c );
-        set_leds( i++ & 0x1 );
+        toggle_led( 0 );
     }
 }
 
 
 void put_binary( char *b, int size )
 {
-    for ( int i = 0; i < size; i++ )
+    for ( int i = 0; i < size; i++ ) {
         target.putc( *b++ );
+        toggle_led( 0 );
+    }
 }
 
-
 void set_flag()
 {
     timeout_flag    = 1;
@@ -82,6 +83,7 @@
 
                 *s++    = c;
                 i++;
+                toggle_led( 1 );
             }
 
             if ( timeout_flag )
@@ -101,9 +103,11 @@
     timeout.attach( &set_flag, 1 );
 
     for ( i = 0; i < length; i++ ) {
-        if ( target.readable() )
+        if ( target.readable() ) {
             *b++    = target.getc();
-
+            toggle_led( 1 );
+        }
+        
         if ( timeout_flag )
             return ( i );
     }
--- a/target_handling.cpp	Thu Sep 19 01:08:20 2013 +0000
+++ b/target_handling.cpp	Fri Sep 20 00:40:08 2013 +0000
@@ -12,31 +12,38 @@
     target_param    *tpp;
     char            str_buf0[ STR_BUFF_SIZE ];
     char            str_buf1[ STR_BUFF_SIZE ];
+    int             retry_count = 3;
 
     set_target_baud_rate( baud_date );
-    
-    reset_target( ENTER_TO_ISP_MODE );
-    
-    try_and_check( "?", "Synchronized", 0 );
-    
+
+    while ( retry_count-- ) {
+        reset_target( ENTER_TO_ISP_MODE );
+
+        if ( !try_and_check( "?", "Synchronized", 0 ) )
+            break;
+    }
+
+    if ( !retry_count )
+        return ( NULL );
+        
     try_and_check2( "Synchronized\r\n", "OK", 0 );
     try_and_check2( "12000\r\n", "OK", 0 );
     try_and_check2( "U 23130\r\n", "0", 0 );
     try_and_check2( "A 0\r\n", "0", 0 );
-    
+
     try_and_check( "K\r\n", "0", 0 );
     get_string( str_buf0 );
     get_string( str_buf1 );
-    
+
     printf( "    result of \"K\" = %s %s\r\n", str_buf0, str_buf1 );
-    
+
     try_and_check( "J\r\n", "0", 0 );
     get_string( str_buf0 );
-    
+
     printf( "    result of \"J\" = %s\r\n", str_buf0 );
-    
+
     tpp  = find_target_param( str_buf0 );
-    
+
     return ( tpp );
 }
 
@@ -46,10 +53,10 @@
     reset_pin   = 1;
     isp_pin     = isp_pin_state;
     wait_ms( 100 );
-    
+
     reset_pin   = 0;
     wait_ms( 100 );
-    
+
     reset_pin   = 1;
     wait_ms( 100 );
 }
--- a/target_table.cpp	Thu Sep 19 01:08:20 2013 +0000
+++ b/target_table.cpp	Fri Sep 20 00:40:08 2013 +0000
@@ -25,6 +25,7 @@
             return ( &(target_table[ i ]) );
     }
     
-    return ( target_table );
+    //  return ( target_table );
+    return ( NULL );
 }
 
--- a/verification.cpp	Thu Sep 19 01:08:20 2013 +0000
+++ b/verification.cpp	Fri Sep 20 00:40:08 2013 +0000
@@ -5,6 +5,8 @@
 #include    "serial_utilities.h"
 #include    "writing.h"
 #include    "ika.h"
+#include    "error_code.h"
+
 
 int     verify_binary_data( FILE *fp );
 int     verify_uucoded_data( FILE *fp );
@@ -37,10 +39,10 @@
     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" );
+        return( ERROR_AT_MALLOC_FOR_VERIFY_FILE_BUFF );
 
     if ( NULL == (br    = (char *)malloc( flash_reading_size * sizeof( char ) )) )
-        error( "malloc error happened (in verify process, read data buffer)\r\n" );
+        return( ERROR_AT_MALLOC_FOR_VERIFY_DATA_BUFF );
 
 
     while ( size    = fread( bf, sizeof( char ), flash_reading_size, fp ) ) {
--- a/writing.cpp	Thu Sep 19 01:08:20 2013 +0000
+++ b/writing.cpp	Fri Sep 20 00:40:08 2013 +0000
@@ -5,6 +5,7 @@
 #include    "uu_coding.h"
 #include    "serial_utilities.h"
 #include    "ika.h"
+#include    "error_code.h"
 
 
 int     write_uuencoded_data( FILE *fp, int ram_size, int sector_size, unsigned int  );
@@ -45,7 +46,7 @@
     //  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" );
+        return( ERROR_AT_MALLOC_FOR_WRITE_BUFF );
 
     for ( int i = flash_writing_size; i < transfer_size; i++ )
         b[ i ]  = 0;    //  this is not neccesary but just stuffing stuffing bytes
@@ -71,17 +72,21 @@
             put_string( command_str );
 
             if ( !((i + 1) % 20) ) {
-                send_RAM_transfer_checksum( checksum );
+                if ( send_RAM_transfer_checksum( checksum ) )
+                    return ( ERROR_AT_SENDING_CHECKSUM );
+
                 checksum   = 0;
             }
         }
 
-        send_RAM_transfer_checksum( checksum );
+        if ( send_RAM_transfer_checksum( checksum ) )
+            return ( ERROR_AT_SENDING_CHECKSUM );
+
         checksum   = 0;
 
         sprintf( command_str, "P %d %d\r\n", total_size / sector_size, total_size / sector_size );
         if ( try_and_check( command_str, "0", 0 ) )
-            return ( ERROR_AT_P_COMMAND );
+            return ( ERROR_AT_PREPARE_COMMAND );
 
         sprintf( command_str, "C %d %d %d\r\n", total_size, ram_start, flash_writing_size );
         if ( try_and_check( command_str, "0", 0 ) )
@@ -107,7 +112,7 @@
     flash_writing_size  = 256;
 
     if ( NULL == (b     = (char *)malloc( flash_writing_size * sizeof( char ) )) )
-        error( "malloc error happened\r\n" );
+        return( ERROR_AT_MALLOC_FOR_WRITE_BUFF );
 
     while ( size    = fread( b, sizeof( char ), flash_writing_size, fp ) ) {
 
@@ -126,7 +131,7 @@
 
         sprintf( command_str, "P %d %d\r\n", total_size / sector_size, total_size / sector_size );
         if ( try_and_check( command_str, "0", 0 ) )
-            return ( ERROR_AT_P_COMMAND );
+            return ( ERROR_AT_PREPARE_COMMAND );
 
         sprintf( command_str, "C %d %d %d\r\n", total_size, ram_start, flash_writing_size );
         if ( try_and_check( command_str, "0", 0 ) )
@@ -184,4 +189,14 @@
     }
 
     return ( flash_writing_size[ i ] );
-}
\ No newline at end of file
+}
+
+
+int post_writing_process( target_param *tpp )
+{
+    if ( tpp->write_type == UUENCODE )
+        return ( try_and_check( "G 0 T\r\n", "0", 0 ) );
+    else
+        return ( 0 );
+}
+
--- a/writing.h	Thu Sep 19 01:08:20 2013 +0000
+++ b/writing.h	Fri Sep 20 00:40:08 2013 +0000
@@ -3,5 +3,6 @@
 
 int     write_flash( FILE *fp, target_param *tpp );
 void    add_isp_checksum( char *b );
+int     post_writing_process( target_param *tpp );