Skip to content

Commit ec623ff

Browse files
committed
TinyUSB
RP2 only: make TinyUSB optional (required for msc function). Enumerate msc only if sdcard is present.
1 parent b8dcb29 commit ec623ff

File tree

5 files changed

+84
-55
lines changed

5 files changed

+84
-55
lines changed

src/bbx/BbxGizmoSdcard_RP2.cpp

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,33 @@
55
#include <Arduino.h> //Serial
66
#include <SPI.h>
77
#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+
}
929

1030
//--------------------------------------------------------------------+
1131
// TinyUSB SDCard Config
1232
//--------------------------------------------------------------------+
13-
14-
// FsFile system on SD Card
15-
SdFat sd;
33+
#ifdef USE_TINYUSB
34+
#include <Adafruit_TinyUSB.h>
1635

1736
// USB Mass Storage object
1837
Adafruit_USBD_MSC usb_msc;
@@ -42,54 +61,44 @@ static void msc_flush_cb (void) {
4261

4362

4463
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+
}
6686
}
67-
}
6887

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);
7292

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;
8396
}
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;
9297
}
98+
#else //USE_TINYUSB
99+
void hal_usb_setup() {}
100+
#endif //USE_TINYUSB
101+
93102

94103
//--------------------------------------------------------------------+
95104
// BbxGizmoSdcard
@@ -122,9 +131,10 @@ BbxGizmoSdcard* BbxGizmoSdcard::create(BbxConfig *config) {
122131

123132
bool BbxGizmoSdcard::sd_setup() {
124133
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())
126135
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+
128138
return setup_done;
129139
}
130140

@@ -393,4 +403,19 @@ int BbxGizmoSdcard::read(const char* filename, uint8_t **data) {
393403
return len;
394404
}
395405

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+
396421
#endif //#ifdef ARDUINO_ARCH_RP2040

src/bbx/BbxGizmoSdcard_RP2.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
#pragma once
44

5-
#ifndef USE_TINYUSB
6-
#error Enable "Adafruit TinyUSB" - Use Arduino IDE menu: Tools -> USB Stack -> Adafruit TinyUSB
7-
#endif
8-
95
#include "bbx.h"
106
#include <SdFat.h> //FsFile
117

@@ -39,7 +35,8 @@ class BbxGizmoSdcard : public BbxGizmo {
3935
void bench() override;
4036
void info() override;
4137
int read(const char* filename, uint8_t **data) override;
42-
38+
void printSummary() override;
39+
4340
private:
4441
bool sd_setup();
4542
bool sd_listDir(const char * dirname, uint8_t levels=0);

src/bbx/bbx.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ int Bbx::read(const char* filename, uint8_t **data) {
134134
return gizmo->read(filename, data);
135135
}
136136

137+
void Bbx::printSummary() {
138+
if(!gizmo) return;
139+
gizmo->printSummary();
140+
}
141+
137142
//-------------------------------
138143
// Loggers
139144
//-------------------------------

src/bbx/bbx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class BbxGizmo {
5454
virtual void info() = 0; //card info
5555

5656
virtual int read(const char* filename, uint8_t **data) {(void) filename; (void) data; return 0;}
57+
virtual void printSummary() {}
5758
};
5859

5960
class Bbx {
@@ -75,6 +76,7 @@ class Bbx {
7576
void bench(); //benchmark read/write to blackbox
7677
void info(); //blackbox info (memory size, free space, etc.)
7778
int read(const char* filename, uint8_t **data); //read file into data (malloc), returns file size.
79+
void printSummary();
7880

7981
//loggers
8082
void log_bar();

src/madflight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void madflight_setup() {
164164

165165
// LED and BBX summary
166166
cfg.printModule("led");
167-
cfg.printModule("bbx");
167+
bbx.printSummary();
168168

169169
// RCL - Radio Control Link
170170
rcl.config.gizmo = (Cfg::rcl_gizmo_enum)cfg.rcl_gizmo; //the gizmo to use

0 commit comments

Comments
 (0)