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