void* pointer arithmetic bug

This commit is contained in:
LowPowerLab 2015-02-08 22:13:47 -05:00
parent 924c265cef
commit 46b3d40a0c
1 changed files with 6 additions and 5 deletions

View File

@ -193,22 +193,23 @@ void SPIFlash::writeByte(long addr, uint8_t byt) {
/// This version handles both page alignment and data blocks larger than 256 bytes. /// This version handles both page alignment and data blocks larger than 256 bytes.
/// ///
void SPIFlash::writeBytes(long addr, const void* buf, uint16_t len) { void SPIFlash::writeBytes(long addr, const void* buf, uint16_t len) {
int n; uint16_t n;
int maxBytes = 256-(addr%256); // force the first set of bytes to stay within the first page uint16_t maxBytes = 256-(addr%256); // force the first set of bytes to stay within the first page
uint16_t offset = 0;
while (len>0) while (len>0)
{ {
n = (len<=maxBytes) ? len : maxBytes; n = (len<=maxBytes) ? len : maxBytes;
command(SPIFLASH_BYTEPAGEPROGRAM, true); // Byte/Page Program command(SPIFLASH_BYTEPAGEPROGRAM, true); // Byte/Page Program
SPI.transfer(addr >> 16); SPI.transfer(addr >> 16);
SPI.transfer(addr >> 8); SPI.transfer(addr >> 8);
SPI.transfer(addr); SPI.transfer(addr);
for (uint16_t i = 0; i < n; i++) for (uint16_t i = 0; i < n; i++)
SPI.transfer(((byte*) buf)[i]); SPI.transfer(((byte*) buf)[offset + i]);
unselect(); unselect();
addr+=n; // adjust the addresses and remaining bytes by what we've just transferred. addr+=n; // adjust the addresses and remaining bytes by what we've just transferred.
buf +=n; offset +=n;
len -= n; len -= n;
maxBytes = 256; // now we can do up to 256 bytes per loop maxBytes = 256; // now we can do up to 256 bytes per loop
} }