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

Method writeStr

src/SPIFlash.cpp:1030–1130  ·  view source on GitHub ↗

Writes a string to a specific location on a page Takes three arguments - 1. _addr --> Any address - from 0 to capacity 2. data --> One String to be written to a particular location on a page 3. errorCheck --> Turned on by default. Checks for writing errors WARNING: You can only write to previously erased memory locations (see datasheet). Use the eraseSector()/eraseBlock32K/eraseBlock64K commands t

Source from the content-addressed store, hash-verified

1028// WARNING: You can only write to previously erased memory locations (see datasheet).
1029// Use the eraseSector()/eraseBlock32K/eraseBlock64K commands to first clear memory (write 0xFFs)
1030bool SPIFlash::writeStr(uint32_t _addr, String &data, bool errorCheck) {
1031
1032 #ifdef RUNDIAGNOSTIC
1033 _spifuncruntime = micros();
1034 #endif
1035
1036 uint32_t _sz = (sizeof(char)*(data.length()+1));
1037
1038 char _outCharArray[_sz];
1039 data.toCharArray(_outCharArray, _sz);
1040
1041 if(_isChipPoweredDown() || !_addressCheck(_addr, sizeof(_sz)) || !_notPrevWritten(_addr, sizeof(_sz)+_sz) || !_notBusy() || !_writeEnable()) {
1042 return false;
1043 }
1044
1045 _beginSPI(PAGEPROG);
1046 for (uint8_t i = 0; i < sizeof(_sz); i++) {
1047 _nextByte(WRITE, _sz >> (8*i));
1048 }
1049 CHIP_DESELECT
1050 _endSPI();
1051
1052 if(!_addressCheck(_addr+sizeof(_sz), _sz) || !_notBusy() || !_writeEnable()) {
1053 return false;
1054 }
1055 uint16_t maxBytes = SPI_PAGESIZE-(_addr % SPI_PAGESIZE); // Force the first set of bytes to stay within the first page
1056
1057 if (_sz <= maxBytes) {
1058 CHIP_SELECT
1059 _nextByte(WRITE, PAGEPROG);
1060 _transferAddress();
1061 //_nextBuf(PAGEPROG, &_outCharArray[0], _sz);
1062 for (uint16_t i = 0; i < _sz; ++i) {
1063 _nextByte(WRITE, _outCharArray[i]);
1064 }
1065 CHIP_DESELECT
1066 }
1067 else {
1068 uint16_t length = _sz;
1069 uint16_t writeBufSz;
1070 uint16_t data_offset = 0;
1071
1072 do {
1073 writeBufSz = (length<=maxBytes) ? length : maxBytes;
1074
1075 CHIP_SELECT
1076 _nextByte(WRITE, PAGEPROG);
1077 _transferAddress();
1078 for (uint16_t i = 0; i < writeBufSz; ++i) {
1079 _nextByte(WRITE, _outCharArray[data_offset + i]);
1080 }
1081 CHIP_DESELECT
1082
1083 _currentAddress += writeBufSz;
1084 data_offset += writeBufSz;
1085 length -= writeBufSz;
1086 maxBytes = 256; // Now we can do up to 256 bytes per loop
1087

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected