class SerialFlashChip | class SerialFlashChip | ||||
{ | { | ||||
public: | public: | ||||
static bool begin(); | |||||
static bool begin(uint8_t pin = 6); | |||||
static uint32_t capacity(const uint8_t *id); | static uint32_t capacity(const uint8_t *id); | ||||
static uint32_t blockSize(); | static uint32_t blockSize(); | ||||
static void sleep(); | static void sleep(); |
#include "SerialFlash.h" | #include "SerialFlash.h" | ||||
#include "util/SerialFlash_directwrite.h" | #include "util/SerialFlash_directwrite.h" | ||||
#define CSCONFIG() pinMode(6, OUTPUT) | |||||
#define CSASSERT() digitalWriteFast(6, LOW) | |||||
#define CSRELEASE() digitalWriteFast(6, HIGH) | |||||
#define CSASSERT() DIRECT_WRITE_LOW(cspin_basereg, cspin_bitmask) | |||||
#define CSRELEASE() DIRECT_WRITE_HIGH(cspin_basereg, cspin_bitmask) | |||||
#define SPICONFIG SPISettings(50000000, MSBFIRST, SPI_MODE0) | #define SPICONFIG SPISettings(50000000, MSBFIRST, SPI_MODE0) | ||||
#if !defined(__arm__) || !defined(CORE_TEENSY) | |||||
#define digitalWriteFast(pin, state) digitalWrite((pin), (state)) | |||||
#endif | |||||
uint16_t SerialFlashChip::dirindex = 0; | uint16_t SerialFlashChip::dirindex = 0; | ||||
uint8_t SerialFlashChip::flags = 0; | uint8_t SerialFlashChip::flags = 0; | ||||
uint8_t SerialFlashChip::busy = 0; | uint8_t SerialFlashChip::busy = 0; | ||||
static volatile IO_REG_TYPE *cspin_basereg; | |||||
static IO_REG_TYPE cspin_bitmask; | |||||
#define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address | #define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address | ||||
#define FLAG_STATUS_CMD70 0x02 // requires special busy flag check | #define FLAG_STATUS_CMD70 0x02 // requires special busy flag check | ||||
#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands | #define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands | ||||
//#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands | //#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands | ||||
//#define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks | //#define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks | ||||
bool SerialFlashChip::begin() | |||||
bool SerialFlashChip::begin(uint8_t pin) | |||||
{ | { | ||||
uint8_t id[3]; | uint8_t id[3]; | ||||
uint8_t f; | uint8_t f; | ||||
uint32_t size; | uint32_t size; | ||||
cspin_basereg = PIN_TO_BASEREG(pin); | |||||
cspin_bitmask = PIN_TO_BITMASK(pin); | |||||
SPI.begin(); | SPI.begin(); | ||||
CSCONFIG(); | |||||
pinMode(pin, OUTPUT); | |||||
CSRELEASE(); | CSRELEASE(); | ||||
readID(id); | readID(id); | ||||
f = 0; | f = 0; | ||||
return true; | return true; | ||||
} | } | ||||
// chips tested: https://github.com/PaulStoffregen/SerialFlash/pull/12#issuecomment-169596992 | |||||
// | |||||
void SerialFlashChip::sleep() | void SerialFlashChip::sleep() | ||||
{ | { | ||||
if (busy) wait(); | if (busy) wait(); |
#include <SD.h> | #include <SD.h> | ||||
#include <SPI.h> | #include <SPI.h> | ||||
const int SDchipSelect = 4; // Audio Shield has SD card CS on pin 10 | |||||
const int FlashChipSelect = 6; | |||||
const int SDchipSelect = 4; // Audio Shield has SD card CS on pin 10 | |||||
const int FlashChipSelect = 6; // digital pin for flash chip CS pin | |||||
void setup() { | void setup() { | ||||
//uncomment these if using Teensy audio shield | //uncomment these if using Teensy audio shield | ||||
if (!SD.begin(SDchipSelect)) { | if (!SD.begin(SDchipSelect)) { | ||||
error("Unable to access SD card"); | error("Unable to access SD card"); | ||||
} | } | ||||
if (!SerialFlash.begin()) { | |||||
if (!SerialFlash.begin(FlashChipSelect)) { | |||||
error("Unable to access SPI Flash chip"); | error("Unable to access SPI Flash chip"); | ||||
} | } | ||||
//SPI Pins (these are the values on the Audio board; change them if you have different ones) | //SPI Pins (these are the values on the Audio board; change them if you have different ones) | ||||
#define MOSI 7 | |||||
#define MOSI 7 | |||||
#define MISO 12 | #define MISO 12 | ||||
#define SCK 14 | |||||
#define SCK 14 | |||||
#define CSPIN 6 | |||||
void setup(){ | void setup(){ | ||||
Serial.begin(9600); //Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored | Serial.begin(9600); //Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored | ||||
SPI.setMOSI(MOSI); | SPI.setMOSI(MOSI); | ||||
SPI.setMISO(MISO); | SPI.setMISO(MISO); | ||||
SPI.setSCK(SCK); | SPI.setSCK(SCK); | ||||
SerialFlash.begin(); | |||||
SerialFlash.begin(CSPIN); | |||||
//We start by formatting the flash... | //We start by formatting the flash... | ||||
uint8_t id[3]; | uint8_t id[3]; |
#include <SerialFlash.h> | #include <SerialFlash.h> | ||||
#include <SPI.h> | #include <SPI.h> | ||||
const int FlashChipSelect = 6; // digital pin for flash chip CS pin | |||||
SerialFlashFile file; | SerialFlashFile file; | ||||
const unsigned long testIncrement = 4096; | const unsigned long testIncrement = 4096; | ||||
while (!Serial && (millis() - startMillis < 10000)) ; | while (!Serial && (millis() - startMillis < 10000)) ; | ||||
delay(100); | delay(100); | ||||
SerialFlash.begin(); | |||||
SerialFlash.begin(FlashChipSelect); | |||||
unsigned char id[3]; | unsigned char id[3]; | ||||
SerialFlash.readID(id); | SerialFlash.readID(id); | ||||
unsigned long size = SerialFlash.capacity(id); | unsigned long size = SerialFlash.capacity(id); |
#include <SerialFlash.h> | #include <SerialFlash.h> | ||||
#include <SPI.h> | #include <SPI.h> | ||||
const int FlashChipSelect = 6; | |||||
const int FlashChipSelect = 6; // digital pin for flash chip CS pin | |||||
void setup() { | void setup() { | ||||
//uncomment these if using Teensy audio shield | //uncomment these if using Teensy audio shield | ||||
delay(100); | delay(100); | ||||
Serial.println("All Files on SPI Flash chip:"); | Serial.println("All Files on SPI Flash chip:"); | ||||
if (!SerialFlash.begin()) { | |||||
if (!SerialFlash.begin(FlashChipSelect)) { | |||||
error("Unable to access SPI Flash chip"); | error("Unable to access SPI Flash chip"); | ||||
} | } | ||||
// the exact part number and manufacturer of the chip. | // the exact part number and manufacturer of the chip. | ||||
#include <SerialFlash.h> | #include <SerialFlash.h> | ||||
#include <SPI.h> | #include <SPI.h> | ||||
const int FlashChipSelect = 6; // digital pin for flash chip CS pin | |||||
SerialFlashFile file; | SerialFlashFile file; | ||||
const unsigned long testIncrement = 4096; | const unsigned long testIncrement = 4096; | ||||
delay(100); | delay(100); | ||||
Serial.println("Raw SerialFlash Hardware Test"); | Serial.println("Raw SerialFlash Hardware Test"); | ||||
SerialFlash.begin(); | |||||
SerialFlash.begin(FlashChipSelect); | |||||
if (test()) { | if (test()) { | ||||
Serial.println(); | Serial.println(); |