Skip to content

Commit

Permalink
Add feature set USB speed
Browse files Browse the repository at this point in the history
- New function tud_speed_set to set the intended USB speed
  • Loading branch information
RockyZeroFour committed Feb 5, 2024
1 parent 2506930 commit 239beb9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/device/dcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ void dcd_connect(uint8_t rhport) TU_ATTR_WEAK;
// Disconnect by disabling internal pull-up resistor on D+/D-
void dcd_disconnect(uint8_t rhport) TU_ATTR_WEAK;

// Set new (max) intended bus speed
void dcd_speed_set(tusb_speed_t speed) TU_ATTR_WEAK;

//--------------------------------------------------------------------+
// Endpoint API
//--------------------------------------------------------------------+
Expand Down
9 changes: 9 additions & 0 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ tusb_speed_t tud_speed_get(void)
return (tusb_speed_t) _usbd_dev.speed;
}

bool tud_speed_set(tusb_speed_t speed)
{
TU_VERIFY(dcd_speed_set);

dcd_speed_set(speed);

return true;
}

bool tud_connected(void)
{
return _usbd_dev.connected;
Expand Down
4 changes: 4 additions & 0 deletions src/device/usbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ extern void dcd_int_handler(uint8_t rhport);
// Get current bus speed
tusb_speed_t tud_speed_get(void);

// Set new (max) intended bus speed
// Return false if not supported
bool tud_speed_set(tusb_speed_t speed);

// Check if device is connected (may not mounted/configured yet)
// True if just got out of Bus Reset and received the very first data from host
bool tud_connected(void);
Expand Down
22 changes: 21 additions & 1 deletion src/portable/synopsys/dwc2/dcd_dwc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ static uint16_t ep0_pending[2]; // Index determines direction
static uint16_t _allocated_fifo_words_tx; // TX FIFO size in words (IN EPs)
static bool _out_ep_closed; // Flag to check if RX FIFO size needs an update (reduce its size)

static tusb_speed_t _usb_max_speed = TUSB_SPEED_INVALID; // Value to indicate the intended (max) USB speed

// Flush the TX-FIFO and wait until we have confirmed it cleared
static void dcd_flush_tx_endpoint(dwc2_regs_t * dwc2, uint8_t epnum)
{
Expand Down Expand Up @@ -493,7 +495,20 @@ static void phy_hs_init(dwc2_regs_t * dwc2)
// Set max speed
uint32_t dcfg = dwc2->dcfg;
dcfg &= ~DCFG_DSPD_Msk;
dcfg |= DCFG_DSPD_HS << DCFG_DSPD_Pos;

// Handle the intended (max) speed
switch (_usb_max_speed)
{
// Limit to Full-Speed
case TUSB_SPEED_FULL:
dcfg |= DCFG_DSPD_FS_HSPHY << DCFG_DSPD_Pos;
break;

// Default to Hi-Speed if speed is not Full-Speed
default:
dcfg |= DCFG_DSPD_HS << DCFG_DSPD_Pos;
break;
}

// XCVRDLY: transceiver delay between xcvr_sel and txvalid during device chirp is required
// when using with some PHYs such as USB334x (USB3341, USB3343, USB3346, USB3347)
Expand Down Expand Up @@ -1366,4 +1381,9 @@ void dcd_int_handler(uint8_t rhport)
// }
}

void dcd_speed_set(tusb_speed_t speed)
{
_usb_max_speed = speed;
}

#endif

0 comments on commit 239beb9

Please sign in to comment.