this transfers data (which is stored in "bin" file in mbed storage) into LPC1114, LPC1115, LPC81x, LPC82x, LPC1768/LPC1769 and LPC11U68/LPC11E68 internal flash memory through ISP.

Dependencies:   mbed MODSERIAL DirectoryList

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers isp.cpp Source File

isp.cpp

00001 
00002 #include    "mbed.h"
00003 #include    "target_table.h"
00004 #include    "serial_utilities.h"
00005 #include    "command_interface.h"
00006 #include    "writing.h"
00007 #include    "uu_coding.h"
00008 #include    "target_handling.h"
00009 #include    "verification.h"
00010 #include    "isp.h"
00011 #include    "_user_settings.h"
00012 
00013 #define     CRP_WORD_OFFSET     0x2FC
00014 
00015 
00016 BusOut          leds( LED4, LED3, LED2, LED1 );
00017 Ticker          success;
00018 
00019 int             file_size( FILE *fp );
00020 unsigned int    read_crp( FILE *fp );
00021 unsigned int    crp_check( FILE *fp );
00022 void            success_indicator();
00023 void            user_action_waiting_indicator();
00024 
00025 
00026 int isp_flash_write( const char *file_name )
00027 {
00028     FILE            *fp;
00029     target_param    *tpp;
00030     int             data_size;
00031     int             last_sector;
00032     int             transferred_size;
00033     int             err;
00034 
00035     if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) ) {
00036         return ( ERROR_AT_TARGET_OPEN );
00037     }
00038 
00039     printf( "  target device found : type       = \"%s\"\r\n",      tpp->type_name );
00040     printf( "                        ID         =  0x%08X\r\n",     tpp->id );
00041     printf( "                        RAM size   =  %10d bytes\r\n", tpp->ram_size );
00042     printf( "                        flash size =  %10d bytes\r\n", tpp->flash_size );
00043 
00044     //
00045     //  If user selected "flash erase only"
00046     //
00047 
00048     if ( *file_name == 0xFF ) {
00049         last_sector = find_sector( tpp->flash_size - 1, tpp );
00050         
00051         //  The file selector returns 0xFF as first character of the string
00052         //      if user selected "flash erase".
00053 
00054         printf( "\r\n  ==== flash erasing ====\r\n" );
00055         
00056         if ( erase_sectors( last_sector ) )
00057             return ( ERROR_AT_SECTOR_ERASE );
00058 
00059         printf( "  sectors from 0 to %d are erased. bye\r\n", last_sector );
00060         exit ( 0 ); //  quit from app
00061     }
00062 
00063     //
00064     //  Normal operation
00065     //
00066 
00067     printf( "  opening file: \"%s\"\r\n", file_name );
00068 
00069     if ( NULL == (fp    = fopen( file_name, "rb" )) ) {
00070         return ( ERROR_AT_FILE_OPEN );
00071     }
00072 
00073     data_size   = file_size( fp );
00074 
00075     if ( !data_size )
00076         return ( ERROR_DATA_SIZE_ZERO );
00077 
00078 //  last_sector = (data_size - 1) / tpp->sector_size;
00079     last_sector = find_sector( data_size - 1, tpp );
00080 
00081     if ( data_size < (CRP_WORD_OFFSET + sizeof( unsigned int )) ) {
00082         printf( "  CRP check is not performed because data size is less than 0x300(768) bytes\r\n" );
00083     } else {
00084         if ( crp_check( fp ) ) {
00085             printf( "  the CRP is enabled in the data source file\r\n" );
00086 
00087 #ifdef CHECK_CRP_CODE
00088             printf( "  aborting execution by CRP warning\r\n" );
00089 
00090             return ( WARNING_CRP_CODE_DETECTED );
00091 #else
00092             printf( "  this program continues to write the CRP enabled binary into the target flash\r\n" );
00093 #endif
00094         }
00095     }
00096 
00097     printf( "  data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 );
00098     printf( "  resetting target\r\n" );
00099 
00100 #ifdef ENABLE_WRITING
00101     if ( erase_sectors( find_sector( data_size - 1, tpp ) ) )
00102         return ( ERROR_AT_SECTOR_ERASE );
00103 
00104     printf( "\r\n  ==== flash writing ====\r\n" );
00105 
00106     if ( err    = write_flash( fp, tpp, &transferred_size, data_size ) )
00107         return ( err );
00108 
00109     printf( "  -- %d bytes data are written\r\n", transferred_size );
00110     leds_off();
00111 #else
00112     printf( "\r\n  ==== writing is disabled ====\r\n\r\n" );
00113 #endif
00114 
00115 
00116 #if 0
00117     if ( tpp->id == 0x26113F37 ) {
00118         //  The MEMMAP register should be operated if want to verify the LPC176x flash data
00119     }
00120 #endif
00121 
00122 
00123 #ifdef ENABLE_VERIFYING
00124     if ( (tpp->id == 0x26113F37) || (tpp->id == 0x26013F37) ) {
00125         printf( "\r\n  ==== for the LPC176x, verification is not supported ====\r\n\r\n" );
00126     } else {
00127         printf( "\r\n  ==== flash reading and verifying ====\r\n" );
00128 
00129         if ( err    = verify_flash( fp, tpp, &transferred_size, data_size ) )
00130             return ( err );
00131 
00132         printf( "  -- %d bytes data are read and verified\r\n", transferred_size );
00133         leds_off();
00134     }
00135 #else
00136     printf( "\r\n  ==== verifying has been skipped ====\r\n\r\n" );
00137 }
00138 #endif
00139 
00140 
00141     fclose( fp );
00142 
00143     post_writing_process( tpp );
00144 
00145     return ( NO_ERROR );
00146 }
00147 
00148 
00149 int file_size( FILE *fp )
00150 {
00151     int     size;
00152 
00153     fseek( fp, 0, SEEK_END ); // seek to end of file
00154     size    = ftell( fp );    // get current file pointer
00155     fseek( fp, 0, SEEK_SET ); // seek back to beginning of file
00156 
00157     return size;
00158 }
00159 
00160 
00161 unsigned int crp_check( FILE *fp )
00162 {
00163     unsigned int    crp;
00164 
00165     switch ( crp    = read_crp( fp ) ) {
00166         case NO_ISP :
00167             printf( "\r\n  WARNING : CRP code detected 0x%08X (NO_ISP)\r\n", crp );
00168             break;
00169         case CRP1 :
00170             printf( "\r\n  WARNING : CRP code detected 0x%08X (CRP1)\r\n", crp );
00171             break;
00172         case CRP2 :
00173             printf( "\r\n  WARNING : CRP code detected 0x%08X (CRP2)\r\n", crp );
00174             break;
00175         case CRP3 :
00176             printf( "\r\n  WARNING : CRP code detected 0x%08X (CRP3)\r\n", crp );
00177             break;
00178         default :
00179             crp = 0x0;  //  no CRP code detected
00180             break;
00181     }
00182 
00183     return ( crp );
00184 }
00185 
00186 
00187 unsigned int read_crp( FILE *fp )
00188 {
00189     unsigned int    crp;
00190 
00191     fseek( fp, CRP_WORD_OFFSET, SEEK_SET ); // seek to 0x2FC (764th byte)
00192 
00193     if ( 1 != fread( &crp, sizeof( crp ), 1, fp ) )
00194         return ( CRP_CHECK_ERROR );
00195 
00196     fseek( fp, 0, SEEK_SET ); // seek back to beginning of file
00197 
00198     return ( crp );
00199 }
00200 
00201 
00202 void start_target_in_normal_mode( int baud_rate )
00203 {
00204     set_target_baud_rate( baud_rate );
00205     reset_target( NO_ISP_MODE );
00206 }
00207 
00208 void start_success_indicator( void )
00209 {
00210     success.attach( &success_indicator, 0.1 );
00211 }
00212 
00213 void success_indicator()
00214 {
00215     static int  i   = 0;
00216 
00217     leds    = 0x1 << (i++ & 0x3);
00218 }
00219 
00220 void toggle_led( char v )
00221 {
00222     leds    = leds ^ (0x1 << v);
00223 }
00224 
00225 
00226 void leds_off( void )
00227 {
00228     leds    = 0x0;
00229 }
00230 
00231 void show_progress( int total_size, int file_size )
00232 {
00233     printf( "  -- %5.1f%%\r", ((float)total_size/(float)file_size) * 100.0 );
00234     fflush( stdout );
00235 }