Skip to content

Commit

Permalink
WIP error logging hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tlyu committed Dec 1, 2022
1 parent 7e7a034 commit e810339
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
31 changes: 31 additions & 0 deletions cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}

/*!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e810339

Please sign in to comment.