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