|
5 | 5 | #include <Arduino.h> //Serial |
6 | 6 | #include <SPI.h> |
7 | 7 | #include <SdFat.h> //pico-arduino bundled version, alternative: #include "SdFat_Adafruit_Fork.h" //use adafruit lib |
8 | | -#include <Adafruit_TinyUSB.h> |
| 8 | + |
| 9 | +// FsFile system on SD Card |
| 10 | +SdFat sd; |
| 11 | +// returns true if card was found |
| 12 | +static bool sdcard_begin(BbxConfig *config) { |
| 13 | + sd.end(); //force begin() to re-initialize SD |
| 14 | + |
| 15 | + //begin sdcard |
| 16 | + switch(config->gizmo) { |
| 17 | + case Cfg::bbx_gizmo_enum::mf_SDSPI : |
| 18 | + if (!sd.begin( SdSpiConfig(config->spi_cs, SHARED_SPI, SD_SCK_MHZ(50), config->spi_bus) )) return false; |
| 19 | + break; |
| 20 | + case Cfg::bbx_gizmo_enum::mf_SDMMC : |
| 21 | + if (!sd.begin( SdioConfig(config->pin_mmc_clk, config->pin_mmc_cmd, config->pin_mmc_dat) )) return false; |
| 22 | + break; |
| 23 | + default: |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + return true; |
| 28 | +} |
9 | 29 |
|
10 | 30 | //--------------------------------------------------------------------+ |
11 | 31 | // TinyUSB SDCard Config |
12 | 32 | //--------------------------------------------------------------------+ |
13 | | - |
14 | | -// FsFile system on SD Card |
15 | | -SdFat sd; |
| 33 | +#ifdef USE_TINYUSB |
| 34 | +#include <Adafruit_TinyUSB.h> |
16 | 35 |
|
17 | 36 | // USB Mass Storage object |
18 | 37 | Adafruit_USBD_MSC usb_msc; |
@@ -42,54 +61,44 @@ static void msc_flush_cb (void) { |
42 | 61 |
|
43 | 62 |
|
44 | 63 | void hal_usb_setup() { |
45 | | - //make sure this function is called only once |
46 | | - static bool called = false; |
47 | | - if(called) return; |
48 | | - called = true; |
49 | | - |
50 | | - // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
51 | | - usb_msc.setID("madfligh", "SD Card", "1.0"); |
52 | | - |
53 | | - // Set read write callback |
54 | | - usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb); |
55 | | - |
56 | | - // Still initialize MSC but tell usb stack that MSC is not ready to read/write |
57 | | - // If we don't initialize, board will be enumerated as CDC only |
58 | | - usb_msc.setUnitReady(false); |
59 | | - usb_msc.begin(); |
60 | | - |
61 | | - // If already enumerated, additional class driver begin() e.g msc, hid, midi won't take effect until re-enumeration |
62 | | - if (TinyUSBDevice.mounted()) { |
63 | | - TinyUSBDevice.detach(); |
64 | | - delay(10); |
65 | | - TinyUSBDevice.attach(); |
| 64 | + //make sure usb setup runs only once |
| 65 | + static bool usb_setup_done = false; |
| 66 | + if(!usb_setup_done) { |
| 67 | + usb_setup_done = true; |
| 68 | + |
| 69 | + // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively |
| 70 | + usb_msc.setID("madfligh", "SD Card", "1.0"); |
| 71 | + |
| 72 | + // Set read write callback |
| 73 | + usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb); |
| 74 | + |
| 75 | + // Still initialize MSC but tell usb stack that MSC is not ready to read/write |
| 76 | + // If we don't initialize, board will be enumerated as CDC only |
| 77 | + usb_msc.setUnitReady(false); |
| 78 | + usb_msc.begin(); |
| 79 | + |
| 80 | + // If already enumerated, additional class driver begin() e.g msc, hid, midi won't take effect until re-enumeration |
| 81 | + if (TinyUSBDevice.mounted()) { |
| 82 | + TinyUSBDevice.detach(); |
| 83 | + delay(10); |
| 84 | + TinyUSBDevice.attach(); |
| 85 | + } |
66 | 86 | } |
67 | | -} |
68 | 87 |
|
69 | | -// returns true if card was found |
70 | | -static bool sdcard_begin(BbxConfig *config) { |
71 | | - sd.end(); //force begin() to re-initialize SD |
| 88 | + static bool unit_ready = false; |
| 89 | + if(sd.clusterCount() > 0 && !unit_ready) { |
| 90 | + // Set disk size, SD block size is always 512 |
| 91 | + usb_msc.setCapacity(sd.card()->sectorCount(), 512); |
72 | 92 |
|
73 | | - //begin sdcard |
74 | | - switch(config->gizmo) { |
75 | | - case Cfg::bbx_gizmo_enum::mf_SDSPI : |
76 | | - if (!sd.begin( SdSpiConfig(config->spi_cs, SHARED_SPI, SD_SCK_MHZ(50), config->spi_bus) )) return false; |
77 | | - break; |
78 | | - case Cfg::bbx_gizmo_enum::mf_SDMMC : |
79 | | - if (!sd.begin( SdioConfig(config->pin_mmc_clk, config->pin_mmc_cmd, config->pin_mmc_dat) )) return false; |
80 | | - break; |
81 | | - default: |
82 | | - return false; |
| 93 | + // MSC is ready for read/write |
| 94 | + usb_msc.setUnitReady(true); |
| 95 | + unit_ready = true; |
83 | 96 | } |
84 | | - |
85 | | - // Set disk size, SD block size is always 512 |
86 | | - usb_msc.setCapacity(sd.card()->sectorCount(), 512); |
87 | | - |
88 | | - // MSC is ready for read/write |
89 | | - usb_msc.setUnitReady(true); |
90 | | - |
91 | | - return true; |
92 | 97 | } |
| 98 | +#else //USE_TINYUSB |
| 99 | +void hal_usb_setup() {} |
| 100 | +#endif //USE_TINYUSB |
| 101 | + |
93 | 102 |
|
94 | 103 | //--------------------------------------------------------------------+ |
95 | 104 | // BbxGizmoSdcard |
@@ -122,9 +131,10 @@ BbxGizmoSdcard* BbxGizmoSdcard::create(BbxConfig *config) { |
122 | 131 |
|
123 | 132 | bool BbxGizmoSdcard::sd_setup() { |
124 | 133 | if(setup_done) return true; |
125 | | - hal_usb_setup(); //start tinyusb if not started yet |
| 134 | + hal_usb_setup(); //start tinyusb if not started yet (need to do this before sdcard_begin()) |
126 | 135 | setup_done = sdcard_begin(config); |
127 | | - //if(!setup_done) Serial.println("BBX: ERROR - Sdcard Mount Failed"); |
| 136 | + hal_usb_setup(); //connect card if inserted |
| 137 | + |
128 | 138 | return setup_done; |
129 | 139 | } |
130 | 140 |
|
@@ -393,4 +403,19 @@ int BbxGizmoSdcard::read(const char* filename, uint8_t **data) { |
393 | 403 | return len; |
394 | 404 | } |
395 | 405 |
|
| 406 | +void BbxGizmoSdcard::printSummary() { |
| 407 | + Serial.printf("BBX: SDCARD - "); |
| 408 | + if(sd.clusterCount() > 0) { |
| 409 | + Serial.printf("Size: %d MB, ", (int)((float)sd.clusterCount()*sd.bytesPerCluster()/1000000)); |
| 410 | + Serial.printf("Free: %d MB, ", (int)((float)sd.freeClusterCount()*sd.bytesPerCluster()/1000000)); |
| 411 | + } else { |
| 412 | + Serial.printf("No card inserted, "); |
| 413 | + } |
| 414 | + #ifdef USE_TINYUSB |
| 415 | + Serial.printf("TinyUSB Mass Storage available\n"); |
| 416 | + #else |
| 417 | + Serial.printf("No TinyUSB Mass Storage\n"); |
| 418 | + #endif |
| 419 | +} |
| 420 | + |
396 | 421 | #endif //#ifdef ARDUINO_ARCH_RP2040 |
0 commit comments