How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ytabouret
 
Posts: 4
Joined: Wed Mar 29, 2023 6:19 am

How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post by ytabouret »

Hello,
By default the SPI FRAM frequency is 1 MHz.
How to modify it?
Thanks for any help.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post by mikeysklar »

Which SPI FRAM board from Adafruit are you using?

The defaults I have seen are 1MHz for I2C, but 20MHz for SPI.

User avatar
ytabouret
 
Posts: 4
Joined: Wed Mar 29, 2023 6:19 am

Re: How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post by ytabouret »

I use this model:
Adafruit SPI Non-Volatile FRAM Breakout - 64Kbit / 8KByte Product ID: 1897

The library is "Adafruit_FRAM_SPI.h" in which I find on line 45 information on a frequency of 1MHz:

"Adafruit_FRAM_SPI(int8_t cs, SPIClass *theSPI = &SPI, uint32_t freq = 1000000);"

I tried changing this frequency but it didn't work faster.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post by mikeysklar »

Are you using Hardware SPI pins on your ESP32?

Can you show the model on ESP32 you are using and pins?

The HW SPI mode allows you to pass in the frequency when creating the constructor. Had you tried initializing your code using HW SPI pins and the 20MHz frequency or had you modified the driver?

Code: Select all

/*!
 *  @brief  Instantiates a new SPI FRAM class using hardware SPI
 *  @param  cs
 *          Required chip select pin number
 *  @param  *theSPI
 *          SPI interface object, defaults to &SPI
 *  @param  freq
 *          The SPI clock frequency to use, defaults to 1MHz
 */
Adafruit_FRAM_SPI::Adafruit_FRAM_SPI(int8_t cs, SPIClass *theSPI,
                                     uint32_t freq) {
  if (spi_dev) {
    delete spi_dev;
  }

  spi_dev = new Adafruit_SPIDevice(cs, freq, SPI_BITORDER_MSBFIRST, SPI_MODE0,
                                   theSPI);
}

User avatar
ytabouret
 
Posts: 4
Joined: Wed Mar 29, 2023 6:19 am

Re: How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post by ytabouret »

Hello,
I am using an ESP32 WROOM 32U.
The chosen pins are the SPI3 (VSPI):
Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(18, 19, 23, 5);

Which is twice as fast as my trials with SPI2 (HSPI):
Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(14, 12, 13, 15);

With VSPI writing a byte takes about 27µs, reading takes about 22µs.
I didn't change anything in the driver (I'm a beginner), I just read the 1MHz information in the driver, hence my question of whether we can do better.

I did not configure the SPI frequency not knowing how to do it.

If I assume correctly, 27µs per byte is 0.3MHz or I didn't understand?

Thanks for any help.

Code: Select all

// https://learn.adafruit.com/adafruit-spi-fram-breakout/wiring-and-test

// ESP32 WROOM DA Module
#include <SPI.h>
#include "Adafruit_FRAM_SPI.h"

// Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(SCL, MISO, MOSI, CS);

Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(18, 19, 23, 5);  // This VSPI (SPI3) is twice as fast as HSPI (VSPI2) below
// Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(14, 12, 13, 15);

uint8_t Adr, Valeur,Valeur_Lue;

void setup(void) {

  Serial.begin(115200);

  if (fram.begin()) {
    //Serial.println("Found SPI FRAM");
  } else {
    Serial.println("No SPI FRAM found ... check your connections\r\n");
    while (1)
      ;
  }
}

void loop(void) {

  // ********************************  To write a byte : 27µs
  Adr = 10;
  byte Valeur = 78;

  Ecriture_F(Adr, Valeur);
  Serial.print("Valeur ecrite :");
  Serial.println(Valeur);

  // *********************************** To read a byte : 22 µs
  Lecture_F(Adr);
  Serial.print("Valeur lue :");
  Serial.println(Valeur_Lue);

  while (1)
    ;
}

void Ecriture_F(int Adr, byte Valeur) {  // 27 µs
  fram.writeEnable(true);
  fram.write8(Adr, Valeur);
}

void Lecture_F(int Adr) {  // 22 µs
  Valeur_Lue = fram.read8(Adr);
}

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post by mikeysklar »

By declaring each of the pins you will be using bitbang mode despite specifying HW SPI pins.

You want to pass the SPI device and baudrate to get top speed. This will default to the VLSPI pins by default.

Code: Select all

Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(CS_PIN, SPI, 20000000);

User avatar
ytabouret
 
Posts: 4
Joined: Wed Mar 29, 2023 6:19 am

Re: How to modify the SPI FRAM frequency ? (ESP32-Arduino IDE)

Post by ytabouret »

Thank you for your answer but I am a beginner and it is not clear to me.
Could you tell me how to modify the example contained in the driver below to get the best?
Thanks for any help.

Code: Select all

#include <SPI.h>
#include "Adafruit_FRAM_SPI.h"

/* Example code for the Adafruit SPI FRAM breakout */
uint8_t FRAM_CS = 10;

//Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_CS);  // use hardware SPI

uint8_t FRAM_SCK= 13;
uint8_t FRAM_MISO = 12;
uint8_t FRAM_MOSI = 11;
//Or use software SPI, any pins!
Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_SCK, FRAM_MISO, FRAM_MOSI, FRAM_CS);

uint16_t          addr = 0;

void setup(void) {
  Serial.begin(9600);
  while (!Serial) delay(10);     // will pause Zero, Leonardo, etc until serial console opens
  
  if (fram.begin()) {
    Serial.println("Found SPI FRAM");
  } else {
    Serial.println("No SPI FRAM found ... check your connections\r\n");
    while (1);
  }
  
  // Read the first byte
  uint8_t test = fram.read8(0x0);
  Serial.print("Restarted "); Serial.print(test); Serial.println(" times");

  // Test write ++
  fram.writeEnable(true);
  fram.write8(0x0, test+1);
  fram.writeEnable(false);

  fram.writeEnable(true);
  fram.write(0x1, (uint8_t *)"FTW!", 5);
  fram.writeEnable(false);

  // dump the entire 8K of memory!
  uint8_t value;
  for (uint16_t a = 0; a < 8192; a++) {
    value = fram.read8(a);
    if ((a % 32) == 0) {
      Serial.print("\n 0x"); Serial.print(a, HEX); Serial.print(": ");
    }
    Serial.print("0x"); 
    if (value < 0x1) 
      Serial.print('0');
    Serial.print(value, HEX); Serial.print(" ");
  }
}

void loop(void) {

}

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

Return to “Arduino”