Skip to content

[bug] Program crashes when SPI.begin() is called before setup() #159

Description

Hardware

Seeed Studio XIAO MG24

Core version

3.0.0

Arduino IDE version

2.3.7

Operating system

Windows 11

Radio stack variant

No radio

OpenThread Border Router device (if using Matter)

No response

Issue description

When running SPI.begin() before setup, the program crashes.

For context: I'm using mcp2515 library which makes this call inside the main class constructor. For this reason, just instantiating the main MCP2515 class as a global object will trigger the call to SPI.begin(). This library is popularly used, and instantiating it globally doesn't produce a crash on other Arduino stacks.

I prepared a minimal example emulating what I describe.

Serial output

When SPI.begin() is called during setup, minimal example program works as expected:

Running loop...
Running loop...
Running loop...
Running loop...

When SPI.begin() is called before setup, program crashes and there's no serial output.

RTT output (if using Matter)

No response

Minimal reproducer code

The following code produces the crash:

#include <Arduino.h>
#include <SPI.h>

class foo
{
public:
    foo() {
        SPI.begin();
    }
};

foo myFoo;

void setup()
{
    Serial.begin(115200);

    // SPI.begin();
}

void loop()
{
    Serial.println("Running loop...");

    delay(1000);
}

The following code runs with no problems:

#include <Arduino.h>
#include <SPI.h>

void setup()
{
    Serial.begin(115200);

    SPI.begin();
}

void loop()
{
    Serial.println("Running loop...");

    delay(1000);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIssue is a bug report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions