Read/Writing to SPI Flash memory

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mbiasotti
 
Posts: 84
Joined: Sun Jul 17, 2016 6:27 pm

Read/Writing to SPI Flash memory

Post by mbiasotti »

Hello Forum and M0 and M4 users.

I'm wanting to adapt the sketch I show below from addressing eeprom to using the SPI flash on 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:

Code: Select all

/*
  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;
    }
  }
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”