Skip to content

Instantly share code, notes, and snippets.

@drichelson
Created January 9, 2016 22:45
Show Gist options
  • Save drichelson/4aba47b878ca22df2e29 to your computer and use it in GitHub Desktop.
Save drichelson/4aba47b878ca22df2e29 to your computer and use it in GitHub Desktop.
Teensy 3.2 SerialFlash
#define CS_PIN 10
#define SI_PIN 11
#define SO_PIN 12
#define SCK_PIN 13
#define FILE_NAME "log.txt"
#include <SerialFlash.h>
#include <SPI.h>
SerialFlashFile file;
void initFlash() {
SPI.setCS(CS_PIN);
SPI.setMOSI(SI_PIN);
SPI.setMISO(SO_PIN);
SPI.setSCK(SCK_PIN);
if (!SerialFlash.begin(CS_PIN)) {
Serial.println("Unable to access SPI Flash chip");
}
SerialFlash.opendir();
if (SerialFlash.exists(FILE_NAME)) {
Serial.println(F("File exists, so we'll delete it"));
SerialFlash.remove(FILE_NAME);
}
//create 1MBit file
if (!SerialFlash.createErasable(FILE_NAME, 1000000)) {
Serial.println(F("Could not create file"));
}
file = SerialFlash.open(FILE_NAME);
if (file) { // true if the file exists
Serial.println(F("Found file"));
Serial.println(file.size());
}
else {
Serial.println("Unable to access file");
}
char bufferA[1] = {'a'};
char bufferB[2] = {'b', 'b'};
char bufferC[3] = {'c', 'c', 'c'};
Serial.println(file.write(bufferA, 1));
Serial.println(file.write(bufferB, 2));
Serial.println(file.write(bufferC, 3));
char readBuffer[6];
file.seek(0);
file.read(readBuffer, 6);
Serial.println(readBuffer);
file.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment