Skip to content

Add USB transfer RX/TX LED blinking #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cores/arduino/USB/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@

USBDevice_SAMD21G18x usbd;

/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
#define TX_RX_LED_PULSE_MS 100
#ifdef PIN_LED_TXL
static volatile uint8_t txLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
#endif
#ifdef PIN_LED_RXL
static volatile uint8_t rxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
#endif
static char isRemoteWakeUpEnabled = 0;
static char isEndpointHalt = 0;

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

void USBDeviceClass::init()
{
#ifdef PIN_LED_TXL
txLEDPulse = 0;
pinMode(PIN_LED_TXL, OUTPUT);
digitalWrite(PIN_LED_TXL, HIGH);
#endif

#ifdef PIN_LED_RXL
rxLEDPulse = 0;
pinMode(PIN_LED_RXL, OUTPUT);
digitalWrite(PIN_LED_RXL, HIGH);
#endif

// Enable USB clock
PM->APBBMASK.reg |= PM_APBBMASK_USB;

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

#ifdef PIN_LED_RXL
digitalWrite(PIN_LED_RXL, LOW);
rxLEDPulse = TX_RX_LED_PULSE_MS;
#endif

armRecv(ep);

usbd.epBank0DisableTransferComplete(ep);
Expand Down Expand Up @@ -620,6 +645,11 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
}
#endif

#ifdef PIN_LED_TXL
digitalWrite(PIN_LED_TXL, LOW);
txLEDPulse = TX_RX_LED_PULSE_MS;
#endif

// Flash area
while (len != 0)
{
Expand Down Expand Up @@ -826,6 +856,17 @@ void USBDeviceClass::ISRHandler()
if (usbd.isStartOfFrameInterrupt())
{
usbd.ackStartOfFrameInterrupt();

// check whether the one-shot period has elapsed. if so, turn off the LED
#ifdef PIN_LED_TXL
if (txLEDPulse && !(--txLEDPulse))
digitalWrite(PIN_LED_TXL, HIGH);
#endif

#ifdef PIN_LED_RXL
if (rxLEDPulse && !(--rxLEDPulse))
digitalWrite(PIN_LED_RXL, HIGH);
#endif
}

// Endpoint 0 Received Setup interrupt
Expand Down