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