MCPcopy Create free account
hub / github.com/LowPowerLab/SPIFlash / writeBytes

Method writeBytes

SPIFlash.cpp:234–255  ·  view source on GitHub ↗

write multiple bytes to flash memory (up to 64K) WARNING: you can only write to previously erased memory locations (see datasheet) use the block erase commands to first clear memory (write 0xFFs) This version handles both page alignment and data blocks larger than 256 bytes.

Source from the content-addressed store, hash-verified

232/// This version handles both page alignment and data blocks larger than 256 bytes.
233///
234void SPIFlash::writeBytes(uint32_t addr, const void* buf, uint16_t len) {
235 uint16_t n;
236 uint16_t maxBytes = 256-(addr%256); // force the first set of bytes to stay within the first page
237 uint16_t offset = 0;
238 while (len>0)
239 {
240 n = (len<=maxBytes) ? len : maxBytes;
241 command(SPIFLASH_BYTEPAGEPROGRAM, true); // Byte/Page Program
242 SPI.transfer(addr >> 16);
243 SPI.transfer(addr >> 8);
244 SPI.transfer(addr);
245
246 for (uint16_t i = 0; i < n; i++)
247 SPI.transfer(((uint8_t*) buf)[offset + i]);
248 unselect();
249
250 addr+=n; // adjust the addresses and remaining bytes by what we've just transferred.
251 offset +=n;
252 len -= n;
253 maxBytes = 256; // now we can do up to 256 bytes per loop
254 }
255}
256
257/// erase entire flash memory array
258/// may take several seconds depending on size, but is non blocking

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected