diff --git a/CMakeLists.txt b/CMakeLists.txt index f8ad10f..395b907 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ target_link_libraries(mtgcard PUBLIC pico_stdlib hardware_spi hardware_dma + hardware_clocks tinyusb_device tinyusb_board pico_unique_id diff --git a/include/cardslot.h b/include/cardslot.h index d355684..437787e 100644 --- a/include/cardslot.h +++ b/include/cardslot.h @@ -8,7 +8,7 @@ class CardSlot { private: - static constexpr size_t FLASH_SIZE_MB = 2; + static constexpr size_t FLASH_SIZE_MB = 8; public: CardSlot(lib::Flash& flash, uint16_t card_index); diff --git a/include/tusb_config.h b/include/tusb_config.h index 7626957..e3c9b36 100644 --- a/include/tusb_config.h +++ b/include/tusb_config.h @@ -15,6 +15,7 @@ extern "C" #define CFG_TUD_VENDOR 1 #define CFG_TUD_VENDOR_RX_BUFSIZE 0 +#define CFG_TUD_VENDOR_TX_BUFSIZE 256 #ifdef __cplusplus } diff --git a/src/flash.cpp b/src/flash.cpp index 6fbdbf4..6e9b67c 100644 --- a/src/flash.cpp +++ b/src/flash.cpp @@ -44,8 +44,8 @@ Flash::Flash(uint8_t sck, uint8_t tx, uint8_t rx, uint8_t cs, int baudrate) : void Flash::read_page(uint16_t page_index) { - const uint8_t cmd[5] = { CMD_READ, static_cast(page_index >> 8), - static_cast(page_index), 0, 0 }; + const uint8_t cmd[4] = { CMD_READ, static_cast(page_index >> 8), + static_cast(page_index), 0 }; gpio_put(cs, false); spi_write_blocking(spi, cmd, 4); @@ -92,6 +92,7 @@ void Flash::write_enable() gpio_put(cs, false); spi_write_blocking(spi, &cmd, 1); gpio_put(cs, true); + sleep_us(100); } void Flash::wait_done() diff --git a/src/main.cpp b/src/main.cpp index eee76a3..733ec79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "menu.h" #include "pixelstream.h" +#include #include #include #include @@ -17,21 +18,24 @@ bi_decl(bi_program_feature_group(0x1111, 0, "Display Pinout")); bi_decl(bi_ptr_int32(0x1111, 0, DISPLAY_SCK, 2)); bi_decl(bi_ptr_int32(0x1111, 0, DISPLAY_TX, 3)); bi_decl(bi_ptr_int32(0x1111, 0, DISPLAY_CS, 5)); -bi_decl(bi_ptr_int32(0x1111, 0, DISPLAY_DC, 6)); +bi_decl(bi_ptr_int32(0x1111, 0, DISPLAY_DC, 4)); bi_decl(bi_program_feature_group(0x1111, 1, "Flash Pinout")); -bi_decl(bi_ptr_int32(0x1111, 1, FLASH_SCK, 26)); -bi_decl(bi_ptr_int32(0x1111, 1, FLASH_TX, 27)); -bi_decl(bi_ptr_int32(0x1111, 1, FLASH_RX, 28)); -bi_decl(bi_ptr_int32(0x1111, 1, FLASH_CS, 29)); +bi_decl(bi_ptr_int32(0x1111, 1, FLASH_SCK, 10)); +bi_decl(bi_ptr_int32(0x1111, 1, FLASH_TX, 11)); +bi_decl(bi_ptr_int32(0x1111, 1, FLASH_RX, 12)); +bi_decl(bi_ptr_int32(0x1111, 1, FLASH_CS, 9)); +bi_decl(bi_ptr_int32(0x1111, 1, FLASH_IO_2, 13)); +bi_decl(bi_ptr_int32(0x1111, 1, FLASH_IO_3, 14)); bi_decl(bi_program_feature_group(0x1111, 2, "Buttons")); -bi_decl(bi_ptr_int32(0x1111, 2, BUTTON_LEFT, 8)); -bi_decl(bi_ptr_int32(0x1111, 2, BUTTON_MIDDLE, 0)); -bi_decl(bi_ptr_int32(0x1111, 2, BUTTON_RIGHT, 20)); +bi_decl(bi_ptr_int32(0x1111, 2, BUTTON_LEFT, 15)); +bi_decl(bi_ptr_int32(0x1111, 2, BUTTON_MIDDLE, 16)); +bi_decl(bi_ptr_int32(0x1111, 2, BUTTON_RIGHT, 17)); -Display display(DISPLAY_SCK, DISPLAY_TX, DISPLAY_CS, DISPLAY_DC, 120000000); -Flash flash(FLASH_SCK, FLASH_TX, FLASH_RX, FLASH_CS, 70000000); +Display display(DISPLAY_SCK, DISPLAY_TX, DISPLAY_CS, DISPLAY_DC, + 100 * 1000 * 1000); +Flash flash(FLASH_SCK, FLASH_TX, FLASH_RX, FLASH_CS, 25 * 1000 * 1000); enum class RX { @@ -66,12 +70,25 @@ struct int32_t card_index = 0; } rx; +volatile uint8_t debug_requested = false; + constexpr int MS_BUTTON_TIMEOUT = 500; } int main() { + set_sys_clock_khz(200 * 1000, false); + + gpio_set_function(FLASH_IO_2, GPIO_FUNC_SIO); + gpio_set_function(FLASH_IO_3, GPIO_FUNC_SIO); + + gpio_set_dir(FLASH_IO_2, GPIO_OUT); + gpio_set_dir(FLASH_IO_3, GPIO_OUT); + + gpio_put(FLASH_IO_2, 1); + gpio_put(FLASH_IO_3, 1); + gpio_set_function(BUTTON_LEFT, GPIO_FUNC_SIO); gpio_set_function(BUTTON_MIDDLE, GPIO_FUNC_SIO); gpio_set_function(BUTTON_RIGHT, GPIO_FUNC_SIO); @@ -130,6 +147,15 @@ int main() menus::get_current_menu()->onTick(dt); previous_time = current; + + if (debug_requested > 0 && tud_vendor_write_available() > 0) + { + flash.read_page((debug_requested - 1) * CardSlot::TOTAL_PAGE_COUNT); + tud_vendor_write(flash.page(), Flash::Page::SIZE); + tud_vendor_write_flush(); + sleep_ms(500); + debug_requested -= 1; + } } } @@ -151,6 +177,14 @@ void tud_vendor_rx_cb(uint8_t itf, const uint8_t* buffer, uint16_t bufsize) return; } + // special command to debug flash + if (buffer[0] == 0x69) + { + debug_requested = CardSlot::MAX_CARDS; + + return; + } + rx.state = RX::GETTING_CMC; rx.card_index = CardSlot::get_unused(flash);