Intel HEXファイルの内容確認
「.HEX」ファイル内のデータを取り出すには、開始セクタ(Block)とFATチェーンからmsc_disk変数の該当場所を参照すれば良いのですが、FATが2BlockあるのでBlock数を+2してmsc_disk変数にアクセスします。
(セクタ番号13であれば、実データはmsc_disk[15][*]を参照する)
さてXC8コンパイラの生成するIntel HEXファイルデータの仕様は下記に分かりやすくまとまっています。
Intel HEX - Wikipedia
Intel HEX自体は処理に難しいところはなく1行ずつ読込んでPICマイコンに書出せば良いのですが、いくつか留意すべき点があります。
- Intel HEXファイルデータではアドレス値はバイト単位だが、PICマイコンでの読書きはワード単位なのでアドレス値を1/2する必要がある。例えば下記のレコードの場合、HEXファイルデータのアドレスオフセットはF0Chだが、PICマイコンへの書込みオフセットは786hになります。
: 10 0F0C 00 72302100990023008C018E010A302100 DF - PICマイコンのConfiguration Memory(ワードオフセット8000h〜)はIntel HEXファイルデータ上では10000h〜に配置されます。つまり下記拡張アドレスレコードに続いて記載されます。
: 02 0000 04 0001 F9 - PICマイコンのEEPROMはIntel HEXファイルデータ上では1E000h〜に配置されます。つまり上記の拡張アドレスレコードが指定された後、下記のようなレコードに記載されます。EEPROMのデータ長は8bitですが、書込み時は上位に0をfillしたワードデータとして取り扱います。
: 10 E000 00 FE00FD00FF00FF00FF00FF00FF00FF00 1B EEPROMデータはProgram MemoryやConfiguration Memoryとは独立したMap上に配置されており、HEXファイルで指定されたアドレス1E000hをワードオフセット0として、LOAD DATA FOR DATA MEMORYコマンドで書込みます。
あとはレコードタイプによる処理分けと書込むアドレス(オフセット)とデータをASCIIからバイナリデータに変換すれば書込みへの準備は整います。
下記サンプルコードにてmsc_disk変数からの読込み、データバッファへの保存、チェックサムによる正当性判断を行っています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
//****************************************************************************** //* core1_main.c : test program for RP2040 core1 with using Tiny USB on core0. * //* * //* usage: * //* This program is running on CPU core-1 to avoid unexpected wait for USB. * //* * //* * //* history: * //* Sep16,2022 start coding. N.Mato * //* Sep17,2022 add "find_new_hexfile()" funtion. N.Mato * //* Sep18,2022 add "hex_file_validity_check()" funtion. N.Mato * //* * //* Copyright(c)2022 Rigel Intelligence * //****************************************************************************** //*** includes ***************************************************************** #include <stdio.h> #include "pico/stdlib.h" #include "pico/types.h" #include "pico/multicore.h" //*** definitions ************************************************************** #define NEW_FILE_FOUND 100 // flag from Core0 to Core1 #define DISK_BLOCK_NUM 480 // 240KB #define DISK_BLOCK_SIZE 512 #define FILE_NAME_LENGTH 170 #define FAT12_TABLE1 1 // Block number of file allocation table #define FAT12_TABLE2 2 // Block number of file allocation table #define NUM_FAT_SECTS 2 // Number of blocks that used for FAT #define FAT_END_MARK 0xfff // File end sector mark of FAT #define ROOT_DIR_ENTRY 3 // Block number of root directory entry // free flag of 1st byte of directory entry #define DIR_ENT_FREE_0 0 #define DIR_ENT_FREE_E5 0xE5 // File attributes #define ATTR_READ_ONLY 0x1 #define ATTR_HIDDEN 0x2 #define ATTR_SYSTEM 0x4 #define ATTR_VOLUME_ID 0x8 #define ATTR_DIRECTORY 0x10 #define ATTR_ARCHIVE 0x20 #define ATTR_LONG_NAME 0xF #define LAST_LONG_ENTRY 0x40 //*** structs/unions *********************************************************** // Directory entry typedef struct { char dir_Name[8]; // Short name char dir_Ext[3]; // Short name (extention) uint8_t dir_Attr; // File attributes uint8_t dir_NTRes; // Reserved for WindowsNT uint8_t dir_CrtTimeTenth; // Millisecond time stamp (0-199) uint16_t dir_CrtTime; // Time files was created uint16_t dir_CrtDate; // Date files was created uint16_t dir_LsrAccDate; // Last access date uint16_t dir_FstClusHi; // High word of first cluster (0 for FAT12) uint16_t dir_WrtTime; // Time of last write uint16_t dir_WrtDate; // Date of last write uint16_t dir_FstClusLo; // Low word of first cluster uint32_t dir_FileSize; // File size in bytes } DirectoryEntry_st; // Directory entry (Long file name) typedef struct { uint8_t ldir_Ord; // Order of long dir entries (1-20, 0x40 last flag) uint8_t ldir_Name1[10]; // Char 1-5 of the long name uint8_t ldir_Attr; // Attributes: ATTR_LONG_NAME (mask with 0xF) uint8_t ldir_Type; // Zero uint8_t ldir_Checksum; uint8_t ldir_Name2[12]; // Char 6-11 of the long name uint8_t ldir_FstClusLO[2]; // Zero uint8_t ldir_Name3[4]; // Char 12-13 of the long name } LongDirectoryEntry_st; typedef union { DirectoryEntry_st sfn; // Short file name LongDirectoryEntry_st lfn; // Long file name } DirEnt_un; // 'HEX' file paramters typedef struct { int start_sector; // 1st sector on storage int current_sector; // current accessing sector of using file int file_size; // size of HEX file (Bytes) int read_size; // already read Bytes of using file uint16_t file_name[FILE_NAME_LENGTH]; // Max 128 chars for file name (2bytes code) } HexFileParams_st; // 1-record structure of 'HEX' file typedef struct { char start_code; // should be set as ':' uint8_t byte_count; // 255 max, data payload length uint16_t address_offset; // absolute address = base + offset uint8_t record_type; // 0-5, shows following data type uint8_t data[256]; // data payload, max 255Bytes uint8_t checksum; // checksum of a record } HexRecode_st; //*** prototydes *************************************************************** static int get_FAT12_chain( int currentSector ); static bool find_new_hexfile( HexFileParams_st *fileParams ); static bool hex_file_validity_check( HexFileParams_st *fileParams ); //*** constants/variables ****************************************************** extern uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE]; static HexFileParams_st hexFileParams; static HexRecode_st hex_record; //****************************************************************************** //*** functions **************************************************************** //****************************************************************************** // Pickup FAT12 chain value static int get_FAT12_chain( int currentSector ) { int next_sector; uint8_t *fat_sector = &msc_disk[FAT12_TABLE1][0]; if( currentSector %2 ) { // odd number next_sector = (int )((uint )(fat_sector[currentSector/2 *3 +1] >>4) + ((uint )(fat_sector[currentSector/2 *3 +2]) <<4)); } else { // even number next_sector = (int )(((uint )(fat_sector[currentSector/2 *3 +1] & 0xf) <<8) + (uint )(fat_sector[currentSector/2 *3])); } return next_sector; } // find 'HEX' file in storage static bool find_new_hexfile( HexFileParams_st *fileParams ) { int i, j, k, rootEntCnt; DirEnt_un *rootDirEnt; // Number of root directory entry = fixed value '16' rootEntCnt = msc_disk[0][18]; rootEntCnt <<= 8; rootEntCnt += msc_disk[0][17]; // search root directory entry rootDirEnt = (DirEnt_un *)msc_disk[ROOT_DIR_ENTRY]; // Block3 for( i=2; i<rootEntCnt; i++ ) // skip entry0=volume lavel, entry1=README.TXT { // skip free entry if( (rootDirEnt[i].sfn.dir_Name[0] == DIR_ENT_FREE_0)||(rootDirEnt[i].sfn.dir_Name[0] == DIR_ENT_FREE_E5) ) continue; // skip system files or directory if( rootDirEnt[i].sfn.dir_Attr & (ATTR_HIDDEN|ATTR_SYSTEM|ATTR_VOLUME_ID|ATTR_DIRECTORY) ) continue; // find 'HEX' file if( (rootDirEnt[i].sfn.dir_Ext[0] == 'H')&&(rootDirEnt[i].sfn.dir_Ext[1] == 'E')&&(rootDirEnt[i].sfn.dir_Ext[2] == 'X') ) { // start sector (0-4, reserved) if( rootDirEnt[i].sfn.dir_FstClusLo <= 4 ) break; else fileParams->start_sector = rootDirEnt[i].sfn.dir_FstClusLo; // file size if( rootDirEnt[i].sfn.dir_FileSize == 0 ) break; else fileParams->file_size = rootDirEnt[i].sfn.dir_FileSize; // copy short file name uint16_t *fname = fileParams->file_name; for( j=0; j<8; j++ ) *fname++ = (uint16_t )rootDirEnt[i].sfn.dir_Name[j]; *fname++ = '.'; for( j=0; j<3; j++ ) *fname++ = (uint16_t )rootDirEnt[i].sfn.dir_Ext[j]; *fname = 0; // null terminater // search long file name int order = 1; fname = fileParams->file_name; for( j=i-1; j>1; j-- ) { if( (rootDirEnt[j].lfn.ldir_Attr & ATTR_LONG_NAME) != ATTR_LONG_NAME ) break; // no LFN if( (rootDirEnt[j].lfn.ldir_Ord & 0x1F) != order ) break; // illegal order // overwrite with long file name for( k=0; k<10; k+=2 ) *fname++ = ((uint16_t )rootDirEnt[j].lfn.ldir_Name1[k+1] << 8)+((uint16_t )rootDirEnt[j].lfn.ldir_Name1[k]); for( k=0; k<12; k+=2 ) *fname++ = ((uint16_t )rootDirEnt[j].lfn.ldir_Name2[k+1] << 8)+((uint16_t )rootDirEnt[j].lfn.ldir_Name2[k]); for( k=0; k<4; k+=2 ) *fname++ = ((uint16_t )rootDirEnt[j].lfn.ldir_Name3[k+1] << 8)+((uint16_t )rootDirEnt[j].lfn.ldir_Name3[k]); if( rootDirEnt[j].lfn.ldir_Ord & LAST_LONG_ENTRY ) { *fname = 0; // null terminater break; // end of LFN } order++; } // show file info. ==================================== printf("'HEX' file found.\n"); printf("File Name : "); for(j=0; ; j++) { // file name includes 2-byte length char. if( fileParams->file_name[j] == 0 ) break; // null terminate printf("%c", fileParams->file_name[j]); } printf("\n"); printf("File size : %d\n", fileParams->file_size); printf("Start sct.: %d\n", fileParams->start_sector); // search FAT12 chain // read 1st table -> FAT12 table [startSect -1] int cur_table = fileParams->start_sector, next_table; for( k=0; k<DISK_BLOCK_NUM; k++ ) { // search the next table next_table = get_FAT12_chain(cur_table); if( (next_table > 0xf00)||(next_table == 0) ) { printf("\n"); break; } printf("-[%3d]", next_table); if( k%16==15 ) printf("\n"); cur_table = next_table; } printf("\n"); return( true ); } } printf("no 'HEX' file found.\n"); return( false ); } static uint8_t c2i(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'A': case 'a': return 0xa; case 'B': case 'b': return 0xb; case 'C': case 'c': return 0xc; case 'D': case 'd': return 0xd; case 'E': case 'e': return 0xe; case 'F': case 'f': return 0xf; default: return 0xff; } } // get 1 char from 'HEX' file static char get_char_from_hexfile( int *sector, int *bytes ) { int offset = *bytes % DISK_BLOCK_SIZE; char tmp_char; // actual data block is located +2 sector position tmp_char = msc_disk[*sector +NUM_FAT_SECTS][offset]; offset++; (*bytes)++; if( offset >= DISK_BLOCK_SIZE ) { // set the next sector *sector = get_FAT12_chain( *sector ); if( (*sector != FAT_END_MARK)&&(*sector >= DISK_BLOCK_NUM) ) return '\0'; // no sector number exist offset = 0; } return tmp_char; } // get 1 Byte from 'HEX' file static uint8_t get_byte_from_hexfile( int *sector, int *bytes ) { uint8_t tmp_msb, tmp_lsb; // 4bits each tmp_msb = c2i( get_char_from_hexfile( sector, bytes ) ); tmp_lsb = c2i( get_char_from_hexfile( sector, bytes ) ); return( (tmp_msb <<4) + (tmp_lsb) ); } // get 1 record of 'HEX' file (sequencial read from the start of file) bool get_record_from_hexfile( HexFileParams_st *fileParams, HexRecode_st *hexRecord ) { int i; // check current sector and read(file) size if( (fileParams->current_sector >= DISK_BLOCK_NUM)||(fileParams->read_size >= fileParams->file_size) ) return false; // read 1st data of HEX record = start code ':' hexRecord->start_code = get_char_from_hexfile( &(fileParams->current_sector), &(fileParams->read_size) ); if( hexRecord->start_code != ':' ) return false; // read following 'HEX' record hexRecord->byte_count = get_byte_from_hexfile( &(fileParams->current_sector), &(fileParams->read_size) ); hexRecord->address_offset = (uint16_t )get_byte_from_hexfile( &(fileParams->current_sector), &(fileParams->read_size) ); hexRecord->address_offset <<= 8; hexRecord->address_offset += (uint16_t )get_byte_from_hexfile( &(fileParams->current_sector), &(fileParams->read_size) ); hexRecord->record_type = get_byte_from_hexfile( &(fileParams->current_sector), &(fileParams->read_size) ); for( i=0; i<hexRecord->byte_count; i++ ) hexRecord->data[i] = get_byte_from_hexfile( &(fileParams->current_sector), &(fileParams->read_size) ); hexRecord->checksum = get_byte_from_hexfile( &(fileParams->current_sector), &(fileParams->read_size) ); return true; } static bool hex_file_validity_check( HexFileParams_st *fileParams ) { uint32_t logical_address = 0; // reset read parameters to access from the top of file fileParams->current_sector = fileParams->start_sector; fileParams->read_size = 0; while( fileParams->read_size < fileParams->file_size ) { if( !get_record_from_hexfile( fileParams, &hex_record ) ) { //printf("skip invalid char\n"); continue; } // check record type if( hex_record.record_type == 0x0 ) { // nomal data payload } else if( hex_record.record_type == 0x4 ) { // extended address upper 16bits logical_address = ((uint32_t )hex_record.data[0] <<24); logical_address += ((uint32_t )hex_record.data[1] <<16); } else if( hex_record.record_type == 0x1 ) { // EOF mark } else { // other record type "may not" be used for XC8 PIC compiler return false; } // calc checksum - should be '0' int i; uint8_t tmp_sum = 0; for( i=1; i<=(hex_record.byte_count +4); i++ ) tmp_sum += *((uint8_t *)&hex_record +i); tmp_sum += hex_record.checksum; #if 1 //printf("Sector:%d, total read:%d\n", fileParams->current_sector, fileParams->read_size); printf("byte_count %d\n", hex_record.byte_count); printf("address %08Xh\n", logical_address + (uint32_t )hex_record.address_offset); printf("record_type %02Xh\n", hex_record.record_type); printf("data "); for( i=0; i<hex_record.byte_count; i++) printf(" %02X", hex_record.data[i]); printf("\nsum: "); if( tmp_sum == 0x0 ) printf("OK.\n\n"); else printf("NG.\n\n"); #endif if( tmp_sum != 0x0 ) return false; } return true; } //*** main() ******************************************************************* void core1_main( void ) { bool result = false; printf("\nCore-1 loaded.\n\n"); while(1) { const uint32_t msc_flag = multicore_fifo_pop_blocking(); printf("\nCore-1 called :%d\n\n", msc_flag); if( msc_flag == NEW_FILE_FOUND ) { if( find_new_hexfile( &hexFileParams ) ) { if( hex_file_validity_check( &hexFileParams ) ) printf("'HEX' file validation check: OK.\n\n"); else printf("'HEX' file validation check: NG.\n\n"); } } } } |
サンプルコードと言いつつ、随分と長くなってきました。RP2040にて実行すると、下記のログが得られます。
コメント