feat(port): implement usbd_ep_is_stalled api

This commit is contained in:
sakumisu
2024-07-08 21:50:00 +08:00
parent 4f3a3f496e
commit 7fab3c29f0
5 changed files with 83 additions and 8 deletions

View File

@@ -99,9 +99,9 @@ static bool is_device_configured(uint8_t busid)
static bool usbd_set_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep) static bool usbd_set_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep)
{ {
USB_LOG_DBG("Open ep:0x%02x type:%u mps:%u\r\n", USB_LOG_DBG("Open ep:0x%02x type:%u mps:%u\r\n",
ep->bEndpointAddress, ep->bEndpointAddress,
USB_GET_ENDPOINT_TYPE(ep->bmAttributes), USB_GET_ENDPOINT_TYPE(ep->bmAttributes),
USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize)); USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize));
if (ep->bEndpointAddress & 0x80) { if (ep->bEndpointAddress & 0x80) {
g_usbd_core[busid].tx_msg[ep->bEndpointAddress & 0x7f].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize); g_usbd_core[busid].tx_msg[ep->bEndpointAddress & 0x7f].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
@@ -126,8 +126,8 @@ static bool usbd_set_endpoint(uint8_t busid, const struct usb_endpoint_descripto
static bool usbd_reset_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep) static bool usbd_reset_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep)
{ {
USB_LOG_DBG("Close ep:0x%02x type:%u\r\n", USB_LOG_DBG("Close ep:0x%02x type:%u\r\n",
ep->bEndpointAddress, ep->bEndpointAddress,
USB_GET_ENDPOINT_TYPE(ep->bmAttributes)); USB_GET_ENDPOINT_TYPE(ep->bmAttributes));
return usbd_ep_close(busid, ep->bEndpointAddress) == 0 ? true : false; return usbd_ep_close(busid, ep->bEndpointAddress) == 0 ? true : false;
} }
@@ -556,14 +556,16 @@ static bool usbd_std_device_req_handler(uint8_t busid, struct usb_setup_packet *
break; break;
case USB_REQUEST_GET_CONFIGURATION: case USB_REQUEST_GET_CONFIGURATION:
*data = (uint8_t *)&g_usbd_core[busid].configuration; (*data)[0] = g_usbd_core[busid].configuration;
*len = 1; *len = 1;
break; break;
case USB_REQUEST_SET_CONFIGURATION: case USB_REQUEST_SET_CONFIGURATION:
value &= 0xFF; value &= 0xFF;
if (!usbd_set_configuration(busid, value, 0)) { if (value == 0) {
g_usbd_core[busid].configuration = 0;
} else if (!usbd_set_configuration(busid, value, 0)) {
ret = false; ret = false;
} else { } else {
g_usbd_core[busid].configuration = value; g_usbd_core[busid].configuration = value;
@@ -663,6 +665,7 @@ static bool usbd_std_endpoint_req_handler(uint8_t busid, struct usb_setup_packet
{ {
uint8_t ep = (uint8_t)setup->wIndex; uint8_t ep = (uint8_t)setup->wIndex;
bool ret = true; bool ret = true;
uint8_t stalled;
/* Only when device is configured, then endpoint requests can be valid. */ /* Only when device is configured, then endpoint requests can be valid. */
if (!is_device_configured(busid)) { if (!is_device_configured(busid)) {
@@ -671,7 +674,12 @@ static bool usbd_std_endpoint_req_handler(uint8_t busid, struct usb_setup_packet
switch (setup->bRequest) { switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS: case USB_REQUEST_GET_STATUS:
(*data)[0] = 0x00; usbd_ep_is_stalled(busid, ep, &stalled);
if (stalled) {
(*data)[0] = 0x01;
} else {
(*data)[0] = 0x00;
}
(*data)[1] = 0x00; (*data)[1] = 0x00;
*len = 2; *len = 2;
break; break;

View File

@@ -822,6 +822,29 @@ int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep)
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled) int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{ {
uint32_t regval;
uint8_t ep_idx = USB_EP_GET_IDX(ep);
if (ep_idx == 0) {
} else {
if (USB_EP_DIR_IS_OUT(ep)) {
regval = getreg32(BFLB_USB_BASE + USB_DEV_OUTMPS1_OFFSET + (ep_idx - 1) * 4);
if (regval & USB_STL_OEP1) {
*stalled = 1;
} else {
*stalled = 0;
}
} else {
regval = getreg32(BFLB_USB_BASE + USB_DEV_INMPS1_OFFSET + (ep_idx - 1) * 4);
if (regval & USB_STL_IEP1) {
*stalled = 1;
} else {
*stalled = 0;
}
}
}
return 0; return 0;
} }

View File

@@ -834,8 +834,20 @@ int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep)
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled) int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{ {
uint8_t ep_idx = USB_EP_GET_IDX(ep);
if (USB_EP_DIR_IS_OUT(ep)) { if (USB_EP_DIR_IS_OUT(ep)) {
if(USB_OTG_OUTEP(ep_idx)->DOEPCTL & USB_OTG_DOEPCTL_STALL) {
*stalled = 1;
} else {
*stalled = 0;
}
} else { } else {
if(USB_OTG_INEP(ep_idx)->DIEPCTL & USB_OTG_DIEPCTL_STALL) {
*stalled = 1;
} else {
*stalled = 0;
}
} }
return 0; return 0;
} }

View File

@@ -255,8 +255,20 @@ int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep)
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled) int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{ {
uint8_t ep_idx = USB_EP_GET_IDX(ep);
if (USB_EP_DIR_IS_OUT(ep)) { if (USB_EP_DIR_IS_OUT(ep)) {
if (PCD_GET_EP_RX_STATUS(USB, ep_idx) & USB_EP_RX_STALL) {
*stalled = 1;
} else {
*stalled = 0;
}
} else { } else {
if (PCD_GET_EP_TX_STATUS(USB, ep_idx) & USB_EP_TX_STALL) {
*stalled = 1;
} else {
*stalled = 0;
}
} }
return 0; return 0;
} }

View File

@@ -501,6 +501,26 @@ int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep)
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled) int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{ {
uint8_t ep_idx = USB_EP_GET_IDX(ep);
uint8_t old_ep_idx;
old_ep_idx = musb_get_active_ep();
musb_set_active_ep(ep_idx);
if (USB_EP_DIR_IS_OUT(ep)) {
if(HWREGB(USB_BASE + MUSB_IND_RXCSRL_OFFSET) & USB_RXCSRL1_STALLED) {
*stalled = 1;
} else {
*stalled = 0;
}
} else {
if(HWREGB(USB_BASE + MUSB_IND_TXCSRL_OFFSET) & USB_TXCSRL1_STALLED) {
*stalled = 1;
} else {
*stalled = 0;
}
}
musb_set_active_ep(old_ep_idx);
return 0; return 0;
} }