// Private method uint8_t EEPROM_24XX1025::writeChunk(uint32_t fulladdr, const void *data, uint8_t bytesToWrite) { // Used to turn 1-128 byte writes into full page writes (i.e. turn them into proper single-page writes) if (bytesToWrite == 0 || bytesToWrite > 128 || fulladdr >= DEVICE_SIZE) return 0; if (fulladdr + bytesToWrite > DEVICE_SIZE) bytesToWrite = DEVICE_SIZE - fulladdr; // Calculate the 16-bit address, and the page number of the first and second (if applicable) // blocks we're going to write to. uint32_t pageaddr = TO_PAGEADDR(fulladdr); uint8_t firstBlock = BLOCKNUM(fulladdr); uint8_t secondBlock = BLOCKNUM(fulladdr + bytesToWrite - 1); // These page numbers are *relative to the block number*, i.e. firstPage = 0 may mean at byte 0 or byte 65536 // depending on firstBlock above. Same goes for secondPage/secondBlock of course. uint16_t firstPage = pageaddr / 128; // pageaddr is already relative to block! uint16_t secondPage = (TO_PAGEADDR(pageaddr + bytesToWrite - 1))/128; if (firstPage == secondPage && firstBlock == secondBlock) { // Data doesn't "cross the border" between pages. Easy! return writeSinglePage(fulladdr, data, bytesToWrite); } else { // The data spans two pages, e.g. begins at address 120 and is 12 bytes long, which would make it go // past the edge of this page (addresses 0 - 127) and onto the next. // We need to split this write manually. uint8_t bytesInFirstPage = ((firstPage + 1) * 128) - pageaddr; uint8_t bytesInSecondPage = bytesToWrite - bytesInFirstPage; uint8_t ret = 0; // Write the data that belongs to the first page if ((ret = writeSinglePage(TO_FULLADDR(firstBlock, pageaddr), data, bytesInFirstPage)) != bytesInFirstPage) { return ret; } // Write the data that belongs to the second page if ((ret = writeSinglePage(TO_FULLADDR(secondBlock, secondPage * 128), (const void*)((byte *)data + bytesInFirstPage), bytesInSecondPage)) != bytesInSecondPage) { return bytesInFirstPage + ret; } } return bytesToWrite; }
int main(int argc, char *argv[]) { HEX_LINE *hexFile; initializeHardware(); deactivateReset(); enableAVRProgramming(); hexFile = parseHexFile(argv[1]); printHexfile(hexFile); eraseAVRFlash(); writeSinglePage(hexFile); readSinglePage(); deactivateReset(); deleteHexfile(hexFile); freeHardwareResources(); return 0; } // end main()