Skip to content

Commit

Permalink
WIP trace logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tlyu committed Nov 25, 2022
1 parent d1a5c0b commit 172abb0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 6 deletions.
94 changes: 94 additions & 0 deletions cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ void EPBuffer<L>::flush()
return;
}
this->currentlyFlushing = true;
Serial1.print('_');
Serial1.print(this->ep);
Serial1.print('>');
Serial1.println(this->len());
Serial1.flush();

// Only attempt to send if the device is configured enough.
switch (USBCore().usbDev().cur_status) {
Expand All @@ -246,6 +251,11 @@ void EPBuffer<L>::flush()
// reset.
this->txWaiting = true;
USBCore().usbDev().drv_handler->ep_write((uint8_t*)this->buf, this->ep, this->len());
Serial1.print('>');
Serial1.print(this->ep);
Serial1.print('>');
Serial1.println(this->len());
Serial1.flush();
}
break;
}
Expand Down Expand Up @@ -300,6 +310,9 @@ uint8_t* EPBuffer<L>::ptr()
template<size_t L>
bool EPBuffer<L>::waitForReadComplete()
{
Serial1.print(F("RW"));
Serial1.println(this->ep);
Serial1.flush();
// auto start = getCurrentMillis();
auto ok = true;
while (ok && this->rxWaiting) {
Expand All @@ -309,6 +322,8 @@ bool EPBuffer<L>::waitForReadComplete()
// return false;
// }
}
Serial1.println(ok);
Serial1.flush();
return ok;
}

Expand Down Expand Up @@ -415,6 +430,8 @@ bool EPBuffers_<L, C>::pollEPStatus()
* part of a control transfer, or a non-control transfer,
* so we can decide whether to abort the flush.
*/
Serial1.println(F("SURPRISE SETUP"));
Serial1.flush();
}
usb_transc *transc = &USBCore().usbDev().transc_out[ep_num];
auto count = 0;
Expand Down Expand Up @@ -602,12 +619,38 @@ class ClassCore
void (*oldResetHandler)(usb_dev *usbd);
void handleReset(usb_dev *usbd)
{
Serial1.println(F("Reset"));
Serial1.flush();
EPBuffers().init();
oldResetHandler(usbd);
}

static void handleErr(void) {
Serial1.println(F("Error"));
Serial1.flush();
}

void (*oldSuspendHandler)();
void handleSuspend()
{
Serial1.println(F("Suspend"));
Serial1.flush();
oldSuspendHandler();
}

void (*oldResumeHandler)();
void handleResume()
{
usb_disable_interrupts();
Serial1.println(F("Resume"));
Serial1.flush();
usb_enable_interrupts();
oldResumeHandler();
}

USBCore_::USBCore_()
{
Serial1.begin(115200);
/*
* Use global ‘usbd’ here, instead of wrapped version, to avoid
* initialization loop.
Expand All @@ -618,6 +661,14 @@ USBCore_::USBCore_()
oldResetHandler = usbd.drv_handler->ep_reset;
usbd.drv_handler->ep_reset = handleReset;

usbd.drv_handler->err = handleErr;

oldSuspendHandler = usbd.drv_handler->suspend;
usbd.drv_handler->suspend = handleSuspend;

oldResumeHandler = usbd.drv_handler->suspend_leave;
usbd.drv_handler->suspend_leave = handleResume;

this->oldTranscSetup = usbd.ep_transc[0][TRANSC_SETUP];
usbd.ep_transc[0][TRANSC_SETUP] = USBCore_::transcSetupHelper;

Expand All @@ -644,6 +695,10 @@ void USBCore_::disconnect()
// Must be called via ISR
int USBCore_::sendControl(uint8_t flags, const void* data, int len)
{
Serial1.print(F("SC"));
Serial1.println(len);
Serial1.flush();

uint8_t* d = (uint8_t*)data;
auto usbd = &USBCore().usbDev();
auto l = min(len, this->maxWrite);
Expand All @@ -658,6 +713,11 @@ int USBCore_::sendControl(uint8_t flags, const void* data, int len)
if ((l != 0) && (flags & TRANSFER_RELEASE)) {
usbd->transc_in[0].xfer_buf = this->ctlBuf;
usbd->transc_in[0].xfer_len = this->ctlIdx;

Serial1.print(F("CF"));
Serial1.println(this->ctlIdx);
Serial1.flush();

}

// Return ‘len’, rather than ‘wrote’, because PluggableUSB
Expand Down Expand Up @@ -692,6 +752,18 @@ void USBCore_::ctlOut(usb_dev* usbd)
auto transc = &usbd->transc_out[0];
if (this->ctlOutBuf) {

Serial1.print(F("ctlOut "));
Serial1.println(this->ctlOutLen);
Serial1.print('<');
for (int i = 0; i < this->ctlOutLen; i++) {
uint8_t x = this->ctlBuf[i];
Serial1.print((uint8_t)(x >> 4), 16);
Serial1.print((uint8_t)(x & 0x0f), 16);
Serial1.print(' ');
}
Serial1.println();
Serial1.flush();

memcpy(this->ctlOutBuf, this->ctlBuf, this->ctlOutLen);
this->ctlOutBuf = NULL;
}
Expand Down Expand Up @@ -735,9 +807,18 @@ int USBCore_::send(uint8_t ep, const void* data, int len)
auto wrote = 0;
auto usbd = &USBCore().usbDev();

usb_disable_interrupts();
Serial1.print('S');
Serial1.print(ep);
Serial1.print('>');
Serial1.println(len);
Serial1.flush();
usb_enable_interrupts();
#ifdef USBD_REMOTE_WAKEUP
usb_disable_interrupts();
if (usbd->cur_status == USBD_SUSPENDED && usbd->pm.remote_wakeup) {
Serial1.println(F("Remote wakeup"));
Serial1.flush();
usb_enable_interrupts();
usbd_remote_wakeup_active(usbd);
} else {
Expand Down Expand Up @@ -799,6 +880,9 @@ int USBCore_::flush(uint8_t ep)
auto usbd = &USBCore().usbDev();
usbd->transc_in[0].xfer_buf = ctlBuf;
usbd->transc_in[0].xfer_len = ctlIdx;
Serial1.print(F("CF"));
Serial1.println(ctlIdx);
Serial1.flush();
} else {
EPBuffers().buf(ep).flush();
}
Expand Down Expand Up @@ -859,6 +943,15 @@ void USBCore_::transcSetup(usb_dev* usbd, uint8_t ep)
return;
}

for (int i = 0; i < 8; i++) {
uint8_t x = ((uint8_t *)&usbd->control.req)[i];
Serial1.print((uint8_t)(x >> 4), 16);
Serial1.print((uint8_t)(x & 0x0f), 16);
Serial1.print((char)' ');
}
Serial1.println();
Serial1.flush();

this->maxWrite = usbd->control.req.wLength;
switch (usbd->control.req.bmRequestType & USB_REQTYPE_MASK) {
/* standard device request */
Expand Down Expand Up @@ -968,6 +1061,7 @@ void USBCore_::sendDeviceConfigDescriptor()

void USBCore_::sendZLP(usb_dev* usbd, uint8_t ep)
{
// Serial1.println(F("ZLP"));
usbd->drv_handler->ep_write(nullptr, ep, 0);
}

Expand Down
4 changes: 2 additions & 2 deletions cores/arduino/gd32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ void usb_connect()
void usb_enable_interrupts()
{
/* 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;
}

void usb_disable_interrupts()
{
/* 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);
}

void usb_disconnect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ struct _usb_dev {

__IO uint8_t cur_status;
__IO uint8_t backup_status;
__IO uint8_t nreset;
__IO uint8_t nsusp;
__IO uint8_t nresume;
__IO uint8_t nsuspsusp;
__IO uint8_t nerror;

usb_pm pm;
#ifdef LPM_ENABLED
Expand Down Expand Up @@ -195,6 +200,7 @@ struct _usb_handler {
void (*suspend) (void);
void (*suspend_leave) (void);
void (*resume) (usb_dev *udev);
void (*err) (void);

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 @@ -72,13 +72,16 @@ static void usbd_suspend (void);
static void usbd_leave_suspend (void);
static uint8_t usbd_ep_status (usb_dev *udev, uint8_t ep_addr);

void usbd_err(void) {}

struct _usb_handler usbd_drv_handler = {
.dp_pullup = usbd_dp_pullup,
.init = usbd_core_reset,
.deinit = usbd_core_stop,
.suspend = usbd_suspend,
.suspend_leave = usbd_leave_suspend,
.resume = usbd_resume,
.err = usbd_err,
.set_addr = usbd_address_set,
.ep_reset = usbd_ep_reset,
.ep_disable = usbd_ep_disable,
Expand Down Expand Up @@ -154,7 +157,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 @@ -163,12 +163,15 @@ void usbd_isr (void)
}
}

if (INTF_ERRIF & int_flag) {
udev->drv_handler->err();
udev->nerror++;
CLR(ERRIF);
}
if (INTF_WKUPIF & int_flag) {
/* clear wakeup interrupt flag in INTF */
CLR(WKUPIF);

/* restore the old cur_status */
udev->cur_status = udev->backup_status;
udev->nresume++;

#ifdef LPM_ENABLED
if ((0U == udev->pm.remote_wakeup_on) && (0U == udev->lpm.L1_resume)) {
Expand All @@ -186,6 +189,9 @@ void usbd_isr (void)
resume_mcu(udev);
}
#endif /* LPM_ENABLED */

/* clear wakeup interrupt flag in INTF */
CLR(WKUPIF);
}

if (INTF_SPSIF & int_flag) {
Expand Down Expand Up @@ -233,6 +239,7 @@ void usbd_isr (void)
CLR(RSTIF);

udev->drv_handler->ep_reset(udev);
udev->nreset++;
}

#ifdef LPM_ENABLED
Expand Down Expand Up @@ -285,7 +292,10 @@ static void usbd_int_suspend (usb_dev *udev)
*/
if (udev->cur_status != USBD_SUSPENDED) {
udev->backup_status = udev->cur_status;
} else {
udev->nsuspsusp++;
}
udev->nsusp++;

/* set device in suspended state */
udev->cur_status = (uint8_t)USBD_SUSPENDED;
Expand Down

0 comments on commit 172abb0

Please sign in to comment.