Hi Everyone
Project using prop shield + teensy 3.2 + SerialFlash Library
I'm trying to write a program that logs data to Flash. I need it to run for ~ 2 hours, I am planning to adjust my sampling rate to max out the available storage.
I had planned to write each data-frame to a file and then sequentially step through the files to read them out. I finally got it working, but I seem to use the SerialFlash library in a way that its not intended to be used.
Using my one file per data-frame approach I am unable to create a file after I have stored 600 frames.
I am not sure if this is because there is a problem with my code, or because I have wrong assumptions on how the library works or because there is a problem with the library. Could someone please check this for me?
EDIT: I've confirmed that the file-size is not an issue. I have tested 4 byte files and I have tested 256 byte files, and 600 files is still the maximum.
Here is my code for logging files, as you see the files are numbered based on the counter variable. Once my file-name hits 599, the next file I try to create fails ("File creation failed!!!")
Any suggestions?
This is probably irrelevant, but for reference and to understand how I am thinking about this, here is the code I use for reading:
Project using prop shield + teensy 3.2 + SerialFlash Library
I'm trying to write a program that logs data to Flash. I need it to run for ~ 2 hours, I am planning to adjust my sampling rate to max out the available storage.
I had planned to write each data-frame to a file and then sequentially step through the files to read them out. I finally got it working, but I seem to use the SerialFlash library in a way that its not intended to be used.
Using my one file per data-frame approach I am unable to create a file after I have stored 600 frames.
I am not sure if this is because there is a problem with my code, or because I have wrong assumptions on how the library works or because there is a problem with the library. Could someone please check this for me?
EDIT: I've confirmed that the file-size is not an issue. I have tested 4 byte files and I have tested 256 byte files, and 600 files is still the maximum.
Here is my code for logging files, as you see the files are numbered based on the counter variable. Once my file-name hits 599, the next file I try to create fails ("File creation failed!!!")
Any suggestions?
Code:
///////////////////////////
// Logs data to flash. Goes with readFiles.ino
/////////////////
#include <SerialFlash.h>
#include <SPI.h>
const int FlashChipSelect = 6; // digital pin for flash chip CS pin
//for IMU ////////
#include <NXPMotionSense.h>
#include <Wire.h>
#include <EEPROM.h>
#include <util/crc16.h>
NXPMotionSense imu;
//////////////////
int counter = 0;
SerialFlashFile file; //Working file
String dataAsString;
void setup()
{
Serial.begin(9600); //open serial port
while (!Serial); //wait until the serial port is open
delay(100);
Serial.println("Checking if it finds the Flash Chip...");
if (!SerialFlash.begin(FlashChipSelect)) {
Serial.println("");
Serial.println("Unable to access SPI Flash chip");
}
Serial.println("");
Serial.println("SPI Flash found");
uint8_t id[5];
SerialFlash.readID(id);
Serial.println("ID:");
Serial.print("0x");
Serial.print(id[0], HEX);
Serial.print(", 0x");
Serial.print(id[1], HEX);
Serial.print(", 0x");
Serial.print(id[2], HEX);
Serial.print(", 0x");
Serial.println(id[3], HEX);
Serial.println("... so far, so good");
Serial.println("");
imu.begin();
}
void loop() {
//IMU data
int ax, ay, az;
int gx, gy, gz;
int mx, my, mz;
// get and print uncalibrated data
if (imu.available()) {
imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
dataAsString = "" + String(ax) + "!!";
// dataAsString = "Acc: " + String(ax) + ", " + String(ay) + ", " + String(az) + "!!";
Serial.print("Testing the String: ");
Serial.println(dataAsString);
String currentTime = String(counter);
createFile(dataAsString, currentTime); //This creates a test file IF it doesn't exist
counter++;
}
}
////////////////////////////////////////////////////////////////////////////
//This function creates a file and writes the array specified above to it //
////////////////////////////////////////////////////////////////////////////
void createFile( String dataString, String millisForFilename ) {
//create the name (this should be cleaned up)
String fileNameAsString = millisForFilename + ".dat" ;
int nameLength = fileNameAsString.length() + 1;
char filename[nameLength];
fileNameAsString.toCharArray(filename, nameLength);
String dataAsString = dataString;
int fileSize = dataAsString.length();
char dataToWrite [fileSize + 1];
dataAsString.toCharArray(dataToWrite, fileSize + 1);
Serial.print("String length: ");
Serial.println(fileSize);
Serial.print("Char array length: ");
Serial.println(sizeof(dataToWrite));
Serial.println("");
//Check if file exists, if not, create it.
if (SerialFlash.exists(filename) == 0)
{
//File doesn't exist
if (SerialFlash.create(filename, fileSize) == true)
{
Serial.println(filename);
}
else
{
Serial.println("File creation failed!!!");
}
}
if (SerialFlash.exists(filename ))
{
Serial.println("File Exists, trying to open...");
file = SerialFlash.open(filename );
if (file)
{
// true if the file exists
// Serial.println(filename + ".dat opened");
uint8_t buffer[fileSize];
for (int i = 0; i < fileSize; i++) {
buffer[i] = dataToWrite[i];
}
Serial.print("Writing Data to File ...");
//IF YOU WANT TO WRITE TO ANOTHER POSITION IN THE FILE, USE "file.seek();"
//IF YOU WANT TO CHECK YOUR CURRENT POSITION, USE "file.position()"
file.write(buffer, fileSize);
Serial.println("... completed");
Serial.print("Position after writing: ");
Serial.println(file.position());
}
else
{
// Serial.println(filename + ".dat not opened!!!");
}
}
}
This is probably irrelevant, but for reference and to understand how I am thinking about this, here is the code I use for reading:
Code:
//////////////////////////////
//This sketch sequentially opens every file in flash memory and prints its contents to the serial port.
//////////////////////////////
#include <SerialFlash.h>
#include <SPI.h>
SerialFlashFile file; //Working file
const int FlashChipSelect = 6; // digital pin for flash chip CS pin
//const int FlashChipSelect = 21; // Arduino 101 built-in SPI Flash
void setup() {
Serial.begin(9600);
// wait for Arduino Serial Monitor (Just so we don't miss the display)
while (!Serial) ;
delay(100);
Serial.println("All Data from all Files on SPI Flash chip:");
Serial.println("");
if (!SerialFlash.begin(FlashChipSelect)) {
Serial.print("Unable to access SPI Flash chip");
delay(2500);
}
SerialFlash.opendir();
while (true) { //this lists by order of creation all files
char filename[64]; //filename
uint32_t filesize; //filesize (we will use this to read from them.
if (SerialFlash.readdir(filename, sizeof(filename), filesize)) { //if it found a file
file = SerialFlash.open(filename); //open it
char buffer2[filesize]; //create a buffer the size of the file
file.read(buffer2, filesize); //copy the file to the buffer
Serial.print(filename);
Serial.print(": ");
for (int i = 0; i < filesize; i++) {
Serial.print(buffer2[i]); //print the content of the buffer to the serial port
}
Serial.println(""); //new line at end of file
file.close(); //close file
} else { //if no more files are found
Serial.println("");
Serial.println("no more Data");
break; // no more files
}
}
}
void loop() {
}
Last edited: