Skip to content

Commit 90cfe53

Browse files
committed
Various device code optimizations
Added `end()` methods to MSC classes Made begin() methods safe to be called multiple times Optimized CDC class
1 parent 275e6ff commit 90cfe53

File tree

10 files changed

+91
-131
lines changed

10 files changed

+91
-131
lines changed

cores/esp32/FirmwareMSC.cpp

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ static bool msc_update_setup_disk(const char * volume_label, uint32_t serial_num
140140
return true;
141141
}
142142

143+
static void msc_update_delete_disk(){
144+
fw_entry = NULL;
145+
fw_size = 0;
146+
fw_end_sector = 0;
147+
fw_start_sector = 0;
148+
msc_table = NULL;
149+
msc_boot = NULL;
150+
msc_table_sectors = 0;
151+
msc_total_sectors = 0;
152+
msc_run_partition = NULL;
153+
msc_ota_partition = NULL;
154+
msc_update_state = MSC_UPDATE_IDLE;
155+
msc_update_start_sector = 0;
156+
msc_update_bytes_written = 0;
157+
msc_update_entry = NULL;
158+
free(msc_ram_disk);
159+
msc_ram_disk = NULL;
160+
}
161+
143162
//filter out entries to only include BINs in the root folder
144163
static fat_dir_entry_t * msc_update_get_root_bin_entry(uint8_t index){
145164
fat_dir_entry_t * entry = (fat_dir_entry_t *)(msc_ram_disk + ((msc_boot->sectors_per_alloc_table+1) * DISK_SECTOR_SIZE) + (index * sizeof(fat_dir_entry_t)));
@@ -336,31 +355,38 @@ static bool msc_start_stop(uint8_t power_condition, bool start, bool load_eject)
336355

337356
static volatile TaskHandle_t msc_task_handle = NULL;
338357
static void msc_task(void *pvParameters){
339-
for (;;) {
340-
if(msc_update_state == MSC_UPDATE_END){
341-
delay(100);
342-
esp_restart();
343-
}
358+
for (;;) {
359+
if(msc_update_state == MSC_UPDATE_END){
344360
delay(100);
361+
esp_restart();
345362
}
346-
msc_task_handle = NULL;
347-
vTaskDelete(NULL);
363+
delay(100);
364+
}
365+
msc_task_handle = NULL;
366+
vTaskDelete(NULL);
348367
}
349368

350369
FirmwareMSC::FirmwareMSC():msc(){}
351370

352-
FirmwareMSC::~FirmwareMSC(){}
371+
FirmwareMSC::~FirmwareMSC(){
372+
end();
373+
}
353374

354375
bool FirmwareMSC::begin(){
376+
if(msc_ram_disk){
377+
return true;
378+
}
379+
355380
if(!msc_update_setup_disk(USB_FW_MSC_VOLUME_NAME, USB_FW_MSC_SERIAL_NUMBER)){
356381
return false;
357382
}
358383

359384
if(!msc_task_handle){
360-
xTaskCreateUniversal(msc_task, "msc_disk", 1024, NULL, 2, (TaskHandle_t*)&msc_task_handle, 0);
361-
if(!msc_task_handle){
362-
return false;
363-
}
385+
xTaskCreateUniversal(msc_task, "msc_disk", 1024, NULL, 2, (TaskHandle_t*)&msc_task_handle, 0);
386+
if(!msc_task_handle){
387+
msc_update_delete_disk();
388+
return false;
389+
}
364390
}
365391

366392
msc.vendorID(USB_FW_MSC_VENDOR_ID);
@@ -374,6 +400,15 @@ bool FirmwareMSC::begin(){
374400
return true;
375401
}
376402

403+
void FirmwareMSC::end(){
404+
msc.end();
405+
if(msc_task_handle){
406+
vTaskDelete(msc_task_handle);
407+
msc_task_handle = NULL;
408+
}
409+
msc_update_delete_disk();
410+
}
411+
377412
void FirmwareMSC::onEvent(esp_event_handler_t callback){
378413
onEvent(ARDUINO_FIRMWARE_MSC_ANY_EVENT, callback);
379414
}

cores/esp32/FirmwareMSC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class FirmwareMSC {
5757
FirmwareMSC();
5858
~FirmwareMSC();
5959
bool begin();
60+
void end();
6061
void onEvent(esp_event_handler_t callback);
6162
void onEvent(arduino_firmware_msc_event_t event, esp_event_handler_t callback);
6263
};

cores/esp32/USBCDC.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ USBCDC * devices[MAX_USB_CDC_DEVICES] = {NULL, NULL};
2828
static uint16_t load_cdc_descriptor(uint8_t * dst, uint8_t * itf)
2929
{
3030
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB CDC");
31-
// Interface number, string index, attributes, detach timeout, transfer size */
3231
uint8_t descriptor[TUD_CDC_DESC_LEN] = {
3332
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
3433
TUD_CDC_DESCRIPTOR(*itf, str_index, 0x85, 64, 0x03, 0x84, 64)
@@ -41,7 +40,6 @@ static uint16_t load_cdc_descriptor(uint8_t * dst, uint8_t * itf)
4140
// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE
4241
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
4342
{
44-
//isr_log_v("itf: %u, dtr: %u, rts: %u", itf, dtr, rts);
4543
if(itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL){
4644
devices[itf]->_onLineState(dtr, rts);
4745
}
@@ -50,7 +48,6 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
5048
// Invoked when line coding is change via SET_LINE_CODING
5149
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding)
5250
{
53-
//isr_log_v("itf: %u, bit_rate: %u, data_bits: %u, stop_bits: %u, parity: %u", itf, p_line_coding->bit_rate, p_line_coding->data_bits, p_line_coding->stop_bits, p_line_coding->parity);
5451
if(itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL){
5552
devices[itf]->_onLineCoding(p_line_coding->bit_rate, p_line_coding->stop_bits, p_line_coding->parity, p_line_coding->data_bits);
5653
}
@@ -59,7 +56,6 @@ void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding)
5956
// Invoked when received new data
6057
void tud_cdc_rx_cb(uint8_t itf)
6158
{
62-
//isr_log_v("itf: %u", itf);
6359
if(itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL){
6460
devices[itf]->_onRX();
6561
}
@@ -72,15 +68,14 @@ void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms){
7268

7369
// Invoked when space becomes available in TX buffer
7470
void tud_cdc_tx_complete_cb(uint8_t itf){
75-
//isr_log_v("itf: %u", itf);
76-
if(itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL){
71+
if(itf < MAX_USB_CDC_DEVICES && devices[itf] != NULL && devices[itf]->tx_sem != NULL){
7772
xSemaphoreGive(devices[itf]->tx_sem);
7873
devices[itf]->_onTX();
7974
}
8075
}
8176

8277
static size_t tinyusb_cdc_write(uint8_t itf, const uint8_t *buffer, size_t size){
83-
if(itf >= MAX_USB_CDC_DEVICES){
78+
if(itf >= MAX_USB_CDC_DEVICES || devices[itf] == NULL || devices[itf]->tx_sem == NULL){
8479
return 0;
8580
}
8681
if(!tud_cdc_n_connected(itf)){
@@ -113,21 +108,21 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c)
113108
tinyusb_cdc_write(0, (const uint8_t *)&c, 1);
114109
}
115110

116-
//void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
117-
118111
static void usb_unplugged_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
119112
((USBCDC*)arg)->_onUnplugged();
120113
}
121114

122-
USBCDC::USBCDC(uint8_t itfn) : itf(itfn), bit_rate(0), stop_bits(0), parity(0), data_bits(0), dtr(false), rts(false), connected(false), reboot_enable(true), rx_queue(NULL) {
115+
USBCDC::USBCDC(uint8_t itfn) : itf(itfn), bit_rate(0), stop_bits(0), parity(0), data_bits(0), dtr(false), rts(false), connected(false), reboot_enable(true), rx_queue(NULL), tx_sem(NULL) {
123116
tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
124117
if(itf < MAX_USB_CDC_DEVICES){
125-
devices[itf] = this;
126-
tx_sem = NULL;
127118
arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this);
128119
}
129120
}
130121

122+
USBCDC::~USBCDC(){
123+
end();
124+
}
125+
131126
void USBCDC::onEvent(esp_event_handler_t callback){
132127
onEvent(ARDUINO_USB_CDC_ANY_EVENT, callback);
133128
}
@@ -137,6 +132,10 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback
137132

138133
size_t USBCDC::setRxBufferSize(size_t rx_queue_len){
139134
if(rx_queue){
135+
if(!rx_queue_len){
136+
vQueueDelete(rx_queue);
137+
rx_queue = NULL;
138+
}
140139
return 0;
141140
}
142141
rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
@@ -148,15 +147,19 @@ size_t USBCDC::setRxBufferSize(size_t rx_queue_len){
148147

149148
void USBCDC::begin(unsigned long baud)
150149
{
151-
setRxBufferSize(256);//default if not preset
152150
if(tx_sem == NULL){
153151
tx_sem = xSemaphoreCreateBinary();
154152
xSemaphoreTake(tx_sem, 0);
155153
}
154+
setRxBufferSize(256);//default if not preset
155+
devices[itf] = this;
156156
}
157157

158158
void USBCDC::end()
159159
{
160+
connected = false;
161+
devices[itf] = NULL;
162+
setRxBufferSize(0);
160163
if (tx_sem != NULL) {
161164
vSemaphoreDelete(tx_sem);
162165
tx_sem = NULL;
@@ -317,15 +320,15 @@ size_t USBCDC::read(uint8_t *buffer, size_t size)
317320

318321
void USBCDC::flush(void)
319322
{
320-
if(itf >= MAX_USB_CDC_DEVICES){
323+
if(itf >= MAX_USB_CDC_DEVICES || tx_sem == NULL){
321324
return;
322325
}
323326
tud_cdc_n_write_flush(itf);
324327
}
325328

326329
int USBCDC::availableForWrite(void)
327330
{
328-
if(itf >= MAX_USB_CDC_DEVICES){
331+
if(itf >= MAX_USB_CDC_DEVICES || tx_sem == NULL){
329332
return -1;
330333
}
331334
return tud_cdc_n_write_available(itf);

cores/esp32/USBCDC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class USBCDC: public Stream
5454
{
5555
public:
5656
USBCDC(uint8_t itf=0);
57+
~USBCDC();
5758

5859
void onEvent(esp_event_handler_t callback);
5960
void onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback);

cores/esp32/USBMSC.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ USBMSC::USBMSC(){
204204
}
205205
}
206206

207+
USBMSC::~USBMSC(){
208+
end();
209+
}
210+
207211
bool USBMSC::begin(uint32_t block_count, uint16_t block_size){
208212
msc_luns[_lun].block_size = block_size;
209213
msc_luns[_lun].block_count = block_count;
@@ -213,6 +217,18 @@ bool USBMSC::begin(uint32_t block_count, uint16_t block_size){
213217
return true;
214218
}
215219

220+
void USBMSC::end(){
221+
msc_luns[_lun].media_present = false;
222+
msc_luns[_lun].vendor_id[0] = 0;
223+
msc_luns[_lun].product_id[0] = 0;
224+
msc_luns[_lun].product_rev[0] = 0;
225+
msc_luns[_lun].block_size = 0;
226+
msc_luns[_lun].block_count = 0;
227+
msc_luns[_lun].start_stop = NULL;
228+
msc_luns[_lun].read = NULL;
229+
msc_luns[_lun].write = NULL;
230+
}
231+
216232
void USBMSC::vendorID(const char * vid){
217233
cplstr(msc_luns[_lun].vendor_id, vid, 8);
218234
}

cores/esp32/USBMSC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class USBMSC
3333
{
3434
public:
3535
USBMSC();
36+
~USBMSC();
3637
bool begin(uint32_t block_count, uint16_t block_size);
38+
void end();
3739
void vendorID(const char * vid);//max 8 chars
3840
void productID(const char * pid);//max 16 chars
3941
void productRevision(const char * ver);//max 4 chars

cores/esp32/esp32-hal-tinyusb.c

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -692,84 +692,4 @@ uint8_t tinyusb_get_free_out_endpoint(void){
692692
return 0;
693693
}
694694

695-
/*
696-
void usb_dw_reg_dump(void)
697-
{
698-
#define USB_PRINT_REG(r) printf("USB0." #r " = 0x%x;\n", USB0.r)
699-
#define USB_PRINT_IREG(i, r) printf("USB0.in_ep_reg[%u]." #r " = 0x%x;\n", i, USB0.in_ep_reg[i].r)
700-
#define USB_PRINT_OREG(i, r) printf("USB0.out_ep_reg[%u]." #r " = 0x%x;\n", i, USB0.out_ep_reg[i].r)
701-
uint8_t i;
702-
USB_PRINT_REG(gotgctl);
703-
USB_PRINT_REG(gotgint);
704-
USB_PRINT_REG(gahbcfg);
705-
USB_PRINT_REG(gusbcfg);
706-
USB_PRINT_REG(grstctl);
707-
USB_PRINT_REG(gintsts);
708-
USB_PRINT_REG(gintmsk);
709-
USB_PRINT_REG(grxstsr);
710-
USB_PRINT_REG(grxstsp);
711-
USB_PRINT_REG(grxfsiz);
712-
USB_PRINT_REG(gnptxsts);
713-
USB_PRINT_REG(gpvndctl);
714-
USB_PRINT_REG(ggpio);
715-
USB_PRINT_REG(guid);
716-
USB_PRINT_REG(gsnpsid);
717-
USB_PRINT_REG(ghwcfg1);
718-
USB_PRINT_REG(ghwcfg2);
719-
USB_PRINT_REG(ghwcfg3);
720-
USB_PRINT_REG(ghwcfg4);
721-
USB_PRINT_REG(glpmcfg);
722-
USB_PRINT_REG(gpwrdn);
723-
USB_PRINT_REG(gdfifocfg);
724-
USB_PRINT_REG(gadpctl);
725-
USB_PRINT_REG(hptxfsiz);
726-
USB_PRINT_REG(hcfg);
727-
USB_PRINT_REG(hfir);
728-
USB_PRINT_REG(hfnum);
729-
USB_PRINT_REG(hptxsts);
730-
USB_PRINT_REG(haint);
731-
USB_PRINT_REG(haintmsk);
732-
USB_PRINT_REG(hflbaddr);
733-
USB_PRINT_REG(hprt);
734-
USB_PRINT_REG(dcfg);
735-
USB_PRINT_REG(dctl);
736-
USB_PRINT_REG(dsts);
737-
USB_PRINT_REG(diepmsk);
738-
USB_PRINT_REG(doepmsk);
739-
USB_PRINT_REG(daint);
740-
USB_PRINT_REG(daintmsk);
741-
USB_PRINT_REG(dtknqr1);
742-
USB_PRINT_REG(dtknqr2);
743-
USB_PRINT_REG(dvbusdis);
744-
USB_PRINT_REG(dvbuspulse);
745-
USB_PRINT_REG(dtknqr3_dthrctl);
746-
USB_PRINT_REG(dtknqr4_fifoemptymsk);
747-
USB_PRINT_REG(deachint);
748-
USB_PRINT_REG(deachintmsk);
749-
USB_PRINT_REG(pcgctrl);
750-
USB_PRINT_REG(pcgctrl1);
751-
USB_PRINT_REG(gnptxfsiz);
752-
for (i = 0; i < 4; i++) {
753-
printf("USB0.dieptxf[%u] = 0x%x;\n", i, USB0.dieptxf[i]);
754-
}
755-
// for (i = 0; i < 16; i++) {
756-
// printf("USB0.diepeachintmsk[%u] = 0x%x;\n", i, USB0.diepeachintmsk[i]);
757-
// }
758-
// for (i = 0; i < 16; i++) {
759-
// printf("USB0.doepeachintmsk[%u] = 0x%x;\n", i, USB0.doepeachintmsk[i]);
760-
// }
761-
for (i = 0; i < 7; i++) {
762-
printf("// EP %u:\n", i);
763-
USB_PRINT_IREG(i, diepctl);
764-
USB_PRINT_IREG(i, diepint);
765-
USB_PRINT_IREG(i, dieptsiz);
766-
USB_PRINT_IREG(i, diepdma);
767-
USB_PRINT_IREG(i, dtxfsts);
768-
USB_PRINT_OREG(i, doepctl);
769-
USB_PRINT_OREG(i, doepint);
770-
USB_PRINT_OREG(i, doeptsiz);
771-
USB_PRINT_OREG(i, doepdma);
772-
}
773-
}
774-
*/
775695
#endif /* CONFIG_TINYUSB_ENABLED */

libraries/USB/examples/FirmwareMSC/FirmwareMSC.ino

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#if !ARDUINO_USB_MSC_ON_BOOT
55
FirmwareMSC MSC_Update;
66
#endif
7-
87
#if ARDUINO_USB_CDC_ON_BOOT
98
#define HWSerial Serial0
109
#define USBSerial Serial
@@ -65,15 +64,9 @@ void setup() {
6564

6665
USB.onEvent(usbEventCallback);
6766
MSC_Update.onEvent(usbEventCallback);
68-
#if !ARDUINO_USB_MSC_ON_BOOT //Provide Firmware MSC
69-
MSC_Update.begin();
70-
#endif
71-
#if !ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
72-
USBSerial.begin();
73-
#endif
74-
#if !ARDUINO_USB_ON_BOOT
75-
USB.begin();
76-
#endif
67+
MSC_Update.begin();
68+
USBSerial.begin();
69+
USB.begin();
7770
}
7871

7972
void loop() {

0 commit comments

Comments
 (0)