Skip to content

Commit ba02d34

Browse files
committed
Add USB transfer RX/TX LED blinking
Only enabled if PIN_LED_TXL or PIN_LED_RXL is defined
1 parent dd6890a commit ba02d34

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cores/arduino/USB/USBCore.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828

2929
USBDevice_SAMD21G18x usbd;
3030

31+
/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
32+
#define TX_RX_LED_PULSE_MS 100
33+
#ifdef PIN_LED_TXL
34+
static volatile uint8_t txLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
35+
#endif
36+
#ifdef PIN_LED_RXL
37+
static volatile uint8_t rxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
38+
#endif
3139
static char isRemoteWakeUpEnabled = 0;
3240
static char isEndpointHalt = 0;
3341

@@ -273,6 +281,18 @@ void USBDeviceClass::handleEndpoint(uint8_t ep)
273281

274282
void USBDeviceClass::init()
275283
{
284+
#ifdef PIN_LED_TXL
285+
txLEDPulse = 0;
286+
pinMode(PIN_LED_TXL, OUTPUT);
287+
digitalWrite(PIN_LED_TXL, HIGH);
288+
#endif
289+
290+
#ifdef PIN_LED_RXL
291+
rxLEDPulse = 0;
292+
pinMode(PIN_LED_RXL, OUTPUT);
293+
digitalWrite(PIN_LED_RXL, HIGH);
294+
#endif
295+
276296
// Enable USB clock
277297
PM->APBBMASK.reg |= PM_APBBMASK_USB;
278298

@@ -522,6 +542,11 @@ uint32_t USBDeviceClass::recv(uint32_t ep, void *_data, uint32_t len)
522542
if (available(ep) < len)
523543
len = available(ep);
524544

545+
#ifdef PIN_LED_RXL
546+
digitalWrite(PIN_LED_RXL, LOW);
547+
rxLEDPulse = TX_RX_LED_PULSE_MS;
548+
#endif
549+
525550
armRecv(ep);
526551

527552
usbd.epBank0DisableTransferComplete(ep);
@@ -620,6 +645,11 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
620645
}
621646
#endif
622647

648+
#ifdef PIN_LED_TXL
649+
digitalWrite(PIN_LED_TXL, LOW);
650+
txLEDPulse = TX_RX_LED_PULSE_MS;
651+
#endif
652+
623653
// Flash area
624654
while (len != 0)
625655
{
@@ -826,6 +856,17 @@ void USBDeviceClass::ISRHandler()
826856
if (usbd.isStartOfFrameInterrupt())
827857
{
828858
usbd.ackStartOfFrameInterrupt();
859+
860+
// check whether the one-shot period has elapsed. if so, turn off the LED
861+
#ifdef PIN_LED_TXL
862+
if (txLEDPulse && !(--txLEDPulse))
863+
digitalWrite(PIN_LED_TXL, HIGH);
864+
#endif
865+
866+
#ifdef PIN_LED_RXL
867+
if (rxLEDPulse && !(--rxLEDPulse))
868+
digitalWrite(PIN_LED_RXL, HIGH);
869+
#endif
829870
}
830871

831872
// Endpoint 0 Received Setup interrupt

0 commit comments

Comments
 (0)