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