void dfuExecCommand() { if (dfuSubCommand == DFU_CMD_ERASE_PAGE) { debug("erasing ...\n"); if (address >= ENTRY && address <= FLASH_END) { eraseFlash(address); } else { dfu_status.bState = dfuERROR; dfu_status.bStatus = errADDRESS; } } else if (dfuSubCommand == DFU_CMD_MASS_ERASE) { u32 erase_address; debug("start mass-erase\n"); for (erase_address = ENTRY; erase_address < FLASH_END; erase_address += ERASE_PAGE_SIZE) { eraseFlash(erase_address); } debug("stop mass-erase\n"); } else if (dfuSubCommand == DFU_CMD_DOWNLOAD) { if (address >= ENTRY && address <= FLASH_END) { int counter; int written = 0; if ((FLASH_END - address + 1) < transfer_length) { transfer_length = FLASH_END - address + 1; } counter = transfer_length; debug2("writing address: %lx\n", address); while (counter > 0) { if (counter >= FLASH_WRITE_SIZE) { writeFlash(address + written, (u8 __data *)&transfer[written], FLASH_WRITE_SIZE); } else { writeFlash(address + written, (u8 __data *)&transfer[written], counter); } counter = counter - FLASH_WRITE_SIZE; written = written + FLASH_WRITE_SIZE; } } else { dfu_status.bState = dfuERROR; dfu_status.bStatus = errADDRESS; } } else { debug("do nothing\n"); } }
void wcycle_pwm_ctl (unsigned char t) { if (t > 100) { t = 100; } if (t == TA1CCR1) { return; } writeFlash (t); TA1CCR1 = unsigned (t) * PWM_STEP; last_succ_speed = t; }
void writeFlashBlock() { uint32_t blockOffset = 0; while (BlockCount) { writeFlash((uint32_t *)firmwareAddress, &Block_buffer[blockOffset]); blockOffset += FLASH_PAGESIZE/4; // 32-bit words firmwareAddress += FLASH_PAGESIZE; if (BlockCount > FLASH_PAGESIZE) { BlockCount -= FLASH_PAGESIZE; } else { BlockCount = 0; } } }
void HttpFirmwareUpdate::writeRawData(pbuf* buf, int startPos) { pbuf *cur = buf; while (cur != NULL && cur->len > 0 && !writeError) { char* ptr = (char*) cur->payload + startPos; int len = cur->len - startPos; int res = writeFlash(ptr, pos, len); //debugf("Write 0x%X %d %d", pos, len, res); pos += res; writeError |= (res != len); if (writeError) debugf("WriteError %d != %d", res, len); cur = cur->next; startPos = 0; } }
void programPage(uint16_t address) { uint16_t length = (getch() << 8) | getch(); // It is weird that makeWord does not work here uint8_t memtype = getch(); switch (memtype) { case 'F': writeFlash(address, length); break; case 'E': writeEeprom(address, length); break; default: Serial.write(STK_FAILED); break; } }
void flashBootloader(const char * filename) { FIL file; f_open(&file, filename, FA_READ); uint8_t buffer[1024]; UINT count; lcd_clear(); displayProgressBar(STR_WRITING); static uint8_t unlocked = 0; if (!unlocked) { unlocked = 1; unlockFlash(); } for (int i=0; i<BOOTLOADER_SIZE; i+=1024) { watchdogSetTimeout(100/*1s*/); if (f_read(&file, buffer, 1024, &count) != FR_OK || count != 1024) { POPUP_WARNING(STR_SDCARD_ERROR); break; } if (i==0 && !isBootloaderStart((uint32_t *)buffer)) { POPUP_WARNING(STR_INCOMPATIBLE); break; } for (int j=0; j<1024; j+=FLASH_PAGESIZE) { writeFlash(CONVERT_UINT_PTR(FIRMWARE_ADDRESS+i+j), (uint32_t *)(buffer+j)); } updateProgressBar(i, BOOTLOADER_SIZE); SIMU_SLEEP(30/*ms*/); } if (unlocked) { lockFlash(); unlocked = 0; } f_close(&file); }
int32_t fat12Write(const uint8_t *buffer, uint16_t sector, uint16_t count) { enum FatWriteOperation { FATWRITE_NONE, FATWRITE_EEPROM, FATWRITE_FIRMWARE }; static unsigned int operation = FATWRITE_NONE; TRACE("FAT12 Write(sector=%d, count=%d)", sector, count); if (sector < 3) { // reserved, read-only } else if (sector < 3 + (EESIZE/BLOCKSIZE)) { // eeprom while (count) { if (operation == FATWRITE_NONE && isEepromStart(buffer)) { TRACE("EEPROM start found in sector %d", sector); operation = FATWRITE_EEPROM; } if (operation == FATWRITE_EEPROM) { eeWriteBlockCmp((uint8_t *)buffer, (sector-3)*BLOCKSIZE, BLOCKSIZE); } buffer += BLOCKSIZE; sector++; count--; if (sector-3 >= (EESIZE/BLOCKSIZE)) { TRACE("EEPROM end written at sector %d", sector-1); operation = FATWRITE_NONE; } } } else if (sector < 3 + (EESIZE/BLOCKSIZE) + (FLASHSIZE/BLOCKSIZE)) { // firmware uint32_t address; address = sector - 3 - (EESIZE/BLOCKSIZE); address *= BLOCKSIZE; address += FIRMWARE_ADDRESS; while (count) { for (uint32_t i=0; i<BLOCKSIZE/FLASH_PAGESIZE; i++) { if (address >= FIRMWARE_ADDRESS+BOOTLOADER_SIZE/*protect bootloader*/ && address <= FIRMWARE_ADDRESS+FLASHSIZE-FLASH_PAGESIZE) { if (address == FIRMWARE_ADDRESS+BOOTLOADER_SIZE && isFirmwareStart(buffer)) { TRACE("FIRMWARE start found in sector %d", sector); operation = FATWRITE_FIRMWARE; } if (operation == FATWRITE_FIRMWARE) { writeFlash((uint32_t *)address, (uint32_t *)buffer); } } address += FLASH_PAGESIZE; buffer += FLASH_PAGESIZE; } sector++; count--; if (sector-3-(EESIZE/BLOCKSIZE) >= (FLASHSIZE/BLOCKSIZE)) { TRACE("FIRMWARE end written at sector %d", sector-1); operation = FATWRITE_NONE; } } } return 0 ; }
int fwrite2(MYFILE *fp, void *buffer, int nBytes) { int startsector; int endsector; uint8_t realsector; int readbytes; int pagenum, blockoffset, pageoffset; //first it checks whether there is enough space for the writing to take place, then it does the actual writing in the same way as above if (fp->fpos + nBytes <= fp->size) { //there is no need to change the size or allocate more space for the write } else { int allocate; allocate = (fp->size/2048)*2048; if (fp->size%2048>0) allocate+=2048; if (fp->fpos + nBytes > allocate) newSector(fp->addr); if (fp->fpos + nBytes > fp->size) { fp->size = fp->fpos + nBytes; write16uint(fp->addr, FILE_SIZEOFFSET, fp->size); } } startsector = fp->fpos/2048; endsector = (fp->fpos+nBytes-1)/2048; if (startsector == endsector) { blockoffset = fp->fpos%2048; pageoffset = blockoffset%256; pagenum = blockoffset/256; realsector = read8uint(fp->addr, FILE_ADDRPAGEOFFSET+startsector); pagenum = pagenum+(realsector-1)*8; //now pagenum, offset2 means the actual start location just read it writeFlash(pagenum, pageoffset, buffer, nBytes); } else { blockoffset = fp->fpos%2048; pageoffset = blockoffset%256; pagenum = blockoffset/256; realsector = read8uint(fp->addr, FILE_ADDRPAGEOFFSET+startsector); pagenum = pagenum+(realsector-1)*8; readbytes = 256 - pageoffset; //now pagenum, offset2 means the actual start location just read it writeFlash(pagenum, pageoffset, buffer, readbytes); buffer = (void *)((char *)buffer + readbytes); blockoffset = (fp->fpos+nBytes-1)%2048; pageoffset = blockoffset%256; pagenum = blockoffset/256; realsector = read8uint(fp->addr, FILE_ADDRPAGEOFFSET+endsector); pagenum = pagenum+(realsector-1)*8; readbytes = nBytes - readbytes; //now pagenum, offset2 means the actual start location just read it writeFlash(pagenum, 0, buffer, readbytes); } return 0; }
int main(int argc, char **argv) { setLogLevel(LOGLEVEL_INFO); const char *device = DEFAULT_BASEBAND_DEVICE; unsigned int rate = 921600; char mode = '\0'; char *secpack = NULL; char *binfile = NULL; unsigned int start = 0; unsigned int length = 0; int ch; while ((ch = getopt(argc, argv, "D:B:b:n:s:r:xw:vh")) != -1) { switch (ch) { case 'v': setLogLevel(LOGLEVEL_TRACE); break; case 'x': mode = 'x'; break; case 'h': usage(); return 0; case 'D': device = optarg; break; case 'B': rate = (unsigned int) strtoul(optarg, NULL, 10); break; case 'b': start = (unsigned int) strtoul(optarg, NULL, 0); if (start < 0xa0000000 || start >= 0xb0000000) { start = (start & ~0xf0000000) | 0xa0000000; fprintf(stderr, "Start address is not in flash range, corrected to 0x%08x.\n", start); } break; case 'n': length = (unsigned int) strtoul(optarg, NULL, 0); break; case 's': secpack = optarg; break; case 'r': mode = 'r'; binfile = optarg; break; case 'w': mode = 'w'; binfile = optarg; break; default: usage(); return 1; } } resetBaseband(); int fd = openBaseband(device); if (fd == -1) { fprintf(stderr, "Can't open %s: %s", device, strerror(errno)); return 2; } setBaudRate(fd, rate); VersionPacket version = getBootVersion(fd); fprintf(stderr, "Got %d.%d, %s\n", version.major, version.minor, version.major <= 3 ? "GOOD." : "BAD. You need a secpack that is more recent than the installed firmware."); prepareFlash(fd); int err = 0; switch (mode) { case 'x': sendSecPack(fd, secpack); eraseBaseband(fd, start, start + length); endSecPack(fd); break; case 'r': err = readFlash(fd, binfile, start, length); break; case 'w': sendSecPack(fd, secpack); //eraseBaseband(fd, start, start + length); err = writeFlash(fd, binfile, start, length); endSecPack(fd); break; default: break; } close(fd); return err; }
void downloadBootloader() { int Buffer[SerBufferSize]; // serial input buffer int1 notDone = 1; unsigned int recLen; // HEX file record length unsigned int16 writeAddr; // HEX file write address unsigned char recType; // HEX file record type unsigned char i=0,j; // general counters /// unsigned int16 UserBootVectorAddr; // holds the address of our new boot vector unsigned int positionInMemoryBlock; unsigned int16 memoryBlockAddress; unsigned int bufferIndex; unsigned int writeLen; int16 BankAddress; int1 skipWriting=0; int relocateCount=0; #define REC_SIZE 0x10 int relocateRecordLen[6]; int relocateOriginalAddress[6]; // int flashReadBuffer[getenv("FLASH_ERASE_SIZE")]; // buffer for the relocated first mem block int flashReadBuffer[REC_SIZE]; // buffer for the relocated first mem block usb_init(); while(!usb_cdc_connected()); while (notDone) { ////////////////////////////////////////// /// Wait for ':' do { while (!usb_cdc_kbhit()); } while (usb_cdc_getc() != ':'); ///////////////////////////////////////// // Record length recLen = read8(); ///////////////////////////////////////// // Write Address writeAddr = ((int16)read8() << 8) + read8(); ///////////////////////////////////////// // Rec Type usb_cdc_getc(); // ignore the first digit, it is always '0' recType = usb_cdc_getc(); if (recType == '1') { // End of file record notDone = 0; } else if (recType == '4') { // bank select record BankAddress = (read8()<<8) + read8(); if (BankAddress != 0) skipWriting = 1; else skipWriting = 0; } else if ((recType == '0') && !(skipWriting)) { // data record /// get the data for (i=0; i < recLen ; i++) { Buffer[i] = read8(); } /* // if data is in the EEPROM area -> do nothing if ((writeAddr >= 0x2100) && (writeAddr <= 0x21FF)) { // we don't support EEPROM records yet } */ // if writing to the first memory block -> we need to temporarily // relocate it elsewhere. Since this area contains the boot and // interrupt vectors -> it should be written as the very last memory // block. if (writeAddr & (0xFFFF ^ (getenv("FLASH_ERASE_SIZE")-1)) == 0) { relocateOriginalAddress[relocateCount] = writeAddr; // remmber the orginal location relocateRecordLen[relocateCount] = recLen; // remember each record's len writeAddr = RESERVED_MEMORY_START + (relocateCount * REC_SIZE); relocateCount++; } positionInMemoryBlock = writeAddr & (getenv("FLASH_ERASE_SIZE")-1); memoryBlockAddress = writeAddr & (0xFFFF ^ (getenv("FLASH_ERASE_SIZE")-1)); writeFlash(memoryBlockAddress, positionInMemoryBlock, recLen, Buffer); } if (notDone) // Tells the PC to send the next line printf(active_comm_putc, "%c",READY_FOR_NEXT); } // Tells the PC that we are finished printf(active_comm_putc,"%c", FINISH_FLAG); delay_ms(100); ///////////////////////////////////////////////////////////////////////// // // Now we move the relocated first block to its original place // // read the entire flash block to memory disable_interrupts(GLOBAL); for (i=0;i<relocateCount;i++) { read_program_memory(RESERVED_MEMORY_START + (i*REC_SIZE), flashReadBuffer, REC_SIZE); writeAddr = relocateOriginalAddress[i]; writeFlash(0x0, writeAddr, relocateRecordLen[i], flashReadBuffer ); } output_low(USER_LED); delay_ms(100); reset_cpu(); }
int A3818UpgradeFromFile(int32_t A3818_handle, FILE * binfile, int fwcopy, A3818Upgrade_Mode mode) { uint32_t baseAddress; long currentTime, elapsedTime; int finish; unsigned char c, c1; uint32_t bp; uint32_t bufferLength; uint32_t *buffer; unsigned int verifyErrors = 0; buffer = malloc(BITSTREAM_BYTES * sizeof(uint32_t)); switch(fwcopy) { case 0 : baseAddress = FIRST_FIRMWARE_PAGE_BASE_WORD_ADDRESS; break; case 1 : baseAddress = SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS; break; case 2 : baseAddress = THIRD_FIRMWARE_PAGE_BASE_WORD_ADDRESS; break; case 3 : baseAddress = FOURTH_FIRMWARE_PAGE_BASE_WORD_ADDRESS; break; default : baseAddress = SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS; break; } bp = 0; finish = 0; // Carica il bitsream in un buffer di memoria currentTime = get_time(); while( !finish ) { c = (unsigned char)fgetc(binfile); // read one byte from file c1 = (unsigned char)fgetc(binfile); // read one byte from file (Strataflash a 16 bit) #ifdef __SWAP__ swap = 0; // Swap primo byte for( i = 0; i < 8; i++ ) if( c & (1 << i) ) swap = swap | (0x80 >> i); swap1 = 0; // Swap secondo byte for( i = 0; i < 8; i++ ) if( c1 & (1 << i) ) swap1 = swap1 | (0x80 >> i); buffer[bp] = (uint32_t) ((swap1 <<8) | swap); // HACK : swap o non swap? #else buffer[bp] = (uint32_t) ((c1 <<8) | c); #endif // __SWAP__ bp++; if( feof(binfile) ) finish = 1; } // end of while loop bufferLength = (--bp); if ((bufferLength*2) != BITSTREAM_BYTES) { printf("\nERROR: Input BIN file length (%d bytes) is different than expected bitstream size (%d bytes). Exiting.....\n", bufferLength*2, BITSTREAM_BYTES); free(buffer); return -1; } elapsedTime = get_time() - currentTime; printf("\nBitstream (%d bytes) loaded in %ld milliseconds\n", bufferLength*2, elapsedTime); A3818_EnableBPIAccess(A3818_handle); /* Cancellazione della zona di flash riservata al firmware */ if ( (mode == A3818_UPGRADE_FULL ) || (mode == A3818_UPGRADE_ERASE_ONLY) ) { printf("Erasing flash "); fflush(stdout); currentTime = get_time(); eraseFirmware(A3818_handle, baseAddress, fwcopy); elapsedTime = get_time() - currentTime; printf("\nFlash erased in %ld milliseconds\n", elapsedTime); } /* Programmazione immagine firmware in flash */ if ( (mode == A3818_UPGRADE_FULL ) ) { currentTime = get_time(); writeFlash(A3818_handle, buffer, bufferLength, baseAddress); elapsedTime = get_time() - currentTime; printf("\n%d 16-bit words programmed in %ld milliseconds\n", bufferLength, elapsedTime); } /* Verifica immagine firmware in flash */ if ( (mode == A3818_UPGRADE_FULL ) || (mode == A3818_UPGRADE_VERIFY_ONLY) ) { currentTime = get_time(); verifyErrors = verifyFlash(A3818_handle, buffer, bufferLength, baseAddress); elapsedTime = get_time() - currentTime; printf("\n%d 16-bit words verified in %ld milliseconds\n", bufferLength, elapsedTime); } A3818_EnableSPIAccess(A3818_handle); if( verifyErrors > 0 ) { printf("\n\n%d errors found during verify!\n",verifyErrors); } else { printf("\n\nFirmware updated without errors. Written %d words\n",bufferLength); } free(buffer); return 0; }