From 81603cc836ba74a253e7c79ef94e9d3bcde6a777 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Thu, 8 Dec 2022 22:32:50 -0600 Subject: [PATCH] WIP timeouts --- cores/arduino/USBCore.cpp | 24 ++++++++++++++++++++++++ cores/arduino/USBCore.h | 6 ++++++ 2 files changed, 30 insertions(+) diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index e82acc38..b68a1ea6 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -133,6 +133,14 @@ void EPBuffer::init(uint8_t ep) this->reset(); this->rxWaiting = false; this->txWaiting = false; + this->timedOut = false; + this->timeout = 100; +} + +template +void EPBuffer::setTimeout(uint16_t timeout) +{ + this->timeout = timeout; } template @@ -236,6 +244,9 @@ void EPBuffer::flush() // fall through case USBD_CONFIGURED: case USBD_SUSPENDED: { + if (this->timedOut) { + break; + } // This will temporarily reenable and disable interrupts auto canWrite = this->waitForWriteComplete(); if (canWrite) { @@ -246,6 +257,7 @@ void EPBuffer::flush() // Only start the next transmission if the device hasn't been // reset. this->txWaiting = true; + this->startTime = millis(); usbd_ep_send(&USBCore().usbDev(), this->ep, (uint8_t *)this->buf, this->len()); USBCore().logEP('>', this->ep, '>', this->len()); } @@ -287,6 +299,7 @@ template void EPBuffer::transcIn() { this->txWaiting = false; + this->timedOut = false; } // Unused? @@ -306,6 +319,12 @@ bool EPBuffer::waitForWriteComplete() auto ok = true; do { usb_enable_interrupts(); + if (this->txWaiting && millis() - this->startTime > this->timeout) { + ok = false; + this->timedOut = true; + USBCore().logEP('X', this->ep, '>', this->len()); + break; + } switch (USBCore().usbDev().cur_status) { case USBD_DEFAULT: case USBD_ADDRESSED: @@ -854,6 +873,11 @@ int USBCore_::flush(uint8_t ep) return 0; } +void USBCore_::setTimeout(uint8_t ep, uint16_t timeout) +{ + EPBuffers().buf(ep).setTimeout(timeout); +} + void USBCore_::transcSetupHelper(usb_dev* usbd, uint8_t ep) { USBCore_* core = (USBCore_*)usbd->user_data; diff --git a/cores/arduino/USBCore.h b/cores/arduino/USBCore.h index dfa150e6..3c34e5d4 100644 --- a/cores/arduino/USBCore.h +++ b/cores/arduino/USBCore.h @@ -78,6 +78,7 @@ class EPBuffer void flush(); uint8_t* ptr(); void enableOutEndpoint(); + void setTimeout(uint16_t timeout); void transcIn(); void transcOut(); @@ -114,6 +115,10 @@ class EPBuffer */ volatile bool currentlyFlushing = false; + volatile uint32_t startTime; + uint16_t timeout; + volatile bool timedOut; + uint8_t ep; }; @@ -155,6 +160,7 @@ class USBCore_ int recv(uint8_t ep, void* data, int len); int recv(uint8_t ep); int flush(uint8_t ep); + void setTimeout(uint8_t ep, uint16_t timeout); // Debug counters volatile uint16_t nreset;