Skip to content

Commit

Permalink
bug fix, improved work with large structures
Browse files Browse the repository at this point in the history
  • Loading branch information
x-radio committed Mar 16, 2024
2 parents 327b5d8 + f7f4858 commit 0bed9b1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void setup() {
}

void loop() {
ee.data.step++; //you can do it realy often -
ee.commit(); //this case about 340 times per sector!!!
ee.data.step++; //change data in RAM
ee.commit(); //fix it in flash memory
delay(1000);
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/Complex/Complex.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void setup() {
Serial.println();
//-------------------------
param.setPort(Serial); //where should it print
param.begin(1000, 2); //5 sectors 1000 -> 999
param.begin(1000, 2); //2 sectors 1000 -> 999
param.printMsg(); //print if begin() was ok or not

Serial.println(param.data.devID, HEX); //get ID from our paramPROM
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=EEBoom
version=1.0.0
version=1.1.0
author=Nikita Popov <x-radio@bk.ru>
maintainer=Nikita Popov <x-radio@bk.ru>
sentence=Simple and powerful EEPROM emulation for esp8266 and ESP32
sentence=Simple and powerful EEPROM emulation for ESP8266 and ESP32
paragraph=Typed data access and minimized flash memory wear - the best way to add emulated EEPROM to your project
category=Data Storage
url=https://github.com/x-radio/EEBoom
Expand Down
71 changes: 41 additions & 30 deletions src/eeboom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
//=============================================================================
#define DFLT_SECT_CNT 2
#define FLASH_LIFE_CYCLES 10000
#define CHUNK_SIZE (1<<6)
//=============================================================================
bool EEBoomBase::begin(uint16_t FirstSect, uint16_t SectCnt)
{
uint8_t temp;
uint8_t temp; //valid slot flag
uint16_t oldSect = 0;
uint16_t oldAddr = 0;
uint32_t fullAddr;
uint8_t firstSlot[slotSize]; //in total there will be 2 structures on the stack, they will store current or old data depending on the situation
uint8_t secndSlot[slotSize];
uint8_t *crntPntr = firstSlot; //will point to one structure or the other.
uint8_t *oldPntr = secndSlot;
uint8_t *tempPntr = NULL; //is needed to move main pointers from structure to structure
uint16_t *checkSumm;
uint32_t oldFullAddr = 0;
uint32_t offset; //fullAddr offset - step through whole slot
uint8_t chunk[CHUNK_SIZE];
uint16_t crntSumm = 0; //check summ accum
uint16_t size = 0; //size of crnt chunk

if(SectCnt == 0) return false;

Expand All @@ -37,34 +37,46 @@ bool EEBoomBase::begin(uint16_t FirstSect, uint16_t SectCnt)
fullAddr = eeBase-sectSize*i;
for(uint16_t x = 0; x < slotCnt; x++)
{
spi_flash_read(fullAddr, (uint32_t*)crntPntr, slotSize);
checkSumm = (uint16_t*)(crntPntr + dataSize + ds); //pointer to crc (in the slot right after the end of data and dummy)
if(*checkSumm == getCheckSumm((uint8_t*)crntPntr)) //in the case of a good copy
temp = 1; //leave a flag
else
{ //if the copy is broken
if(temp == 1) //but there have been some good ones
goto profit; //brake cycles
offset = crntSumm = 0;
while (offset < slotSize)
{
if(offset+CHUNK_SIZE < slotSize)
size = CHUNK_SIZE;
else
size = slotSize - offset; //if last chunk

spi_flash_read(fullAddr+offset, (uint32_t*)chunk, size);

if(offset+CHUNK_SIZE < slotSize)
calculateSumm(&crntSumm, chunk, size);
else
calculateSumm(&crntSumm, chunk, size-ds-2); //exept dummy and crc fields in last chunk

offset += CHUNK_SIZE;
}
fullAddr += slotSize;
if(crntSumm == *(uint16_t*)(chunk+size-2)) //if checksumm from last chunk is equal to calculated one
temp = 1; //in case of a good copy leave a flag
else
if(temp == 1) goto profit; //if the copy is broken but there have been some good ones brake cycles
else break; //if the copy is broken and there was no valid one then next page

oldFullAddr = fullAddr;
oldSect = crntSect; //if it has not left the loop after the first pass (zero address), then the page will not need to be changed
oldAddr = crntAddr;
fullAddr += slotSize;
crntAddr++;
tempPntr = oldPntr; //so change pointers between structures
oldPntr = crntPntr;
crntPntr = tempPntr;
oldSect = crntSect; //if it has not left the loop after the first pass (zero address), then the page will not need to be changed
}
crntSect++;
}
if(temp == 1 && sectCnt == 1) goto profit; //in case of single-page working
if(temp == 1) goto profit; //in case of single-page working
lastMsg = ZERO_INIT;
crntAddr = -1; //
crntAddr = -1;
crntSect = 0;
return true;
profit: //if the data is found, load it
crntAddr = oldAddr; //return the addresses to the last valid ones
crntSect = oldSect;
memcpy((void*)slot, (void*)oldPntr, slotSize); //load the finished slot
spi_flash_read(oldFullAddr, (uint32_t*)slot, slotSize); //load the finished slot
lastMsg = INIT_OK;
return true;
}
Expand Down Expand Up @@ -113,7 +125,8 @@ bool EEBoomBase::commit()
uint16_t oldAddr = crntAddr;
uint8_t flag = 0; //the page has changed and we have to erase the flash after recording, the next intit can stay on the old...

*crc = getCheckSumm(data); //calculate the checksum (in the inheritor structure)
*crc = 0;
calculateSumm(crc, data, dataSize);

if(lastMsg == NO_PARTITION) return false;

Expand Down Expand Up @@ -235,15 +248,13 @@ void EEBoomBase::printInfo(uint32_t CommitIntrvlMin)
stream->printf("Approximate flash life time: %d days\r\n", lt/24/60);
}
//----------------------------
uint16_t EEBoomBase::getCheckSumm(uint8_t* addr)
void EEBoomBase::calculateSumm(uint16_t *summ, uint8_t *chunk, uint16_t size)
{
uint16_t checkSumm = 0;
for(uint8_t i = 0; i < dataSize; i++)
for(uint16_t i = 0; i < size; i++)
{
checkSumm += *addr * 44111 + 1;
addr++;
*summ = (*summ + *chunk) * 44111 + 1;
chunk++;
}
return checkSumm;
}
//----------------------------
bool EEBoomBase::sectIsClr()
Expand Down
6 changes: 3 additions & 3 deletions src/eeboom.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class EEBoomBase
uint16_t crntSect;
uint16_t crntAddr;
uint8_t lastMsg = NO_MSG;
Stream *stream = nullptr;
Stream *stream = &Serial;

EEBoomBase(uint16_t SlotSize, const uint16_t DataSize, uint8_t Ds, uint8_t *Slot, uint8_t *Data, uint16_t *Crc):
slotSize(SlotSize), dataSize(DataSize), ds(Ds),
slot(Slot), data(Data), crc(Crc) {};

uint16_t getCheckSumm(uint8_t* addr);
void calculateSumm(uint16_t *summ, uint8_t *chunk, uint16_t size);
bool sectIsClr();

public:
Expand Down Expand Up @@ -79,7 +79,7 @@ class EEBoom : public EEBoomBase
Type& data = slot.dt;

static_assert(sizeof(Slot) % 4 == 0, "Caution, EEBoom slot is not a multiple of 4");
static_assert(sizeof(Slot) < sectSize-2, "Caution, EEBoom slot is too large");
static_assert(sizeof(Slot) <= sectSize, "Caution, EEBoom slot is too large");
};
//=============================================================================
class EETool
Expand Down

0 comments on commit 0bed9b1

Please sign in to comment.