Skip to content

Commit

Permalink
WIP logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tlyu committed Nov 27, 2022
1 parent f85bc4c commit 30646c3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 63 deletions.
136 changes: 76 additions & 60 deletions cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,7 @@ void EPBuffer<L>::flush()
return;
}
this->currentlyFlushing = true;
Serial1.print('_');
Serial1.print(this->ep);
Serial1.print('>');
Serial1.println(this->len());
Serial1.flush();
USBCore().logEP('_', this->ep, '>', this->len());

// Only attempt to send if the device is configured enough.
switch (USBCore().usbDev().cur_status) {
Expand All @@ -250,12 +246,8 @@ void EPBuffer<L>::flush()
// Only start the next transmission if the device hasn't been
// 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();
usbd_ep_send(&USBCore().usbDev(), this->ep, (uint8_t *)this->buf, this->len());
USBCore().logEP('>', this->ep, '>', this->len());
}
break;
}
Expand Down Expand Up @@ -441,6 +433,7 @@ class ClassCore
// Called when ep0 gets a SETUP packet after configuration.
static uint8_t reqProcess(usb_dev* usbd, usb_req* req)
{
USBCore().logStatus("ClassCore");
(void)usbd;

// TODO: remove this copy.
Expand Down Expand Up @@ -508,31 +501,37 @@ class ClassCore
void (*oldResetHandler)(usb_dev *usbd);
void handleReset(usb_dev *usbd)
{
Serial1.println(F("Reset"));
Serial1.flush();
USBCore().logStatus("Reset");
EPBuffers().init();
oldResetHandler(usbd);
}

static void handleErr(void) {
Serial1.println(F("Error"));
Serial1.flush();
static void handleErr(uint8_t len)
{
if (len == 0xff) {
USBCore().logStatus("Error");
} else {
Serial1.println((uint16_t)USBD_EPxCS(0), 16);
uint8_t buf[64] = {0};
USBCore().logEP('!', 0, '<', len);
len = USBCore().usbDev().drv_handler->ep_read(buf, 0, (uint8_t)EP_BUF_SNG);
USBCore().hexDump('!', buf, 64);
USBCore().logEP('.', 0, '!', len);
}
}

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

void (*oldResumeHandler)();
void handleResume()
{
usb_disable_interrupts();
Serial1.println(F("Resume"));
Serial1.flush();
USBCore().logStatus("Resume");
usb_enable_interrupts();
oldResumeHandler();
}
Expand Down Expand Up @@ -560,6 +559,39 @@ USBCore_::USBCore_()

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

this->oldTranscOut = usbd.ep_transc[0][TRANSC_OUT];
usbd.ep_transc[0][TRANSC_OUT] = USBCore_::transcOutHelper;

this->oldTranscIn = usbd.ep_transc[0][TRANSC_IN];
usbd.ep_transc[0][TRANSC_IN] = USBCore_::transcInHelper;
}

void USBCore_::logEP(char kind, uint8_t ep, char dir, size_t len)
{
Serial1.print(kind);
Serial1.print(ep);
Serial1.print(dir);
Serial1.println(len);
// Serial1.flush();
}

void USBCore_::hexDump(char prefix, const uint8_t *buf, size_t len)
{
Serial1.print(prefix);
for (size_t i = 0; i < len; i++) {
Serial1.print(buf[i] >> 4, 16);
Serial1.print((buf[i] & 0x0f), 16);
Serial1.print(' ');
}
Serial1.println();
// Serial1.flush();
}

void USBCore_::logStatus(const char *status)
{
Serial1.println(status);
// Serial1.flush();
}

void USBCore_::connect()
Expand Down Expand Up @@ -594,9 +626,7 @@ void USBCore_::setupClass(uint16_t wLength)
// Must be called via ISR, or when the endpoint isn't in VALID status.
int USBCore_::sendControl(uint8_t flags, const void* data, int len)
{
Serial1.print(F("SC"));
Serial1.println(len);
Serial1.flush();
USBCore().logEP('+', 0, '>', len);

uint8_t* d = (uint8_t*)data;
auto usbd = &USBCore().usbDev();
Expand Down Expand Up @@ -636,6 +666,7 @@ int USBCore_::recvControl(void* data, int len)
this->ctlOutBuf = (uint8_t *)data;
this->ctlOutLen = len;
usb_transc_config(&USBCore().usbDev().transc_out[0], this->ctlBuf, len, 0);
USBCore().logEP('_', 0, '<', len);
return len;
}

Expand All @@ -646,18 +677,7 @@ 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();
USBCore().hexDump('<', this->ctlBuf, transc->xfer_count);

memcpy(this->ctlOutBuf, this->ctlBuf, this->ctlOutLen);
this->ctlOutBuf = NULL;
Expand Down Expand Up @@ -703,17 +723,12 @@ int USBCore_::send(uint8_t ep, const void* data, int len)
auto usbd = &USBCore().usbDev();

usb_disable_interrupts();
Serial1.print('S');
Serial1.print(ep);
Serial1.print('>');
Serial1.println(len);
Serial1.flush();
USBCore().logEP('+', ep, '>', len);
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();
USBCore().logStatus("Remote wakeup");
usb_enable_interrupts();
usbd_remote_wakeup_active(usbd);
} else {
Expand Down Expand Up @@ -771,13 +786,12 @@ int USBCore_::recv(uint8_t ep)
// Flushes an outbound transmission as soon as possible.
int USBCore_::flush(uint8_t ep)
{
if (ep == 0 && ctlIdx != 0) {
if (ep == 0) {
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();
USBCore().logEP('_', 0, '>', ctlIdx);
USBCore().hexDump('>', ctlBuf, ctlIdx);
} else {
EPBuffers().buf(ep).flush();
}
Expand Down Expand Up @@ -811,36 +825,39 @@ usb_dev& USBCore_::usbDev()
void USBCore_::transcSetup(usb_dev* usbd, uint8_t ep)
{
(void)ep;
(void)usbd->drv_handler->ep_read((uint8_t *)(&usbd->control.req), 0, (uint8_t)EP_BUF_SNG);
Serial1.println((uint16_t)USBD_EPxCS(0), 16);
auto count = usbd->drv_handler->ep_read((uint8_t *)(&usbd->control.req), 0, (uint8_t)EP_BUF_SNG);

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();
USBCore().hexDump('^', (uint8_t *)&usbd->control.req, count);

this->oldTranscSetup(usbd, ep);
}

// Called in interrupt context.
void USBCore_::transcOut(usb_dev* usbd, uint8_t ep)
{
auto transc = &usbd->transc_out[ep];
auto count = transc->xfer_count;
if (ep == 0) {
return;
this->oldTranscOut(usbd, ep);
} else {
EPBuffers().buf(ep).transcOut();
}
EPBuffers().buf(ep).transcOut();
USBCore().logEP('.', ep, '<', count);
}

// Called in interrupt context.
void USBCore_::transcIn(usb_dev* usbd, uint8_t ep)
{
auto transc = &usbd->transc_in[ep];
USBCore().logEP('.', ep, '>', transc->xfer_count);

if (ep == 0) {
return;
this->oldTranscIn(usbd, ep);
} else {
EPBuffers().buf(ep).transcIn();
}
EPBuffers().buf(ep).transcIn();
transc->xfer_count = 0;
}

void USBCore_::buildDeviceConfigDescriptor()
Expand Down Expand Up @@ -872,7 +889,6 @@ void USBCore_::buildDeviceConfigDescriptor()

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: 4 additions & 0 deletions cores/arduino/USBCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ class USBCore_

void setupClass(uint16_t wLength);
void ctlOut(usb_dev* udev);

void logEP(char kind, uint8_t ep, char dir, size_t len);
void hexDump(char prefix, const uint8_t *buf, size_t len);
void logStatus(const char *status);
/*
* Static member function helpers called from ISR.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ struct _usb_handler {
void (*suspend) (void);
void (*suspend_leave) (void);
void (*resume) (usb_dev *udev);
void (*err) (void);
void (*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 @@ -57,6 +57,7 @@ void _usb_setup_transc (usb_dev *udev, uint8_t ep_num)
uint16_t count = udev->drv_handler->ep_read((uint8_t *)(&udev->control.req), 0U, (uint8_t)EP_BUF_SNG);

if (count != USB_SETUP_PACKET_LEN) {
udev->drv_handler->err(count);
usb_stall_transc(udev);

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ 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) {}
void usbd_err(uint8_t len) { (void)len; }

struct _usb_handler usbd_drv_handler = {
.dp_pullup = usbd_dp_pullup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void usbd_isr (void)
}

if (INTF_ERRIF & int_flag) {
udev->drv_handler->err();
udev->drv_handler->err(0xff);
udev->nerror++;
CLR(ERRIF);
}
Expand Down

0 comments on commit 30646c3

Please sign in to comment.