Marzogh / SPIMemory

Arduino library for Flash Memory Chips (SPI based only). Formerly SPIFlash
http://spimemory.readthedocs.io/en/latest/
GNU General Public License v3.0
435 stars 138 forks source link

Not working with W25Q80DVUXIE TR using ESP32 #235

Open AdamMarciniak opened 2 years ago

AdamMarciniak commented 2 years ago

I'm using the ESP32's HSPI ports for communication. When I run the example test code, the device isn't found and the JEDEC ID just shows up as zeros.

I know my wiring is correct and all since I am able to manually send SPI commands to the chip and have it correctly send back the JEDEC ID like so:

Any ideas?

#include <SPI.h>
#define JEDECID  0x9F
#define WB_READ_STATUS_REG_1  0x05

static const int spiClk = 1000000; // 1 MHz

SPIClass SPI1(HSPI);

void setup() {
  Serial.begin(115200);
  SPI1.begin();
  SPI1.setDataMode(0);
  SPI1.setBitOrder(MSBFIRST);
  pinMode(15, OUTPUT); //HSPI SS
}

// the loop function runs over and over again until power down or reset
void loop() {
  byte b1, b2, b3;
  char buf[128];
  digitalWrite(15, HIGH);
  digitalWrite(15, LOW);
  SPI1.transfer(JEDECID);
  b1 = SPI1.transfer(0); // manufacturer id
  b2 = SPI1.transfer(0); // memory type
  b3 = SPI1.transfer(0); // capacity
  digitalWrite(15, HIGH);
  digitalWrite(15, HIGH);  
  digitalWrite(15, LOW);
  SPI1.transfer(WB_READ_STATUS_REG_1);       
  while (SPI1.transfer(0) & 1) {}; 
  digitalWrite(15, HIGH);  
  sprintf(buf, "Manufacturer ID: %02xh\nMemory Type: %02xh\nCapacity: %02xh", b1, b2, b3);
  Serial.println(buf);
  Serial.println("Ready");

  delay(1000);
}
AdamMarciniak commented 2 years ago

Here's the culprit:

SPIFlash::SPIFlash(uint8_t cs, SPIClass *spiinterface) {
  _spi = spiinterface;  //Sets SPI interface - if no user selection is made, this defaults to SPI
  if (_spi == &SPI) {
    _SPIInUse = STDSPI;
 }
  else {
    _SPIInUse = ALTSPI;
  }
  csPin = cs;
  pinMode(csPin, OUTPUT);
  CHIP_DESELECT
}

When using HSPI, it causes _SPIInUse to be ALTSPI. When I modify the code to use STDSPI, it works!

I'm not sure what the difference is much or how best to modify this but there it is :)