Using SPI Flash memory like eeprom

( I originally posted this to the Adafruit. Forum but so far have not gotten a response)

I'm wanting to adapt the sketch I show below from addressing eeprom to using the SPI flash on my Adafruit my my itsybitsy M4. I've successfully used the example SDfat read and write to access a csv string of characters that represent the brightness byte value of the dotstar LEDs I'm trying to control.
Instead of reading a single file and decerning to parse the string with its comma separations, I'd like to use the SPI flash like an eeprom where I can access the byte values. I see from the Adafruit SPI flash library that I can possibly use thiese to do this:

void Adafruit_FRAM_SPI::write ( uint32_t addr, const uint8_t * valuesize_t count )
void Adafruit_FRAM_SPI::read ( uint32_t addr,uint8_t * values,size_t count)

Does anyone have an example of doing this that they can share with me? I want to write the values to the flash one time only and then read from it, assigning its values to different segments (head,tails) of my Dotstar. I know how to do the Dotstar part of it, just need to know how to write the values to the SPI flash (not creating single file to read from) and then access the byte values per addr. ? Do I first need to format the SP Flash space?

here's my current sketch:

/*
  Candle Flicker - For up to 14 channels of lamps
  using Mega 2560: COM4
  created by mark biasotti
*/
#include <EEPROM.h>

int pwmspeed = 50; // 50 = good speed - cycle of PWM 40 is original speed of recording
int nominal_ledvalue = 180;
int Led11 = 12;
int Led12 = 13;
int sequence_counter = 0;  // for # of bytes of eeprom reached

struct ledstructure {
  int Led_pin;  // 2 thru 11 for 10 total PWM leds
  int long start_addr; //stores predefined Red value for channel
  int long current_addr;
  int currentledvalue = nominal_ledvalue;
} LED[10];  // 10 PWM channels with 11-14 not being used

void setup() {
  Serial.begin(57600);
  randomSeed(analogRead(0));
  analogWrite(Led11, 254);  // colman lantern on pin output 11 no flicker
  analogWrite(Led12, 254);    // colman lantern pin out 12 no flicker
  LED[0].start_addr = 0;      // assign starting postion for access eeprom sequence
  LED[1].start_addr = 341;
  LED[2].start_addr = 682;
  LED[3].start_addr = 1054;
  LED[4].start_addr = 1395;
  LED[5].start_addr = 1736;
  LED[6].start_addr = 2077;
  LED[7].start_addr = 2418;
  LED[8].start_addr = 2759;
  LED[9].start_addr = 3100;
  LED[10].start_addr = 3441;
  LED[11].start_addr = 3782;

  LED[0].Led_pin = 2;      // assigns actual output PWM pins to Leds 0-9
  LED[1].Led_pin = 3;
  LED[2].Led_pin = 4;
  LED[3].Led_pin = 5;
  LED[4].Led_pin = 6;
  LED[5].Led_pin = 7;
  LED[6].Led_pin = 8;
  LED[7].Led_pin = 9;
  LED[8].Led_pin = 10;
  LED[9].Led_pin = 11;
  LED[10].Led_pin = 45;
  LED[11].Led_pin = 46;

  for (int i = 0; i < 12; i++) {  // sets initial start address for each LED structure
    LED[i].current_addr = LED[i].start_addr;
    Serial.print ("Led ");
    Serial.print (i);
    Serial.print (" current address = ");
    Serial.println(LED[i].current_addr);
  }
}

void assign_current_addr() {

}

void loop() {
  while (sequence_counter <= 4095) {
    
    for ( int i = 0; i < 12; i++) {   //10 is the number of sequenced PWM LED channels
      LED[i].currentledvalue = EEPROM.read(LED[i].current_addr);
      analogWrite(LED[i].Led_pin, LED[i].currentledvalue);

      if ((LED[i].current_addr > 4095) && sequence_counter < 4095 ) {  // for offset LEDs in eeprom sequence starts at beginning of sequence
        LED[i].current_addr = 0;
      }
      LED[i].current_addr++;
      /*      Serial.print("sequence # = ");
            Serial.print(sequence_counter);
            Serial.print("  LED # = ");
            Serial.print (i);
            Serial.print("  address # = ");
            Serial.print((LED[i].current_addr));
            Serial.print("  current value  =  ");
            Serial.println((LED[i].currentledvalue));
      */
    }
    delay(pwmspeed);
    sequence_counter++;
    if (sequence_counter > 4095) {
      sequence_counter = 0;
    }
  }
}

What storage chip do you have connected to your board?

He is using:
Adafruit ItsyBitsy M4 Express featuring ATSAMD51 : ID 3800 : $14.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Adafruit ItsyBitsy M4 Express featuring the Microchip ATSAMD51 ! Small, powerful, with a ultra fast ATSAMD51 Cortex M4 processor running at 120 MHz

OK. So it has "2 MB SPI FLASH chip for storing files and CircuitPython code storage.". The details say it is a "QSPI" FLASH chip. QSPI is like SPI but shifts 4 bits at a time.

They have a (now deprecated) library for talking QSPI: GitHub - adafruit/Adafruit_QSPI
It was deprecated in favor of using
GitHub - adafruit/Adafruit_SPIFlash: Arduino library for external (Q)SPI flash device
with SdFat to implement a file system on FLASH. Since you don't want a file system, just raw access, this older library might be the thing for you.

1 Like

Thanks al for verifying. I have the more up-to-date library and I’ll try doing raw access, but will also try withe the deprecated library. Was hoping someone else has try doing this and can verify this is the right way to do it.

Unfortunately, the QSPI library (depreciated library) from Adafruit does not work with my Itsybitsy M4, so I've reposted the question back to the Adafruit forum.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.