mirror of
https://github.com/torlando-tech/pyxis.git
synced 2026-05-24 16:15:18 +00:00
b4afa6d3f7
SD card was unresponsive (MISO stuck 0xFF) because Display's HSPI peripheral had already claimed the GPIO pins via the matrix, preventing FSPI from routing MISO. Fix by initializing SD card BEFORE Display, using the global SPI (FSPI) instance — matching LilyGo's reference code. - Move SD card init before display init in boot sequence - Use global SPI (FSPI) instead of Display's SPIClass(HSPI) - Lower SPI frequency to 800kHz matching LilyGo example - Drive all CS lines (display, LoRa, SD) high before SD init - Add MISO=38 to Display's SPI.begin for post-init bus sharing - Add Display::get_spi() accessor for future shared use Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
// Copyright (c) 2024 microReticulum contributors
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#ifndef HARDWARE_TDECK_SDACCESS_H
|
|
#define HARDWARE_TDECK_SDACCESS_H
|
|
|
|
#include "Config.h"
|
|
|
|
#ifdef ARDUINO
|
|
#include <Arduino.h>
|
|
#include <SD.h>
|
|
#include <SPI.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/semphr.h>
|
|
|
|
namespace Hardware {
|
|
namespace TDeck {
|
|
|
|
/**
|
|
* Mutex-protected SD card access for shared SPI bus.
|
|
*
|
|
* The T-Deck Plus shares HSPI (SCK=40, MOSI=41, MISO=38) across
|
|
* display (CS=12), LoRa (CS=9), and SD card (CS=39). All SPI
|
|
* operations must be serialized via a shared FreeRTOS mutex.
|
|
*
|
|
* SDAccess wraps SD.begin() and all file operations in the mutex
|
|
* so callers don't need to manage bus arbitration themselves.
|
|
*/
|
|
class SDAccess {
|
|
public:
|
|
/**
|
|
* Initialize SD card on the shared SPI bus.
|
|
* Must be called BEFORE Display::init() to avoid SPI peripheral conflicts.
|
|
* Uses the global SPI (FSPI) instance matching LilyGo's reference code.
|
|
*
|
|
* @param mutex Shared SPI bus mutex (created in main.cpp)
|
|
* @return true if SD card mounted successfully
|
|
*/
|
|
static bool init(SemaphoreHandle_t mutex);
|
|
|
|
/** Check if SD card is mounted and ready */
|
|
static bool is_ready() { return _ready; }
|
|
|
|
/** Get the shared SPI mutex (for SDLogger to wrap its own operations) */
|
|
static SemaphoreHandle_t get_mutex() { return _spi_mutex; }
|
|
|
|
/**
|
|
* Read an entire file into a buffer with mutex protection.
|
|
* @return bytes read, or -1 on error
|
|
*/
|
|
static int read_file(const char* path, uint8_t* buf, size_t max_len);
|
|
|
|
/** Check if a file exists (with mutex) */
|
|
static bool file_exists(const char* path);
|
|
|
|
/**
|
|
* Acquire the SPI bus mutex for streaming operations.
|
|
* Caller MUST call release_bus() when done.
|
|
* @param timeout_ms Max wait time
|
|
* @return true if mutex acquired
|
|
*/
|
|
static bool acquire_bus(uint32_t timeout_ms = 500);
|
|
|
|
/** Release the SPI bus mutex after streaming operations */
|
|
static void release_bus();
|
|
|
|
private:
|
|
static SemaphoreHandle_t _spi_mutex;
|
|
static bool _ready;
|
|
|
|
static constexpr uint32_t SD_SPI_FREQ = 800000; // 800kHz (matches LilyGo example)
|
|
};
|
|
|
|
} // namespace TDeck
|
|
} // namespace Hardware
|
|
|
|
#endif // ARDUINO
|
|
#endif // HARDWARE_TDECK_SDACCESS_H
|