Skip to content

Commit 667da69

Browse files
committed
added automatic one-shot TX and RX LED control for sketch USB
1 parent 6b7d24e commit 667da69

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

hardware/arduino/cores/arduino/USBCore.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
#define EP_TYPE_ISOCHRONOUS_IN 0x41
3131
#define EP_TYPE_ISOCHRONOUS_OUT 0x40
3232

33+
/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
34+
#define TX_RX_LED_PULSE_MS 100
35+
volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
36+
volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
37+
3338
//==================================================================
3439
//==================================================================
3540

@@ -108,11 +113,17 @@ void Recv(volatile u8* data, u8 count)
108113
{
109114
while (count--)
110115
*data++ = UEDATX;
116+
117+
RXLED1; // light the RX LED
118+
RxLEDPulse = TX_RX_LED_PULSE_MS;
111119
}
112120

113121
static inline u8 Recv8()
114122
{
115-
return UEDATX;
123+
RXLED1; // light the RX LED
124+
RxLEDPulse = TX_RX_LED_PULSE_MS;
125+
126+
return UEDATX;
116127
}
117128

118129
static inline void Send8(u8 d)
@@ -206,13 +217,13 @@ u8 USB_Available(u8 ep)
206217
return FifoByteCount();
207218
}
208219

209-
// Non Blocking recieve
220+
// Non Blocking receive
210221
// Return number of bytes read
211222
int USB_Recv(u8 ep, void* d, int len)
212223
{
213224
if (!_usbConfiguration || len < 0)
214225
return -1;
215-
226+
216227
LockEP lock(ep);
217228
u8 n = FifoByteCount();
218229
len = min(n,len);
@@ -222,7 +233,7 @@ int USB_Recv(u8 ep, void* d, int len)
222233
*dst++ = Recv8();
223234
if (len && !FifoByteCount()) // release empty buffer
224235
ReleaseRX();
225-
236+
226237
return len;
227238
}
228239

@@ -289,6 +300,8 @@ int USB_Send(u8 ep, const void* d, int len)
289300
ReleaseTX();
290301
}
291302
}
303+
TXLED1; // light the TX LED
304+
TxLEDPulse = TX_RX_LED_PULSE_MS;
292305
return r;
293306
}
294307

@@ -306,11 +319,6 @@ const u8 _initEndpoints[] =
306319
#ifdef HID_ENABLED
307320
EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
308321
#endif
309-
310-
#ifdef MSC_ENABLED
311-
EP_TYPE_BULK_OUT, // MSC_ENDPOINT_OUT
312-
EP_TYPE_BULK_IN, // MSC_ENDPOINT_IN
313-
#endif
314322
};
315323

316324
#define EP_SINGLE_64 0x32 // EP0
@@ -350,11 +358,6 @@ bool ClassInterfaceRequest(Setup& setup)
350358
return CDC_Setup(setup);
351359
#endif
352360

353-
#ifdef MSC_ENABLED
354-
if (MSC_INTERFACE == i)
355-
return MSC_Setup(setup);
356-
#endif
357-
358361
#ifdef HID_ENABLED
359362
if (HID_INTERFACE == i)
360363
return HID_Setup(setup);
@@ -425,9 +428,6 @@ int SendInterfaces()
425428
total += HID_GetInterface(&interfaces);
426429
#endif
427430

428-
#ifdef MSC_ENABLED
429-
total += MSC_GetInterface(&interfaces);
430-
#endif
431431
return interfaces;
432432
}
433433

@@ -504,8 +504,6 @@ ISR(USB_COM_vect)
504504
Recv((u8*)&setup,8);
505505
ClearSetupInt();
506506

507-
//printHex((u8*)&setup,8);
508-
509507
u8 requestType = setup.bmRequestType;
510508
if (requestType & REQUEST_DEVICETOHOST)
511509
WaitIN();
@@ -596,12 +594,18 @@ ISR(USB_GEN_vect)
596594
UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
597595
}
598596

599-
// Start of Frame
597+
// Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
600598
if (udint & (1<<SOFI))
601599
{
602600
#ifdef CDC_ENABLED
603601
USB_Flush(CDC_TX); // Send a tx frame if found
604602
#endif
603+
604+
// check whether the one-shot period has elapsed. if so, turn off the LED
605+
if (TxLEDPulse && !(--TxLEDPulse))
606+
TXLED0;
607+
if (RxLEDPulse && !(--RxLEDPulse))
608+
RXLED0;
605609
}
606610
}
607611

@@ -634,6 +638,8 @@ void USB_::attach()
634638
USBCON = ((1<<USBE)|(1<<OTGPADE)); // start USB clock
635639
UDIEN = (1<<EORSTE)|(1<<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
636640
UDCON = 0; // enable attach resistor
641+
642+
TX_RX_LED_INIT;
637643
}
638644

639645
void USB_::detach()
@@ -649,14 +655,6 @@ bool USB_::configured()
649655

650656
void USB_::poll()
651657
{
652-
#ifdef MSC_ENABLED
653-
if (!_usbConfiguration)
654-
return;
655-
656-
// Service disk
657-
if (USB_Available(MSC_RX))
658-
MSC_Data(MSC_RX,MSC_TX);
659-
#endif
660658
}
661659

662660
#endif /* if defined(USBCON) */

hardware/arduino/variants/leonardo/pins_arduino.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#include <avr/pgmspace.h>
2929

3030
#define ARDUINO_MODEL_USB_PID 0x0034
31+
#define TX_RX_LED_INIT DDRE |= (1<<6), DDRB |= (1<<0)
32+
#define TXLED0 PORTE |= (1<<6)
33+
#define TXLED1 PORTE &= ~(1<<6)
34+
#define RXLED0 PORTB |= (1<<0)
35+
#define RXLED1 PORTB &= ~(1<<0)
3136

3237
// Map SPI port to 'new' pins D14..D17
3338
// D14 PB0 RXLED,SS/PCINT0

0 commit comments

Comments
 (0)