Skip to content

Commit 271d1e5

Browse files
committed
USB: port to latest tinyusb
1 parent 9a63d63 commit 271d1e5

File tree

7 files changed

+50
-41
lines changed

7 files changed

+50
-41
lines changed

cores/arduino/usb/USB.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,17 @@ void _usbhs_interrupt_handler(void)
281281

282282
#if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST
283283
tuh_int_handler(1);
284+
tuh_task();
284285
#endif
285286

286287
#if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE
287288
tud_int_handler(1);
288-
#endif
289289
tud_task();
290+
#endif
290291
}
291292

292293
extern "C" {
293-
void tud_set_irq_usbfs(IRQn_Type q);
294-
void tud_set_irq_usbhs(IRQn_Type q);
294+
void tusb_rusb2_set_irqnum(uint8_t rhport, int32_t irqnum);
295295
}
296296

297297
__attribute__((weak)) void configure_usb_mux() {}
@@ -324,7 +324,11 @@ void __USBStart() {
324324
usb_irq_cfg.address_of_handler = (uint32_t)_usbfs_interrupt_handler;
325325
usb_irq_cfg.first_irq_number = FSP_INVALID_VECTOR;
326326
IRQManager::getInstance().addPeripheral(IRQ_USB,(void*)&usb_irq_cfg);
327-
tud_set_irq_usbfs((IRQn_Type)(usb_irq_cfg.first_irq_number));
327+
#if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE
328+
tusb_rusb2_set_irqnum(BOARD_TUD_RHPORT, usb_irq_cfg.first_irq_number);
329+
#else
330+
tusb_rusb2_set_irqnum(BOARD_TUH_RHPORT, usb_irq_cfg.first_irq_number);
331+
#endif
328332
#endif
329333
#endif
330334

@@ -334,7 +338,11 @@ void __USBStart() {
334338
usb_irq_cfg.address_of_handler = (uint32_t)_usbhs_interrupt_handler;
335339
usb_irq_cfg.first_irq_number = FSP_INVALID_VECTOR;
336340
IRQManager::getInstance().addPeripheral(IRQ_USB_HS,(void*)&usb_irq_cfg);
337-
tud_set_irq_usbhs((IRQn_Type)(usb_irq_cfg.first_irq_number));
341+
#if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE
342+
tusb_rusb2_set_irqnum(BOARD_TUD_RHPORT, usb_irq_cfg.first_irq_number);
343+
#else
344+
tusb_rusb2_set_irqnum(BOARD_TUH_RHPORT, usb_irq_cfg.first_irq_number);
345+
#endif
338346
#endif
339347
#endif
340348

@@ -353,6 +361,10 @@ void __USBStart() {
353361
/* init device port*/
354362
tud_init(BOARD_TUD_RHPORT);
355363

364+
#ifdef VUSB_LDO_ENABLE
365+
((R_USB_FS0_Type*)R_USB_FS0_BASE)->USBMC_b.VDCEN = 1;
366+
#endif
367+
356368
#if 0 //defined(AZURE_RTOS_THREADX)
357369
static TX_BYTE_POOL byte_pool_0;
358370
static TX_THREAD thread;

extras/tinyusb

Submodule tinyusb updated 517 files

libraries/UsbHostMsd/UsbHostMsd.cpp

+8-20
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,10 @@ int USBHostMSD::write(const void *buffer, bd_addr_t addr, bd_size_t size) {
113113
block_number = addr / block_size;
114114
count = size / block_size;
115115

116-
for (uint32_t b = block_number; b < block_number + count; b++) {
117-
in_progress = true;
118-
if(tuh_msc_write10(usb_host_msd_get_device_address(), get_lun(), buf, b, 1, complete_cb, 0)) {
119-
while(in_progress) {
120-
delay(10);
121-
}
122-
buf += block_size;
123-
}
124-
else {
125-
return -1;
116+
in_progress = true;
117+
if(tuh_msc_write10(usb_host_msd_get_device_address(), get_lun(), buf, block_number, count, complete_cb, 0)) {
118+
while (in_progress) {
119+
tuh_task();
126120
}
127121
}
128122
return 0;
@@ -145,16 +139,10 @@ int USBHostMSD::read(void *buffer, bd_addr_t addr, bd_size_t size)
145139
block_number = addr / block_size;
146140
count = size / block_size;
147141

148-
for (uint32_t b = block_number; b < block_number + count; b++) {
149-
in_progress = true;
150-
if(tuh_msc_read10(usb_host_msd_get_device_address(), get_lun(), buf, b, 1, complete_cb, 0)) {
151-
while(in_progress) {
152-
delay(10);
153-
}
154-
buf += block_size;
155-
}
156-
else {
157-
return -1;
142+
in_progress = true;
143+
if(tuh_msc_read10(usb_host_msd_get_device_address(), get_lun(), buf, block_number, count, complete_cb, 0)) {
144+
while (in_progress) {
145+
tuh_task();
158146
}
159147
}
160148
return 0;

libraries/UsbHostMsd/tu_msc.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -78,39 +78,41 @@ void usb_host_msd_attach_mnt_cbk(void (*fnc)(void)) {
7878
//--------------------------------------------------------------------+
7979
static scsi_inquiry_resp_t inquiry_resp;
8080

81-
bool inquiry_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
81+
bool inquiry_complete_cb(uint8_t dev_addr, tuh_msc_complete_data_t const * cb_data)
8282
{
83+
msc_cbw_t const* cbw = cb_data->cbw;
84+
msc_csw_t const* csw = cb_data->csw;
85+
8386
if (csw->status != 0)
8487
{
85-
printf("Inquiry failed\r\n");
88+
TU_LOG2("Inquiry failed\r\n");
8689
return false;
8790
}
8891

8992
// Print out Vendor ID, Product ID and Rev
90-
printf("%.8s %.16s rev %.4s\r\n", inquiry_resp.vendor_id, inquiry_resp.product_id, inquiry_resp.product_rev);
93+
TU_LOG2("%.8s %.16s rev %.4s\r\n", inquiry_resp.vendor_id, inquiry_resp.product_id, inquiry_resp.product_rev);
9194

9295
// Get capacity of device
9396
uint32_t const block_count = tuh_msc_get_block_count(dev_addr, cbw->lun);
9497
uint32_t const block_size = tuh_msc_get_block_size(dev_addr, cbw->lun);
9598

96-
printf("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size));
97-
printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size);
99+
TU_LOG2("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size));
100+
TU_LOG2("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size);
98101

99102
return true;
100103
}
101104

102105
//------------- IMPLEMENTATION -------------//
103106
void tuh_msc_mount_cb(uint8_t dev_addr) {
104-
#ifdef USB_DEBUG
105-
mylogadd("[EVENT]: Mass Storage Device - MOUNT -> device address %i", dev_addr);
106-
#endif
107+
TU_LOG2("[EVENT]: Mass Storage Device - MOUNT -> device address %i", dev_addr);
108+
109+
uint8_t const lun = 0;
110+
tuh_msc_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb, 0);
107111

108112
max_lun = tuh_msc_get_maxlun(dev_addr);
109113

110-
#ifdef USB_DEBUG
111-
mylogadd(" : Max LUN %i", dev_addr);
112-
#endif
113-
114+
TU_LOG2(" : Max LUN %i", dev_addr);
115+
114116
if(max_lun > 0) {
115117
if(usb_msc_device != NULL) {
116118
free(usb_msc_device);
@@ -157,9 +159,7 @@ void tuh_msc_mount_cb(uint8_t dev_addr) {
157159

158160
void tuh_msc_umount_cb(uint8_t dev_addr) {
159161
(void) dev_addr;
160-
#ifdef USB_DEBUG
161-
mylogadd("[CALL]: tuh_msc_umount_cb %i --------- UMOUNT", dev_addr);
162-
#endif
162+
TU_LOG2("[CALL]: tuh_msc_umount_cb %i --------- UMOUNT", dev_addr);
163163
if(usb_msc_device != NULL) {
164164
free(usb_msc_device);
165165
usb_msc_device = NULL;

variants/MINIMA/pins_arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ static const uint8_t SS = PIN_SPI_CS;
157157
#define USB_PID (0x0069)
158158
#define USB_NAME "UNO R4 Minima"
159159

160+
#define VUSB_LDO_ENABLE 1
161+
160162
/* EEPROM DEFINES */
161163

162164
#define ARDUINO_FLASH_TYPE LP_FLASH

variants/PORTENTA_C33/tusb_config.h

+5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@
102102
#define CFG_TUD_DFU_RUNTIME 1
103103

104104
#define CFG_TUH_MSC 1
105+
#define CFG_TUH_HUB 1
106+
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
107+
#define CFG_TUH_ENDPOINT_MAX 8
108+
#define CFG_TUH_API_EDPT_XFER 1
109+
105110

106111
// CDC FIFO size of TX and RX
107112
#define CFG_TUD_CDC_RX_BUFSIZE ((TUD_OPT_HIGH_SPEED ? 512 : 64) * 8)

variants/UNOWIFIR4/pins_arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ static const uint8_t SS = PIN_SPI_CS;
159159
#define USB_PID (0x006D)
160160
#define USB_NAME "UNO R4 WiFi"
161161

162+
#define VUSB_LDO_ENABLE 1
163+
162164
/* EEPROM DEFINES */
163165

164166
#define ARDUINO_FLASH_TYPE LP_FLASH

0 commit comments

Comments
 (0)