Skip to content

Commit 440afc5

Browse files
committed
add USB CDC RX & TX LEDs with 10ms minimum on time, tested
1 parent 9d193cc commit 440afc5

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cores/arduino/USB/CDC.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <stdio.h>
2424
#include <stdint.h>
2525

26+
uint8_t USB_TXLEDticks = 0, USB_RXLEDticks = 0;
27+
2628
#ifdef CDC_ENABLED
2729

2830
#define CDC_SERIAL_BUFFER_SIZE 256
@@ -98,6 +100,12 @@ bool CDC_Setup(USBSetup& setup)
98100
uint8_t requestType = setup.bmRequestType;
99101
uint8_t r = setup.bRequest;
100102

103+
digitalWrite(PIN_LED_RXL, HIGH);
104+
digitalWrite(PIN_LED_TXL, HIGH);
105+
pinMode(PIN_LED_RXL, OUTPUT);
106+
pinMode(PIN_LED_TXL, OUTPUT);
107+
USB_TXLEDticks = USB_RXLEDticks = 0;
108+
101109
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
102110
{
103111
if (r == CDC_GET_LINE_CODING)
@@ -209,6 +217,9 @@ int Serial_::read(void)
209217
{
210218
ring_buffer *buffer = &cdc_rx_buffer;
211219

220+
digitalWrite(PIN_LED_RXL, LOW);
221+
USB_RXLEDticks = 10; // how many ms to keep LED on
222+
212223
// if the head isn't ahead of the tail, we don't have any characters
213224
if (buffer->head == buffer->tail && !buffer->full)
214225
{
@@ -243,6 +254,9 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
243254
bytes sent before the user opens the connection or after
244255
the connection is closed are lost - just like with a UART. */
245256

257+
digitalWrite(PIN_LED_TXL, LOW);
258+
USB_TXLEDticks = 10; // how many ms to keep LED on
259+
246260
// TODO - ZE - check behavior on different OSes and test what happens if an
247261
// open connection isn't broken cleanly (cable is yanked out, host dies
248262
// or locks up, or host virtual serial port hangs)

cores/arduino/delay.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "delay.h"
2020
#include "Arduino.h"
2121

22+
extern uint8_t USB_TXLEDticks, USB_RXLEDticks;
23+
24+
2225
#ifdef __cplusplus
2326
extern "C" {
2427
#endif
@@ -83,6 +86,21 @@ void SysTick_DefaultHandler(void)
8386
// Increment tick count each ms
8487
_ulTickCount++;
8588
tickReset();
89+
90+
// turn off CDC LEDs if time
91+
if (USB_RXLEDticks) {
92+
USB_RXLEDticks--;
93+
if (USB_RXLEDticks == 0) {
94+
digitalWrite(PIN_LED_RXL, HIGH);
95+
}
96+
}
97+
98+
if (USB_TXLEDticks) {
99+
USB_TXLEDticks--;
100+
if (USB_TXLEDticks == 0) {
101+
digitalWrite(PIN_LED_TXL, HIGH);
102+
}
103+
}
86104
}
87105

88106
#ifdef __cplusplus

0 commit comments

Comments
 (0)