Skip to content

Commit 6905192

Browse files
committed
update tinyusb, config and add rp2040 driver for max3421e as host
1 parent e291865 commit 6905192

File tree

5 files changed

+46
-29
lines changed

5 files changed

+46
-29
lines changed

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ TU_ATTR_WEAK void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance,
186186
!defined(PLATFORMIO)
187187
extern "C" void hcd_int_handler_esp32(uint8_t rhport, bool in_isr);
188188
#define tuh_int_handler_esp32 hcd_int_handler_esp32
189+
189190
#else
190191
#define tuh_int_handler_esp32 tuh_int_handler
192+
191193
#endif
192194

193195
static void max3421_intr_task(void *param) {
@@ -321,8 +323,13 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
321323
} else {
322324
gpio_intr_disable((gpio_num_t)host->_intr);
323325
}
326+
327+
#elif defined(ARDUINO_ARCH_RP2040)
328+
//--- RP2040 ---//
329+
irq_set_enabled(IO_IRQ_BANK0, enabled);
330+
324331
#else
325-
#error "MAX3421e host is not Unsupported by this architecture"
332+
#error "MAX3421e host is not supported by this architecture"
326333
#endif
327334
}
328335

src/arduino/ports/rp2040/tusb_config_rp2040.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,23 @@ extern "C" {
3838
#define CFG_TUD_ENABLED 0
3939
#define CFG_TUH_ENABLED 1
4040
#define CFG_TUH_RPI_PIO_USB 0
41+
4142
#else
4243
// native as device
4344
#define CFG_TUD_ENABLED 1
4445

45-
// Enable host stack with pio-usb if Pico-PIO-USB library is available
4646
#if __has_include("pio_usb.h")
47+
// Enable host stack with pio-usb if Pico-PIO-USB library is available
4748
#define CFG_TUH_ENABLED 1
4849
#define CFG_TUH_RPI_PIO_USB 1
49-
#endif
50-
#endif
50+
51+
#else
52+
// Otherwise enable host controller with MAX3421E
53+
#define CFG_TUH_ENABLED 1
54+
#define CFG_TUH_MAX3421 1
55+
56+
#endif // pio_usb.h
57+
#endif // USE_TINYUSB_HOST
5158

5259
#ifndef CFG_TUSB_MCU
5360
#define CFG_TUSB_MCU OPT_MCU_RP2040

src/portable/analog/max3421/hcd_max3421.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ enum {
176176
//--------------------------------------------------------------------+
177177

178178
typedef struct {
179+
uint8_t daddr;
180+
179181
struct TU_ATTR_PACKED {
180182
uint8_t ep_dir : 1;
181183
uint8_t is_iso : 1;
@@ -184,17 +186,19 @@ typedef struct {
184186
uint8_t xfer_pending : 1;
185187
uint8_t xfer_complete : 1;
186188
};
189+
187190
struct TU_ATTR_PACKED {
188-
uint8_t daddr : 4;
189191
uint8_t ep_num : 4;
192+
uint16_t packet_size : 12;
190193
};
191194

192-
uint16_t packet_size;
193195
uint16_t total_len;
194196
uint16_t xferred_len;
195197
uint8_t* buf;
196198
} max3421_ep_t;
197199

200+
TU_VERIFY_STATIC(sizeof(max3421_ep_t) == 12, "size is not correct");
201+
198202
typedef struct {
199203
// cached register
200204
uint8_t sndbc;
@@ -325,7 +329,7 @@ static void fifo_read(uint8_t rhport, uint8_t * buffer, uint16_t len, bool in_is
325329
static inline void hirq_write(uint8_t rhport, uint8_t data, bool in_isr) {
326330
reg_write(rhport, HIRQ_ADDR, data, in_isr);
327331
// HIRQ write 1 is clear
328-
_hcd_data.hirq &= ~data;
332+
_hcd_data.hirq &= (uint8_t) ~data;
329333
}
330334

331335
static inline void hien_write(uint8_t rhport, uint8_t data, bool in_isr) {
@@ -395,13 +399,13 @@ static void free_ep(uint8_t daddr) {
395399
}
396400

397401
static max3421_ep_t * find_next_pending_ep(max3421_ep_t * cur_ep) {
398-
size_t const idx = cur_ep - _hcd_data.ep;
402+
size_t const idx = (size_t) (cur_ep - _hcd_data.ep);
399403

400404
// starting from next endpoint
401405
for (size_t i = idx + 1; i < CFG_TUH_MAX3421_ENDPOINT_TOTAL; i++) {
402406
max3421_ep_t* ep = &_hcd_data.ep[i];
403407
if (ep->xfer_pending && ep->packet_size) {
404-
// TU_LOG3("next pending i = %u\n", i);
408+
// TU_LOG3("next pending i = %u\r\n", i);
405409
return ep;
406410
}
407411
}
@@ -410,7 +414,7 @@ static max3421_ep_t * find_next_pending_ep(max3421_ep_t * cur_ep) {
410414
for (size_t i = 0; i <= idx; i++) {
411415
max3421_ep_t* ep = &_hcd_data.ep[i];
412416
if (ep->xfer_pending && ep->packet_size) {
413-
// TU_LOG3("next pending i = %u\n", i);
417+
// TU_LOG3("next pending i = %u\r\n", i);
414418
return ep;
415419
}
416420
}
@@ -542,8 +546,8 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * e
542546
(void) rhport;
543547
(void) daddr;
544548

545-
uint8_t ep_num = tu_edpt_number(ep_desc->bEndpointAddress);
546-
uint8_t ep_dir = tu_edpt_dir(ep_desc->bEndpointAddress);
549+
uint8_t const ep_num = tu_edpt_number(ep_desc->bEndpointAddress);
550+
tusb_dir_t const ep_dir = tu_edpt_dir(ep_desc->bEndpointAddress);
547551

548552
max3421_ep_t * ep;
549553
if (daddr == 0 && ep_num == 0) {
@@ -552,15 +556,15 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t daddr, tusb_desc_endpoint_t const * e
552556
ep = allocate_ep();
553557
TU_ASSERT(ep);
554558
ep->daddr = daddr;
555-
ep->ep_num = ep_num;
556-
ep->ep_dir = ep_dir;
559+
ep->ep_num = (uint8_t) (ep_num & 0x0f);
560+
ep->ep_dir = (ep_dir == TUSB_DIR_IN) ? 1 : 0;
557561
}
558562

559563
if ( TUSB_XFER_ISOCHRONOUS == ep_desc->bmAttributes.xfer ) {
560564
ep->is_iso = 1;
561565
}
562566

563-
ep->packet_size = tu_edpt_packet_size(ep_desc);
567+
ep->packet_size = (uint16_t) (tu_edpt_packet_size(ep_desc) & 0x7ff);
564568

565569
return true;
566570
}
@@ -582,7 +586,7 @@ void xact_out(uint8_t rhport, max3421_ep_t *ep, bool switch_ep, bool in_isr) {
582586
}
583587
sndbc_write(rhport, xact_len, in_isr);
584588

585-
uint8_t hxfr = ep->ep_num | HXFR_OUT_NIN | (ep->is_iso ? HXFR_ISO : 0);
589+
uint8_t const hxfr = (uint8_t ) (ep->ep_num | HXFR_OUT_NIN | (ep->is_iso ? HXFR_ISO : 0));
586590
hxfr_write(rhport, hxfr, in_isr);
587591
}
588592

@@ -595,7 +599,7 @@ void xact_in(uint8_t rhport, max3421_ep_t *ep, bool switch_ep, bool in_isr) {
595599
reg_write(rhport, HCTL_ADDR, hctl, in_isr);
596600
}
597601

598-
uint8_t hxfr = ep->ep_num | (ep->is_iso ? HXFR_ISO : 0);
602+
uint8_t const hxfr = (uint8_t) (ep->ep_num | (ep->is_iso ? HXFR_ISO : 0));
599603
hxfr_write(rhport, hxfr, in_isr);
600604
}
601605

@@ -628,13 +632,13 @@ TU_ATTR_ALWAYS_INLINE static inline void xact_inout(uint8_t rhport, max3421_ep_t
628632
// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked
629633
bool hcd_edpt_xfer(uint8_t rhport, uint8_t daddr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) {
630634
uint8_t const ep_num = tu_edpt_number(ep_addr);
631-
uint8_t const ep_dir = tu_edpt_dir(ep_addr);
635+
uint8_t const ep_dir = (uint8_t) tu_edpt_dir(ep_addr);
632636

633637
max3421_ep_t* ep = find_opened_ep(daddr, ep_num, ep_dir);
634638
TU_VERIFY(ep);
635639

636640
// control transfer can switch direction
637-
ep->ep_dir = ep_dir;
641+
ep->ep_dir = ep_dir ? 1u : 0u;
638642

639643
ep->buf = buffer;
640644
ep->total_len = buflen;
@@ -736,9 +740,9 @@ static void handle_connect_irq(uint8_t rhport, bool in_isr) {
736740
// However, since we are always in full speed mode, we can just check J-state
737741
if (jk == HRSL_KSTATUS) {
738742
new_mode |= MODE_LOWSPEED;
739-
TU_LOG3("Low speed\n");
743+
TU_LOG3("Low speed\r\n");
740744
}else {
741-
TU_LOG3("Full speed\n");
745+
TU_LOG3("Full speed\r\n");
742746
}
743747
new_mode |= MODE_SOFKAENAB;
744748
mode_write(rhport, new_mode, in_isr);
@@ -758,9 +762,9 @@ static void xfer_complete_isr(uint8_t rhport, max3421_ep_t *ep, xfer_result_t re
758762

759763
// save data toggle
760764
if (ep->ep_dir) {
761-
ep->data_toggle = (hrsl & HRSL_RCVTOGRD) ? 1 : 0;
765+
ep->data_toggle = (hrsl & HRSL_RCVTOGRD) ? 1u : 0u;
762766
}else {
763-
ep->data_toggle = (hrsl & HRSL_SNDTOGRD) ? 1 : 0;
767+
ep->data_toggle = (hrsl & HRSL_SNDTOGRD) ? 1u : 0u;
764768
}
765769

766770
ep->xfer_pending = 0;
@@ -944,7 +948,7 @@ void hcd_int_handler(uint8_t rhport, bool in_isr) {
944948
}
945949

946950
// clear all interrupt except SNDBAV_IRQ (never clear by us). Note RCVDAV_IRQ, HXFRDN_IRQ already clear while processing
947-
hirq &= ~HIRQ_SNDBAV_IRQ;
951+
hirq &= (uint8_t) ~HIRQ_SNDBAV_IRQ;
948952
if ( hirq ) {
949953
hirq_write(rhport, hirq, in_isr);
950954
}

src/portable/nordic/nrf5x/dcd_nrf5x.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
340340
NRF_USBD->INTENSET = TU_BIT(USBD_INTEN_ENDEPIN0_Pos + epnum);
341341
NRF_USBD->EPINEN |= TU_BIT(epnum);
342342
}
343+
// clear stall and reset DataToggle
344+
NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep_addr;
345+
NRF_USBD->DTOGGLE = (USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos) | ep_addr;
343346
}
344347
else
345348
{
@@ -375,10 +378,6 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
375378
}
376379
}
377380

378-
// clear stall and reset DataToggle
379-
NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep_addr;
380-
NRF_USBD->DTOGGLE = (USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos) | ep_addr;
381-
382381
__ISB(); __DSB();
383382

384383
return true;

src/portable/raspberrypi/rp2040/hcd_rp2040.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "tusb_option.h"
2929

30-
#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUH_RPI_PIO_USB
30+
#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUH_RPI_PIO_USB && !CFG_TUH_MAX3421
3131

3232
#include "pico.h"
3333
#include "rp2040_usb.h"

0 commit comments

Comments
 (0)