summaryrefslogtreecommitdiff
path: root/src/drivers/gpios.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2025-01-10 02:17:11 +0000
committercooljqln <cooljqln@noreply.codeberg.org>2025-01-10 02:17:11 +0000
commit94c30b759192231b8172bbb7d81125c8181261a1 (patch)
tree3000ddfd33ddb3598f0f8546b94550bad500ec88 /src/drivers/gpios.cpp
parent90118996194d90449e7e7d1fbeb08da587b10642 (diff)
downloadtangara-fw-94c30b759192231b8172bbb7d81125c8181261a1.tar.gz
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 <ailuruxx@gmail.com> Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/176 Co-authored-by: jacqueline <me@jacqueline.id.au> Co-committed-by: jacqueline <me@jacqueline.id.au>
Diffstat (limited to 'src/drivers/gpios.cpp')
-rw-r--r--src/drivers/gpios.cpp37
1 files changed, 37 insertions, 0 deletions
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<uint8_t, uint8_t> 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<std::mutex> 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<std::mutex> 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<SdTarget>(
+ (ports_ & (1 << static_cast<int>(Pin::kSdMuxSwitch))) > 0);
+}
+
} // namespace drivers