Hi,
I already posted about this issue(that was solved on breadboard) here:
The difference now that it is on a PCB and connected in parellel to ADXL chip(sharing the SPI):
The code is:
#include <SPI.h>
#include <SerialFlash.h>
void setup(){
Serial.begin(115200);
delay(1000);
Serial.println("FLASH test");
setupFlash();
}
void loop() {
#define PRINT_INTERVAL 10
#define PRINT_INTERVAL2 10000
#define PRINT_INTERVAL3 20000
static unsigned long lastPrintTime = millis();
if ( millis() - lastPrintTime < PRINT_INTERVAL )
{
}
else if ( millis() - lastPrintTime < PRINT_INTERVAL2 ) {
useFlash();
delay(4000);
}
else {
useFlash();
delay(4000);
}
}
void useFlash() {
Serial.println("");
Serial.println("");
Serial.println("SerialFlash Read and Write");
Serial.println("(switch your terminal to no line endings)");
Serial.println("--------------------------");
Serial.println("1) Create a new file");
Serial.println("2) Open a file");
Serial.println("3) Delete a file");
Serial.println("4) CP LOW");
Serial.println("5) CP HIGH");
Serial.println("6) SLEEP");
Serial.println("7) WAKEUP");
Serial.println("--------------------------");
Serial.println("Select a number");
while(!Serial.available()){}
char choice = Serial.read();
while(Serial.available()){Serial.read();}
switch(choice){
case '1':
newFile();
break;
case '2':
openFile();
break;
case '3':
deleteFile();
break;
case '4':
//digitalWrite(FLASH_CS_PIN,LOW);
break;
case '5':
//digitalWrite(FlashChipSelect,HIGH);
break;
case '6':
SerialFlash.sleep();
break;
case '7':
SerialFlash.wakeup();
break;
default:
Serial.println("Invalid Selection");
}
}
void setupFlash() {
#define FLASH_CS_PIN 16
if (!SerialFlash.begin(FLASH_CS_PIN)) {
Serial.println("Unable to access SPI Flash chip");
while(1){}
}
}
void loopAnalysis()
{
static unsigned long previousMillis = 0;
static unsigned long lastMillis = 0;
static unsigned long minLoopTime = 0xFFFFFFFF;
static unsigned long maxLoopTime = 0;
static unsigned long loopCounter = 0;
#define INTERVAL 1000
unsigned long currentMillis = millis();
if ( currentMillis - previousMillis > INTERVAL )
{
Serial.print( "Loops: " );
Serial.print( loopCounter );
Serial.print( " ( " );
Serial.print( minLoopTime );
Serial.print( " / " );
Serial.print( maxLoopTime );
Serial.println( " )" );
previousMillis = currentMillis;
loopCounter = 0;
minLoopTime = 0xFFFFFFFF;
maxLoopTime = 0;
}
loopCounter++;
unsigned long loopTime = currentMillis - lastMillis;
lastMillis = currentMillis;
if ( loopTime < minLoopTime )
{
minLoopTime = loopTime;
}
if ( loopTime > maxLoopTime )
{
maxLoopTime = loopTime;
}
}
/* Create a new file
*
* Request filename up to 20 chars
* Request a size up to 256 bytes
* Request some contents
* Create a file
*/
void newFile(){
Serial.println("Enter a filename"); // Request filename from user
while(!Serial.available()){} // Wait for user
char filename[20] = {}; // buffer to store user filename
Serial.readBytesUntil(' ', filename, 20);
while(Serial.available()){Serial.read();}
Serial.println("Enter a filesize in bytes"); // Request file size from user
while(!Serial.available()){} // Wait for user
char sizeArray[3] = {}; // buffer to store requested file size
Serial.readBytesUntil(' ', sizeArray, 3);
while(Serial.available()){Serial.read();}
int filesize = atoi(sizeArray); // Convert char array to int (i.e. "40" to 40)
if(SerialFlash.create(filename, filesize)){ // Returns false if file already exists
SerialFlashFile file; // Open the file we just created for writing
file = SerialFlash.open(filename);
Serial.println("Write some file contents:"); // Request file contents from user
while(!Serial.available()){} // Wait for user
char contents[256] = {}; // buffer to store file contents
Serial.readBytesUntil(255, contents, 256);
while(Serial.available()){Serial.read();} // Empty read buffer
file.write(contents, filesize); // Write the contents buffer
Serial.println("");
Serial.print("New file ");
Serial.print(filename);
Serial.print(" created with size ");
Serial.print(filesize);
Serial.println(" bytes!");
}else{
Serial.println("");
Serial.println("There was an error creating this file (does it already exist?)");
}
return;
}
/* Open a file
*
* Print the directory listing
* Request filename up to 20 chars
* Open file and display contents
*/
void openFile(){
printDir(); // Function to print the directory listing
Serial.println("Enter a filename to OPEN"); // Request file name from user
Serial.println();
while(!Serial.available()){} // Wait for user
char filename[20] = {}; // buffer to store the file name
Serial.readBytesUntil(' ', filename, 20);
while(Serial.available()){Serial.read();}
Serial.println(filename);
SerialFlashFile file;
file = SerialFlash.open(filename); // open the file
if (file) {
Serial.print("File Name: ");
Serial.println(filename);
Serial.println();
Serial.print("File Size: ");
Serial.print(file.size());
Serial.println(" bytes");
Serial.println();
Serial.println("File Contents:");
char buffer[256] = {}; // create a buffer for the file contents
file.read(buffer, 256); // read file to buffer
Serial.print(buffer);
}else{
Serial.println("File not found!");
}
return;
}
/* Delete a file
*
* Print the directory listing
* Request filename up to 20 chars
* Delete File
*/
void deleteFile() {
printDir();
Serial.println("Enter a filename to DELETE"); // Request file name from user
while(!Serial.available()){} // Wait for user
char filename[20] = {}; // buffer to store file name
Serial.readBytesUntil(' ', filename, 20);
while(Serial.available()){Serial.read();}
SerialFlash.remove(filename); // Delete the file
return;
}
/* Print Directory
*
* Print a list of all files on the chip
* Stolen from SerialFlash library example "ListFiles"
*/
void printDir(){
Serial.println("Directory Listing");
Serial.println("-----------------");
SerialFlash.opendir();
while (1) {
char filename[64];
uint32_t filesize;
if (SerialFlash.readdir(filename, sizeof(filename), filesize)) {
Serial.print(" ");
Serial.print(filename);
spaces(20 - strlen(filename));
Serial.print(" ");
Serial.print(filesize);
Serial.print(" bytes");
Serial.println();
} else {
break; // no more files
}
}
}
void spaces(int num) {
for (int i=0; i < num; i++) {
Serial.print(" ");
}
}
The only thing changed here is the CS pin which is now 16
Again it fails on SerialFlash.create() and tried changing to various frequencies in SerialFlashChip.cpp file with no success.
Any idea?
Thanks