unsigned short CHexManager::GetNextHexRecord(char *HexRec, unsigned int BuffLen) { unsigned short len = 0; if(!feof(HexFilePtr)) { fgets(Ascii, BuffLen, HexFilePtr); if(Ascii[0] != ':') { // Not a valid hex record. return 0; } // Convert rest to hex. len = ConvertAsciiToHex((void *)&Ascii[1], (void *)HexRec); HexCurrLineNo++; } return len; }
void BOOTLDR_Run() { volatile UINT i; FILEHANDLE *myFile; FILEHANDLE *currentFile; FW_VERSION newVersion; CHAR buffer[64]; newVersion.major = 0; newVersion.minor = 0; newVersion.build = 0; //if(!ForcedFirmwareExists() && !NewerFirmwareExists() && ValidAppPresent()) if(!ShouldFlashFirmware(sTargetFile, &newVersion) && ValidAppPresent()) { // No new firmware to load. Jump // directly to the application JumpToApp(); } // At this point sTargetFile will have the name of the file we should use //strcpy(sTargetFile, "fw/force/v0_0_2.hex"); myFile = FATFS_fopen(sTargetFile,"r"); if(myFile == NULL)// Make sure the file is present. { //Indicate error and stay in while loop. ErrorFunction(); } // Erase Flash (Block Erase the program Flash) EraseFlash(); // Initialize the state-machine to read the records. record.status = REC_NOT_FOUND; while(1) { // For a faster read, read 512 bytes at a time and buffer it. readBytes = FATFS_fread((void *)&asciiBuffer[pointer], 1, 512, myFile); //readBytes = FSfread((void *)&asciiBuffer[pointer],1,512,myFile); if(readBytes == 0) { // Nothing to read. Come out of this loop // break; FATFS_fclose(myFile); // Something fishy. The hex file has ended abruptly, looks like there was no "end of hex record". //Indicate error and stay in while loop. ErrorFunction(); } for(i = 0; i < (readBytes + pointer); i ++) { // This state machine seperates-out the valid hex records from the read 512 bytes. switch(record.status) { case REC_FLASHED: case REC_NOT_FOUND: if(asciiBuffer[i] == ':') { // We have a record found in the 512 bytes of data in the buffer. record.start = &asciiBuffer[i]; record.len = 0; record.status = REC_FOUND_BUT_NOT_FLASHED; } break; case REC_FOUND_BUT_NOT_FLASHED: if((asciiBuffer[i] == 0x0A) || (asciiBuffer[i] == 0xFF)) { // We have got a complete record. (0x0A is new line feed and 0xFF is End of file) // Start the hex conversion from element // 1. This will discard the ':' which is // the start of the hex record. ConvertAsciiToHex(&record.start[1],hexRec); // We're done so let's close the file and update current.txt if(hexRec[3] == END_OF_FILE_RECORD) { FATFS_fclose(myFile); currentFile = FATFS_fopen("fw/current.txt", "wo"); if(currentFile != NULL) { sprintf(buffer, "v%d_%d_%d.hex", newVersion.major, newVersion.minor, newVersion.build); FATFS_fwrite(buffer, 1, strlen(buffer), currentFile); FATFS_fclose(currentFile); } } WriteHexRecord2Flash(hexRec); record.status = REC_FLASHED; // Blink the green LED to indicate programming in progress mLED_Green_Toggle(); } break; } // Move to next byte in the buffer. record.len ++; } if(record.status == REC_FOUND_BUT_NOT_FLASHED) { // We still have a half read record in the buffer. The next half part of the record is read // when we read 512 bytes of data from the next file read. memcpy(asciiBuffer, record.start, record.len); pointer = record.len; record.status = REC_NOT_FOUND; } else { pointer = 0; } }//while(1) }// end BOOTLDR_Run function
static void file_flash(FSFILE* file) { UINT readBytes; UINT i; // Erase Flash (Block Erase the program Flash) if (NVMemBlockErase() != 0) { error(ERR_NV_ERASE); } record.status = REC_NOT_FOUND; // Initialize the state-machine to read the records. while (1) { USBTasks(); BlinkBlueLED(); BlinkOrangeLED(); // For a faster read, fetch 512 bytes at a time and buffer it. readBytes = FSfread((void*)&asciiBuffer[pointer], 1, 512, file); if (readBytes == 0) { // Nothing to read. Come out of this loop // break; // Jump to start of application // Disable all enabled interrupts (only USB) // before jumping to the application code. IEC5bits.USB1IE = 0; // JumpToApp(); return; } for (i = 0; i < (readBytes + pointer); i ++) { // This state machine seperates-out the valid hex records from the read 512 bytes. switch (record.status) { case REC_FLASHED: case REC_NOT_FOUND: if (asciiBuffer[i] == ':') { // We have a record found in the 512 bytes of data in the buffer. record.start = &asciiBuffer[i]; record.len = 0; record.status = REC_FOUND_BUT_NOT_FLASHED; } break; case REC_FOUND_BUT_NOT_FLASHED: if ((asciiBuffer[i] == 0x0A) || (asciiBuffer[i] == 0xFF)) { // We have got a complete record. (0x0A is new line feed and 0xFF is End of file) // Start the hex conversion from element // 1. This will discard the ':' which is // the start of the hex record. ConvertAsciiToHex(&record.start[1], hexRec); WriteHexRecord2Flash(hexRec); record.status = REC_FLASHED; } break; } record.len ++; // Move to next byte in the buffer. } if (record.status == REC_FOUND_BUT_NOT_FLASHED) { // We still have a half read record in the buffer. The next half part of the record is read // when we read 512 bytes of data from the next file read. memcpy(asciiBuffer, record.start, record.len); pointer = record.len; record.status = REC_NOT_FOUND; } else { pointer = 0; } } }