W25Q64中跨页写入数据
static uint8_t W25Q64_WriteFontBuffer(uint32_t addr, uint8_t *buf, uint32_t len, uint8_t type) {uint32_t pageRemain;uint8_t verifySuccess = 1;uint32_t totalWritten = 0;uint8_t singleRowLength = 0;static uint8_t charDataBuffer[MAX_CHAR_SIZE];static uint32_t charDataBufferIndex = 0;static struct {uint8_t buffer[MAX_CHAR_SIZE]; uint32_t stored; uint32_t expected; uint32_t startAddr; } crossPageChar = {0};uint32_t lastCharSize = 0;uint32_t lastCharOffset = 0;static uint8_t readBackBuf[W25Q64_PAGE_SIZE];while (len) {pageRemain = W25Q64_PAGE_SIZE - (addr % W25Q64_PAGE_SIZE);if (pageRemain > len)pageRemain = len;W25Q64_PageProgram(addr, buf, pageRemain);W25Q64_ReadBuffer(addr, readBackBuf, pageRemain);for (uint32_t i = 0; i < pageRemain; i++) {if (readBackBuf[i] != buf[i]) {printf("Verify failed at addr=0x%06lX: wrote=0x%02X, read=0x%02X\n",addr + i, buf[i], readBackBuf[i]);verifySuccess = 0;break;}}if (!verifySuccess) {charDataBufferIndex = 0;memset(&crossPageChar, 0, sizeof(crossPageChar));return 0; };uint32_t offset = 0;while (offset < pageRemain) {uint32_t currentPos = totalWritten + offset;uint8_t *readData = NULL;uint32_t headerSize = 0;switch (type) {case 0: singleRowLength = 12;headerSize = 16 + singleRowLength * (95 - 1);break;case 1: singleRowLength = 16;headerSize = 16 + singleRowLength * (95 - 1);break;case 2: singleRowLength = 48;headerSize = 16 + singleRowLength * (95 - 1);break;case 3: singleRowLength = 64;headerSize = 16 + singleRowLength * (95 - 1);break;case 4: singleRowLength = 3 + 24;headerSize = 16 + singleRowLength * (2 - 1);break;case 5: singleRowLength = 3 + 32;headerSize = 16 + singleRowLength * (20 - 1);break;case 6: singleRowLength = 3 + 72;headerSize = 16 + singleRowLength * (2 - 1);break;case 7: singleRowLength = 3 + 128;headerSize = 16 + singleRowLength * (2 - 1);break;}if (crossPageChar.expected > 0) {uint32_t needed = crossPageChar.expected - crossPageChar.stored;uint32_t toCopy = (needed < (pageRemain - offset)) ? needed : (pageRemain - offset);memcpy(crossPageChar.buffer + crossPageChar.stored, readBackBuf + offset, toCopy);crossPageChar.stored += toCopy;offset += toCopy;if (crossPageChar.stored == crossPageChar.expected) {if (charDataBufferIndex + crossPageChar.expected <= MAX_CHAR_SIZE) {memcpy(charDataBuffer + charDataBufferIndex, crossPageChar.buffer, crossPageChar.expected);charDataBufferIndex += crossPageChar.expected;lastCharSize = crossPageChar.expected;lastCharOffset = crossPageChar.startAddr;} else {printf("Error: Character buffer overflow!\n");}memset(&crossPageChar, 0, sizeof(crossPageChar));}continue;}if (currentPos >= headerSize) {uint32_t charOffset = (currentPos - headerSize) % singleRowLength;uint32_t charStartPos = currentPos - charOffset;if (currentPos == charStartPos) {if ((offset + singleRowLength) <= pageRemain) {if (charDataBufferIndex + singleRowLength <= MAX_CHAR_SIZE) {memcpy(charDataBuffer + charDataBufferIndex,readBackBuf + offset,singleRowLength);charDataBufferIndex += singleRowLength;lastCharSize = singleRowLength;lastCharOffset = addr + offset;} else {printf("Error: Character buffer overflow!\n");}offset += singleRowLength;} else {uint32_t firstPart = pageRemain - offset;memcpy(crossPageChar.buffer,readBackBuf + offset,firstPart);crossPageChar.stored = firstPart;crossPageChar.expected = singleRowLength;crossPageChar.startAddr = addr + offset;offset += firstPart;}continue;}}offset++;}addr += pageRemain;buf += pageRemain;len -= pageRemain;totalWritten += pageRemain;}if (charDataBufferIndex > 0) {const char *typeNames[] = {"ASCII_6X12", "ASCII_8X16", "ASCII_12X24", "ASCII_16X32","chinese_12x12", "chinese_16x16", "chinese_24x24", "chinese_32x32"};printf("\r\n---------------- Last Character Data (Type=%s) ----------------\r\n", typeNames[type]);printf("End row start address: 0x%06lX, Size: %lu bytes\r\n", lastCharOffset, lastCharSize);uint32_t lastCharStart = (charDataBufferIndex >= lastCharSize) ?(charDataBufferIndex - lastCharSize) : 0;for (uint32_t j = 0; j < lastCharSize; j++) {printf("[%lu]: 0x%02X ", j, charDataBuffer[lastCharStart + j]);if ((j + 1) % singleRowLength == 0)printf("\r\n");}printf("\r\n----------------------------------------------------------------\r\n");}charDataBufferIndex = 0;memset(&crossPageChar, 0, sizeof(crossPageChar));return verifySuccess;
}