Add shared SPI bus mutex for SD card, display, and LoRa coexistence

The T-Deck Plus shares HSPI across the display (CS=12), LoRa (CS=9),
and SD card (CS=39). Previously SD logging was disabled because
SD.begin() reconfigured the SPI bus and blanked the display.

This introduces a FreeRTOS mutex created in main.cpp and injected into
Display, SX1262Interface, and a new SDAccess class so all three
peripherals serialize their SPI transactions safely.

- Add SDAccess class wrapping SD.begin() and file ops with mutex
- Add set_spi_mutex() to Display and SX1262Interface
- Wrap Display flush, fill, draw, and power ops in mutex
- Refactor SDLogger to use SDAccess mutex instead of owning SD.begin()
- Wire up mutex creation and injection order in setup()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
torlando-tech
2026-03-04 00:19:10 -05:00
co-authored by Claude Opus 4.6
parent d9411fb4bb
commit d03f0b308f
9 changed files with 313 additions and 25 deletions
+11 -3
View File
@@ -12,9 +12,17 @@
using namespace RNS;
#ifdef ARDUINO
// Static members for SPI mutex (shared with display)
// Static members for SPI mutex (shared with display and SD card)
SemaphoreHandle_t SX1262Interface::_spi_mutex = nullptr;
bool SX1262Interface::_mutex_initialized = false;
void SX1262Interface::set_spi_mutex(SemaphoreHandle_t mutex) {
_spi_mutex = mutex;
_mutex_initialized = (mutex != nullptr);
if (_mutex_initialized) {
DEBUG("SX1262Interface: Using external SPI mutex");
}
}
#endif
SX1262Interface::SX1262Interface(const char* name) : InterfaceImpl(name) {
@@ -58,15 +66,15 @@ bool SX1262Interface::start() {
INFO(" CR: 4/" + std::to_string(_config.coding_rate));
INFO(" TX Power: " + std::to_string(_config.tx_power) + " dBm");
// Initialize SPI mutex if not already done
// Use external mutex if provided, otherwise create our own (fallback)
if (!_mutex_initialized) {
WARNING("SX1262Interface: No external SPI mutex set, creating own");
_spi_mutex = xSemaphoreCreateMutex();
if (_spi_mutex == nullptr) {
ERROR("SX1262Interface: Failed to create SPI mutex");
return false;
}
_mutex_initialized = true;
DEBUG("SX1262Interface: SPI mutex created");
}
// Acquire SPI mutex