Skip to content

Commit b992b6d

Browse files
committed
apply find_pipe() and forloop for hcd
1 parent daf1c73 commit b992b6d

File tree

2 files changed

+29
-47
lines changed

2 files changed

+29
-47
lines changed

src/portable/renesas/rusb2/dcd_rusb2.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,6 @@ void dcd_int_handler(uint8_t rhport)
10051005
const unsigned s = rusb->BRDYSTS & m;
10061006
/* clear active bits (don't write 0 to already cleared bits according to the HW manual) */
10071007
rusb->BRDYSTS = ~s;
1008-
10091008
for (unsigned p = 0; p < PIPE_COUNT; ++p) {
10101009
if (tu_bit_test(s, p)) {
10111010
process_pipe_brdy(rhport, p);

src/portable/renesas/rusb2/hcd_rusb2.c

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
//--------------------------------------------------------------------+
4646
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
4747
//--------------------------------------------------------------------+
48+
enum {
49+
PIPE_COUNT = 10,
50+
};
4851

4952
TU_ATTR_PACKED_BEGIN
5053
TU_ATTR_BIT_FIELD_ORDER_BEGIN
@@ -75,7 +78,7 @@ TU_ATTR_BIT_FIELD_ORDER_END
7578
typedef struct
7679
{
7780
bool need_reset; /* The device has not been reset after connection. */
78-
pipe_state_t pipe[10];
81+
pipe_state_t pipe[PIPE_COUNT];
7982
uint8_t ep[4][2][15]; /* a lookup table for a pipe index from an endpoint address */
8083
uint8_t ctl_mps[5]; /* EP0 max packet size for each device */
8184
} hcd_data_t;
@@ -86,46 +89,30 @@ typedef struct
8689
static hcd_data_t _hcd;
8790

8891
// TODO merged with DCD
89-
// Transfer conditions specifiable for each pipe:
92+
// Transfer conditions specifiable for each pipe for most MCUs
9093
// - Pipe 0: Control transfer with 64-byte single buffer
91-
// - Pipes 1 and 2: Bulk isochronous transfer continuous transfer mode with programmable buffer size up
92-
// to 2 KB and optional double buffer
93-
// - Pipes 3 to 5: Bulk transfer continuous transfer mode with programmable buffer size up to 2 KB and
94-
// optional double buffer
95-
// - Pipes 6 to 9: Interrupt transfer with 64-byte single buffer
96-
enum {
97-
PIPE_1ST_BULK = 3,
98-
PIPE_1ST_INTERRUPT = 6,
99-
PIPE_COUNT = 10,
100-
};
101-
102-
static unsigned find_pipe(unsigned xfer) {
103-
switch ( xfer ) {
104-
case TUSB_XFER_ISOCHRONOUS:
105-
for (int i = 1; i < PIPE_1ST_BULK; ++i) {
106-
if ( 0 == _hcd.pipe[i].ep ) return i;
107-
}
108-
break;
109-
110-
case TUSB_XFER_BULK:
111-
for (int i = PIPE_1ST_BULK; i < PIPE_1ST_INTERRUPT; ++i) {
112-
if ( 0 == _hcd.pipe[i].ep ) return i;
113-
}
114-
for (int i = 1; i < PIPE_1ST_BULK; ++i) {
115-
if ( 0 == _hcd.pipe[i].ep ) return i;
116-
}
117-
break;
94+
// - Pipes 1 and 2: Bulk or ISO
95+
// - Pipes 3 to 5: Bulk
96+
// - Pipes 6 to 9: Interrupt
97+
//
98+
// Note: for small mcu such as
99+
// - RA2A1: only pipe 4-7 are available, and no support for ISO
100+
static unsigned find_pipe(unsigned xfer_type) {
101+
const uint8_t pipe_idx_arr[4][2] = {
102+
{ 0, 0 }, // Control
103+
{ 1, 2 }, // Isochronous
104+
{ 1, 5 }, // Bulk
105+
{ 6, 9 }, // Interrupt
106+
};
118107

119-
case TUSB_XFER_INTERRUPT:
120-
for (int i = PIPE_1ST_INTERRUPT; i < PIPE_COUNT; ++i) {
121-
if ( 0 == _hcd.pipe[i].ep ) return i;
122-
}
123-
break;
108+
// find backward since only pipe 1, 2 support ISO
109+
const uint8_t idx_first = pipe_idx_arr[xfer_type][0];
110+
const uint8_t idx_last = pipe_idx_arr[xfer_type][1];
124111

125-
default:
126-
/* No support for control transfer */
127-
break;
112+
for (int i = idx_last; i >= idx_first; i--) {
113+
if (0 == _hcd.pipe[i].ep) return i;
128114
}
115+
129116
return 0;
130117
}
131118

@@ -718,7 +705,7 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const
718705
}
719706

720707
rusb->PIPECFG = cfg;
721-
rusb->BRDYSTS = 0x1FFu ^ TU_BIT(num);
708+
rusb->BRDYSTS = 0x3FFu ^ TU_BIT(num);
722709
rusb->NRDYENB |= TU_BIT(num);
723710
rusb->BRDYENB |= TU_BIT(num);
724711

@@ -846,14 +833,10 @@ void hcd_int_handler(uint8_t rhport, bool in_isr) {
846833
unsigned s = rusb->BRDYSTS & m;
847834
/* clear active bits (don't write 0 to already cleared bits according to the HW manual) */
848835
rusb->BRDYSTS = ~s;
849-
while (s) {
850-
#if defined(__CCRX__)
851-
const unsigned num = Mod37BitPosition[(-s & s) % 37];
852-
#else
853-
const unsigned num = __builtin_ctz(s);
854-
#endif
855-
process_pipe_brdy(rhport, num);
856-
s &= ~TU_BIT(num);
836+
for (unsigned p = 0; p < PIPE_COUNT; ++p) {
837+
if (tu_bit_test(s, p)) {
838+
process_pipe_brdy(rhport, p);
839+
}
857840
}
858841
}
859842
}

0 commit comments

Comments
 (0)