Skip to content

Commit

Permalink
Improve atomic operations for usb2 registers, use device endpoint sta…
Browse files Browse the repository at this point in the history
…te at init
  • Loading branch information
kauwua committed Feb 24, 2025
1 parent fe8f121 commit a476569
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 110 deletions.
243 changes: 133 additions & 110 deletions src/wch-ch56x-lib/USBDevice/usb20.c

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions src/wch-ch56x-lib/USBDevice/usb2_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,55 @@ usb2_get_tx_endpoint_len_reg(uint8_t endp_num)
return NULL;
}

/**
* @brief Get RX control register address
* @param endp_num endpoint number
* @return
*/
__attribute__((always_inline)) static inline vuint8_t*
usb2_get_mod_reg(uint8_t endp_num)
{
switch (endp_num)
{
case ENDP_0:
return &R8_UEP0_RX_CTRL;
break;
case ENDP_1:
case ENDP_9:
return &R8_UEP1_RX_CTRL;
break;
case ENDP_2:
case ENDP_10:
return &R8_UEP2_RX_CTRL;
break;
case ENDP_3:
case ENDP_11:
return &R8_UEP3_RX_CTRL;
break;
case ENDP_4:
case ENDP_8:
case ENDP_12:
return &R8_UEP4_RX_CTRL;
break;
case ENDP_5:
case ENDP_13:
return &R8_UEP5_RX_CTRL;
break;
case ENDP_6:
case ENDP_14:
return &R8_UEP6_RX_CTRL;
break;
case ENDP_7:
case ENDP_15:
return &R8_UEP7_RX_CTRL;
break;
default:
return NULL;
break;
}
return NULL;
}

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions tests/test_firmware_hspi/User/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ void init_endpoints(void)
usb_device_0.endpoints.rx[0].max_packet_size = 512;
usb_device_0.endpoints.rx[0].max_burst = 1;
usb_device_0.endpoints.rx[0].max_packet_size_with_burst = sizeof(endp0_buffer);
usb_device_0.endpoints.rx[0].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[1].buffer = NULL;
usb_device_0.endpoints.tx[1].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[1].max_burst = 0;
usb_device_0.endpoints.tx[1].max_packet_size_with_burst = sizeof(endp1_tx_buffer);
usb_device_0.endpoints.tx[1].state = ENDP_STATE_NAK;
}

bool_t is_board1; /* Return true or false */
Expand Down
3 changes: 3 additions & 0 deletions tests/test_firmware_loopback/User/usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,19 @@ void init_endpoints(void)
usb_device_0.endpoints.rx[0].max_packet_size = 512;
usb_device_0.endpoints.rx[0].max_burst = 1;
usb_device_0.endpoints.rx[0].max_packet_size_with_burst = sizeof(endp0_buffer);
usb_device_0.endpoints.rx[0].state = ENDP_STATE_ACK;

usb_device_0.endpoints.rx[1].buffer = endp1_rx_buffer;
usb_device_0.endpoints.rx[1].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[1].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[1].max_packet_size_with_burst = sizeof(endp1_rx_buffer);
usb_device_0.endpoints.rx[1].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[1].buffer = NULL;
usb_device_0.endpoints.tx[1].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[1].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[1].max_packet_size_with_burst = sizeof(endp1_rx_buffer);
usb_device_0.endpoints.tx[1].state = ENDP_STATE_NAK;
}

#endif
2 changes: 2 additions & 0 deletions tests/test_firmware_serdes/User/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ void init_endpoints(void)
usb_device_0.endpoints.rx[0].max_packet_size = 512;
usb_device_0.endpoints.rx[0].max_burst = 1;
usb_device_0.endpoints.rx[0].max_packet_size_with_burst = sizeof(endp0_buffer);
usb_device_0.endpoints.rx[0].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[1].buffer = NULL;
usb_device_0.endpoints.tx[1].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[1].max_burst = 0;
usb_device_0.endpoints.tx[1].max_packet_size_with_burst = sizeof(endp1_tx_buffer);
usb_device_0.endpoints.tx[1].state = ENDP_STATE_NAK;
}

bool_t is_board1; /* Return true or false */
Expand Down
7 changes: 7 additions & 0 deletions tests/test_firmware_unittests/User/test_usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,43 @@ bool test_usb_device(void)
usb_device_0.endpoints.tx[1].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[1].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[1].max_packet_size_with_burst = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[1].state = ENDP_STATE_NAK;

usb_device_0.endpoints.tx[2].buffer = NULL;
usb_device_0.endpoints.tx[2].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[2].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[2].max_packet_size_with_burst = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[2].state = ENDP_STATE_NAK;

usb_device_0.endpoints.tx[3].buffer = NULL;
usb_device_0.endpoints.tx[3].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[3].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[3].max_packet_size_with_burst = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[3].state = ENDP_STATE_NAK;

usb_device_0.endpoints.tx[4].buffer = NULL;
usb_device_0.endpoints.tx[4].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[4].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[4].max_packet_size_with_burst = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[4].state = ENDP_STATE_NAK;

usb_device_0.endpoints.tx[5].buffer = NULL;
usb_device_0.endpoints.tx[5].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[5].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[5].max_packet_size_with_burst = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[5].state = ENDP_STATE_NAK;

usb_device_0.endpoints.tx[6].buffer = NULL;
usb_device_0.endpoints.tx[6].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[6].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[6].max_packet_size_with_burst = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[6].state = ENDP_STATE_NAK;

usb_device_0.endpoints.tx[7].buffer = NULL;
usb_device_0.endpoints.tx[7].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[7].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[7].max_packet_size_with_burst = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[7].state = ENDP_STATE_NAK;

bool success = true;

Expand Down
29 changes: 29 additions & 0 deletions tests/test_firmware_usb_loopback/User/usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,146 +234,175 @@ void init_endpoints(void)
usb_device_0.endpoints.rx[0].max_packet_size = 512;
usb_device_0.endpoints.rx[0].max_burst = 1;
usb_device_0.endpoints.rx[0].max_packet_size_with_burst = sizeof(endp0_buffer);
usb_device_0.endpoints.rx[0].state = ENDP_STATE_ACK;

usb_device_0.endpoints.rx[1].buffer = endp1_rx_buffer;
usb_device_0.endpoints.rx[1].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[1].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[1].max_packet_size_with_burst = sizeof(endp1_rx_buffer);
usb_device_0.endpoints.rx[1].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[1].buffer = NULL;
usb_device_0.endpoints.tx[1].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[1].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[1].max_packet_size_with_burst = sizeof(endp1_rx_buffer);
usb_device_0.endpoints.tx[1].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[2].buffer = endp2_rx_buffer;
usb_device_0.endpoints.rx[2].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[2].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[2].max_packet_size_with_burst = sizeof(endp2_rx_buffer);
usb_device_0.endpoints.rx[2].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[2].buffer = NULL;
usb_device_0.endpoints.tx[2].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[2].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[2].max_packet_size_with_burst = sizeof(endp2_rx_buffer);
usb_device_0.endpoints.tx[2].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[3].buffer = endp3_rx_buffer;
usb_device_0.endpoints.rx[3].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[3].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[3].max_packet_size_with_burst = sizeof(endp3_rx_buffer);
usb_device_0.endpoints.rx[3].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[3].buffer = NULL;
usb_device_0.endpoints.tx[3].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[3].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[3].max_packet_size_with_burst = sizeof(endp3_rx_buffer);
usb_device_0.endpoints.tx[3].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[4].buffer = endp4_rx_buffer;
usb_device_0.endpoints.rx[4].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[4].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[4].max_packet_size_with_burst = sizeof(endp4_rx_buffer);
usb_device_0.endpoints.rx[4].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[4].buffer = NULL;
usb_device_0.endpoints.tx[4].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[4].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[4].max_packet_size_with_burst = sizeof(endp4_rx_buffer);
usb_device_0.endpoints.tx[4].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[5].buffer = endp5_rx_buffer;
usb_device_0.endpoints.rx[5].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[5].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[5].max_packet_size_with_burst = sizeof(endp5_rx_buffer);
usb_device_0.endpoints.rx[5].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[5].buffer = NULL;
usb_device_0.endpoints.tx[5].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[5].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[5].max_packet_size_with_burst = sizeof(endp5_rx_buffer);
usb_device_0.endpoints.tx[5].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[6].buffer = endp6_rx_buffer;
usb_device_0.endpoints.rx[6].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[6].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[6].max_packet_size_with_burst = sizeof(endp6_rx_buffer);
usb_device_0.endpoints.rx[6].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[6].buffer = NULL;
usb_device_0.endpoints.tx[6].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[6].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[6].max_packet_size_with_burst = sizeof(endp6_rx_buffer);
usb_device_0.endpoints.tx[6].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[7].buffer = endp7_rx_buffer;
usb_device_0.endpoints.rx[7].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[7].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[7].max_packet_size_with_burst = sizeof(endp7_rx_buffer);
usb_device_0.endpoints.rx[7].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[7].buffer = NULL;
usb_device_0.endpoints.tx[7].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[7].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[7].max_packet_size_with_burst = sizeof(endp7_rx_buffer);
usb_device_0.endpoints.tx[7].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[9].buffer = endp1_rx_buffer;
usb_device_0.endpoints.rx[9].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[9].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[9].max_packet_size_with_burst = sizeof(endp1_rx_buffer);
usb_device_0.endpoints.rx[9].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[9].buffer = NULL;
usb_device_0.endpoints.tx[9].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[9].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[9].max_packet_size_with_burst = sizeof(endp1_rx_buffer);
usb_device_0.endpoints.tx[9].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[10].buffer = endp2_rx_buffer;
usb_device_0.endpoints.rx[10].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[10].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[10].max_packet_size_with_burst = sizeof(endp2_rx_buffer);
usb_device_0.endpoints.rx[10].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[10].buffer = NULL;
usb_device_0.endpoints.tx[10].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[10].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[10].max_packet_size_with_burst = sizeof(endp2_rx_buffer);
usb_device_0.endpoints.tx[10].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[11].buffer = endp3_rx_buffer;
usb_device_0.endpoints.rx[11].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[11].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[11].max_packet_size_with_burst = sizeof(endp3_rx_buffer);
usb_device_0.endpoints.rx[11].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[11].buffer = NULL;
usb_device_0.endpoints.tx[11].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[11].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[11].max_packet_size_with_burst = sizeof(endp3_rx_buffer);
usb_device_0.endpoints.tx[11].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[12].buffer = endp4_rx_buffer;
usb_device_0.endpoints.rx[12].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[12].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[12].max_packet_size_with_burst = sizeof(endp4_rx_buffer);
usb_device_0.endpoints.rx[12].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[12].buffer = NULL;
usb_device_0.endpoints.tx[12].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[12].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[12].max_packet_size_with_burst = sizeof(endp4_rx_buffer);
usb_device_0.endpoints.tx[12].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[13].buffer = endp5_rx_buffer;
usb_device_0.endpoints.rx[13].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[13].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[13].max_packet_size_with_burst = sizeof(endp5_rx_buffer);
usb_device_0.endpoints.rx[13].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[13].buffer = NULL;
usb_device_0.endpoints.tx[13].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[13].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[13].max_packet_size_with_burst = sizeof(endp5_rx_buffer);
usb_device_0.endpoints.tx[13].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[14].buffer = endp6_rx_buffer;
usb_device_0.endpoints.rx[14].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[14].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[14].max_packet_size_with_burst = sizeof(endp6_rx_buffer);
usb_device_0.endpoints.rx[14].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[14].buffer = NULL;
usb_device_0.endpoints.tx[14].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[14].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[14].max_packet_size_with_burst = sizeof(endp6_rx_buffer);
usb_device_0.endpoints.tx[14].state = ENDP_STATE_NAK;

usb_device_0.endpoints.rx[15].buffer = endp7_rx_buffer;
usb_device_0.endpoints.rx[15].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.rx[15].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.rx[15].max_packet_size_with_burst = sizeof(endp7_rx_buffer);
usb_device_0.endpoints.rx[15].state = ENDP_STATE_ACK;

usb_device_0.endpoints.tx[15].buffer = NULL;
usb_device_0.endpoints.tx[15].max_packet_size = ENDP_1_15_MAX_PACKET_SIZE;
usb_device_0.endpoints.tx[15].max_burst = DEF_ENDP_OUT_BURST_LEVEL;
usb_device_0.endpoints.tx[15].max_packet_size_with_burst = sizeof(endp7_rx_buffer);
usb_device_0.endpoints.tx[15].state = ENDP_STATE_NAK;
}

#endif
Loading

0 comments on commit a476569

Please sign in to comment.