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

Method writeCharArray

src/SPIFlash.cpp:652–725  ·  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 chars be written to a particular location on a page 3. bufferSize --> Size of the array of chars - 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

650// WARNING: You can only write to previously erased memory locations (see datasheet).
651// Use the eraseSector()/eraseBlock32K/eraseBlock64K commands to first clear memory (write 0xFFs)
652bool SPIFlash::writeCharArray(uint32_t _addr, char *data_buffer, size_t bufferSize, bool errorCheck) {
653 #ifdef RUNDIAGNOSTIC
654 _spifuncruntime = micros();
655 #endif
656 if (!_prep(PAGEPROG, _addr, bufferSize)) {
657 return false;
658 }
659 uint16_t maxBytes = SPI_PAGESIZE-(_addr % SPI_PAGESIZE); // Force the first set of bytes to stay within the first page
660
661 if (bufferSize <= maxBytes) {
662 CHIP_SELECT
663 _nextByte(WRITE, PAGEPROG);
664 _transferAddress();
665 //_nextBuf(PAGEPROG, &data_buffer[0], bufferSize);
666 for (uint16_t i = 0; i < bufferSize; ++i) {
667 _nextByte(WRITE, data_buffer[i]);
668 }
669 CHIP_DESELECT
670 }
671 else {
672 uint16_t length = bufferSize;
673 uint16_t writeBufSz;
674 uint16_t data_offset = 0;
675
676 do {
677 writeBufSz = (length<=maxBytes) ? length : maxBytes;
678
679 CHIP_SELECT
680 _nextByte(WRITE, PAGEPROG);
681 _transferAddress();
682 for (uint16_t i = 0; i < writeBufSz; ++i) {
683 _nextByte(WRITE, data_buffer[data_offset + i]);
684 }
685 CHIP_DESELECT
686
687 _currentAddress += writeBufSz;
688 data_offset += writeBufSz;
689 length -= writeBufSz;
690 maxBytes = 256; // Now we can do up to 256 bytes per loop
691
692 if(!_notBusy() || !_writeEnable()){
693 return false;
694 }
695
696 } while (length > 0);
697 }
698
699 if (!errorCheck) {
700 _endSPI();
701 #ifdef RUNDIAGNOSTIC
702 _spifuncruntime = micros() - _spifuncruntime;
703 #endif
704 return true;
705 }
706 else {
707 if (!_notBusy()) {
708 return false;
709 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected