apply find_pipe() and forloop for hcd

This commit is contained in:
hathach
2024-04-11 12:06:13 +07:00
parent daf1c7303c
commit b992b6d7a6
2 changed files with 29 additions and 47 deletions

View File

@@ -1005,7 +1005,6 @@ void dcd_int_handler(uint8_t rhport)
const unsigned s = rusb->BRDYSTS & m; const unsigned s = rusb->BRDYSTS & m;
/* clear active bits (don't write 0 to already cleared bits according to the HW manual) */ /* clear active bits (don't write 0 to already cleared bits according to the HW manual) */
rusb->BRDYSTS = ~s; rusb->BRDYSTS = ~s;
for (unsigned p = 0; p < PIPE_COUNT; ++p) { for (unsigned p = 0; p < PIPE_COUNT; ++p) {
if (tu_bit_test(s, p)) { if (tu_bit_test(s, p)) {
process_pipe_brdy(rhport, p); process_pipe_brdy(rhport, p);

View File

@@ -45,6 +45,9 @@
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION // MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
enum {
PIPE_COUNT = 10,
};
TU_ATTR_PACKED_BEGIN TU_ATTR_PACKED_BEGIN
TU_ATTR_BIT_FIELD_ORDER_BEGIN TU_ATTR_BIT_FIELD_ORDER_BEGIN
@@ -75,7 +78,7 @@ TU_ATTR_BIT_FIELD_ORDER_END
typedef struct typedef struct
{ {
bool need_reset; /* The device has not been reset after connection. */ bool need_reset; /* The device has not been reset after connection. */
pipe_state_t pipe[10]; pipe_state_t pipe[PIPE_COUNT];
uint8_t ep[4][2][15]; /* a lookup table for a pipe index from an endpoint address */ uint8_t ep[4][2][15]; /* a lookup table for a pipe index from an endpoint address */
uint8_t ctl_mps[5]; /* EP0 max packet size for each device */ uint8_t ctl_mps[5]; /* EP0 max packet size for each device */
} hcd_data_t; } hcd_data_t;
@@ -86,46 +89,30 @@ typedef struct
static hcd_data_t _hcd; static hcd_data_t _hcd;
// TODO merged with DCD // TODO merged with DCD
// Transfer conditions specifiable for each pipe: // Transfer conditions specifiable for each pipe for most MCUs
// - Pipe 0: Control transfer with 64-byte single buffer // - Pipe 0: Control transfer with 64-byte single buffer
// - Pipes 1 and 2: Bulk isochronous transfer continuous transfer mode with programmable buffer size up // - Pipes 1 and 2: Bulk or ISO
// to 2 KB and optional double buffer // - Pipes 3 to 5: Bulk
// - Pipes 3 to 5: Bulk transfer continuous transfer mode with programmable buffer size up to 2 KB and // - Pipes 6 to 9: Interrupt
// optional double buffer //
// - Pipes 6 to 9: Interrupt transfer with 64-byte single buffer // Note: for small mcu such as
enum { // - RA2A1: only pipe 4-7 are available, and no support for ISO
PIPE_1ST_BULK = 3, static unsigned find_pipe(unsigned xfer_type) {
PIPE_1ST_INTERRUPT = 6, const uint8_t pipe_idx_arr[4][2] = {
PIPE_COUNT = 10, { 0, 0 }, // Control
}; { 1, 2 }, // Isochronous
{ 1, 5 }, // Bulk
{ 6, 9 }, // Interrupt
};
static unsigned find_pipe(unsigned xfer) { // find backward since only pipe 1, 2 support ISO
switch ( xfer ) { const uint8_t idx_first = pipe_idx_arr[xfer_type][0];
case TUSB_XFER_ISOCHRONOUS: const uint8_t idx_last = pipe_idx_arr[xfer_type][1];
for (int i = 1; i < PIPE_1ST_BULK; ++i) {
if ( 0 == _hcd.pipe[i].ep ) return i;
}
break;
case TUSB_XFER_BULK: for (int i = idx_last; i >= idx_first; i--) {
for (int i = PIPE_1ST_BULK; i < PIPE_1ST_INTERRUPT; ++i) { if (0 == _hcd.pipe[i].ep) return i;
if ( 0 == _hcd.pipe[i].ep ) return i;
}
for (int i = 1; i < PIPE_1ST_BULK; ++i) {
if ( 0 == _hcd.pipe[i].ep ) return i;
}
break;
case TUSB_XFER_INTERRUPT:
for (int i = PIPE_1ST_INTERRUPT; i < PIPE_COUNT; ++i) {
if ( 0 == _hcd.pipe[i].ep ) return i;
}
break;
default:
/* No support for control transfer */
break;
} }
return 0; return 0;
} }
@@ -718,7 +705,7 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const
} }
rusb->PIPECFG = cfg; rusb->PIPECFG = cfg;
rusb->BRDYSTS = 0x1FFu ^ TU_BIT(num); rusb->BRDYSTS = 0x3FFu ^ TU_BIT(num);
rusb->NRDYENB |= TU_BIT(num); rusb->NRDYENB |= TU_BIT(num);
rusb->BRDYENB |= TU_BIT(num); rusb->BRDYENB |= TU_BIT(num);
@@ -846,14 +833,10 @@ void hcd_int_handler(uint8_t rhport, bool in_isr) {
unsigned s = rusb->BRDYSTS & m; unsigned s = rusb->BRDYSTS & m;
/* clear active bits (don't write 0 to already cleared bits according to the HW manual) */ /* clear active bits (don't write 0 to already cleared bits according to the HW manual) */
rusb->BRDYSTS = ~s; rusb->BRDYSTS = ~s;
while (s) { for (unsigned p = 0; p < PIPE_COUNT; ++p) {
#if defined(__CCRX__) if (tu_bit_test(s, p)) {
const unsigned num = Mod37BitPosition[(-s & s) % 37]; process_pipe_brdy(rhport, p);
#else }
const unsigned num = __builtin_ctz(s);
#endif
process_pipe_brdy(rhport, num);
s &= ~TU_BIT(num);
} }
} }
} }