ESP32S2 MassStorage with External Flash chip

Hey there, im trying to build a usb mass storage device with ESP32S2 and W25Q80 flash chip connected via SPI and using Adafruit TinyUSB and Adafruit SPIFlash library. :star_struck:

So, msc_ramdisk and msc_sdfat(with sd card adpter) examples works great and shows up as mass storage device, also Marzogh`s SpiMemory library example TestFlash works with flash chip, shows flash ID e.t.c. :slightly_smiling_face:

But i cant figure out why example code msc_external_flash(TinyUSB) or flash_info(Adafruit SPIFlash) doesnt run on ESP32S2. Codes are similar, and i suspect that it does not correctly initialize SPI interface, as oscilliscope is connected to SCK and SS pins, all above written skeches shows activity on SPI bus, but when msc_external_flash or flash_info are uploaded, it shows floating pins. :face_with_raised_eyebrow:
And also these two example sketches with no alteration runs ok, but no SPI communication, when trying set "custom" SPI, ESP chip starts resetting itself. :face_with_monocle:

Yuppers.
Well the line of code where you wrote... Oh no lines of code that you wrote. I could get an ESP32 and a W25Q80 flash chip and try the example code but that would be in another 2 weeks or so. Tell you want. Not really telling but asking, could you post the code that is failing to work?

In code tag thingies.

My code "cleaned" from example sketch flash_info (Adafruit SPIFlash):

#include "SdFat.h"
#include "Adafruit_SPIFlash.h"

#define CUSTOM_CS   34
#define CUSTOM_SPI  SPI //some examples use FSPI insted of SPI, but it doesnt work here

Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI);

Adafruit_SPIFlash flash(&flashTransport);

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit Serial Flash Info example");

  //  flash.begin(); //this one should be used, but esp starts reseting when calling, mby thats the problem?
  flashTransport.begin(); //initializes SPI but obviously flash.getJEDECID() and flash.size() will not work.
}

void loop() {
  Serial.print("JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX);
  Serial.print("Flash size: "); Serial.print(flash.size() / 1024); Serial.println(" KB");
  delay(100);
}

I suspect the problem is somewhere in library itself, but cant find where. Part where SPI is initialized:

Adafruit_FlashTransport_SPI::Adafruit_FlashTransport_SPI(
    uint8_t ss, SPIClass *spiinterface) {
  _cmd_read = SFLASH_CMD_READ;
  _addr_len = 3; // work with most device if not set
  _ss = ss;
  _spi = spiinterface;
  _clock_wr = _clock_rd = 4000000;
}

Adafruit_FlashTransport_SPI::Adafruit_FlashTransport_SPI(uint8_t ss,
                                                         SPIClass &spiinterface)
    : Adafruit_FlashTransport_SPI(ss, &spiinterface) {}

void Adafruit_FlashTransport_SPI::begin(void) {
  pinMode(_ss, OUTPUT);
  digitalWrite(_ss, HIGH);

  _spi->begin();
}

And after that follows:

Adafruit_SPIFlash::Adafruit_SPIFlash() : Adafruit_SPIFlashBase() {
  _cache = NULL;
}

Adafruit_SPIFlash::Adafruit_SPIFlash(Adafruit_FlashTransport *transport)
    : Adafruit_SPIFlashBase(transport) {
  _cache = NULL;
}

bool Adafruit_SPIFlash::begin(SPIFlash_Device_t const *flash_devs,
                              size_t count) {
  bool ret = Adafruit_SPIFlashBase::begin(flash_devs, count);

  // Use cache if not FRAM
  if (_flash_dev && !_flash_dev->is_fram) {
    // new cache object if not already
    if (!_cache) {
      _cache = new Adafruit_FlashCache();
    }
  }

  return ret;
}

I know the single core ESP32S has 2 more GPIO pins but the GPIO ports on the 32S correspond to the ESP32's GPIO API.

The ESP32 has 2 32 bit GPIO ports. The pins below GPIO_NUM_33 are on port A and are designed for general I/O. GPIO_NUM_33 and above are on portB do not have pullups are tuned to match the A:D converter 0 and are input only.

GPIO & RTC GPIO - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)

I can use the GPIO pins on portB as normal GPIO pins by using the RTC API. You might be able to do the same if you require portB pins as outputs.

You might use a different GPIO pin for the CS pin. One on portB.

Tried many other pins, but with no luck, still esp is resetting - Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.

I guess the Guru meditation error is a secret.

I guess you've installed into the Arduino IDE the ESP Exception Decoder and posted the results?

It would give much needed info, but it fails to decode :frowning:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x4009918b  PS      : 0x00060630  A0      : 0x800816e4  A1      : 0x3ffc65c0  
A2      : 0x00000001  A3      : 0x3f0018a6  A4      : 0x00000022  A5      : 0x0000ff00  
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x3ffc0be0  A9      : 0x009c1001  
A10     : 0x3ffc0c78  A11     : 0x00000022  A12     : 0x000000ff  A13     : 0x000000ff  
A14     : 0x000000ff  A15     : 0xff000000  SAR     : 0x0000001d  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000019  LBEG    : 0x000000ff  LEND    : 0x000000ff  LCOUNT  : 0x40026ef5  


Backtrace:0x40099188:0x3ffc65c00x400816e1:0x3ffc65e0 0x40081649:0x3ffc6600 0x40081002:0x3ffc6620 0x400834e6:0x3ffc6650 

Decode Failed

Also tried that code on ESP32-WROVER-IE, and does the same thing - 1 core paniced, and also does not decode backtrace :frowning:

I'd take all the info you have provided and go to the Adafruit Github page supporting the Adafruit SPI Flash library. Open a trouble issue. Post your code, post the type and model of ESP32 you are using, post the error message and let them know the Exceptiondecoder fails to decode the error. Be patient and answer the question the programmer asks. It may take a few days but they should either let you know it is not their library or they will fix their library.

What the error means is that something has wrote across one or 2 other memory heaps. Be clear that you have used other GPIO_Pins.

almost forgot. GO back a version or 2 of the library to see if the issue goes away.

Already done that :cowboy_hat_face:

Hmm, ok, will try :hugs:
Update - tried many different verisons, with no luck, and yeah, looks like issue is in Adafruit_SPIFlash library