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

Method writeCharArray

src/SPIFram.cpp:516–586  ·  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

514// WARNING: You can only write to previously erased memory locations (see datasheet).
515// Use the eraseSector()/eraseBlock32K/eraseBlock64K commands to first clear memory (write 0xFFs)
516bool SPIFram::writeCharArray(uint32_t _addr, char *data_buffer, size_t bufferSize, bool errorCheck) {
517 #ifdef RUNDIAGNOSTIC
518 _spifuncruntime = micros();
519 #endif
520 if(_isChipPoweredDown() || !_addressCheck(_addr, bufferSize) || !_notPrevWritten(_addr, bufferSize) || !_writeEnable()) {
521 return false;
522 }
523 uint16_t maxBytes = SPI_PAGESIZE-(_addr % SPI_PAGESIZE); // Force the first set of bytes to stay within the first page
524
525 if (bufferSize <= maxBytes) {
526 CHIP_SELECT
527 _nextByte(WRITE, PAGEPROG);
528 _transferAddress();
529 //_nextBuf(PAGEPROG, &data_buffer[0], bufferSize);
530 for (uint16_t i = 0; i < bufferSize; ++i) {
531 _nextByte(WRITE, data_buffer[i]);
532 }
533 CHIP_DESELECT
534 }
535 else {
536 uint16_t length = bufferSize;
537 uint16_t writeBufSz;
538 uint16_t data_offset = 0;
539
540 do {
541 writeBufSz = (length<=maxBytes) ? length : maxBytes;
542
543 CHIP_SELECT
544 _nextByte(WRITE, PAGEPROG);
545 _transferAddress();
546 for (uint16_t i = 0; i < writeBufSz; ++i) {
547 _nextByte(WRITE, data_buffer[data_offset + i]);
548 }
549 CHIP_DESELECT
550
551 _currentAddress += writeBufSz;
552 data_offset += writeBufSz;
553 length -= writeBufSz;
554 maxBytes = 256; // Now we can do up to 256 bytes per loop
555
556 if(!_writeEnable()){
557 return false;
558 }
559
560 } while (length > 0);
561 }
562
563 if (!errorCheck) {
564 _endSPI();
565 #ifdef RUNDIAGNOSTIC
566 _spifuncruntime = micros() - _spifuncruntime;
567 #endif
568 return true;
569 }
570 else {
571 _currentAddress = _addr;
572 CHIP_SELECT
573 _nextByte(WRITE, READDATA);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected