Problem creating Adafruit_FRAM_SPI object with global scope (Arduino IDE version 1.8.19)

If the sketch includes an Adafruit_FRAM_SPI object with global scope, the sketch compiles and loads, but a drive letter is not assigned. If I create the object within ‘setup’ it is successful.

The following works:

#include <SPI.h>
#include “Adafruit_FRAM_SPI.h”

bool ledState = false;

uint8_t FRAM_CS = D7;
uint8_t addrSizeInBytes = 3; //Default to address size of three bytes

void setup() {
//Set user LED: Red OFF; Green OFF; Blue ON
pinMode(16, OUTPUT); digitalWrite(16, HIGH);
pinMode(17, OUTPUT); digitalWrite(17, HIGH);
pinMode(25, OUTPUT); digitalWrite(25, LOW);

Serial.begin(9600);
while (!Serial) {}
Serial.println(“starting”);

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

if (fram.begin(addrSizeInBytes)) {
Serial.println(“Found SPI FRAM”);
} else {
Serial.println(“No SPI FRAM found … check your connections\r\n”);
}
}

void loop() {
delay(1000);
digitalWrite(25, ledState ? HIGH : LOW);
ledState = !ledState;
}

(The normal output is:
starting
hardware SPI
Flash Size = 0x40000
Found SPI FRAM
)

But if I move the line creating the object so that the object has global scope it does not work:

ledState = false;

uint8_t FRAM_CS = D7;
uint8_t addrSizeInBytes = 3; //Default to address size of three bytes

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

void setup() {
//Set user LED: Red OFF; Green OFF; Blue ON
pinMode(16, OUTPUT); digitalWrite(16, HIGH);
pinMode(17, OUTPUT); digitalWrite(17, HIGH);
pinMode(25, OUTPUT); digitalWrite(25, LOW);

Serial.begin(9600);
while (!Serial) {}
Serial.println(“starting”);

if (fram.begin(addrSizeInBytes)) {
Serial.println(“Found SPI FRAM”);
} else {
Serial.println(“No SPI FRAM found … check your connections\r\n”);
}
}

void loop() {
delay(1000);
digitalWrite(25, ledState ? HIGH : LOW);
ledState = !ledState;
}

Upon downloading I get the following::
sketch uses 61352 bytes (2%) of program storage space. Maximum is 2093056 bytes.
Global variables use 11444 bytes (4%) of dynamic memory, leaving 250700 bytes for local variables. Maximum is 262144 bytes.
Resetting COM8
Caught exception during reset!
Converting to uf2, output size: 136704, start address: 0x2000
Flashing G: (RPI-RP2)
Wrote 136704 bytes to G:/NEW.UF2

If I then try to start the Serial monitor I get the following:
Error opening serial port ‘COM8’. (Port not found)

I then must hold the ‘B’ switch down while powering up to do anything further with the XIAO.

An object that only works in ‘setup’ is not vey useful…I need global scope:

I don’t understand your problem. Put that definition before setup(), and it’s global. I usually group all of my globals together:

bool ledState = false;
uint8_t FRAM_CS = D7;
uint8_t addrSizeInBytes = 3; //Default to address size of three bytes
Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_CS, &SPI); // use hardware SPI
void setup() {
//
// etc.
//

What exact device are you using (XIAO—what?) Before getting into SPI, have you executed a simply “Blinky” program? How about a program with some Serial output?

Regards,

Dave

Footnote:
That type of construct is common, especially among programmers who learned C first. Many folks say the following is more “elegant” in C++, and does exactly the same thing. My observation is that people who learned C++ without learning C first are more likely to use this:

Adafruit_FRAM_SPI fram(FRAM_CS, &SPI);

Results are exactly the same (but I said that already).

You didn’t read the whole post,the second sketch (with the declaration prior to 'setup) leaves the Xiao without a drive letter. The Xiao is RP2040. There is serial output, and the sketch does include blinking the LED.

Update: creating the FRAM object with global scope also fails on the Raspberry PI PICO, so I would assume it would fail on any RP2040 device. I downleveled the Adafruit_FRAM_SPI library (from 2.4.1 to 1.0.1) and the sketch now loads and runs successfully. The major difference between the two levels of the library is that the higher level is dependent on Adafruit’s SPIDevice object.