#2793·u8g2

Stm32duino hardware spi SSD1306

Author: urobasaCreated Jul 15, 2026Updated Aug 23, 2026

STM32duino: Allow selecting SPI pins or a specific SPIClass instance for hardware SPI

Hello,

I am using the official STM32duino core with an STM32F103C8T6 Blue Pill and an SSD1306 128x64 display in 4-wire SPI mode.

The display is connected to SPI1:

  • SCK: PA5
  • MOSI: PA7
  • DC: PB1
  • RESET: PB0
  • CS: not exposed by the display module

Software SPI works correctly:

cpp
U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(
  U8G2_R0,
  PA5,
  PA7,
  U8X8_PIN_NONE,
  PB1,
  PB0
);

However, the hardware SPI constructor does not produce any output:

cpp
U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g2(
  U8G2_R0,
  U8X8_PIN_NONE,
  PB1,
  PB0
);

I also tried configuring the default STM32duino SPI instance before u8g2.begin():

cpp
SPI.setMOSI(PA7);
SPI.setSCLK(PA5);
SPI.begin();

u8g2.begin();

The display still remains blank.

The same display and wiring have previously worked with a custom STM32 hardware SPI driver at 18 MHz, so the display, wiring and SPI clock frequency do not appear to be the cause.

From reviewing U8x8lib.cpp, the Arduino hardware SPI byte callback always uses the global SPI object:

cpp
SPI.begin();
SPI.beginTransaction(...);
SPI.transfer(...);
SPI.endTransaction();

ESP32 has special handling that allows SPI pins to be passed to SPI.begin(...), as demonstrated in issue #2614. STM32duino uses a different API:

cpp
SPI.setMOSI(mosi);
SPI.setSCLK(sclk);
SPI.begin();

or:

cpp
SPIClass customSPI(mosi, miso, sclk);

Would it be possible to add one of the following?

  1. Support for supplying SCK and MOSI pins to STM32 hardware SPI constructors.
  2. Support for providing a SPIClass* or SPIClass& instance to U8g2.
  3. An STM32-specific hardware SPI callback that applies setMOSI() and setSCLK() before calling SPI.begin().

Providing a SPIClass instance would also allow reliable selection between SPI1, SPI2 and other SPI peripherals without depending on the global default SPI object.

Board/core details:

  • MCU: STM32F103C8T6
  • Board configuration: Generic STM32F1 / Blue Pill F103C8/CB
  • Core: official STM32duino Arduino_Core_STM32
  • Display: SSD1306 128x64, 4-wire SPI without exposed CS
  • Working software SPI pins: PA5 and PA7