Skip to content

Added callback support for USB host suspend/wake #4241

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions hardware/arduino/avr/cores/arduino/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ typedef unsigned long u32;
#define EP_TYPE_ISOCHRONOUS_IN ((1<<EPTYPE0) | (1<<EPDIR))
#define EP_TYPE_ISOCHRONOUS_OUT (1<<EPTYPE0)

typedef void (*usbDeviceCallBack)();

class USBDevice_
{
public:
Expand All @@ -65,6 +67,10 @@ class USBDevice_
void detach(); // Serial port goes down too...
void poll();
bool wakeupHost(); // returns false, when wakeup cannot be processed

void setWakeUpHandler(usbDeviceCallBack delegate);
void setSuspendHandler(usbDeviceCallBack delegate);
bool isSuspended();
};
extern USBDevice_ USBDevice;

Expand Down
76 changes: 53 additions & 23 deletions hardware/arduino/avr/cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@


/* Copyright (c) 2010, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
/* Copyright (c) 2010, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
*/

#include "USBAPI.h"
Expand Down Expand Up @@ -82,6 +82,9 @@ volatile u8 _usbConfiguration = 0;
volatile u8 _usbCurrentStatus = 0; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device
volatile u8 _usbSuspendState = 0; // copy of UDINT to check SUSPI and WAKEUPI bits

volatile usbDeviceCallBack usbWakeUpDelegate;
volatile usbDeviceCallBack usbSuspendDelegate;

static inline void WaitIN(void)
{
while (!(UEINTX & (1<<TXINI)))
Expand Down Expand Up @@ -115,17 +118,17 @@ static inline void Recv(volatile u8* data, u8 count)
{
while (count--)
*data++ = UEDATX;

RXLED1; // light the RX LED
RxLEDPulse = TX_RX_LED_PULSE_MS;
RxLEDPulse = TX_RX_LED_PULSE_MS;
}

static inline u8 Recv8()
{
RXLED1; // light the RX LED
RxLEDPulse = TX_RX_LED_PULSE_MS;

return UEDATX;
return UEDATX;
}

static inline void Send8(u8 d)
Expand Down Expand Up @@ -225,7 +228,7 @@ int USB_Recv(u8 ep, void* d, int len)
{
if (!_usbConfiguration || len < 0)
return -1;

LockEP lock(ep);
u8 n = FifoByteCount();
len = min(n,len);
Expand All @@ -235,7 +238,7 @@ int USB_Recv(u8 ep, void* d, int len)
*dst++ = Recv8();
if (len && !FifoByteCount()) // release empty buffer
ReleaseRX();

return len;
}

Expand Down Expand Up @@ -312,7 +315,7 @@ int USB_Send(u8 ep, const void* d, int len)
u8 _initEndpoints[USB_ENDPOINTS] =
{
0, // Control Endpoint

EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
Expand Down Expand Up @@ -455,7 +458,7 @@ static
bool SendConfiguration(int maxlen)
{
// Count and measure interfaces
InitControl(0);
InitControl(0);
u8 interfaces = SendInterfaces();
ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);

Expand Down Expand Up @@ -733,7 +736,7 @@ ISR(USB_GEN_vect)
if (udint & (1<<SOFI))
{
USB_Flush(CDC_TX); // Send a tx frame if found

// check whether the one-shot period has elapsed. if so, turn off the LED
if (TxLEDPulse && !(--TxLEDPulse))
TXLED0;
Expand All @@ -753,6 +756,12 @@ ISR(USB_GEN_vect)
//USB_ClockEnable();
UDINT &= ~(1<<WAKEUPI);
_usbSuspendState = (_usbSuspendState & ~(1<<SUSPI)) | (1<<WAKEUPI);

if(usbWakeUpDelegate)
{
usbWakeUpDelegate();
}

}
else if (udint & (1<<SUSPI)) // only one of the WAKEUPI / SUSPI bits can be active at time
{
Expand All @@ -763,6 +772,11 @@ ISR(USB_GEN_vect)

UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear any already pending WAKEUP IRQs and the SUSPI request
_usbSuspendState = (_usbSuspendState & ~(1<<WAKEUPI)) | (1<<SUSPI);

if(usbSuspendDelegate)
{
usbSuspendDelegate();
}
}
}

Expand Down Expand Up @@ -793,7 +807,7 @@ void USBDevice_::attach()

UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear already pending WAKEUP / SUSPEND requests
UDIEN = (1<<EORSTE) | (1<<SOFE) | (1<<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND

TX_RX_LED_INIT;
}

Expand Down Expand Up @@ -832,4 +846,20 @@ bool USBDevice_::wakeupHost()
return false;
}

void USBDevice_::setWakeUpHandler(usbDeviceCallBack delegate)
{
usbWakeUpDelegate = delegate;
}

void USBDevice_::setSuspendHandler(usbDeviceCallBack delegate)
{
usbSuspendDelegate = delegate;
}

bool USBDevice_::isSuspended()
{
return (_usbSuspendState & (1 << SUSPI));
}


#endif /* if defined(USBCON) */