Skip to content

Commit ebc5cff

Browse files
committed
RP2: delay USB startup until after SDCARD setup
1 parent 66e8ba5 commit ebc5cff

File tree

12 files changed

+68
-72
lines changed

12 files changed

+68
-72
lines changed

src/bbx/BbxGizmoSdcard_RP2.cpp

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,43 +59,33 @@ static void msc_flush_cb (void) {
5959
// sd.cacheClear(); // clear file system's cache to force refresh ---> only in "SdFat_Adafruit_Fork.h" not in "SdFat.h"
6060
}
6161

62+
void bbx_rp2_usb_detach() {
63+
TinyUSBDevice.detach();
64+
}
65+
6266
void bbx_rp2_usb_setup() {
63-
//make sure usb setup runs only once
64-
static bool usb_setup_done = false;
65-
if(!usb_setup_done) {
66-
usb_setup_done = true;
67-
68-
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
69-
usb_msc.setID("madfligh", "SD Card", "1.0");
70-
71-
// Set read write callback
72-
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
73-
74-
// Still initialize MSC but tell usb stack that MSC is not ready to read/write
75-
// If we don't initialize, board will be enumerated as CDC only
76-
usb_msc.setUnitReady(false);
77-
usb_msc.begin();
78-
79-
// If already enumerated, additional class driver begin() e.g msc, hid, midi won't take effect until re-enumeration
80-
if (TinyUSBDevice.mounted()) {
81-
TinyUSBDevice.detach();
82-
delay(10);
83-
TinyUSBDevice.attach();
84-
}
67+
if(sd.clusterCount() > 0) {
68+
// Set disk size, SD block size is always 512
69+
usb_msc.setCapacity(sd.card()->sectorCount(), 512);
70+
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
71+
usb_msc.setID("madfligh", "SD Card", "1.0");
72+
// Set read write callback
73+
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
74+
// MSC is ready for read/write
75+
usb_msc.setUnitReady(true);
76+
usb_msc.begin();
77+
78+
// If already enumerated, additional class driver begin() e.g msc, hid, midi won't take effect until re-enumeration with TinyUSBDevice.attach(
79+
if (TinyUSBDevice.mounted()) {
80+
TinyUSBDevice.detach();
81+
delay(10);
82+
}
8583
}
8684

87-
static bool unit_ready = false;
88-
if(sd.clusterCount() > 0 && !unit_ready) {
89-
// Set disk size, SD block size is always 512
90-
usb_msc.setCapacity(sd.card()->sectorCount(), 512);
91-
92-
// MSC is ready for read/write
93-
usb_msc.setUnitReady(true);
94-
95-
unit_ready = true;
96-
}
85+
TinyUSBDevice.attach();
9786
}
9887
#else //USE_TINYUSB
88+
void bbx_rp2_usb_detach() {}
9989
void bbx_rp2_usb_setup() {}
10090
#endif //USE_TINYUSB
10191

@@ -131,19 +121,19 @@ BbxGizmoSdcard* BbxGizmoSdcard::create(BbxConfig *config) {
131121

132122
bool BbxGizmoSdcard::sd_setup() {
133123
if(setup_done) return true;
134-
bbx_rp2_usb_setup(); //start tinyusb if not started yet (need to do this before sdcard_begin())
124+
//bbx_rp2_usb_setup(); //start tinyusb if not started yet (need to do this before sdcard_begin())
135125
setup_done = sdcard_begin(config);
136-
bbx_rp2_usb_setup(); //connect card if inserted
126+
//bbx_rp2_usb_setup(); //connect card if inserted
137127

138128
return setup_done;
139129
}
140130

141131
//setup the file system
142132
void BbxGizmoSdcard::setup() {
143133
if(sd_setup()) {
144-
Serial.printf("BBX: SDCARD size:%d MB free:%d MB\n", (int)((float)sd.clusterCount()*sd.bytesPerCluster()/1000000), (int)((float)sd.freeClusterCount()*sd.bytesPerCluster()/1000000));
134+
//Serial.printf("BBX: SDCARD size:%d MB free:%d MB\n", (int)((float)sd.clusterCount()*sd.bytesPerCluster()/1000000), (int)((float)sd.freeClusterCount()*sd.bytesPerCluster()/1000000));
145135
}else{
146-
Serial.printf("BBX: SDCARD not found\n");
136+
//Serial.printf("BBX: SDCARD not found\n");
147137
}
148138
}
149139

src/bbx/BbxGizmoSdcard_RP2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class BbxGizmoSdcard : public BbxGizmo {
3636
void info() override;
3737
int read(const char* filename, uint8_t **data) override;
3838
void printSummary() override;
39-
39+
4040
private:
4141
bool sd_setup();
4242
bool sd_listDir(const char * dirname, uint8_t levels=0);

src/cli/cli.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,16 @@ static void cli_pout() {
198198
for(int i=0;i<OUT_SIZE;i++) {
199199
if(out.getType(i)) {
200200
Serial.printf("%c%d%%:%1.0f\t", out.getType(i), i, 100*out.get(i));
201-
if(out.eperiodEnabled[i]>=0) {
201+
if(out.eperiodEnabled[i]) {
202202
Serial.printf("rpm%d:%d\t", i, out.rpm(i));
203203
}
204204
}
205205
}
206206
}
207207

208208
static void cli_pimu() {
209-
static uint32_t interrupt_cnt_last = 0;
210209
static uint32_t update_cnt_last = 0;
211210
static uint32_t ts_last = 0;
212-
uint32_t delta_int = imu.interrupt_cnt - interrupt_cnt_last;
213-
interrupt_cnt_last = imu.interrupt_cnt;
214211
uint32_t delta_upd = imu.update_cnt - update_cnt_last;
215212
update_cnt_last = imu.update_cnt;
216213
int miss_cnt = (int)imu.interrupt_cnt - imu.update_cnt;
@@ -224,12 +221,10 @@ static void cli_pimu() {
224221
Serial.printf("samp_hz:%d\t", hz);
225222

226223
if(dt == 0) dt = 1;
227-
//Serial.printf("intr_hz:%.0f\t", (float)delta_int/(dt*1e-6));
228224
Serial.printf("imu_loop_hz:%.0f\t", (float)delta_upd/(dt*1e-6));
229225

230226
int stat_cnt = 1;
231227
if(imu.stat_cnt > 0) stat_cnt = imu.stat_cnt;
232-
//Serial.printf("stat_cnt:%d\t", (int)(stat_cnt));
233228
Serial.printf("latency_us:%d\t", (int)(imu.stat_latency / stat_cnt));
234229
Serial.printf("rt_io_us:%d\t", (int)(imu.stat_io_runtime / stat_cnt));
235230
Serial.printf("rt_imu_loop_us:%d\t", (int)((imu.stat_runtime - imu.stat_io_runtime) / stat_cnt));
@@ -238,7 +233,6 @@ static void cli_pimu() {
238233
Serial.printf("rt_max_us:%d\t", (int)imu.stat_runtime_max);
239234
Serial.printf("rt_max%%:%d\t", (int)imu.stat_runtime_max * hz / 10000);
240235
Serial.printf("int_cnt:%d\t", (int)imu.interrupt_cnt);
241-
//Serial.printf("upd_cnt:%d\t", (int)imu.update_cnt);
242236
Serial.printf("miss_cnt:%d\t", (int)miss_cnt);
243237
imu.statReset();
244238
}
@@ -259,7 +253,6 @@ static void cli_pbar() {
259253
static void cli_pgps() {
260254
Serial.printf("gps.time:%d\t", (int)gps.time);
261255
Serial.printf("fix:%d\t", (int)gps.fix);
262-
//Serial.printf("date:%d\t", (int)gps.date);
263256
Serial.printf("sat:%d\t", (int)gps.sat);
264257
Serial.printf("lat:%d\t", (int)gps.lat);
265258
Serial.printf("lon:%d\t", (int)gps.lon);

src/hal/ESP32/hal_ESP32_cpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void hal_eeprom_begin();
7373
void startLoop1Task();
7474

7575
void hal_startup() {} // USB MSC not implemented for ESP32
76+
void hal_usb_setup() {}
7677
void hal_print_resources() {}
7778

7879
void hal_setup()

src/hal/RP2040/hal_RP2040_cpp.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ SPIClass *hal_spi[HAL_SPI_NUM] = {};
6666

6767
//prototypes
6868
void hal_eeprom_begin();
69+
void bbx_rp2_usb_detach(); //defined in bbx/BbxGizmoSdcard_RP2.cpp
6970
void bbx_rp2_usb_setup(); //defined in bbx/BbxGizmoSdcard_RP2.cpp
7071

7172
void hal_startup() {
73+
bbx_rp2_usb_detach();
7274
pio_registry_name_unclaimed("Arduino");
73-
bbx_rp2_usb_setup();
75+
}
76+
77+
void hal_usb_setup() {
78+
bbx_rp2_usb_setup();
7479
}
7580

7681
void hal_print_resources() {

src/hal/RP2040/pio_registry.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool pio_registry_claim(const char* name, const pio_program_t *program, PIO *pio
4141

4242
//name unnamed registry entries
4343
void pio_registry_name_unclaimed(const char* name) {
44-
for(int p = 0; p < NUM_PIOS; p++) {
44+
for(uint p = 0; p < NUM_PIOS; p++) {
4545
PIO pio = PIO_INSTANCE(p);
4646
for(uint sm = 0; sm < NUM_PIO_STATE_MACHINES; sm++) {
4747
if(pio_sm_is_claimed(pio, sm) && !pio_registry[p][sm][0]) {
@@ -53,7 +53,7 @@ void pio_registry_name_unclaimed(const char* name) {
5353

5454
//unclaim unnamed registry entries
5555
void pio_registry_unclaim_unnamed() {
56-
for(int p = 0; p < NUM_PIOS; p++) {
56+
for(uint p = 0; p < NUM_PIOS; p++) {
5757
PIO pio = PIO_INSTANCE(p);
5858
for(uint sm = 0; sm < NUM_PIO_STATE_MACHINES; sm++) {
5959
if(pio_sm_is_claimed(pio, sm) && !pio_registry[p][sm][0]) {
@@ -72,7 +72,7 @@ void pio_registry_register(PIO pio, uint sm, const char* name, int8_t len) {
7272
//show list of SM
7373
void pio_registry_print() {
7474
Serial.printf("\nPIO RESOURCES\n");
75-
for(int p = 0; p < NUM_PIOS; p++) {
75+
for(uint p = 0; p < NUM_PIOS; p++) {
7676
PIO pio = PIO_INSTANCE(p);
7777
for(uint sm = 0; sm < NUM_PIO_STATE_MACHINES; sm++) {
7878
int base = pio_get_gpio_base(pio);
@@ -88,9 +88,9 @@ void pio_registry_print() {
8888
Serial.printf("clkdiv=%7.2f ", pio->sm[sm].clkdiv / 65536.0);
8989
Serial.printf("execctrl=%08X ", pio->sm[sm].execctrl);
9090
Serial.printf("pinctrl=%08X ", pio->sm[sm].pinctrl);
91-
Serial.printf("gpio_out=%2d", base + (pio->sm[sm].pinctrl & 0x0000001f) >> 0);
91+
Serial.printf("gpio_out=%2d", base + ((pio->sm[sm].pinctrl & 0x0000001f) >> 0));
9292
Serial.printf("(cnt=%d) ", (pio->sm[sm].pinctrl & 0x03f00000) >> 20);
93-
Serial.printf("gpio_side=%2d", base + (pio->sm[sm].pinctrl & 0x000003e0) >> 5);
93+
Serial.printf("gpio_side=%2d", base + ((pio->sm[sm].pinctrl & 0x000003e0) >> 5));
9494
Serial.printf("(cnt=%d) ", (pio->sm[sm].pinctrl & 0x1c000000) >> 26);
9595
Serial.printf("wrap=%2d-", (pio->sm[sm].execctrl & 0x00000f80) >> 7);
9696
Serial.printf("%2d ", (pio->sm[sm].execctrl & 0x0001f000) >> 12);

src/hal/STM32/hal_STM32_cpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ SPIClass *hal_spi[HAL_SPI_NUM] = {};
5151
void hal_eeprom_begin();
5252

5353
void hal_startup() {} // USB MSC not implemented for STM32
54+
void hal_usb_setup() {}
5455
void hal_print_resources() {}
5556

5657
void hal_setup()

src/hal/hal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SOFTWARE.
3232

3333
//prototypes
3434
void hal_startup(); //call this to setup USB CDC/MSC before calling Serial.begin() - see bbx/BbxGizmoSdcard_RP2.cpp
35+
void hal_usb_setup();
3536
void hal_setup();
3637
void hal_eeprom_begin();
3738
uint8_t hal_eeprom_read(uint32_t adr);

src/madflight.h

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ const uint8_t Veh::flightmode_ap_ids[6] = VEH_FLIGHTMODE_AP_IDS; //mapping from
9999
const char* Veh::flightmode_names[6] = VEH_FLIGHTMODE_NAMES; //define flightmode name strings for telemetry
100100

101101
void madflight_setup() {
102-
hal_startup(); //setup USB CDC/MSC and do other things which have to be done first
103-
104-
Serial.begin(115200); //start console serial
105-
106-
// CFG - Configuration parameters (execute before delay to start LED)
102+
// HAL - Detach USB to until SDCARD is setup
103+
hal_startup();
104+
105+
// CFG - Configuration parameters (execute before delay to start LED + SDCARD)
107106
cfg.begin();
108107
#ifdef MF_CONFIG_CLEAR
109108
cfg.clear();
@@ -113,14 +112,14 @@ void madflight_setup() {
113112
cfg.loadFromEeprom(); //load parameters from EEPROM
114113
cfg.load_madflight(madflight_board, madflight_config); //load config
115114

116-
// LED - Setup LED (execute before delay)
115+
// LED - Setup LED (execute before delay to turn it on)
117116
led.config.gizmo = (Cfg::led_gizmo_enum)cfg.led_gizmo;
118117
led.config.pin = cfg.pin_led;
119118
led.setup();
120119
led.color(0x0000ff); //turn on blue to signal startup
121120
led.enabled = false; //do not change state until setup compled
122121

123-
// BBX - Black Box (execute before delay to start USB-MSC)
122+
// BBX - Black Box (execute before delay to start USB-MSC if card is inserted)
124123
bbx.config.gizmo = (Cfg::bbx_gizmo_enum)cfg.bbx_gizmo; //the gizmo to use
125124
bbx.config.spi_bus = hal_get_spi_bus(cfg.bbx_spi_bus); //SPI bus
126125
bbx.config.spi_cs = cfg.pin_bbx_cs; //SPI select pin
@@ -129,10 +128,16 @@ void madflight_setup() {
129128
bbx.config.pin_mmc_cmd = cfg.pin_mmc_cmd;
130129
bbx.setup();
131130

132-
// 6 second startup delay
131+
// USB - Start USB-CDC (Serial) and USB-MSC (if sdcard is inserted)
132+
hal_usb_setup();
133+
134+
// Serial - Start serial console
135+
Serial.begin(115200);
136+
137+
// Delay - 6 second startup delay
133138
for(int i = 12; i > 0; i--) {
134139
Serial.printf(MADFLIGHT_VERSION " starting %d ...\n", i);
135-
Serial.flush();
140+
Serial.flush();
136141
#ifndef MF_DEBUG
137142
delay(500);
138143
#else
@@ -161,12 +166,13 @@ void madflight_setup() {
161166
// HAL - Hardware abstraction layer setup: serial, spi, i2c (see hal.h)
162167
hal_setup();
163168

169+
// I2C - Show i2c devices
164170
cli.print_i2cScan(); //print i2c scan
165171

166172
// LED and BBX summary
167173
cfg.printModule("led");
168174
bbx.printSummary();
169-
175+
170176
// RCL - Radio Control Link
171177
rcl.config.gizmo = (Cfg::rcl_gizmo_enum)cfg.rcl_gizmo; //the gizmo to use
172178
rcl.config.ser_bus_id = cfg.rcl_ser_bus; //serial bus id
@@ -200,7 +206,7 @@ void madflight_setup() {
200206
bat.config.rshunt = cfg.bat_cal_i;
201207
bat.setup();
202208

203-
//RDR
209+
// RDR - Radar/Lidar/Sonar sensors
204210
rdr.config.gizmo = (Cfg::rdr_gizmo_enum)cfg.rdr_gizmo; //the gizmo to use
205211
rdr.config.rdr_ser_bus = cfg.rdr_ser_bus; //serial bus
206212
rdr.config.rdr_baud = cfg.rdr_baud; //baud rate
@@ -210,7 +216,7 @@ void madflight_setup() {
210216
rdr.config.rdr_i2c_adr = cfg.rdr_i2c_adr;
211217
rdr.setup();
212218

213-
//OFL
219+
// OFL - Optical flow sensor
214220
ofl.config.ofl_gizmo = (Cfg::ofl_gizmo_enum)cfg.ofl_gizmo; //the gizmo to use
215221
ofl.config.ofl_spi_bus = cfg.ofl_spi_bus; // spi bus
216222
ofl.config.pin_ofl_cs = cfg.pin_ofl_cs; // spi cs pin
@@ -257,7 +263,7 @@ void madflight_setup() {
257263
imu.config.uses_i2c = ((Cfg::imu_bus_type_enum)cfg.imu_bus_type == Cfg::imu_bus_type_enum::mf_I2C);
258264
imu.config.pin_clkin = cfg.pin_imu_clkin; //CLKIN pin for ICM-42866-P - only tested for RP2 targets
259265

260-
// Some sensors need a couple of tries...
266+
// Some IMU sensors need a couple of tries...
261267
int tries = 10;
262268
while(true) {
263269
int rv = imu.setup(); //request 1000 Hz sample rate, returns 0 on success, positive on error, negative on warning
@@ -268,7 +274,7 @@ void madflight_setup() {
268274
if(!imu.installed() && (Cfg::imu_gizmo_enum)cfg.imu_gizmo != Cfg::imu_gizmo_enum::mf_NONE) {
269275
madflight_die("IMU install failed.");
270276
}
271-
// start IMU update handler
277+
// Start IMU update handler
272278
if(imu.installed()) {
273279
ahr.setInitalOrientation(); //do this before IMU update handler is started
274280

@@ -277,12 +283,12 @@ void madflight_setup() {
277283
if(!imu.waitNewSample()) madflight_die("IMU interrupt not firing. Is pin 'pin_imu_int' connected?");
278284

279285
#ifndef MF_DEBUG
280-
// switch off LED to signal calibration
286+
// Switch off LED to signal calibration
281287
led.enabled = true;
282288
led.off();
283289
led.enabled = false;
284290

285-
//Calibrate for zero gyro readings, assuming vehicle not moving when powered up. Comment out to only use cfg values. (Use CLI to calibrate acc.)
291+
// Calibrate for zero gyro readings, assuming vehicle not moving when powered up. Comment out to only use cfg values. (Use CLI to calibrate acc.)
286292
cli.calibrate_gyro();
287293
#endif
288294
}
@@ -293,7 +299,7 @@ void madflight_setup() {
293299
// CLI - Command Line Interface
294300
cli.begin();
295301

296-
// Enable LED, and switch it to green to signal end of startup.
302+
// LED - Enable and switch it to green to signal end of startup.
297303
led.enabled = true;
298304
led.color(0x00ff00); //switch color to green
299305
led.on();

src/mag/MagGizmoMMC5603.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class MagGizmoMMC5603 : public MagGizmo {
6262
}
6363

6464
bool update_raw() {
65-
if(dev->readReg(0x18) & 0x80 == 0x00) return false; //exit if no new data
65+
if((dev->readReg(0x18) & 0x80) == 0x00) return false; //exit if no new data
6666

6767
uint8_t d[9];
6868
dev->readReg(0x00, d, 9);

0 commit comments

Comments
 (0)