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

Method writeByteArray

src/SPIFlash.cpp:568–642  ·  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

566// WARNING: You can only write to previously erased memory locations (see datasheet).
567// Use the eraseSector()/eraseBlock32K/eraseBlock64K commands to first clear memory (write 0xFFs)
568bool SPIFlash::writeByteArray(uint32_t _addr, uint8_t *data_buffer, size_t bufferSize, bool errorCheck) {
569 #ifdef RUNDIAGNOSTIC
570 _spifuncruntime = micros();
571 #endif
572 if (!_prep(PAGEPROG, _addr, bufferSize)) {
573 return false;
574 }
575 uint16_t maxBytes = SPI_PAGESIZE-(_addr % SPI_PAGESIZE); // Force the first set of bytes to stay within the first page
576
577 if (bufferSize <= maxBytes) {
578 CHIP_SELECT
579 _nextByte(WRITE, PAGEPROG);
580 _transferAddress();
581 //_nextBuf(PAGEPROG, &data_buffer[0], bufferSize);
582 for (uint16_t i = 0; i < bufferSize; ++i) {
583 _nextByte(WRITE, data_buffer[i]);
584 }
585 CHIP_DESELECT
586 }
587 else {
588 uint16_t length = bufferSize;
589 uint16_t writeBufSz;
590 uint16_t data_offset = 0;
591
592 do {
593 writeBufSz = (length<=maxBytes) ? length : maxBytes;
594
595 CHIP_SELECT
596 _nextByte(WRITE, PAGEPROG);
597 _transferAddress();
598 //_nextBuf(PAGEPROG, &data_buffer[data_offset], writeBufSz);
599 for (uint16_t i = 0; i < writeBufSz; ++i) {
600 _nextByte(WRITE, data_buffer[data_offset + i]);
601 }
602 CHIP_DESELECT
603
604 _currentAddress += writeBufSz;
605 data_offset += writeBufSz;
606 length -= writeBufSz;
607 maxBytes = 256; // Now we can do up to 256 bytes per loop
608
609 if(!_notBusy() || !_writeEnable()){
610 return false;
611 }
612
613 } while (length > 0);
614 }
615
616 if (!errorCheck) {
617 _endSPI();
618 #ifdef RUNDIAGNOSTIC
619 _spifuncruntime = micros() - _spifuncruntime;
620 #endif
621 return true;
622 }
623 else {
624 if (!_notBusy()) {
625 return false;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected