From 94c30b759192231b8172bbb7d81125c8181261a1 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 10 Jan 2025 02:17:11 +0000 Subject: Isolate the SD card from the SPI bus when talking to the display (#176) This should help significantly with https://codeberg.org/cool-tech-zone/tangara-fw/issues/121, which seems to be caused by some SD cards being very picky about being the only SPI device on their bus. Co-authored-by: ailurux Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/176 Co-authored-by: jacqueline Co-committed-by: jacqueline --- src/drivers/gpios.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/drivers/gpios.cpp') diff --git a/src/drivers/gpios.cpp b/src/drivers/gpios.cpp index 52b2b874..cf7cf14e 100644 --- a/src/drivers/gpios.cpp +++ b/src/drivers/gpios.cpp @@ -95,6 +95,14 @@ auto Gpios::WriteSync(Pin pin, bool value) -> bool { return Flush(); } +auto Gpios::ShouldRead() -> bool { + if (!gpio_get_level(GPIO_NUM_34) || has_written_) { + has_written_ = false; + return true; + } + return false; +} + auto Gpios::Flush() -> bool { std::pair ports_ab = unpack(ports_); @@ -104,6 +112,7 @@ auto Gpios::Flush() -> bool { .write_ack(ports_ab.first, ports_ab.second) .stop(); + has_written_ = true; return transaction.Execute() == ESP_OK; } @@ -138,4 +147,32 @@ auto Gpios::Read() -> bool { return true; } +auto Gpios::SdMuxEnable(bool en) -> void { + std::unique_lock lock(mux_mutex_); + mux_en_ = en; + if (SdMuxTarget() == SD_MUX_ESP) { + WriteSync(Pin::kSdMuxDisable, !en); + } + // Don't touch the mux if it's pointed at the SAMD21. We'll write the new + // value when the mux points back at us again. +} + +auto Gpios::SdMuxTarget(SdTarget target) -> void { + std::unique_lock lock(mux_mutex_); + WriteBuffered(Pin::kSdMuxSwitch, target); + if (target == SD_MUX_ESP) { + WriteBuffered(Pin::kSdMuxDisable, !mux_en_); + } else { + // Mux is always enabled when it's pointing at the SAMD21, since it's the + // only thing on that SPI bus. + WriteBuffered(Pin::kSdMuxDisable, 0); + } + Flush(); +} + +auto Gpios::SdMuxTarget() -> SdTarget { + return static_cast( + (ports_ & (1 << static_cast(Pin::kSdMuxSwitch))) > 0); +} + } // namespace drivers -- cgit v1.2.3