Skip to content

Commit a445e8c

Browse files
committed
hcd/fsdev: more refactor
Signed-off-by: HiFiPhile <[email protected]>
1 parent adcfc4b commit a445e8c

File tree

1 file changed

+61
-62
lines changed

1 file changed

+61
-62
lines changed

src/portable/st/stm32_fsdev/hcd_stm32_fsdev.c

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,15 @@ typedef struct {
8989
uint8_t ep_type;
9090
uint8_t allocated[2];
9191
uint8_t retry[2];
92-
uint8_t result;
93-
} hcd_xfer_t;
92+
} hcd_channel_t;
9493

9594
// Root hub port state
9695
static struct {
9796
bool connected;
9897
} _hcd_port;
9998

10099
typedef struct {
101-
hcd_xfer_t xfer[FSDEV_EP_COUNT];
100+
hcd_channel_t channel[FSDEV_EP_COUNT];
102101
hcd_endpoint_t edpt[CFG_TUH_FSDEV_ENDPOINT_MAX];
103102
} hcd_data_t;
104103

@@ -129,8 +128,8 @@ static inline void endpoint_dealloc(hcd_endpoint_t* edpt) {
129128
edpt->allocated = 0;
130129
}
131130

132-
static inline void channel_dealloc(hcd_xfer_t* xfer, tusb_dir_t dir) {
133-
xfer->allocated[dir] = 0;
131+
static inline void channel_dealloc(hcd_channel_t* ch, tusb_dir_t dir) {
132+
ch->allocated[dir] = 0;
134133
}
135134

136135
// Write channel state in specified direction
@@ -241,40 +240,40 @@ static void ch_handle_ack(uint8_t ch_id, uint32_t ch_reg, tusb_dir_t dir) {
241240
if (ep_id == TUSB_INDEX_INVALID_8) return;
242241

243242
hcd_endpoint_t* edpt = &_hcd_data.edpt[ep_id];
244-
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
243+
hcd_channel_t* channel = &_hcd_data.channel[ch_id];
245244

246245
if (dir == TUSB_DIR_OUT) {
247246
// OUT/TX direction
248-
if (edpt->buflen != xfer->queued_len[TUSB_DIR_OUT]) {
247+
if (edpt->buflen != channel->queued_len[TUSB_DIR_OUT]) {
249248
// More data to send
250-
uint16_t const len = tu_min16(edpt->buflen - xfer->queued_len[TUSB_DIR_OUT], edpt->max_packet_size);
249+
uint16_t const len = tu_min16(edpt->buflen - channel->queued_len[TUSB_DIR_OUT], edpt->max_packet_size);
251250
uint16_t pma_addr = (uint16_t) btable_get_addr(ch_id, BTABLE_BUF_TX);
252-
fsdev_write_packet_memory(pma_addr, &(edpt->buffer[xfer->queued_len[TUSB_DIR_OUT]]), len);
251+
fsdev_write_packet_memory(pma_addr, &(edpt->buffer[channel->queued_len[TUSB_DIR_OUT]]), len);
253252
btable_set_count(ch_id, BTABLE_BUF_TX, len);
254-
xfer->queued_len[TUSB_DIR_OUT] += len;
253+
channel->queued_len[TUSB_DIR_OUT] += len;
255254
channel_write_status(ch_id, ch_reg, TUSB_DIR_OUT, EP_STAT_VALID, false);
256255
} else {
257256
// Transfer complete
258-
channel_dealloc(xfer, TUSB_DIR_OUT);
257+
channel_dealloc(channel, TUSB_DIR_OUT);
259258
edpt->pid = (ch_reg & USB_CHEP_DTOG_TX) ? 1 : 0;
260-
hcd_event_xfer_complete(daddr, ep_num, xfer->queued_len[TUSB_DIR_OUT], XFER_RESULT_SUCCESS, true);
259+
hcd_event_xfer_complete(daddr, ep_num, channel->queued_len[TUSB_DIR_OUT], XFER_RESULT_SUCCESS, true);
261260
}
262261
} else {
263262
// IN/RX direction
264263
uint16_t const rx_count = btable_get_count(ch_id, BTABLE_BUF_RX);
265264
uint16_t pma_addr = (uint16_t) btable_get_addr(ch_id, BTABLE_BUF_RX);
266265

267-
fsdev_read_packet_memory(edpt->buffer + xfer->queued_len[TUSB_DIR_IN], pma_addr, rx_count);
268-
xfer->queued_len[TUSB_DIR_IN] += rx_count;
266+
fsdev_read_packet_memory(edpt->buffer + channel->queued_len[TUSB_DIR_IN], pma_addr, rx_count);
267+
channel->queued_len[TUSB_DIR_IN] += rx_count;
269268

270-
if ((rx_count < edpt->max_packet_size) || (xfer->queued_len[TUSB_DIR_IN] >= edpt->buflen)) {
269+
if ((rx_count < edpt->max_packet_size) || (channel->queued_len[TUSB_DIR_IN] >= edpt->buflen)) {
271270
// Transfer complete (short packet or all bytes received)
272-
channel_dealloc(xfer, TUSB_DIR_IN);
271+
channel_dealloc(channel, TUSB_DIR_IN);
273272
edpt->pid = (ch_reg & USB_CHEP_DTOG_RX) ? 1 : 0;
274-
hcd_event_xfer_complete(daddr, ep_num | TUSB_DIR_IN_MASK, xfer->queued_len[TUSB_DIR_IN], XFER_RESULT_SUCCESS, true);
273+
hcd_event_xfer_complete(daddr, ep_num | TUSB_DIR_IN_MASK, channel->queued_len[TUSB_DIR_IN], XFER_RESULT_SUCCESS, true);
275274
} else {
276275
// More data expected
277-
uint16_t const cnt = tu_min16(edpt->buflen - xfer->queued_len[TUSB_DIR_IN], edpt->max_packet_size);
276+
uint16_t const cnt = tu_min16(edpt->buflen - channel->queued_len[TUSB_DIR_IN], edpt->max_packet_size);
278277
btable_set_rx_bufsize(ch_id, BTABLE_BUF_RX, cnt);
279278
channel_write_status(ch_id, ch_reg, TUSB_DIR_IN, EP_STAT_VALID, false);
280279
}
@@ -302,13 +301,13 @@ static void ch_handle_stall(uint8_t ch_id, uint32_t ch_reg, tusb_dir_t dir) {
302301
uint8_t const ep_num = ch_reg & USB_EPADDR_FIELD;
303302
uint8_t const daddr = (ch_reg & USB_CHEP_DEVADDR_Msk) >> USB_CHEP_DEVADDR_Pos;
304303

305-
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
306-
channel_dealloc(xfer, dir);
304+
hcd_channel_t* channel = &_hcd_data.channel[ch_id];
305+
channel_dealloc(channel, dir);
307306

308307
channel_write_status(ch_id, ch_reg, dir, EP_STAT_DISABLED, false);
309308

310309
hcd_event_xfer_complete(daddr, ep_num | (dir == TUSB_DIR_IN ? TUSB_DIR_IN_MASK : 0),
311-
xfer->queued_len[dir], XFER_RESULT_STALLED, true);
310+
channel->queued_len[dir], XFER_RESULT_STALLED, true);
312311
}
313312

314313
// Handle error response
@@ -319,30 +318,30 @@ static void ch_handle_error(uint8_t ch_id, uint32_t ch_reg, tusb_dir_t dir) {
319318
uint8_t ep_id = endpoint_find(daddr, ep_num | (dir == TUSB_DIR_IN ? TUSB_DIR_IN_MASK : 0));
320319
if (ep_id == TUSB_INDEX_INVALID_8) return;
321320

322-
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
321+
hcd_channel_t* channel = &_hcd_data.channel[ch_id];
323322

324323
ch_reg &= USB_EPREG_MASK | CH_STAT_MASK(dir);
325324
ch_reg &= ~(dir == TUSB_DIR_OUT ? USB_CH_ERRTX : USB_CH_ERRRX);
326325

327-
if (xfer->retry[dir] < HCD_XFER_ERROR_MAX) {
326+
if (channel->retry[dir] < HCD_XFER_ERROR_MAX) {
328327
// Retry
329-
xfer->retry[dir]++;
328+
channel->retry[dir]++;
330329
ch_change_status(&ch_reg, dir, EP_STAT_VALID);
331330
} else {
332331
// Failed after retries
333-
channel_dealloc(xfer, dir);
332+
channel_dealloc(channel, dir);
334333
ch_change_status(&ch_reg, dir, EP_STAT_DISABLED);
335334
hcd_event_xfer_complete(daddr, ep_num | (dir == TUSB_DIR_IN ? TUSB_DIR_IN_MASK : 0),
336-
xfer->queued_len[dir], XFER_RESULT_FAILED, true);
335+
channel->queued_len[dir], XFER_RESULT_FAILED, true);
337336
}
338337
ch_write(ch_id, ch_reg, false);
339338
}
340339

341340
// Handle CTR interrupt for the TX/OUT direction
342341
static void handle_ctr_tx(uint32_t ch_id) {
343342
uint32_t ch_reg = ch_read(ch_id) | USB_EP_CTR_TX | USB_EP_CTR_RX;
344-
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
345-
TU_VERIFY(xfer->allocated[TUSB_DIR_OUT] == 1,);
343+
hcd_channel_t* channel = &_hcd_data.channel[ch_id];
344+
TU_VERIFY(channel->allocated[TUSB_DIR_OUT] == 1,);
346345

347346
if ((ch_reg & USB_CH_ERRTX) == 0U) {
348347
// No error
@@ -361,8 +360,8 @@ static void handle_ctr_tx(uint32_t ch_id) {
361360
// Handle CTR interrupt for the RX/IN direction
362361
static void handle_ctr_rx(uint32_t ch_id) {
363362
uint32_t ch_reg = ch_read(ch_id) | USB_EP_CTR_TX | USB_EP_CTR_RX;
364-
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
365-
TU_VERIFY(xfer->allocated[TUSB_DIR_IN] == 1,);
363+
hcd_channel_t* channel = &_hcd_data.channel[ch_id];
364+
TU_VERIFY(channel->allocated[TUSB_DIR_IN] == 1,);
366365

367366
if ((ch_reg & USB_CH_ERRRX) == 0U) {
368367
// No error
@@ -579,12 +578,12 @@ bool hcd_edpt_abort_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
579578
tusb_dir_t const dir = tu_edpt_dir(ep_addr);
580579

581580
for (uint8_t i = 0; i < FSDEV_EP_COUNT; i++) {
582-
hcd_xfer_t* xfer = &_hcd_data.xfer[i];
581+
hcd_channel_t* channel = &_hcd_data.channel[i];
583582

584-
if (xfer->allocated[dir] == 1 &&
585-
xfer->dev_addr == dev_addr &&
586-
xfer->ep_num == tu_edpt_number(ep_addr)) {
587-
channel_dealloc(xfer, dir);
583+
if (channel->allocated[dir] == 1 &&
584+
channel->dev_addr == dev_addr &&
585+
channel->ep_num == tu_edpt_number(ep_addr)) {
586+
channel_dealloc(channel, dir);
588587
uint32_t ch_reg = ch_read(i) | USB_EP_CTR_TX | USB_EP_CTR_RX;
589588
channel_write_status(i, ch_reg, dir, EP_STAT_DISABLED, true);
590589
}
@@ -654,14 +653,14 @@ static void edpoint_close(uint8_t ep_id) {
654653

655654
// disable active channel belong to this endpoint
656655
for (uint8_t i = 0; i < FSDEV_EP_COUNT; i++) {
657-
hcd_xfer_t* xfer = &_hcd_data.xfer[i];
656+
hcd_channel_t* channel = &_hcd_data.channel[i];
658657
uint32_t ch_reg = ch_read(i) | USB_EP_CTR_TX | USB_EP_CTR_RX;
659-
if (xfer->allocated[TUSB_DIR_OUT] == 1 && xfer->edpt[TUSB_DIR_OUT] == edpt) {
660-
channel_dealloc(xfer, TUSB_DIR_OUT);
658+
if (channel->allocated[TUSB_DIR_OUT] == 1 && channel->edpt[TUSB_DIR_OUT] == edpt) {
659+
channel_dealloc(channel, TUSB_DIR_OUT);
661660
channel_write_status(i, ch_reg, TUSB_DIR_OUT, EP_STAT_DISABLED, true);
662661
}
663-
if (xfer->allocated[TUSB_DIR_IN] == 1 && xfer->edpt[TUSB_DIR_IN] == edpt) {
664-
channel_dealloc(xfer, TUSB_DIR_IN);
662+
if (channel->allocated[TUSB_DIR_IN] == 1 && channel->edpt[TUSB_DIR_IN] == edpt) {
663+
channel_dealloc(channel, TUSB_DIR_IN);
665664
channel_write_status(i, ch_reg, TUSB_DIR_IN, EP_STAT_DISABLED, true);
666665
}
667666
}
@@ -689,27 +688,27 @@ static uint8_t channel_alloc(uint8_t dev_addr, uint8_t ep_addr, uint8_t ep_type)
689688
// Find channel allocate for same ep_num but other direction
690689
tusb_dir_t const other_dir = (dir == TUSB_DIR_IN) ? TUSB_DIR_OUT : TUSB_DIR_IN;
691690
for (uint8_t i = 0; i < FSDEV_EP_COUNT; i++) {
692-
if (_hcd_data.xfer[i].allocated[dir] == 0 &&
693-
_hcd_data.xfer[i].allocated[other_dir] == 1 &&
694-
_hcd_data.xfer[i].dev_addr == dev_addr &&
695-
_hcd_data.xfer[i].ep_num == ep_num &&
696-
_hcd_data.xfer[i].ep_type == ep_type) {
697-
_hcd_data.xfer[i].allocated[dir] = 1;
698-
_hcd_data.xfer[i].queued_len[dir] = 0;
699-
_hcd_data.xfer[i].retry[dir] = 0;
691+
if (_hcd_data.channel[i].allocated[dir] == 0 &&
692+
_hcd_data.channel[i].allocated[other_dir] == 1 &&
693+
_hcd_data.channel[i].dev_addr == dev_addr &&
694+
_hcd_data.channel[i].ep_num == ep_num &&
695+
_hcd_data.channel[i].ep_type == ep_type) {
696+
_hcd_data.channel[i].allocated[dir] = 1;
697+
_hcd_data.channel[i].queued_len[dir] = 0;
698+
_hcd_data.channel[i].retry[dir] = 0;
700699
return i;
701700
}
702701
}
703702

704703
// Find free channel
705704
for (uint8_t i = 0; i < FSDEV_EP_COUNT; i++) {
706-
if (_hcd_data.xfer[i].allocated[0] == 0 && _hcd_data.xfer[i].allocated[1] == 0) {
707-
_hcd_data.xfer[i].dev_addr = dev_addr;
708-
_hcd_data.xfer[i].ep_num = ep_num;
709-
_hcd_data.xfer[i].ep_type = ep_type;
710-
_hcd_data.xfer[i].allocated[dir] = 1;
711-
_hcd_data.xfer[i].queued_len[dir] = 0;
712-
_hcd_data.xfer[i].retry[dir] = 0;
705+
if (_hcd_data.channel[i].allocated[0] == 0 && _hcd_data.channel[i].allocated[1] == 0) {
706+
_hcd_data.channel[i].dev_addr = dev_addr;
707+
_hcd_data.channel[i].ep_num = ep_num;
708+
_hcd_data.channel[i].ep_type = ep_type;
709+
_hcd_data.channel[i].allocated[dir] = 1;
710+
_hcd_data.channel[i].queued_len[dir] = 0;
711+
_hcd_data.channel[i].retry[dir] = 0;
713712
return i;
714713
}
715714
}
@@ -726,15 +725,15 @@ static bool edpt_xfer_kickoff(uint8_t ep_id) {
726725

727726
tusb_dir_t const dir = tu_edpt_dir(edpt->ep_addr);
728727

729-
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
730-
xfer->edpt[dir] = edpt;
728+
hcd_channel_t* channel = &_hcd_data.channel[ch_id];
729+
channel->edpt[dir] = edpt;
731730

732731
return channel_xfer_start(ch_id, dir);
733732
}
734733

735734
static bool channel_xfer_start(uint8_t ch_id, tusb_dir_t dir) {
736-
hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id];
737-
hcd_endpoint_t* edpt = xfer->edpt[dir];
735+
hcd_channel_t* channel = &_hcd_data.channel[ch_id];
736+
hcd_endpoint_t* edpt = channel->edpt[dir];
738737

739738
uint32_t ch_reg = ch_read(ch_id) & ~USB_EPREG_MASK;
740739
ch_reg |= tu_edpt_number(edpt->ep_addr) | edpt->dev_addr << USB_CHEP_DEVADDR_Pos |
@@ -763,12 +762,12 @@ static bool channel_xfer_start(uint8_t ch_id, tusb_dir_t dir) {
763762
btable_set_addr(ch_id, dir == TUSB_DIR_OUT ? BTABLE_BUF_TX : BTABLE_BUF_RX, pma_addr);
764763

765764
if (dir == TUSB_DIR_OUT) {
766-
uint16_t const len = tu_min16(edpt->buflen - xfer->queued_len[TUSB_DIR_OUT], edpt->max_packet_size);
765+
uint16_t const len = tu_min16(edpt->buflen - channel->queued_len[TUSB_DIR_OUT], edpt->max_packet_size);
767766

768-
fsdev_write_packet_memory(pma_addr, &(edpt->buffer[xfer->queued_len[TUSB_DIR_OUT]]), len);
767+
fsdev_write_packet_memory(pma_addr, &(edpt->buffer[channel->queued_len[TUSB_DIR_OUT]]), len);
769768
btable_set_count(ch_id, BTABLE_BUF_TX, len);
770769

771-
xfer->queued_len[TUSB_DIR_OUT] += len;
770+
channel->queued_len[TUSB_DIR_OUT] += len;
772771
} else {
773772
btable_set_rx_bufsize(ch_id, BTABLE_BUF_RX, edpt->max_packet_size);
774773
}

0 commit comments

Comments
 (0)