From 064507bfe8327703b097c0ef09a582733a14a055 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Sun, 13 Jul 2025 17:17:38 +0800 Subject: [PATCH] update(core/usbd_core): add ep0 state log Signed-off-by: sakumisu <1203593632@qq.com> --- cherryusb_config_template.h | 3 --- core/usbd_core.c | 53 ++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/cherryusb_config_template.h b/cherryusb_config_template.h index 4534d4fe..8b17aea8 100644 --- a/cherryusb_config_template.h +++ b/cherryusb_config_template.h @@ -47,9 +47,6 @@ #define CONFIG_USBDEV_REQUEST_BUFFER_LEN 512 #endif -/* Setup packet log for debug */ -// #define CONFIG_USBDEV_SETUP_LOG_PRINT - /* Send ep0 in data from user buffer instead of copying into ep0 reqdata * Please note that user buffer must be aligned with CONFIG_USB_ALIGN_SIZE */ diff --git a/core/usbd_core.c b/core/usbd_core.c index 2dc954d2..72136e1b 100644 --- a/core/usbd_core.c +++ b/core/usbd_core.c @@ -95,15 +95,25 @@ static void usbd_class_event_notify_handler(uint8_t busid, uint8_t event, void * static void usbd_print_setup(struct usb_setup_packet *setup) { - USB_LOG_INFO("Setup: " - "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n", - setup->bmRequestType, - setup->bRequest, - setup->wValue, - setup->wIndex, - setup->wLength); + USB_LOG_ERR("Setup: " + "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n", + setup->bmRequestType, + setup->bRequest, + setup->wValue, + setup->wIndex, + setup->wLength); } +#if (CONFIG_USB_DBG_LEVEL >= USB_DBG_LOG) +static const char *usb_ep0_state_string[] = { + "setup", + "indata", + "outdata", + "instatus", + "outstatus" +}; +#endif + static bool is_device_configured(uint8_t busid) { return (g_usbd_core[busid].configuration != 0); @@ -524,8 +534,6 @@ static bool usbd_set_interface(uint8_t busid, uint8_t iface, uint8_t alt_setting if_desc = (void *)p; } - USB_LOG_DBG("Current iface %u alt setting %u", - cur_iface, cur_alt_setting); break; case USB_DESCRIPTOR_TYPE_ENDPOINT: @@ -1171,9 +1179,14 @@ static void __usbd_event_ep0_setup_complete_handler(uint8_t busid, struct usb_se { uint8_t *buf; -#ifdef CONFIG_USBDEV_SETUP_LOG_PRINT - usbd_print_setup(setup); -#endif + USB_LOG_DBG("[%s] 0x%02x 0x%02x 0x%04x 0x%04x 0x%04x\r\n", + usb_ep0_state_string[usbd_get_ep0_next_state(busid)], + setup->bmRequestType, + setup->bRequest, + setup->wValue, + setup->wIndex, + setup->wLength); + if (setup->wLength > CONFIG_USBDEV_REQUEST_BUFFER_LEN) { if ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) { USB_LOG_ERR("Request buffer too small\r\n"); @@ -1240,7 +1253,6 @@ static void __usbd_event_ep0_setup_complete_handler(uint8_t busid, struct usb_se */ if ((setup->wLength > g_usbd_core[busid].ep0_data_buf_len) && (!(g_usbd_core[busid].ep0_data_buf_len % USB_CTRL_EP_MPS))) { g_usbd_core[busid].zlp_flag = true; - USB_LOG_DBG("EP0 Set zlp\r\n"); } } @@ -1266,7 +1278,10 @@ static void usbd_event_ep0_in_complete_handler(uint8_t busid, uint8_t ep, uint32 g_usbd_core[busid].ep0_data_buf += nbytes; g_usbd_core[busid].ep0_data_buf_residue -= nbytes; - USB_LOG_DBG("EP0 send %d bytes, %d remained\r\n", (unsigned int)nbytes, (unsigned int)g_usbd_core[busid].ep0_data_buf_residue); + USB_LOG_DBG("[%s] in %d bytes, %d remained\r\n", + usb_ep0_state_string[usbd_get_ep0_next_state(busid)], + (unsigned int)nbytes, + (unsigned int)g_usbd_core[busid].ep0_data_buf_residue); if (g_usbd_core[busid].ep0_data_buf_residue != 0) { /* Start sending the remain data */ @@ -1311,12 +1326,15 @@ static void usbd_event_ep0_out_complete_handler(uint8_t busid, uint8_t ep, uint3 (void)ep; (void)setup; + USB_LOG_DBG("[%s] out %d bytes, %d remained\r\n", + usb_ep0_state_string[usbd_get_ep0_next_state(busid)], + (unsigned int)nbytes, + (unsigned int)g_usbd_core[busid].ep0_data_buf_residue); + if (nbytes > 0) { g_usbd_core[busid].ep0_data_buf += nbytes; g_usbd_core[busid].ep0_data_buf_residue -= nbytes; - USB_LOG_DBG("EP0 recv %d bytes, %d remained\r\n", (unsigned int)nbytes, (unsigned int)g_usbd_core[busid].ep0_data_buf_residue); - if (g_usbd_core[busid].ep0_data_buf_residue == 0) { #ifdef CONFIG_USBDEV_EP0_THREAD usb_osal_mq_send(g_usbd_core[busid].usbd_ep0_mq, USB_EP0_STATE_OUT); @@ -1338,9 +1356,8 @@ static void usbd_event_ep0_out_complete_handler(uint8_t busid, uint8_t ep, uint3 usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue); } } else { - g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP; /* Read out status completely, do nothing */ - USB_LOG_DBG("EP0 recv out status\r\n"); + g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP; } }