Skip to content

Instantly share code, notes, and snippets.

@mcenderdragon
Created May 23, 2019 17:42
Show Gist options
  • Save mcenderdragon/078dd10111164777b16e3aa3e700ea92 to your computer and use it in GitHub Desktop.
Save mcenderdragon/078dd10111164777b16e3aa3e700ea92 to your computer and use it in GitHub Desktop.
#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);
}
uint16_t readStatus() {
digitalWrite(_ss_pin, LOW);
delay(1);
uint16_t status = (SPI.transfer(EE_RDSR)<<8);
status |= SPI.transfer(0) & 0xFF;
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 writeData(uint8_t address, uint8_t data)
{
writeEnable();
digitalWrite(_wp_pin, HIGH);
digitalWrite(_ss_pin, LOW);
SPI.transfer(EE_WRITE);
SPI.transfer(address & 0x7F);
SPI.transfer(data & 0xFF);
digitalWrite(_ss_pin, HIGH);
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;
void sendLoggedBase(uint8_t t)
{
Serial.print("Send ");
Serial.print(t);
Serial.print(" got ");
SPI.transfer(t);
Serial.print(SPI.transfer(0));
Serial.println();
}
void sendLogged(uint8_t t)
{
digitalWrite(SS, LOW);
delay(1);
sendLoggedBase(t);
digitalWrite(SS, HIGH);
delay(1);
}
void loop()
{
uint32_t cks[] = {SPI_CLOCK_DIV128};
for(uint8_t state=0;state<4;state++)
{
SPI.setDataMode(state); //CPOL=0; CPHA=0; -> mode=0
Serial.println(state);
for(uint8_t s=0;s<1;s++)
{
SPI.setClockDivider(cks[s]);
uint16_t status = esp.readStatus();
Serial.print(status);
esp.writeEnable();
delay(1);
Serial.print(" ");
status = esp.readStatus();
Serial.print(status);
Serial.print(" ");
uint8_t d[33];
d[32]=0;
esp.readData(0, d, 32);
Serial.print((char*)d);
Serial.println();
digitalWrite(WP, HIGH);
sendLogged(0);
sendLogged(1);
sendLogged(2);
sendLogged(5);//read status
sendLogged(5);//read status
digitalWrite(SS, LOW);
sendLoggedBase(3);
sendLoggedBase(0);
sendLoggedBase(1);
sendLoggedBase(2);
sendLoggedBase(3);
sendLoggedBase(4);
sendLoggedBase(5);
sendLoggedBase(6);
digitalWrite(SS, HIGH);
delay(1);
digitalWrite(SS, LOW);
delay(1);//write enable
Serial.print("Write ");
Serial.print(6);
SPI.write(6);
Serial.println();
digitalWrite(SS, HIGH);
delay(1);
sendLogged(5);//read status
sendLogged(5);//read status
digitalWrite(SS, LOW);
delay(1);//write diable
Serial.print("Write ");
Serial.print(4);
SPI.write(4);
Serial.println();
digitalWrite(SS, HIGH);
delay(1);
sendLogged(5);//read status
sendLogged(5);//read status
digitalWrite(SS, LOW);
delay(1);//write enable
Serial.print("Write ");
Serial.print(6);
SPI.write(6);
Serial.println();
digitalWrite(SS, HIGH);
delay(1);
sendLogged(5);//read status
digitalWrite(SS, LOW);
sendLoggedBase(2);
sendLoggedBase(1);
sendLoggedBase(23);
digitalWrite(SS, HIGH);
sendLogged(5);//read status
digitalWrite(WP, LOW);
}
}
/*
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);
ESP.deepSleep(1e8);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment