From e810339acdea050dd53e71bc8772fac197c2fe10 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Wed, 30 Nov 2022 15:51:07 -0600 Subject: [PATCH] WIP error logging hook --- cores/arduino/USBCore.cpp | 31 +++++++++++++++++++ .../device/Include/usbd_core.h | 2 ++ .../usbd/Source/usbd_lld_core.c | 6 +++- .../usbd/Source/usbd_lld_int.c | 5 +++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index d3a3d90e..78e19321 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -506,6 +506,34 @@ void handleReset(usb_dev *usbd) oldResetHandler(usbd); } +static void handleSetupErr(uint8_t len) +{ + // Serial1.println((uint16_t)USBD_EPxCS(0), 16); + USBCore().logEP('!', 0, '<', len); + usbd_ep_ram *btable_ep = (usbd_ep_ram *)(USBD_RAM + 2 * (BTABLE_OFFSET & 0xFFF8)); + uint32_t *p = (uint32_t *)(USBD_RAM + 2U * EP0_RX_ADDR); + Serial1.print('!'); + for (int i = 0; i < USBD_EP0_MAX_SIZE/2; i++) { + uint16_t x = p[i]; + Serial1.print((x >> 4) & 0x0f, 16); + Serial1.print(x & 0x0f, 16); + // Serial1.print(' '); + Serial1.print((x >> 12) & 0x0f, 16); + Serial1.print((x >> 8) & 0x0f, 16); + // Serial1.print(' '); + } + Serial1.println(); + // len = USBCore().usbDev().drv_handler->ep_read(buf, 0, (uint8_t)EP_BUF_SNG); + // USBCore().hexDump('!', buf, 64); + auto bytes = (uint16_t)(btable_ep[0].rx_count & EPRCNT_CNT); + USBCore().logEP('.', 0, '!', bytes); +} + +static void handleErr() +{ + USBCore().logStatus("Error"); +} + void (*oldSuspendHandler)(); void handleSuspend() { @@ -535,6 +563,9 @@ USBCore_::USBCore_() oldResetHandler = usbd.drv_handler->ep_reset; usbd.drv_handler->ep_reset = handleReset; + usbd.drv_handler->err = handleErr; + usbd.drv_handler->setup_err = handleSetupErr; + oldSuspendHandler = usbd.drv_handler->suspend; usbd.drv_handler->suspend = handleSuspend; diff --git a/system/GD32F30x_firmware/GD32F30x_usbd_library/device/Include/usbd_core.h b/system/GD32F30x_firmware/GD32F30x_usbd_library/device/Include/usbd_core.h index 9106af0c..4e675f00 100644 --- a/system/GD32F30x_firmware/GD32F30x_usbd_library/device/Include/usbd_core.h +++ b/system/GD32F30x_firmware/GD32F30x_usbd_library/device/Include/usbd_core.h @@ -219,6 +219,8 @@ struct _usb_handler void (*suspend) (void); void (*suspend_leave) (void); void (*resume) (usb_dev *udev); + void (*err) (void); + void (*setup_err) (uint8_t len); void (*ep_reset) (usb_dev *udev); void (*ep_setup) (usb_dev *udev, uint8_t buf_kind, uint32_t buf_addr, const usb_desc_ep *ep_desc); diff --git a/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c b/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c index 280bb4a4..a3e9f645 100644 --- a/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c +++ b/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_core.c @@ -71,6 +71,8 @@ static void usbd_ep_stall_clear (usb_dev *udev, uint8_t ep_addr); static void usbd_ep_data_write (uint8_t *user_fifo, uint8_t ep_num, uint16_t bytes); static uint16_t usbd_ep_data_read (uint8_t *user_fifo, uint8_t ep_num, uint8_t buf_kind); static void usbd_resume (usb_dev *udev); +static void usbd_err(void) {} +static void usbd_setup_err(uint8_t len) { (void)len; } static void usbd_suspend (void); static void usbd_leave_suspend (void); static uint16_t usbd_ep_status (usb_dev *udev, uint8_t ep_addr); @@ -83,6 +85,8 @@ struct _usb_handler usbd_drv_handler = .suspend = usbd_suspend, .suspend_leave = usbd_leave_suspend, .resume = usbd_resume, + .err = usbd_err, + .setup_err = usbd_setup_err, .set_addr = usbd_address_set, .ep_reset = usbd_ep_reset, .ep_disable = usbd_ep_disable, @@ -158,7 +162,7 @@ static void usbd_core_reset (void) #endif /* LPM_ENABLED */ /* enable all interrupts mask bits */ - USBD_CTL |= CTL_STIE | CTL_WKUPIE | CTL_SPSIE | CTL_SOFIE | CTL_ESOFIE | CTL_RSTIE; + USBD_CTL |= CTL_STIE | CTL_ERRIE | CTL_WKUPIE | CTL_SPSIE | CTL_SOFIE | CTL_ESOFIE | CTL_RSTIE; } /*! diff --git a/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_int.c b/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_int.c index 0b15fcb7..a714f5ac 100644 --- a/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_int.c +++ b/system/GD32F30x_firmware/GD32F30x_usbd_library/usbd/Source/usbd_lld_int.c @@ -129,6 +129,7 @@ void usbd_isr (void) USBD_EP_RX_ST_CLEAR(ep_num); if (count != USB_SETUP_PACKET_LEN) { + udev->drv_handler->setup_err(count); usb_stall_transc(udev); return; @@ -178,6 +179,10 @@ void usbd_isr (void) } } + if (INTF_ERRIF & int_flag) { + udev->drv_handler->err(); + CLR(ERRIF); + } if (INTF_WKUPIF & int_flag) { /* restore the old cur_status */ udev->cur_status = udev->backup_status;