MCPcopy Create free account
hub / github.com/Marzogh/SPIMemory / writeByteArray

Method writeByteArray

src/SPIFram.cpp:435–506  ·  view source on GitHub ↗

Writes an array of bytes starting from a specific location in a page. Takes four arguments - 1. _addr --> Any address - from 0 to capacity 2. data_buffer --> The pointer to the array of bytes be written to a particular location on a page 3. bufferSize --> Size of the array of bytes - in number of bytes 4. errorCheck --> Turned on by default. Checks for writing errors WARNING: You can only write to

Source from the content-addressed store, hash-verified

433// WARNING: You can only write to previously erased memory locations (see datasheet).
434// Use the eraseSector()/eraseBlock32K/eraseBlock64K commands to first clear memory (write 0xFFs)
435bool SPIFram::writeByteArray(uint32_t _addr, uint8_t *data_buffer, size_t bufferSize, bool errorCheck) {
436 #ifdef RUNDIAGNOSTIC
437 _spifuncruntime = micros();
438 #endif
439 if(_isChipPoweredDown() || !_addressCheck(_addr, bufferSize) || !_notPrevWritten(_addr, bufferSize) || !_writeEnable()) {
440 return false;
441 }
442 uint16_t maxBytes = SPI_PAGESIZE-(_addr % SPI_PAGESIZE); // Force the first set of bytes to stay within the first page
443
444 if (bufferSize <= maxBytes) {
445 CHIP_SELECT
446 _nextByte(WRITE, PAGEPROG);
447 _transferAddress();
448 //_nextBuf(PAGEPROG, &data_buffer[0], bufferSize);
449 for (uint16_t i = 0; i < bufferSize; ++i) {
450 _nextByte(WRITE, data_buffer[i]);
451 }
452 CHIP_DESELECT
453 }
454 else {
455 uint16_t length = bufferSize;
456 uint16_t writeBufSz;
457 uint16_t data_offset = 0;
458
459 do {
460 writeBufSz = (length<=maxBytes) ? length : maxBytes;
461
462 CHIP_SELECT
463 _nextByte(WRITE, PAGEPROG);
464 _transferAddress();
465 //_nextBuf(PAGEPROG, &data_buffer[data_offset], writeBufSz);
466 for (uint16_t i = 0; i < writeBufSz; ++i) {
467 _nextByte(WRITE, data_buffer[data_offset + i]);
468 }
469 CHIP_DESELECT
470
471 _currentAddress += writeBufSz;
472 data_offset += writeBufSz;
473 length -= writeBufSz;
474 maxBytes = 256; // Now we can do up to 256 bytes per loop
475
476 if(!_writeEnable()){
477 return false;
478 }
479
480 } while (length > 0);
481 }
482
483 if (!errorCheck) {
484 _endSPI();
485 #ifdef RUNDIAGNOSTIC
486 _spifuncruntime = micros() - _spifuncruntime;
487 #endif
488 return true;
489 }
490 else {
491 _currentAddress = _addr;
492 CHIP_SELECT

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected