sumotoy
Well-known member
I never used Serial Flash chip but just get the 'Prop Shield' so I started experimenting with Serial Flash Chip and my Teensy 3.2.
I wroted the following really basic sketch to understand the file system but get weird results, maybe I've done something really stupid but try to understand what's wrong:
What it does?
1) get a list of files present on chip.
2) it check for a specific file existance, if not found it create it, if found it simple erase it.
3) open file, write some chars inside it, close file and get the number of char written
4) attempt to open file, read it and print results, then close it.
It's just for study file system so forgive me the massive printouts.
This chip seems write only 65K chunks, it's very silly use it for write some text but as I said it's just for study, maybe in the future it will be good enable 4k chunks for use as icon/font container.
If everithing goes at it should I have to read this sequence of chars: abcdefghijklmnopqrstuvwxyz, but this happen only at 72Mhz not optimized!
Here's the results:
- Compiled at 72Mhz not optimized: abcdefghijklmnopqrstuvwxyz (correct)
- Compiled at 72Mhz optimized: abcdefghijklmnopqrstuvwxyÿ (missed z and get ÿ)
- Compiled at 96Mhz not optimized: abcdefghijklmnopqrstuvwÿÿÿ (missed y,z and get 3 x ÿ)
- Compiled at 96Mhz Optimized: abcdefghijklmnopqrÿÿÿÿÿÿÿÿ (even worst...)
I'm using Teensyduino 2.8 beta.
Any help?
UPDATE:
Issue solved, was an error in library.
Look HERE
I wroted the following really basic sketch to understand the file system but get weird results, maybe I've done something really stupid but try to understand what's wrong:
Code:
/*
* An horrible sketch to play with SerialFlash on Teensy 3.2
* filesistem.
* Not to be used as state of art example, just a playground to
* discover the commands and methods of SerialFlash library on a Tensy3.2
* .s.u.m.o.t.o.y.
* Here's the results: ------------------------------
* Compiled at 72Mhz not optimized:
* abcdefghijklmnopqrstuvwxyz (correct)
* Compiled at 72Mhz optimized:
* abcdefghijklmnopqrstuvwxyÿ (missed z and get ÿ)
* Compiled at 96Mhz not optimized:
* abcdefghijklmnopqrstuvwÿÿÿ (missed y,z and get 3 x ÿ)
* Compiled at 96Mhz Optimized:
* abcdefghijklmnopqrÿÿÿÿÿÿÿÿ (even worst...)
*/
#include <SerialFlash.h>
#include <SPI.h>
bool serialChipOk = false;
const char * files[2] = {"file01.txt", "file02.txt"};
const unsigned long fileSize[2] = {65536, 65536};
SerialFlashFile file;
void setup() {
//uncomment these if using Teensy audio shield
//SPI.setSCK(14); // Audio shield has SCK on pin 14
//SPI.setMOSI(7); // Audio shield has MOSI on pin 7
Serial.begin(38400);
long unsigned debug_start = millis ();
while (!Serial && ((millis () - debug_start) <= 5000)) ;
if (!SerialFlash.begin()) {
Serial.println(F("Unable to access SPI Flash chip"));
} else {
serialChipOk = true;
Serial.println(F("Serial Flash Chip OK"));
unsigned int filesDir = listFiles();
if (filesDir > 0) {
Serial.print(F("Find "));
Serial.print(filesDir);
Serial.print(" files in directory\n");
} else {
Serial.print(F("NO Files present...\n"));
}
uint32_t buffLen;
if (checkMyFile(0)){//file exists
buffLen = testWriteA();//write some data
readMyFile(0,buffLen);
}
}//
}
void loop() {
// put your main code here, to run repeatedly:
}
//returns how many files are present and prints
//the file names....
unsigned int listFiles()
{
unsigned int count = 0;
SerialFlash.opendir();
while (1) {
char filename[64];
unsigned long filesize;
if (SerialFlash.readdir(filename, sizeof(filename), filesize)) {
Serial.print(" ");
Serial.print(filename);
for (unsigned int i = 0; i < 20 - strlen(filename); i++) {
Serial.print(" ");
}
Serial.print(" ");
Serial.print(filesize);
Serial.print(" bytes");
Serial.println();
count++;
} else {
break; // no more files
}
}
return count;
}
//check if a file exists (and erase it), if not it will create it
//and check if it's created correctly.
//it returns 1 if ok or 0 if not
bool checkMyFile(uint8_t fileIndex)
{
bool state = 0;
file = SerialFlash.open(files[fileIndex]);
if (file) { // true if the file exists
Serial.print(F("Found file: "));
Serial.print(files[fileIndex]);
Serial.print(F(" size:"));
Serial.print(file.size());
Serial.print(F(" - Start Adrs:"));
Serial.println(file.getFlashAddress());
file.erase();//now complete erase file content
state = 1;
} else {
Serial.println(F("Unable to access file, attempt to create it"));
if (!SerialFlash.createErasable(files[fileIndex], fileSize[fileIndex])) {
Serial.println(F("Could not create file, I stop here"));
state = 0;
} else {
//file created
file = SerialFlash.open(files[fileIndex]);//open it
if (file) { // true if the file exists
Serial.println(F("File created"));
state = 1;
} else {
Serial.println(F("Cannot create. I will stop here!"));
state = 0;
}
}
}
file.close();
return state;
}
bool readMyFile(uint8_t fileIndex, uint32_t count)
{
if (count < 1) return 0;
bool state = 0;
unsigned char buff[count+1];
file = SerialFlash.open(files[fileIndex]);
if (file) {
file.seek(0);
file.read(buff, count);
Serial.print("\nHere's Data:(");
Serial.print(count);
Serial.print(") -> ");
for (unsigned int i = 0; i < count; i++) {
Serial.write(buff[i]);
}
state = 1;
Serial.print("\n");
} else {
Serial.println(F("File not exists!"));
state = 0;
}
file.close();
return state;
}
uint32_t testWriteA(){
uint32_t written = 0;
file = SerialFlash.open(files[0]);//open first file
if (file) {
unsigned char buff[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
file.seek(0);//position to start of file
written = file.write(buff,sizeof(buff));
Serial.print(F("Written:"));
Serial.print(written);
Serial.print(F(" bytes"));
Serial.println();
} else {
Serial.println(F("File not exists!"));
}
file.close();
return written;
}
What it does?
1) get a list of files present on chip.
2) it check for a specific file existance, if not found it create it, if found it simple erase it.
3) open file, write some chars inside it, close file and get the number of char written
4) attempt to open file, read it and print results, then close it.
It's just for study file system so forgive me the massive printouts.
This chip seems write only 65K chunks, it's very silly use it for write some text but as I said it's just for study, maybe in the future it will be good enable 4k chunks for use as icon/font container.
If everithing goes at it should I have to read this sequence of chars: abcdefghijklmnopqrstuvwxyz, but this happen only at 72Mhz not optimized!
Here's the results:
- Compiled at 72Mhz not optimized: abcdefghijklmnopqrstuvwxyz (correct)
- Compiled at 72Mhz optimized: abcdefghijklmnopqrstuvwxyÿ (missed z and get ÿ)
- Compiled at 96Mhz not optimized: abcdefghijklmnopqrstuvwÿÿÿ (missed y,z and get 3 x ÿ)
- Compiled at 96Mhz Optimized: abcdefghijklmnopqrÿÿÿÿÿÿÿÿ (even worst...)
I'm using Teensyduino 2.8 beta.
Any help?
UPDATE:
Issue solved, was an error in library.
Look HERE
Last edited: