I'm not sure how to accomplish what I want. I'm logging data, and I've started by allocating one large file with SerialFlash.
I keep track in another file where the data starts in the large file, and I want to keep track of the end of the file on an on-going basis as each block is written. It works if I write the ending block out to the tracking file when stopping the device is triggered (cause I only have to do it once). But I want to keep track of the end of the file as it is being written in case the device gets turned off, so that I still know where the file ended.
The other thing that is confusing me, is that I'm initializing a buffer and writing that buffer to the tracking file initially. But after it's initialized to a value, I seem to be able to change it one more time, but that's it. Maybe you can rewrite a whole block but not a partial block? If I instead rewrote a whole bloc, that's still a problem, cause if the device gets turned off I don't want to be in a partial write for the tracking file.
Maybe I'm missing something?
I can initialize the large file to "-1", so I can tell by reading up until I encounter "-1". But I also want to allow multiple recordings within the same large file, and having the tracking file would allow me to output each separate recording after-the-fact.
I'm hoping I'm missing something simple (often the case) that someone could point me at. Thanks for any input.
Here's some code demonstrating:
https://github.com/PaulStoffregen/SerialFlash
I keep track in another file where the data starts in the large file, and I want to keep track of the end of the file on an on-going basis as each block is written. It works if I write the ending block out to the tracking file when stopping the device is triggered (cause I only have to do it once). But I want to keep track of the end of the file as it is being written in case the device gets turned off, so that I still know where the file ended.
The other thing that is confusing me, is that I'm initializing a buffer and writing that buffer to the tracking file initially. But after it's initialized to a value, I seem to be able to change it one more time, but that's it. Maybe you can rewrite a whole block but not a partial block? If I instead rewrote a whole bloc, that's still a problem, cause if the device gets turned off I don't want to be in a partial write for the tracking file.
Maybe I'm missing something?
I can initialize the large file to "-1", so I can tell by reading up until I encounter "-1". But I also want to allow multiple recordings within the same large file, and having the tracking file would allow me to output each separate recording after-the-fact.
I'm hoping I'm missing something simple (often the case) that someone could point me at. Thanks for any input.
Here's some code demonstrating:
Code:
#include <SerialFlash.h>
#include <SPI.h>
const int FlashChipSelect = 6;
SerialFlashFile dataFile;
int blockSize=256;
const int samplesInBlock = 64;
int32_t startByte = -1, endByte = -1, endMarkerPos = -1;
int bytesInSample = 4;
char dataFilename[12]="test.bin";
int counter=0;
int32_t intWriteBuffer[samplesInBlock];
/**
* Initialize integer buffer
*/
void initIntBuffer(int32_t * buff, int nSize, int initInt) {
for (int i=0;i<nSize;i++) {
buff[i]=initInt;
}
}
void setup() {
Serial.begin(38400);
while (!Serial);
Serial.print("initFlashFS filename: ");
Serial.println(dataFilename);
initializeSPIFlash();
if (SerialFlash.exists(dataFilename)) {
Serial.println("storage file exist");
} else {
Serial.println("storage file does NOT exist... creating");
SerialFlash.createErasable(dataFilename, blockSize);
initIntBuffer(intWriteBuffer,samplesInBlock,-1);
dataFile = SerialFlash.open(dataFilename);
dataFile.write(intWriteBuffer,blockSize);
//can close it instead for testing
// dataFile.close();
}
Serial.println("opening & leaving open");
for (int counter=0;counter<10;counter++) {
//if it's closed above, need to open it
// dataFile = SerialFlash.open(dataFilename);
Serial.print("pos: ");
Serial.println(dataFile.position());
Serial.print("counter: ");
Serial.println(counter);
dataFile.seek(0);
//this works because it writes to a new spot
// dataFile.seek(counter*bytesInSample);
intWriteBuffer[0]=counter;
dataFile.write(intWriteBuffer,bytesInSample);
dataFile.close();
delay(100);
}
// dataFile.close();
sendIntFile(dataFilename,blockSize);
Serial.println("Done");
}
void loop() {
delay(1000);
}
/**
* Send file full of ints with xml-like wrapper
*/
void sendIntFile(char * sendFilename, uint32_t nBytes) {
SerialFlashFile tmpFile;
Serial.print("sending...");
Serial.println(sendFilename);
Serial.print("sending...");
Serial.println(sendFilename);
if (SerialFlash.exists(sendFilename)) {
tmpFile = SerialFlash.open(sendFilename);
if (nBytes < 0) nBytes = tmpFile.size();
Serial.print("<file=");
Serial.print(sendFilename);
Serial.println(">");
while (tmpFile.position() < nBytes) {
Serial.println(tmpFile.position());
tmpFile.read(intWriteBuffer, blockSize);
Serial.println(tmpFile.position());
Serial.println(tmpFile.size());
for (int i=0;i<samplesInBlock;i++) {
Serial.print(intWriteBuffer[i]);
Serial.print(",");
}
Serial.println();
}
tmpFile.close();
Serial.println("</file>");
}
}
/**
* Basic check if SPI Flash is connected
*/
void initializeSPIFlash() {
Serial.println("in init spi flash");
Serial.flush();
delay(3000);
if (!SerialFlash.begin(FlashChipSelect)) {
Serial.println("Unable to access SPI Flash chip");
// error("Unable to access SPI Flash chip");
} else {
Serial.println("past spi flash init");
}
}
https://github.com/PaulStoffregen/SerialFlash
Last edited: