markqvist___RNode_Firmware/SharedSPI.h
GlassOnTin 5e19294dea Add shared SPI bus mutex for LoRa + SD card coexistence
SharedSPI.h: FreeRTOS mutex protecting the shared SPI bus (pins
33/34/35) used by LoRa (SX1262), SD card, and future NFC.

sx126x.cpp: All 6 SPI transaction blocks wrapped with
shared_spi_mutex acquire/release. LoRa uses portMAX_DELAY
(always gets access, just delayed by SD if needed).

USBSD.h: USB MSC SD card access uses shared_spi_mutex with
200ms timeout. Deferred init (3s after boot) for SPI bus
readiness. Metrics: sd_ready, sd_reads, sd_fails counters.

IMULogger.h, Gui.h: SD card writes wrapped in shared_spi_mutex.

Status: SD card correctly reports 29.7GB via USB MSC. Reads
work intermittently (~50% success). Root cause: main loop
takes ~700ms per iteration, causing mutex timeout for MSC
callbacks. Needs loop time investigation to achieve reliable
USB mass storage.

The CO5300 display uses a separate SPI3 bus — not affected.
2026-03-28 20:59:52 +00:00

24 lines
785 B
C

// SharedSPI — global mutex for the shared SPI bus (pins MISO=33, MOSI=34, CLK=35)
//
// Users of this bus MUST acquire shared_spi_mutex before any SPI transaction
// and release it after. This prevents race conditions between:
// - SX1262 LoRa radio (CS=36) — main loop, continuous polling
// - SD card (CS=21) — USB MSC callbacks (TinyUSB task), IMU logger, screenshots
// - ST25R3916 NFC (CS=4) — future, not yet implemented
//
// The CO5300 display uses a separate SPI3 bus and does NOT need this mutex.
#ifndef SHARED_SPI_H
#define SHARED_SPI_H
#include <freertos/semphr.h>
extern SemaphoreHandle_t shared_spi_mutex;
inline void shared_spi_init() {
if (!shared_spi_mutex) {
shared_spi_mutex = xSemaphoreCreateMutex();
}
}
#endif // SHARED_SPI_H