Help identifying problem using Lib EBD + CORE 3.1.0 · Issue #8831 · esp8266/Arduino (original) (raw)
The code example below has always worked great on previous cores. I'm having trouble identifying the problem using ESP8266 CORE 3.1.0. The code is working normally when the DB file is created, but when reusing the file an exeption occurs. I think it might be something related to Append or Write x SEEK of the file using SD CARD.
#include <SPI.h>
#include <SD.h>
#define TABLE_SIZE 8192
#define RECORDS_TO_CREATE 100
String db_name = "/TESTE.db";
File dbFile;
struct LogEvent {
int id;
int temperature;
}
logEvent;
inline void writer (unsigned long address, const byte* data, unsigned int recsize) {
dbFile.seek(address);
dbFile.write(data,recsize);
dbFile.flush();
}
inline void reader (unsigned long address, byte* data, unsigned int recsize) {
dbFile.seek(address);
dbFile.read(data,recsize);
}
// Create an EDB object with the appropriate write and read handlers
EDB db(&writer, &reader);
// Run the demo
void setup()
{
Serial.begin(115200);
Serial.println(" Extended Database Library + External SD CARD storage demo");
Serial.println();
if (!SD.begin(4)) {
Serial.println("No SD-card.");
return;
}
if (SD.exists(db_name.c_str())) {
dbFile = SD.open(db_name.c_str(), FILE_WRITE);
if (!dbFile) {
dbFile = SD.open(db_name.c_str(), FILE_WRITE);
}
if (dbFile) {
Serial.print("Openning current table... ");
EDB_Status result = db.open(0);
if (result == EDB_OK) {
Serial.println("DONE");
} else {
Serial.println("ERROR");
Serial.println("Did not find database in the file " + String(db_name));
Serial.print("Creating new table... ");
db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
Serial.println("DONE");
return;
}
} else {
Serial.println("Could not open file " + String(db_name));
return;
}
} else {
Serial.print("Creating table... ");
// create table at with starting address 0
dbFile = SD.open(db_name.c_str(), FILE_WRITE);
db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
Serial.println("DONE");
}
//recordLimit();
//countRecords();
createRecords(RECORDS_TO_CREATE);
//countRecords();
selectAll();
deleteOneRecord(RECORDS_TO_CREATE / 2);
//countRecords();
selectAll();
appendOneRecord(RECORDS_TO_CREATE + 1);
//countRecords();
selectAll();
insertOneRecord(RECORDS_TO_CREATE / 2);
//countRecords();
selectAll();
updateOneRecord(RECORDS_TO_CREATE);
selectAll();
//countRecords();
deleteAll();
Serial.println("Use insertRec() and deleteRec() carefully, they can be slow");
//countRecords();
for (int i = 1; i <= 20; i++) insertOneRecord(1); // inserting from the beginning gets slower and slower
//countRecords();
for (int i = 1; i <= 20; i++) deleteOneRecord(1); // deleting records from the beginning is slower than from the end
//countRecords();
dbFile.close();
}
void loop()
{
}
// utility functions
void recordLimit()
{
Serial.print("Record Limit: ");
Serial.println(db.limit());
}
void deleteOneRecord(int recno)
{
Serial.print("Deleting recno: ");
Serial.println(recno);
db.deleteRec(recno);
}
void deleteAll()
{
Serial.print("Truncating table... ");
db.clear();
Serial.println("DONE");
}
void countRecords()
{
Serial.print("Record Count: ");
Serial.println(db.count());
}
void createRecords(int num_recs)
{
Serial.print("Creating Records... ");
for (int recno = 1; recno <= num_recs; recno++)
{
logEvent.id = recno;
logEvent.temperature = random(1, 125);
EDB_Status result = db.appendRec(EDB_REC logEvent);
if (result != EDB_OK) printError(result);
}
Serial.println("DONE");
}
void selectAll()
{
for (int recno = 1; recno <= db.count(); recno++)
{
EDB_Status result = db.readRec(recno, EDB_REC logEvent);
if (result == EDB_OK)
{
Serial.print("Recno: ");
Serial.print(recno);
Serial.print(" ID: ");
Serial.print(logEvent.id);
Serial.print(" Temp: ");
Serial.println(logEvent.temperature);
}
else printError(result);
}
}
void updateOneRecord(int recno)
{
Serial.print("Updating record at recno: ");
Serial.print(recno);
Serial.print("... ");
logEvent.id = 1234;
logEvent.temperature = 4321;
EDB_Status result = db.updateRec(recno, EDB_REC logEvent);
if (result != EDB_OK) printError(result);
Serial.println("DONE");
}
void insertOneRecord(int recno)
{
Serial.print("Inserting record at recno: ");
Serial.print(recno);
Serial.print("... ");
logEvent.id = recno;
logEvent.temperature = random(1, 125);
EDB_Status result = db.insertRec(recno, EDB_REC logEvent);
if (result != EDB_OK) printError(result);
Serial.println("DONE");
}
void appendOneRecord(int id)
{
Serial.print("Appending record... ");
logEvent.id = id;
logEvent.temperature = random(1, 125);
EDB_Status result = db.appendRec(EDB_REC logEvent);
if (result != EDB_OK) printError(result);
Serial.println("DONE");
}
void printError(EDB_Status err)
{
Serial.print("ERROR: ");
switch (err)
{
case EDB_OUT_OF_RANGE:
Serial.println("Recno out of range");
break;
case EDB_TABLE_FULL:
Serial.println("Table full");
break;
case EDB_OK:
default:
Serial.println("OK");
break;
}
}
13:36:36.196 -> Extended Database Library + External SD CARD storage demo
13:36:36.196 ->
13:36:36.196 -> Openning current table... DONE
13:36:36.196 -> Creating Records...
13:36:36.196 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
13:36:36.196 ->
13:36:36.196 -> Exception (0):
13:36:36.196 -> epc1=0x4000e25d epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
13:36:36.196 ->
13:36:36.196 -> >>>stack>>>
13:36:36.243 ->
13:36:36.243 -> ctx: cont
13:36:36.243 -> sp: 3ffffd60 end: 3fffffd0 offset: 0190
13:36:36.243 -> 3ffffef0: 00000000 00002710 fffffffc 3ffe890b
13:36:36.243 -> 3fffff00: 4020652c 3ffe88f7 00000001 40206a9a
13:36:36.243 -> 3fffff10: 3ffeeb90 3ffeeb08 00000001 4020139b
13:36:36.243 -> 3fffff20: 3ffeeb10 00000004 3ffeeb90 3ffeeae0
13:36:36.243 -> 3fffff30: 3ffeeb10 3ffeeb90 3ffeeb3c 402016ab
13:36:36.243 -> 3fffff40: feefeffe feefeffe feefeffe feefeffe
13:36:36.243 -> 3fffff50: feefeffe feefeffe 4020a548 00000000
13:36:36.243 -> 3fffff60: 000003e8 3ffefbd0 00000000 00000000
13:36:36.243 -> 3fffff70: 00000000 402026ec 00000000 00000000
13:36:36.291 -> 3fffff80: 3ffeeb58 feefeffe feefeffe feefeffe
13:36:36.291 -> 3fffff90: feefeffe feefeffe feefeffe cff2d90d
13:36:36.291 -> 3fffffa0: feefeffe feefeffe feefeffe 3ffeebfc
13:36:36.291 -> 3fffffb0: 3fffdad0 00000000 3ffeebe8 402071b0
13:36:36.291 -> 3fffffc0: feefeffe feefeffe 3fffdab0 4010103d
13:36:36.291 -> <<<stack<<<
13:36:36.291 ->