Hi All
I need some help / guidance with implementing TDBStore on my STM32. Building a data logger and for a test I’m storing a small packet about every 6 sec to my SB card.
My main problem is that every time I call int32_t result = tdbStoreObj.init();
it takes about 3.5sec to execute. I read somewhere that i should init and deinit each time I access the Store. But at 3.5sec per save it is just not possible.Can I just tdbStoreObj.init();
during my setup?
What are my other options.
My next question will be with 512b block size what is the best way to deal with this
Here is a short version of my code.
/*
MyStore.cpp
Ver: 1.0.0.1
Date: 2 APR 2024
*/
#include <SDMMCBlockDevice.h>
#include <TDBStore.h>
#include "MyStore.h"
char tdbsKeyChar; // This will hold the integer version of 'tdbsKey as an Char, i know its fucked!!!!!
uint32_t tdbsKey; // TDBStore key / Packet Counter
SDMMCBlockDevice sdBlockDevice; // Create an SD block device
TDBStore tdbStoreObj(&sdBlockDevice); // Create a key/value store on the SD
int tdbStoreSave(char* key, dlSdPacket* packet); // Store a SketchStats to the the k/v store
int initBlockDevice();
int sdSetUp();
int dlSavePacket(dlSdPacket* dataPacket();
int sdSetUp() {
int result;
TDBStore* tdbStoreObj = new TDBStore(&sdBlockDevice);
result = initBlockDevice();
if (result == 0) {
//tdbsKeyIterator(tdbStoreObj, "tdbStore");
//tdbsReset(tdbStoreObj, "tdbStore");
}
return result;
}
/*! Setup Save Packet to TDBStore
@param dataPacket [in] Packet pointer
@return [out] in 0 = Good, or Error Codes
*/
int dlSavePacket(dlSdPacket* dataPacket) {
Serial.println("3 fsave, " + String((unsigned int)millis()));
char tdbsKeyChar[32] = {};
sprintf(tdbsKeyChar, "%d", dataPacket->sdKey);
//Open TDBStore break if an Error
int32_t result = tdbStoreObj.init();
if (result != MBED_SUCCESS) {
ReportError(ERT_STORE, ERY_ERROR, "dlSavePacket", result, "Failed to 'tdbStoreObj.init()'", TRUE);
return result;
};
Serial.println("4 INIT, " + String((unsigned int)millis()));
result = tdbStoreSave(tdbsKeyChar, &pakArray[pakIndex]);
if (MBED_SUCCESS == result) {
//Serial.println("\n\tTDBStore->set SUCCESS " );
//tdbsGetStoreSize(tdbsKeyChar);
Serial.println("5 STORE, " + String((unsigned int)millis()));
}
else {
while (true);
ReportError(ERT_STORE, ERY_ERROR, "dlSavePacket", result, "Failed Save to tdbStoreSave", TRUE);
return result;
};
result = tdbStoreObj.deinit();
if (result != MBED_SUCCESS) {
ReportError(ERT_STORE, ERY_ERROR, "dlSavePacket", result, "Failed to 'tdbStoreObj.deinit()'", TRUE);
};
Serial.println("6 fSave exit, " + String((unsigned int)millis()));
return result;
}
/*! Save Packet to TDBStore
@param tdbsKey [in] char Key for tdbStore Key
@param pakStoreSave [pakIndex] [in] packet Packet to be saved
@return [out] in 0 = Good, or Error Codes
*/
int tdbStoreSave(char* key, dlSdPacket* pakStoreSave) {
auto result = tdbStoreObj.set(key, reinterpret_cast<uint8_t*>(&pakStoreSave), sizeof(dlSdPacket), 0);
return result;
}
// Intialise the Blocke device and get settings
int initBlockDevice() {
if (0 != sdBlockDevice.init()) { // Int the SD card
Serial.println("SD Card 'init' failure");
return -1;
};
delay(1000); // Wait for terminal to come up
Serial.println("");
Serial.println("SD Card + TDBStore Test");
srand(micros()); // Feed the RNG for later content generation
Serial.println("Block device size: " + String((unsigned int)sdBlockDevice.size() / 1024.0 / 1024.0) + " MB");
Serial.println("Readable block size: " + String((unsigned int)sdBlockDevice.get_read_size()) + " bytes");
Serial.println("Programmable block size: " + String((unsigned int)sdBlockDevice.get_program_size()) + " bytes");
Serial.println("Erasable block size: " + String((unsigned int)sdBlockDevice.get_erase_size()) + " KB");
if (0 != sdBlockDevice.deinit()) { // Int the SD card
Serial.println("SD Card 'de-init' failure");
return -1;
};
return 0;
}
Edit by sivar2311: changed to pre-formatted text for bettter readability