Add to 11U68 11E68

Dependencies:   DirectoryList MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

Revision:
19:7a7381e78025
Parent:
18:b401da200216
Child:
20:98d7b5878e3e
--- a/main.cpp	Mon Sep 09 15:10:50 2013 +0000
+++ b/main.cpp	Thu Sep 12 19:51:33 2013 +0000
@@ -2,7 +2,7 @@
  *  Sample of ISP operation for NXP MCUs
  *
  *  @author  Tedd OKANO
- *  @version 0.7
+ *  @version 0.8
  *  @date    Sep-2013
  *  
  *  This program programs MCU flash memory through UART. It uses 
@@ -27,11 +27,22 @@
 BusOut          leds( LED4, LED3, LED2, LED1 );
 DigitalOut      reset_pin( p26 );
 DigitalOut      isp_pin( p25 );
-Serial          target ( p28, p27 );
 Serial          pc ( USBTX,USBRX );
 LocalFileSystem local( "local" );
 Ticker          success;
 
+
+#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
+
+
+
 #define     ENTER_TO_ISP_MODE   0
 #define     NO_ISP_MODE         1
 #define     STR_BUFF_SIZE       64
@@ -63,6 +74,7 @@
 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 );
 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 );
@@ -71,8 +83,10 @@
 void    put_string( char *s );
 void    put_binary( char *b, int size );
 void    get_string( char *s );
+int     get_binary( char *b, int length );
 void    success_indicator();
 
+
 #pragma diag_suppress 1293  //  surpressing a warning message of "assignment in condition" ;)
 
 
@@ -142,6 +156,9 @@
             "** The data could not be written :(" :
             "** The data has been written successflly :)"
           );
+    
+    if ( error_state )
+        error( "  ** ISP failed\r\n" );
 
 #define AUTO_PROGRAM_START
 #ifdef  AUTO_PROGRAM_START
@@ -366,6 +383,8 @@
     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 ) {
@@ -387,15 +406,96 @@
         try_and_check( command_str, "0", 0 );
 
         total_size  += size;
-        printf( "  total %d bytes transferred\r", total_size );
+        //printf( "  total %d bytes transferred\r", total_size );
 
     }
 
     free( b );
 
+    verify_binary_data( fp );
+
     return ( total_size );
 }
 
+
+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 );
+}
+
+
 void initialize_uue_table( void )
 {
     int     i;
@@ -542,6 +642,25 @@
 }
 
 
+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 );
+}
+
+
 void success_indicator()
 {
     static int  i   = 0;