Ika Shouyu Poppoyaki - LPC82x supported

Dependencies:   MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

Committer:
okano
Date:
Wed Aug 28 12:07:47 2013 +0000
Revision:
15:051ca36cc64b
Parent:
14:a7b9f74fb856
Child:
16:cac2348cfcfb
wait removed before "reset_target( NO_ISP_MODE )"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 11:8dfc3217d1ca 1 /**
okano 11:8dfc3217d1ca 2 * Sample of ISP operation for NXP MCUs
okano 11:8dfc3217d1ca 3 *
okano 14:a7b9f74fb856 4 * @author Tedd OKANO
okano 14:a7b9f74fb856 5 * @version 0.6
okano 11:8dfc3217d1ca 6 * @date Aug-2013
okano 14:a7b9f74fb856 7 *
okano 14:a7b9f74fb856 8 * This program programs MCU flash memory through UART. It uses
okano 14:a7b9f74fb856 9 * "In-System Programming (ISP)" interface in target MCU (NXP LPC micro-
okano 14:a7b9f74fb856 10 * controllers).
okano 14:a7b9f74fb856 11 *
okano 14:a7b9f74fb856 12 * The ISP is done by PC and serial cable normally. The ISP protocol is
okano 14:a7b9f74fb856 13 * executed software on a PC. The software reads a data file and transfers
okano 14:a7b9f74fb856 14 * the data with the ISP protocol.
okano 14:a7b9f74fb856 15 * This program does same process of that. The mbed perform the function like
okano 14:a7b9f74fb856 16 * "FlashMagic" and "lpc21isp".
okano 14:a7b9f74fb856 17 * (This program not just copies the binary but also insert 4 byte checksum at
okano 14:a7b9f74fb856 18 * address 0x1C.)
okano 14:a7b9f74fb856 19 *
okano 14:a7b9f74fb856 20 * This program currently supports LPC1114(LPC1114FN28/102 - DIP28-ARM) and
okano 14:a7b9f74fb856 21 * LPC810(LPC810M021FN8 - DIP8-ARM).
okano 11:8dfc3217d1ca 22 */
okano 11:8dfc3217d1ca 23
okano 5:ff30f5b58617 24 #include "mbed.h"
okano 5:ff30f5b58617 25 #include "target_table.h"
okano 0:6baefda2e511 26
okano 2:8d75eb0ecd20 27 BusOut leds( LED4, LED3, LED2, LED1 );
okano 2:8d75eb0ecd20 28 DigitalOut reset_pin( p26 );
okano 2:8d75eb0ecd20 29 DigitalOut isp_pin( p25 );
okano 2:8d75eb0ecd20 30 Serial target ( p28, p27 );
okano 2:8d75eb0ecd20 31 LocalFileSystem local( "local" );
okano 0:6baefda2e511 32
okano 12:5a33b5d39792 33 #define ENTER_TO_ISP_MODE 0
okano 12:5a33b5d39792 34 #define NO_ISP_MODE 1
okano 12:5a33b5d39792 35 #define STR_BUFF_SIZE 64
okano 12:5a33b5d39792 36
okano 1:54e619428ae6 37 #define SOURCE_FILE "/local/bin"
okano 8:b220fadbb3d8 38 #define BAUD_RATE 115200
okano 4:55f1977bd11a 39 //#define BAUD_RATE 57600
okano 8:b220fadbb3d8 40 //#define BAUD_RATE 9600
okano 4:55f1977bd11a 41
okano 8:b220fadbb3d8 42 int error_state = 0;
okano 7:815366f003ee 43
okano 7:815366f003ee 44 int file_size( FILE *fp );
okano 7:815366f003ee 45 void reset_target( int isp_pin_state );
okano 7:815366f003ee 46 int try_and_check( char *command, char *expected_return_str, int mode );
okano 7:815366f003ee 47 int try_and_check2( char *command, char *expected_return_str, int mode );
okano 7:815366f003ee 48 void print_command( char *command );
okano 7:815366f003ee 49 void print_result( int r );
okano 7:815366f003ee 50 char read_byte( void );
okano 7:815366f003ee 51 void erase_sectors( int last_sector );
okano 12:5a33b5d39792 52 int write_uuencoded_data( FILE *fp, int ram_size, int sector_size, unsigned int );
okano 12:5a33b5d39792 53 int write_binary_data( FILE *fp, int ram_size, int sector_size, unsigned int ram_start );
okano 7:815366f003ee 54 void initialize_uue_table( void );
okano 11:8dfc3217d1ca 55 long bin2uue( char *bin, char *str, int size );
okano 12:5a33b5d39792 56 int get_flash_writing_size( int ram_size, unsigned int ram_start );
okano 7:815366f003ee 57 void add_isp_checksum( char *b );
okano 7:815366f003ee 58 void send_RAM_transfer_checksum( int checksum );
okano 7:815366f003ee 59 void put_string( char *s );
okano 12:5a33b5d39792 60 void put_binary( char *b, int size );
okano 7:815366f003ee 61 void get_string( char *s );
okano 7:815366f003ee 62
okano 12:5a33b5d39792 63 #pragma diag_suppress 1293 // surpressing a warning message of "assignment in condition" ;)
okano 12:5a33b5d39792 64
okano 7:815366f003ee 65
okano 7:815366f003ee 66 int main()
okano 7:815366f003ee 67 {
okano 7:815366f003ee 68 FILE *fp;
okano 7:815366f003ee 69 char str_buf0[ STR_BUFF_SIZE ];
okano 7:815366f003ee 70 char str_buf1[ STR_BUFF_SIZE ];
okano 7:815366f003ee 71 int data_size;
okano 7:815366f003ee 72 int last_sector;
okano 7:815366f003ee 73 target_param *tpp;
okano 8:b220fadbb3d8 74
okano 7:815366f003ee 75 printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" );
okano 7:815366f003ee 76
okano 7:815366f003ee 77 target.baud( BAUD_RATE );
okano 8:b220fadbb3d8 78
okano 7:815366f003ee 79 reset_target( ENTER_TO_ISP_MODE );
okano 8:b220fadbb3d8 80
okano 7:815366f003ee 81 try_and_check( "?", "Synchronized", 0 );
okano 8:b220fadbb3d8 82
okano 7:815366f003ee 83 try_and_check2( "Synchronized\r\n", "OK", 0 );
okano 7:815366f003ee 84 try_and_check2( "12000\r\n", "OK", 0 );
okano 7:815366f003ee 85 try_and_check2( "U 23130\r\n", "0", 0 );
okano 7:815366f003ee 86 try_and_check2( "A 0\r\n", "0", 0 );
okano 8:b220fadbb3d8 87
okano 7:815366f003ee 88 try_and_check( "K\r\n", "0", 0 );
okano 7:815366f003ee 89 get_string( str_buf0 );
okano 7:815366f003ee 90 get_string( str_buf1 );
okano 8:b220fadbb3d8 91
okano 7:815366f003ee 92 printf( " result of \"K\" = %s %s\r\n", str_buf0, str_buf1 );
okano 8:b220fadbb3d8 93
okano 7:815366f003ee 94 try_and_check( "J\r\n", "0", 0 );
okano 7:815366f003ee 95 get_string( str_buf0 );
okano 8:b220fadbb3d8 96
okano 7:815366f003ee 97 printf( " result of \"J\" = %s\r\n", str_buf0 );
okano 8:b220fadbb3d8 98
okano 7:815366f003ee 99 tpp = find_target_param( str_buf0 );
okano 8:b220fadbb3d8 100 printf( " target device found : type = \"%s\"\r\n", tpp->type_name );
okano 8:b220fadbb3d8 101 printf( " ID = 0x%08X\r\n", tpp->id );
okano 8:b220fadbb3d8 102 printf( " RAM size = %10d bytes\r\n", tpp->ram_size );
okano 8:b220fadbb3d8 103 printf( " flash size = %10d bytes\r\n", tpp->flash_size );
okano 8:b220fadbb3d8 104
okano 12:5a33b5d39792 105 printf( " opening file: \"%s\"\r\n", SOURCE_FILE );
okano 12:5a33b5d39792 106
okano 12:5a33b5d39792 107 if ( NULL == (fp = fopen( SOURCE_FILE, "rb" )) ) {
okano 12:5a33b5d39792 108 error( "couldn't open source file" );
okano 12:5a33b5d39792 109 return ( 1 );
okano 12:5a33b5d39792 110 }
okano 12:5a33b5d39792 111
okano 12:5a33b5d39792 112 data_size = file_size( fp );
okano 12:5a33b5d39792 113 last_sector = data_size / tpp->sector_size;
okano 12:5a33b5d39792 114
okano 12:5a33b5d39792 115 printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 );
okano 12:5a33b5d39792 116 printf( " resetting target\r\n" );
okano 12:5a33b5d39792 117
okano 7:815366f003ee 118 erase_sectors( last_sector );
okano 12:5a33b5d39792 119
okano 12:5a33b5d39792 120 if ( tpp->write_type == BINARY )
okano 12:5a33b5d39792 121 write_binary_data( fp, tpp->ram_size, tpp->sector_size, tpp->ram_start_address );
okano 12:5a33b5d39792 122 else // UUENCODE
okano 12:5a33b5d39792 123 write_uuencoded_data( fp, tpp->ram_size, tpp->sector_size, tpp->ram_start_address );
okano 12:5a33b5d39792 124
okano 7:815366f003ee 125 fclose( fp );
okano 8:b220fadbb3d8 126
okano 8:b220fadbb3d8 127 printf( "\r\n %s\r\n\r\n",
okano 8:b220fadbb3d8 128 error_state ?
okano 8:b220fadbb3d8 129 "** The data could not be written :(" :
okano 8:b220fadbb3d8 130 "** The data has been written successflly :)"
okano 8:b220fadbb3d8 131 );
okano 8:b220fadbb3d8 132
okano 14:a7b9f74fb856 133 //#define AUTO_PROGRAM_START
okano 14:a7b9f74fb856 134 #ifdef AUTO_PROGRAM_START
okano 14:a7b9f74fb856 135 reset_target( NO_ISP_MODE );
okano 14:a7b9f74fb856 136 printf( " ** The program in flash has been started!!\r\n\r\n" );
okano 14:a7b9f74fb856 137 #endif
okano 14:a7b9f74fb856 138
okano 7:815366f003ee 139 int i = 0;
okano 8:b220fadbb3d8 140
okano 7:815366f003ee 141 while ( 1 ) {
okano 7:815366f003ee 142 leds = 0x1 << (i++ & 0x3);
okano 7:815366f003ee 143 wait( 0.1 );
okano 7:815366f003ee 144 }
okano 7:815366f003ee 145 }
okano 7:815366f003ee 146
okano 7:815366f003ee 147
okano 7:815366f003ee 148 int file_size( FILE *fp )
okano 7:815366f003ee 149 {
okano 7:815366f003ee 150 int size;
okano 8:b220fadbb3d8 151
okano 7:815366f003ee 152 fseek( fp, 0, SEEK_END ); // seek to end of file
okano 7:815366f003ee 153 size = ftell( fp ); // get current file pointer
okano 7:815366f003ee 154 fseek( fp, 0, SEEK_SET ); // seek back to beginning of file
okano 8:b220fadbb3d8 155
okano 7:815366f003ee 156 return size;
okano 7:815366f003ee 157 }
okano 7:815366f003ee 158
okano 7:815366f003ee 159
okano 7:815366f003ee 160 void reset_target( int isp_pin_state )
okano 7:815366f003ee 161 {
okano 7:815366f003ee 162 reset_pin = 1;
okano 13:60995bf8b2c7 163 isp_pin = isp_pin_state;
okano 7:815366f003ee 164 wait_ms( 100 );
okano 13:60995bf8b2c7 165
okano 7:815366f003ee 166 reset_pin = 0;
okano 7:815366f003ee 167 wait_ms( 100 );
okano 13:60995bf8b2c7 168
okano 7:815366f003ee 169 reset_pin = 1;
okano 7:815366f003ee 170 wait_ms( 100 );
okano 7:815366f003ee 171 }
okano 7:815366f003ee 172
okano 7:815366f003ee 173
okano 7:815366f003ee 174 int try_and_check( char *command, char *expected_return_str, int mode )
okano 7:815366f003ee 175 {
okano 7:815366f003ee 176 char rtn_str[ STR_BUFF_SIZE ];
okano 8:b220fadbb3d8 177 int result = 1;
okano 8:b220fadbb3d8 178
okano 7:815366f003ee 179 print_command( command );
okano 7:815366f003ee 180 put_string( command );
okano 8:b220fadbb3d8 181
okano 7:815366f003ee 182 get_string( rtn_str );
okano 7:815366f003ee 183 print_result( result = strcmp( expected_return_str, rtn_str ) );
okano 8:b220fadbb3d8 184
okano 8:b220fadbb3d8 185 if ( result && !mode )
okano 8:b220fadbb3d8 186 error( "command failed\r\n" );
okano 8:b220fadbb3d8 187
okano 8:b220fadbb3d8 188 error_state |= result;
okano 8:b220fadbb3d8 189
okano 7:815366f003ee 190 return ( result );
okano 7:815366f003ee 191 }
okano 7:815366f003ee 192
okano 7:815366f003ee 193
okano 7:815366f003ee 194 int try_and_check2( char *command, char *expected_return_str, int mode )
okano 7:815366f003ee 195 {
okano 7:815366f003ee 196 char rtn_str[ STR_BUFF_SIZE ];
okano 8:b220fadbb3d8 197 int result = 1;
okano 8:b220fadbb3d8 198
okano 7:815366f003ee 199 print_command( command );
okano 7:815366f003ee 200 put_string( command );
okano 8:b220fadbb3d8 201
okano 7:815366f003ee 202 get_string( rtn_str ); // just readout echoback
okano 7:815366f003ee 203 get_string( rtn_str );
okano 7:815366f003ee 204 print_result( result = strcmp( expected_return_str, rtn_str ) );
okano 8:b220fadbb3d8 205
okano 8:b220fadbb3d8 206 if ( result && !mode )
okano 8:b220fadbb3d8 207 error( "command failed\r\n" );
okano 8:b220fadbb3d8 208
okano 8:b220fadbb3d8 209 error_state |= result;
okano 8:b220fadbb3d8 210
okano 7:815366f003ee 211 return ( result );
okano 7:815366f003ee 212 }
okano 7:815366f003ee 213
okano 7:815366f003ee 214
okano 7:815366f003ee 215 void print_command( char *command )
okano 7:815366f003ee 216 {
okano 7:815366f003ee 217 char s[ STR_BUFF_SIZE ];
okano 7:815366f003ee 218 char *pos;
okano 8:b220fadbb3d8 219
okano 7:815366f003ee 220 strcpy( s, command );
okano 8:b220fadbb3d8 221
okano 7:815366f003ee 222 if ( pos = strchr( s, '\r' ) )
okano 7:815366f003ee 223 *pos = '\0';
okano 8:b220fadbb3d8 224
okano 7:815366f003ee 225 if ( pos = strchr( s, '\n' ) )
okano 7:815366f003ee 226 *pos = '\0';
okano 8:b220fadbb3d8 227
okano 7:815366f003ee 228 printf( " command-\"%s\" : ", s );
okano 7:815366f003ee 229 }
okano 7:815366f003ee 230
okano 7:815366f003ee 231
okano 7:815366f003ee 232 void print_result( int r )
okano 7:815366f003ee 233 {
okano 7:815366f003ee 234 printf( "%s\r\n", r ? "Fail" : "Pass" );
okano 7:815366f003ee 235 }
okano 7:815366f003ee 236
okano 7:815366f003ee 237
okano 7:815366f003ee 238 char read_byte( void )
okano 7:815366f003ee 239 {
okano 7:815366f003ee 240 while ( !target.readable() )
okano 7:815366f003ee 241 ;
okano 8:b220fadbb3d8 242
okano 7:815366f003ee 243 return ( target.getc() );
okano 7:815366f003ee 244 }
okano 7:815366f003ee 245
okano 7:815366f003ee 246
okano 7:815366f003ee 247 void erase_sectors( int last_sector )
okano 7:815366f003ee 248 {
okano 7:815366f003ee 249 char command_str[ STR_BUFF_SIZE ];
okano 8:b220fadbb3d8 250
okano 7:815366f003ee 251 sprintf( command_str, "P 0 %d\r\n", last_sector );
okano 7:815366f003ee 252 try_and_check( command_str, "0", 0 );
okano 8:b220fadbb3d8 253
okano 7:815366f003ee 254 *(command_str) = 'E';
okano 7:815366f003ee 255 try_and_check( command_str, "0", 0 );
okano 7:815366f003ee 256 }
okano 7:815366f003ee 257
okano 12:5a33b5d39792 258 #define BYTES_PER_LINE 45
okano 12:5a33b5d39792 259 char uue_table[ 64 ];
okano 7:815366f003ee 260
okano 12:5a33b5d39792 261 int write_uuencoded_data( FILE *fp, int ram_size, int sector_size, unsigned int ram_start )
okano 7:815366f003ee 262 {
okano 7:815366f003ee 263 char command_str[ STR_BUFF_SIZE ];
okano 7:815366f003ee 264 long checksum = 0;
okano 7:815366f003ee 265 int total_size = 0;
okano 7:815366f003ee 266 int size;
okano 8:b220fadbb3d8 267
okano 7:815366f003ee 268 int flash_writing_size;
okano 7:815366f003ee 269 int lines_per_transfer;
okano 7:815366f003ee 270 int transfer_size;
okano 8:b220fadbb3d8 271
okano 12:5a33b5d39792 272 char *b;
okano 12:5a33b5d39792 273
okano 7:815366f003ee 274 initialize_uue_table();
okano 8:b220fadbb3d8 275
okano 12:5a33b5d39792 276 flash_writing_size = get_flash_writing_size( ram_size, ram_start );
okano 11:8dfc3217d1ca 277 lines_per_transfer = ((flash_writing_size / BYTES_PER_LINE) + 1);
okano 11:8dfc3217d1ca 278 transfer_size = (((flash_writing_size + 11) / 12) * 12);
okano 8:b220fadbb3d8 279
okano 7:815366f003ee 280 // char b[ transfer_size ]; // this can be done in mbed-compiler. but I should do it in common way
okano 8:b220fadbb3d8 281
okano 7:815366f003ee 282 if ( NULL == (b = (char *)malloc( transfer_size * sizeof( char ) )) )
okano 7:815366f003ee 283 error( "malloc error happened\r\n" );
okano 8:b220fadbb3d8 284
okano 7:815366f003ee 285 for ( int i = flash_writing_size; i < transfer_size; i++ )
okano 7:815366f003ee 286 b[ i ] = 0; // this is not neccesary but just stuffing stuffing bytes
okano 8:b220fadbb3d8 287
okano 7:815366f003ee 288 while ( size = fread( b, sizeof( char ), flash_writing_size, fp ) ) {
okano 8:b220fadbb3d8 289
okano 7:815366f003ee 290 if ( !total_size ) {
okano 7:815366f003ee 291 // overwriting 4 bytes data for address=0x1C
okano 7:815366f003ee 292 // there is a slot for checksum that is checked in (target's) boot process
okano 7:815366f003ee 293 add_isp_checksum( b );
okano 7:815366f003ee 294 }
okano 8:b220fadbb3d8 295
okano 12:5a33b5d39792 296 sprintf( command_str, "W %ld %ld\r\n", ram_start, transfer_size );
okano 7:815366f003ee 297 try_and_check( command_str, "0", 0 );
okano 8:b220fadbb3d8 298
okano 7:815366f003ee 299 for ( int i = 0; i < lines_per_transfer; i++ ) {
okano 12:5a33b5d39792 300
okano 11:8dfc3217d1ca 301 checksum += bin2uue( b + (i * BYTES_PER_LINE), command_str, i == (lines_per_transfer - 1) ? (transfer_size % BYTES_PER_LINE) : BYTES_PER_LINE );
okano 8:b220fadbb3d8 302
okano 13:60995bf8b2c7 303 // printf( " data -- %02d %s\r", i, command_str );
okano 8:b220fadbb3d8 304
okano 7:815366f003ee 305 put_string( command_str );
okano 8:b220fadbb3d8 306
okano 7:815366f003ee 307 if ( !((i + 1) % 20) ) {
okano 7:815366f003ee 308 send_RAM_transfer_checksum( checksum );
okano 7:815366f003ee 309 checksum = 0;
okano 7:815366f003ee 310 }
okano 7:815366f003ee 311 }
okano 8:b220fadbb3d8 312
okano 7:815366f003ee 313 send_RAM_transfer_checksum( checksum );
okano 7:815366f003ee 314 checksum = 0;
okano 8:b220fadbb3d8 315
okano 12:5a33b5d39792 316 sprintf( command_str, "P %d %d\r\n", total_size / sector_size, total_size / sector_size );
okano 7:815366f003ee 317 try_and_check( command_str, "0", 0 );
okano 8:b220fadbb3d8 318
okano 12:5a33b5d39792 319 sprintf( command_str, "C %d %d %d\r\n", total_size, ram_start, flash_writing_size );
okano 7:815366f003ee 320 try_and_check( command_str, "0", 0 );
okano 8:b220fadbb3d8 321
okano 7:815366f003ee 322 total_size += size;
okano 7:815366f003ee 323 }
okano 8:b220fadbb3d8 324
okano 7:815366f003ee 325 try_and_check( "G 0 T\r\n", "0", 0 );
okano 7:815366f003ee 326 free( b );
okano 8:b220fadbb3d8 327
okano 12:5a33b5d39792 328 return ( total_size );
okano 7:815366f003ee 329 }
okano 7:815366f003ee 330
okano 7:815366f003ee 331
okano 12:5a33b5d39792 332 int write_binary_data( FILE *fp, int ram_size, int sector_size, unsigned int ram_start )
okano 12:5a33b5d39792 333 {
okano 12:5a33b5d39792 334 char command_str[ STR_BUFF_SIZE ];
okano 12:5a33b5d39792 335 int total_size = 0;
okano 12:5a33b5d39792 336 int size;
okano 12:5a33b5d39792 337 int flash_writing_size;
okano 12:5a33b5d39792 338 char *b;
okano 12:5a33b5d39792 339
okano 12:5a33b5d39792 340 flash_writing_size = 256;
okano 12:5a33b5d39792 341
okano 12:5a33b5d39792 342 if ( NULL == (b = (char *)malloc( flash_writing_size * sizeof( char ) )) )
okano 12:5a33b5d39792 343 error( "malloc error happened\r\n" );
okano 12:5a33b5d39792 344
okano 12:5a33b5d39792 345 while ( size = fread( b, sizeof( char ), flash_writing_size, fp ) ) {
okano 12:5a33b5d39792 346
okano 12:5a33b5d39792 347 if ( !total_size ) {
okano 12:5a33b5d39792 348 // overwriting 4 bytes data for address=0x1C
okano 12:5a33b5d39792 349 // there is a slot for checksum that is checked in (target's) boot process
okano 12:5a33b5d39792 350 add_isp_checksum( b );
okano 12:5a33b5d39792 351 }
okano 12:5a33b5d39792 352
okano 12:5a33b5d39792 353 sprintf( command_str, "W %ld %ld\r\n", ram_start, flash_writing_size );
okano 12:5a33b5d39792 354 try_and_check( command_str, "0", 0 );
okano 12:5a33b5d39792 355
okano 12:5a33b5d39792 356 put_binary( b, flash_writing_size );
okano 12:5a33b5d39792 357 put_string( "\r\n" );
okano 12:5a33b5d39792 358
okano 12:5a33b5d39792 359 sprintf( command_str, "P %d %d\r\n", total_size / sector_size, total_size / sector_size );
okano 12:5a33b5d39792 360 try_and_check( command_str, "0", 0 );
okano 12:5a33b5d39792 361
okano 12:5a33b5d39792 362 sprintf( command_str, "C %d %d %d\r\n", total_size, ram_start, flash_writing_size );
okano 12:5a33b5d39792 363 try_and_check( command_str, "0", 0 );
okano 12:5a33b5d39792 364
okano 12:5a33b5d39792 365 total_size += size;
okano 12:5a33b5d39792 366 printf( " total %d bytes transferred\r", total_size );
okano 12:5a33b5d39792 367
okano 12:5a33b5d39792 368 }
okano 12:5a33b5d39792 369
okano 12:5a33b5d39792 370 free( b );
okano 12:5a33b5d39792 371
okano 12:5a33b5d39792 372 return ( total_size );
okano 12:5a33b5d39792 373 }
okano 12:5a33b5d39792 374
okano 7:815366f003ee 375 void initialize_uue_table( void )
okano 7:815366f003ee 376 {
okano 7:815366f003ee 377 int i;
okano 8:b220fadbb3d8 378
okano 7:815366f003ee 379 uue_table[0] = 0x60; // 0x20 is translated to 0x60 !
okano 8:b220fadbb3d8 380
okano 7:815366f003ee 381 for (i = 1; i < 64; i++) {
okano 7:815366f003ee 382 uue_table[i] = (char)(0x20 + i);
okano 7:815366f003ee 383 }
okano 7:815366f003ee 384 }
okano 7:815366f003ee 385
okano 7:815366f003ee 386
okano 11:8dfc3217d1ca 387 long bin2uue( char *bin, char *str, int size )
okano 7:815366f003ee 388 {
okano 7:815366f003ee 389 unsigned long v;
okano 7:815366f003ee 390 long checksum = 0;
okano 7:815366f003ee 391 int strpos = 0;
okano 8:b220fadbb3d8 392
okano 11:8dfc3217d1ca 393 *(str + strpos++) = ' ' + size;
okano 8:b220fadbb3d8 394
okano 11:8dfc3217d1ca 395 for ( int i = 0; i < size; i += 3 ) {
okano 7:815366f003ee 396 checksum += *(bin + i + 0) + *(bin + i + 1) + *(bin + i + 2);
okano 7:815366f003ee 397 v = (*(bin + i + 0) << 16) | (*(bin + i + 1) << 8) | (*(bin + i + 2) << 0);
okano 7:815366f003ee 398 *(str + strpos++) = uue_table[ (v >> 18) & 0x3F ];
okano 7:815366f003ee 399 *(str + strpos++) = uue_table[ (v >> 12) & 0x3F ];
okano 7:815366f003ee 400 *(str + strpos++) = uue_table[ (v >> 6) & 0x3F ];
okano 7:815366f003ee 401 *(str + strpos++) = uue_table[ (v >> 0) & 0x3F ];
okano 7:815366f003ee 402 }
okano 7:815366f003ee 403 *(str + strpos++) = '\n';
okano 7:815366f003ee 404 *(str + strpos++) = '\0';
okano 8:b220fadbb3d8 405
okano 7:815366f003ee 406 return checksum;
okano 7:815366f003ee 407 }
okano 6:0ae6fe8c8512 408
okano 6:0ae6fe8c8512 409
okano 12:5a33b5d39792 410 int get_flash_writing_size( int ram_size, unsigned int ram_start )
okano 6:0ae6fe8c8512 411 {
okano 6:0ae6fe8c8512 412 int flash_writing_size[] = {
okano 6:0ae6fe8c8512 413 4096,
okano 6:0ae6fe8c8512 414 1024,
okano 6:0ae6fe8c8512 415 512,
okano 6:0ae6fe8c8512 416 256
okano 6:0ae6fe8c8512 417 };
okano 6:0ae6fe8c8512 418 int available_size;
okano 6:0ae6fe8c8512 419 int i;
okano 8:b220fadbb3d8 420
okano 12:5a33b5d39792 421 available_size = ram_size - (ram_start & 0xFFFF);
okano 8:b220fadbb3d8 422
okano 6:0ae6fe8c8512 423 for ( i = 0; i < sizeof( flash_writing_size ) / sizeof( int ); i++ ) {
okano 6:0ae6fe8c8512 424 if ( flash_writing_size[ i ] < available_size )
okano 6:0ae6fe8c8512 425 break;
okano 6:0ae6fe8c8512 426 }
okano 8:b220fadbb3d8 427
okano 6:0ae6fe8c8512 428 return ( flash_writing_size[ i ] );
okano 6:0ae6fe8c8512 429 }
okano 4:55f1977bd11a 430
okano 4:55f1977bd11a 431
okano 1:54e619428ae6 432 void add_isp_checksum( char *b )
okano 1:54e619428ae6 433 {
okano 1:54e619428ae6 434 // see http://www.lpcware.com/content/nxpfile/lpc177x8x-checksum-insertion-program
okano 8:b220fadbb3d8 435
okano 1:54e619428ae6 436 unsigned int *p;
okano 1:54e619428ae6 437 unsigned int cksum = 0;
okano 8:b220fadbb3d8 438
okano 1:54e619428ae6 439 p = (unsigned int *)b;
okano 8:b220fadbb3d8 440
okano 1:54e619428ae6 441 for ( int i = 0; i < 7; i++ ) {
okano 1:54e619428ae6 442 cksum += *p++;
okano 1:54e619428ae6 443 }
okano 8:b220fadbb3d8 444
okano 1:54e619428ae6 445 printf( " -- value at checksum slot : 0x%08X\r\n", *p );
okano 8:b220fadbb3d8 446
okano 1:54e619428ae6 447 *p = 0xFFFFFFFF - cksum + 1;
okano 1:54e619428ae6 448 printf( " -- calculated checksum : 0x%08X\r\n", *p );
okano 8:b220fadbb3d8 449
okano 1:54e619428ae6 450 printf( " new checksum will be used to program flash\r\n" );
okano 1:54e619428ae6 451 }
okano 1:54e619428ae6 452
okano 1:54e619428ae6 453
okano 4:55f1977bd11a 454 void send_RAM_transfer_checksum( int checksum )
okano 4:55f1977bd11a 455 {
okano 4:55f1977bd11a 456 char command[ 16 ];
okano 8:b220fadbb3d8 457
okano 4:55f1977bd11a 458 sprintf( command, "%d\n", checksum );
okano 4:55f1977bd11a 459 try_and_check( command, "OK", 0 );
okano 4:55f1977bd11a 460 }
okano 4:55f1977bd11a 461
okano 0:6baefda2e511 462
okano 0:6baefda2e511 463 void put_string( char *s )
okano 0:6baefda2e511 464 {
okano 2:8d75eb0ecd20 465 char c;
okano 2:8d75eb0ecd20 466 static int i = 0;
okano 8:b220fadbb3d8 467
okano 3:3c380e643e74 468 while ( c = *s++ ) {
okano 0:6baefda2e511 469 target.putc( c );
okano 2:8d75eb0ecd20 470 leds = i++ & 0x1;
okano 2:8d75eb0ecd20 471 }
okano 0:6baefda2e511 472 }
okano 0:6baefda2e511 473
okano 7:815366f003ee 474
okano 12:5a33b5d39792 475 void put_binary( char *b, int size )
okano 12:5a33b5d39792 476 {
okano 12:5a33b5d39792 477 for ( int i = 0; i < size; i++ )
okano 12:5a33b5d39792 478 target.putc( *b++ );
okano 12:5a33b5d39792 479 }
okano 12:5a33b5d39792 480
okano 12:5a33b5d39792 481
okano 9:ca4c9a2ac8e1 482 Timeout timeout;
okano 9:ca4c9a2ac8e1 483
okano 9:ca4c9a2ac8e1 484 int timeout_flag = 0;
okano 9:ca4c9a2ac8e1 485
okano 9:ca4c9a2ac8e1 486 void set_flag()
okano 9:ca4c9a2ac8e1 487 {
okano 9:ca4c9a2ac8e1 488 timeout_flag = 1;
okano 9:ca4c9a2ac8e1 489 }
okano 9:ca4c9a2ac8e1 490
okano 9:ca4c9a2ac8e1 491
okano 0:6baefda2e511 492 void get_string( char *s )
okano 0:6baefda2e511 493 {
okano 0:6baefda2e511 494 int i = 0;
okano 0:6baefda2e511 495 char c = 0;
okano 9:ca4c9a2ac8e1 496 timeout_flag = 0;
okano 9:ca4c9a2ac8e1 497
okano 9:ca4c9a2ac8e1 498 timeout.attach( &set_flag, 1 );
okano 8:b220fadbb3d8 499
okano 0:6baefda2e511 500 do {
okano 0:6baefda2e511 501 do {
okano 0:6baefda2e511 502 if ( target.readable() ) {
okano 0:6baefda2e511 503 c = target.getc();
okano 8:b220fadbb3d8 504
okano 0:6baefda2e511 505 if ( ( c == '\n') || (c == '\r') )
okano 0:6baefda2e511 506 break;
okano 8:b220fadbb3d8 507
okano 0:6baefda2e511 508 *s++ = c;
okano 0:6baefda2e511 509 i++;
okano 0:6baefda2e511 510 }
okano 9:ca4c9a2ac8e1 511
okano 9:ca4c9a2ac8e1 512 if ( timeout_flag )
okano 9:ca4c9a2ac8e1 513 return;
okano 0:6baefda2e511 514 } while ( 1 );
okano 0:6baefda2e511 515 } while ( !i );
okano 8:b220fadbb3d8 516
okano 0:6baefda2e511 517 *s = '\0';
okano 0:6baefda2e511 518 }
okano 9:ca4c9a2ac8e1 519