From 9bf367bb51102f79e9b83eae2e6b9fea16009ac2 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 8 Apr 2023 16:49:09 +0200 Subject: [PATCH] SD card interface --- ESP32/disk_backend_esp32.h | 4 +++ ESP32/main.ino | 68 +++++++++++++++++++++++++---------- RP2040/disk_backend_esp32.cpp | 1 + RP2040/disk_backend_esp32.h | 1 + RP2040/rp2040.h | 1 + debugger.cpp | 8 +++-- 6 files changed, 62 insertions(+), 21 deletions(-) create mode 120000 RP2040/disk_backend_esp32.cpp create mode 120000 RP2040/disk_backend_esp32.h diff --git a/ESP32/disk_backend_esp32.h b/ESP32/disk_backend_esp32.h index 7b57459..6313665 100644 --- a/ESP32/disk_backend_esp32.h +++ b/ESP32/disk_backend_esp32.h @@ -4,7 +4,11 @@ #include #include "disk_backend.h" +#if defined(BUILD_FOR_RP2040) +#include "rp2040.h" +#else #include "esp32.h" +#endif class disk_backend_esp32 : public disk_backend diff --git a/ESP32/main.ino b/ESP32/main.ino index 7aa80f4..9ba8238 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -8,14 +8,10 @@ #include #endif #include -#if defined(BUILD_FOR_RP2040) -#include -#endif #include #include #include #if defined(BUILD_FOR_RP2040) -//#include #else #include #include @@ -31,12 +27,14 @@ #include "cpu.h" #include "debugger.h" #include "disk_backend.h" -#if !defined(BUILD_FOR_RP2040) #include "disk_backend_esp32.h" +#if !defined(BUILD_FOR_RP2040) #include "disk_backend_nbd.h" #endif #include "error.h" -#if !defined(BUILD_FOR_RP2040) +#if defined(BUILD_FOR_RP2040) +#include "rp2040.h" +#else #include "esp32.h" #endif #include "gen.h" @@ -67,7 +65,9 @@ console *cnsl = nullptr; uint16_t exec_addr = 0; -SdFat32 sd; +#if !defined(BUILD_FOR_RP2040) +SdFat32 SD; +#endif std::atomic_uint32_t stop_event { EVENT_NONE }; @@ -200,8 +200,8 @@ typedef enum { BE_NETWORK, BE_SD } disk_backend_t; std::optional select_disk_backend(console *const c) { #if defined(BUILD_FOR_RP2040) -return BE_SD; -#endif + return BE_SD; +#else c->put_string("1. network (NBD), 2. local SD card, 9. abort"); int ch = -1; @@ -222,6 +222,7 @@ return BE_SD; // if (ch == '2') return BE_SD; +#endif } std::optional select_disk_type(console *const c) @@ -302,17 +303,33 @@ std::optional, std::vector c->put_string_lf("Files on SD-card:"); #if defined(SHA2017) - if (!sd.begin(21, SD_SCK_MHZ(10))) - sd.initErrorHalt(); -#elif defined(BUILD_FOR_RP2040) -// SD.begin(cspin, SPI1); FIXME -#else - if (!sd.begin(SS, SD_SCK_MHZ(15))) - sd.initErrorHalt(); + if (!SD.begin(21, SD_SCK_MHZ(10))) + SD.initErrorHalt(); +#elif !defined(BUILD_FOR_RP2040) + if (!SD.begin(SS, SD_SCK_MHZ(15))) + SD.initErrorHalt(); #endif for(;;) { - sd.ls("/", LS_DATE | LS_SIZE | LS_R); +#if defined(BUILD_FOR_RP2040) + File root = SD.open("/"); + + for(;;) { + auto entry = root.openNextFile(); + if (!entry) + break; + + if (!entry.isDirectory()) { + c->put_string(entry.name()); + c->put_string("\t\t"); + c->put_string_lf(format("%ld", entry.size())); + } + + entry.close(); + } +#else + SD.ls("/", LS_DATE | LS_SIZE | LS_R); +#endif c->flush_input(); @@ -334,7 +351,6 @@ std::optional, std::vector if (fh.open(selected_file.c_str(), O_RDWR)) { fh.close(); -#if !defined(BUILD_FOR_RP2040) // FIXME disk_backend *temp = new disk_backend_esp32(selected_file); if (!temp->begin()) { @@ -351,7 +367,6 @@ std::optional, std::vector if (disk_type.value() == DT_RL02) return { { { }, { temp } } }; -#endif } c->put_string_lf("open failed"); @@ -521,6 +536,21 @@ void setup() { Serial.println(getCpuFrequencyMhz()); #endif +#if 1 +#if defined(BUILD_FOR_RP2040) + SPI.setRX(MISO); + SPI.setTX(MOSI); + SPI.setSCK(SCK); + + for(int i=0; i<3; i++) { + if (SD.begin(false, SD_SCK_MHZ(10), SPI)) + break; + + Serial.println(F("Cannot initialize SD card")); + } +#endif +#endif + #if defined(BUILD_FOR_RP2040) LittleFSConfig cfg; cfg.setAutoFormat(false); diff --git a/RP2040/disk_backend_esp32.cpp b/RP2040/disk_backend_esp32.cpp new file mode 120000 index 0000000..a927282 --- /dev/null +++ b/RP2040/disk_backend_esp32.cpp @@ -0,0 +1 @@ +../ESP32/disk_backend_esp32.cpp \ No newline at end of file diff --git a/RP2040/disk_backend_esp32.h b/RP2040/disk_backend_esp32.h new file mode 120000 index 0000000..046c88c --- /dev/null +++ b/RP2040/disk_backend_esp32.h @@ -0,0 +1 @@ +../ESP32/disk_backend_esp32.h \ No newline at end of file diff --git a/RP2040/rp2040.h b/RP2040/rp2040.h index 9cf9734..83e5757 100644 --- a/RP2040/rp2040.h +++ b/RP2040/rp2040.h @@ -3,6 +3,7 @@ #if defined(BUILD_FOR_RP2040) #include #include +#include #include #include #endif diff --git a/debugger.cpp b/debugger.cpp index 1302088..ebccdb4 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -371,12 +371,14 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto #endif continue; } -#if defined(ESP32) +#if defined(ESP32) || defined(BUILD_FOR_RP2040) else if (cmd == "cfgdisk") { configure_disk(cnsl); continue; } +#endif +#if defined(ESP32) else if (cmd == "cfgnet") { configure_network(cnsl); @@ -453,10 +455,12 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto cnsl->put_string_lf("cfgnet - configure network (e.g. WiFi)"); cnsl->put_string_lf("startnet - start network"); cnsl->put_string_lf("chknet - check network status"); - cnsl->put_string_lf("cfgdisk - configure disk"); cnsl->put_string_lf("serspd - set serial speed in bps (8N1 are default)"); cnsl->put_string_lf("init - reload (disk-)configuration from flash"); #endif +#if defined(ESP32) || defined(BUILD_FOR_RP2040) + cnsl->put_string_lf("cfgdisk - configure disk"); +#endif continue; }