Skip to content

SPI.begin() may leave the SPI peripheral uninitialized #3052

Description

@cumin777

SPIClass::begin() may leave the SPI peripheral uninitialized when the
requested settings are equal to the internally cached default SPISettings.

In this case, configSpi() does not call spi_init(). A subsequent transfer
using the default SPI configuration may therefore fail or reach an
uninitialized peripheral.

Calling beginTransaction() with different SPI settings can mask the issue,
because it causes spi_init() to be called.

This regression was exposed during SPI3 loopback validation on a Seeed XIAO
STM32C5, but the root cause appears to be in the common SPI implementation and
is not board-specific.

A related fix is proposed in PR #3049:
#3049

To Reproduce

Minimal sketch:

#include <SPI.h>

void setup()
{
  SPI.begin();

  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  uint8_t value = SPI.transfer(0x00);
  SPI.endTransaction();
}

void loop()
{
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions