Feather Huzza! ESP8266 & 25AA010A EEPROM
/* | |
SPI Master Demo Sketch | |
Connect the SPI Master device to the following pins on the esp8266: | |
GPIO NodeMCU Name | Uno | |
=================================== | |
15 D8 SS | D10 | |
13 D7 MOSI | D11 | |
12 D6 MISO | D12 | |
14 D5 SCK | D13 | |
Note: If the ESP is booting at a moment when the SPI Master has the Select line HIGH (deselected) | |
the ESP8266 WILL FAIL to boot! | |
See SPISlave_SafeMaster example for possible workaround | |
*/ | |
#include <SPI.h> | |
#define normal | |
#ifdef normal | |
#define EE_READ 0b00000011//3 | |
#define EE_WRITE 0b00000010//2 | |
#define EE_WRDI 0b00000100//4 | |
#define EE_WREN 0b00000110//6 | |
#define EE_RDSR 0b00000101//5 | |
#define EE_WRSR 0b00000001//1 | |
#else | |
#define EE_READ 0b11000000 | |
#define EE_WRITE 0b01000000 | |
#define EE_WRDI 0b00100000 | |
#define EE_WREN 0b01100000 | |
#define EE_RDSR 0b10100000 | |
#define EE_WRSR 0b10000000 | |
#endif | |
class ESPMaster { | |
private: | |
uint8_t _ss_pin; | |
uint8_t _wp_pin; | |
public: | |
ESPMaster(uint8_t ss_pin, uint8_t wp_pin) | |
{ | |
_ss_pin = ss_pin; | |
_wp_pin = wp_pin; | |
} | |
void begin() { | |
pinMode(_ss_pin, OUTPUT); | |
pinMode(_wp_pin, OUTPUT); | |
digitalWrite(_ss_pin, HIGH); | |
digitalWrite(_wp_pin, LOW); | |
SPI.setDataMode(0); //CPOL=0; CPHA=0; -> mode=0 | |
SPI.setClockDivider(SPI_CLOCK_DIV4); | |
} | |
uint8_t readStatus() | |
{ | |
digitalWrite(_ss_pin, LOW); | |
delay(1); | |
SPI.transfer(EE_RDSR); | |
uint8_t status = SPI.transfer(0); | |
digitalWrite(_ss_pin, HIGH); | |
return status; | |
} | |
void writeEnable() | |
{ | |
digitalWrite(_ss_pin, LOW); | |
delay(1); | |
SPI.transfer(EE_WREN); | |
digitalWrite(_ss_pin, HIGH); | |
} | |
void writeDisable() | |
{ | |
digitalWrite(_ss_pin, LOW); | |
SPI.transfer(EE_WRDI); | |
digitalWrite(_ss_pin, HIGH); | |
} | |
void writeStatus(uint8_t status) { | |
writeEnable(); | |
digitalWrite(_wp_pin, HIGH); | |
digitalWrite(_ss_pin, LOW); | |
SPI.transfer(EE_WRSR); | |
SPI.transfer(status & 0x0F); | |
digitalWrite(_wp_pin, LOW); | |
digitalWrite(_ss_pin, HIGH); | |
} | |
void readData(uint8_t address, uint8_t *data, uint8_t length) | |
{ | |
digitalWrite(_ss_pin, LOW); | |
SPI.transfer(EE_READ); | |
SPI.transfer(address); | |
for (uint8_t i = 0; i < length; i++) { | |
data[i] = SPI.transfer(0); | |
} | |
digitalWrite(_ss_pin, HIGH); | |
} | |
bool checkWriteEnable() | |
{ | |
uint8_t status = readStatus(); | |
if( status & 0b010 == 0b010) //Write Enable Latch is set | |
{ | |
return true; | |
} | |
else | |
{ | |
writeEnable(); | |
return false; | |
} | |
} | |
bool writeData(uint8_t address, uint8_t data) | |
{ | |
digitalWrite(_wp_pin, HIGH); | |
checkWriteEnable(); | |
digitalWrite(_ss_pin, LOW); | |
SPI.transfer(EE_WRITE); | |
SPI.transfer(address & 0x7F); | |
SPI.transfer(data & 0xFF); | |
digitalWrite(_ss_pin, HIGH); | |
//data is written | |
uint8_t statuss = 1; | |
while(statuss & 0b001 == 0b001) //write in progress | |
{ | |
statuss = readStatus(); | |
yield(); | |
} | |
digitalWrite(_wp_pin, LOW); | |
uint8_t check = 0; | |
readData(address, &check, 1); | |
return check == data; | |
} | |
}; | |
#define SS 0 | |
#define WP 4 | |
ESPMaster esp(SS, WP); | |
SPISettings spidefault(4000000, MSBFIRST, SPI_MODE0); | |
void setup() { | |
Serial.begin(74880); | |
SPI.setDataMode(SPI_MODE0); //CPOL=0; CPHA=0; -> mode=0 | |
SPI.setClockDivider(SPI_CLOCK_DIV128); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.begin(); | |
esp.begin(); | |
delay(1000); | |
Serial.println("Started"); | |
Serial.print("Satus "); | |
SPI.beginTransaction(spidefault); | |
digitalWrite(SS, LOW); | |
delay(1000); | |
uint8_t status; | |
status = SPI.transfer(EE_RDSR); | |
Serial.print(status); | |
Serial.print(" "); | |
status = SPI.transfer(0); | |
delay(500); | |
digitalWrite(SS, HIGH); | |
SPI.endTransaction(); | |
Serial.println(status); | |
} | |
int i = 0; | |
uint8_t j = 0; | |
uint8_t sendLoggedBase(uint8_t t) | |
{ | |
Serial.print("Send "); | |
Serial.print(t); | |
Serial.print(" got "); | |
SPI.transfer(t); | |
uint8_t got = SPI.transfer(0); | |
Serial.print(got); | |
Serial.println(); | |
return got; | |
} | |
uint8_t sendLogged(uint8_t t) | |
{ | |
digitalWrite(SS, LOW); | |
delay(1); | |
uint8_t got = sendLoggedBase(t); | |
digitalWrite(SS, HIGH); | |
delay(1); | |
return got; | |
} | |
void loop() | |
{ | |
sendLogged(5);//read status | |
uint8_t data[33]; | |
data[32] = 0; | |
esp.readData(0, data, 32); | |
Serial.println((char*)data); | |
i++; | |
j++; | |
if(j>=32) | |
j=0; | |
Serial.print("W: "); | |
Serial.print(j); | |
Serial.print("->"); | |
Serial.print(i); | |
if(esp.writeData(j, i)) | |
Serial.println(" Done"); | |
else | |
Serial.println(" Fail"); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment