Added USB wakeup support
Had no chance to test it yet. Not sure if u2 Series will work as well.
This commit is contained in:
parent
59758e027a
commit
dc7d58b943
4 changed files with 190 additions and 59 deletions
|
|
@ -46,6 +46,7 @@ Gamepad + Mouse Abs doesnt work
|
|||
* Added Keycode functions in Keyboard API
|
||||
* Inlined a lot of the HID API functions to save flash
|
||||
* Added Gamepad
|
||||
* Added USB Wakeup support
|
||||
```
|
||||
|
||||
```
|
||||
|
|
|
|||
1
USBAPI.h
1
USBAPI.h
|
|
@ -50,6 +50,7 @@ public:
|
|||
void attach();
|
||||
void detach(); // Serial port goes down too...
|
||||
void poll();
|
||||
bool wakeupHost(); // returns false, when wakeup cannot be processed
|
||||
};
|
||||
extern USBDevice_ USBDevice;
|
||||
|
||||
|
|
|
|||
237
USBCore.cpp
237
USBCore.cpp
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
/* Copyright (c) 2010, Peter Barrett
|
||||
**
|
||||
** Sleep/Wakeup/SystemControl support added by Michael Dreher
|
||||
**
|
||||
** 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.
|
||||
|
|
@ -98,6 +100,8 @@ D_DEVICE(USB_DEVICE_NO_CLASS, USB_DEVICE_NO_SUB_CLASS, USB_DEVICE_NO_PROTOCOL, 6
|
|||
//==================================================================
|
||||
|
||||
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
|
||||
|
||||
static inline void WaitIN(void)
|
||||
{
|
||||
|
|
@ -563,16 +567,37 @@ ISR(USB_COM_vect)
|
|||
{
|
||||
// Standard Requests
|
||||
u8 r = setup.bRequest;
|
||||
u16 wValue = (setup.wValueH << 8) | setup.wValueL;
|
||||
if (GET_STATUS == r)
|
||||
{
|
||||
Send8(0); // TODO
|
||||
Send8(0);
|
||||
if (requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE))
|
||||
{
|
||||
Send8(_usbCurrentStatus);
|
||||
Send8(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: handle the HALT state of an endpoint here
|
||||
// see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information
|
||||
Send8(0);
|
||||
Send8(0);
|
||||
}
|
||||
}
|
||||
else if (CLEAR_FEATURE == r)
|
||||
{
|
||||
if ((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
|
||||
&& (wValue == DEVICE_REMOTE_WAKEUP))
|
||||
{
|
||||
_usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED;
|
||||
}
|
||||
}
|
||||
else if (SET_FEATURE == r)
|
||||
{
|
||||
if ((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
|
||||
&& (wValue == DEVICE_REMOTE_WAKEUP))
|
||||
{
|
||||
_usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
|
||||
}
|
||||
}
|
||||
else if (SET_ADDRESS == r)
|
||||
{
|
||||
|
|
@ -629,11 +654,113 @@ void USB_Flush(u8 ep)
|
|||
ReleaseTX();
|
||||
}
|
||||
|
||||
// edit by NicoHood
|
||||
// added from teensy definition by paul stoffregen
|
||||
//TODO remove, not needed anymore
|
||||
#if defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
|
||||
#define HW_CONFIG()
|
||||
#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
|
||||
#define USB_CONFIG() (USBCON = (1<<USBE))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)
|
||||
#define HW_CONFIG() (UHWCON = 0x01)
|
||||
#define PLL_CONFIG() (PLLCSR = 0x12)
|
||||
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1287__)
|
||||
#define HW_CONFIG() (UHWCON = 0x81)
|
||||
#define PLL_CONFIG() (PLLCSR = 0x1A)
|
||||
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#elif defined(__AVR_AT90USB1286__)
|
||||
#define HW_CONFIG() (UHWCON = 0x81)
|
||||
#define PLL_CONFIG() (PLLCSR = 0x16)
|
||||
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#endif
|
||||
|
||||
|
||||
static inline void USB_ClockDisable()
|
||||
{
|
||||
#if defined(OTGPADE)
|
||||
USBCON = (USBCON & ~(1 << OTGPADE)) | (1 << FRZCLK); // freeze clock and disable VBUS Pad
|
||||
#else // u2 Series
|
||||
USBCON = (1 << FRZCLK); // freeze clock
|
||||
#endif
|
||||
PLLCSR &= ~(1 << PLLE); // stop PLL
|
||||
}
|
||||
|
||||
static inline void USB_ClockEnable()
|
||||
{
|
||||
#if defined(UHWCON)
|
||||
UHWCON |= (1 << UVREGE); // power internal reg
|
||||
#endif
|
||||
USBCON = (1 << USBE) | (1 << FRZCLK); // clock frozen, usb enabled
|
||||
|
||||
// ATmega32U4
|
||||
#if defined(PINDIV)
|
||||
#if F_CPU == 16000000UL
|
||||
PLLCSR |= (1 << PINDIV); // Need 16 MHz xtal
|
||||
#elif F_CPU == 8000000UL
|
||||
PLLCSR &= ~(1 << PINDIV); // Need 8 MHz xtal
|
||||
#else
|
||||
#error "Clock rate of F_CPU not supported"
|
||||
#endif
|
||||
|
||||
// AT90USB646, AT90USB647, AT90USB1286, AT90USB1287
|
||||
#elif defined(PLLP2)
|
||||
#if F_CPU == 16000000UL
|
||||
#if defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
|
||||
// For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x.
|
||||
PLLCSR = (PLLCSR & ~(1 << PLLP1)) | ((1 << PLLP2) | (1 << PLLP0)); // Need 16 MHz xtal
|
||||
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__)
|
||||
// For AT90USB64x only. Do not use with AT90USB128x.
|
||||
PLLCSR = (PLLCSR & ~(1 << PLLP0)) | ((1 << PLLP2) | (1 << PLLP1)); // Need 16 MHz xtal
|
||||
#else
|
||||
#error "USB Chip not supported, please defined method of USB PLL initialization"
|
||||
#endif
|
||||
#elif F_CPU == 8000000UL
|
||||
// for Atmel AT90USB128x and AT90USB64x
|
||||
PLLCSR = (PLLCSR & ~(1 << PLLP2)) | ((1 << PLLP1) | (1 << PLLP0)); // Need 8 MHz xtal
|
||||
#else
|
||||
#error "Clock rate of F_CPU not supported"
|
||||
#endif
|
||||
#else
|
||||
#error "USB Chip not supported, please defined method of USB PLL initialization"
|
||||
#endif
|
||||
|
||||
PLLCSR |= (1 << PLLE);
|
||||
while (!(PLLCSR & (1 << PLOCK))) // wait for lock pll
|
||||
{
|
||||
}
|
||||
|
||||
// Some tests on specific versions of macosx (10.7.3), reported some
|
||||
// strange behaviors when the board is reset using the serial
|
||||
// port touch at 1200 bps. This delay fixes this behavior.
|
||||
delay(1);
|
||||
#if defined(OTGPADE)
|
||||
USBCON = (USBCON & ~(1 << FRZCLK)) | (1 << OTGPADE); // start USB clock, enable VBUS Pad
|
||||
#else
|
||||
USBCON &= ~(1 << FRZCLK); // start USB clock
|
||||
#endif
|
||||
|
||||
#if defined(RSTCPU)
|
||||
#if defined(LSM)
|
||||
UDCON &= ~((1 << RSTCPU) | (1 << LSM) | (1 << RMWKUP) | (1 << DETACH)); // enable attach resistor, set full speed mode
|
||||
#else // u2 Series
|
||||
UDCON &= ~((1 << RSTCPU) | (1 << RMWKUP) | (1 << DETACH)); // enable attach resistor, set full speed mode
|
||||
#endif
|
||||
#else
|
||||
// AT90USB64x and AT90USB128x don't have RSTCPU
|
||||
UDCON &= ~((1 << LSM) | (1 << RMWKUP) | (1 << DETACH)); // enable attach resistor, set full speed mode
|
||||
#endif
|
||||
}
|
||||
|
||||
// General interrupt
|
||||
ISR(USB_GEN_vect)
|
||||
{
|
||||
u8 udint = UDINT;
|
||||
UDINT = 0;
|
||||
UDINT &= ~((1 << EORSTI) | (1 << SOFI)); // clear the IRQ flags for the IRQs which are handled here, except WAKEUPI and SUSPI (see below)
|
||||
|
||||
// End of Reset
|
||||
if (udint & (1 << EORSTI))
|
||||
|
|
@ -656,6 +783,30 @@ ISR(USB_GEN_vect)
|
|||
if (RxLEDPulse && !(--RxLEDPulse))
|
||||
RXLED0;
|
||||
}
|
||||
|
||||
// the WAKEUPI interrupt is triggered as soon as there are non-idle patterns on the data
|
||||
// lines. Thus, the WAKEUPI interrupt can occur even if the controller is not in the "suspend" mode.
|
||||
// Therefore the we enable it only when USB is suspended
|
||||
if (udint & (1 << WAKEUPI))
|
||||
{
|
||||
UDIEN = (UDIEN & ~(1 << WAKEUPE)) | (1 << SUSPE); // Disable interrupts for WAKEUP and enable interrupts for SUSPEND
|
||||
|
||||
//TODO
|
||||
// WAKEUPI shall be cleared by software (USB clock inputs must be enabled before).
|
||||
//USB_ClockEnable();
|
||||
UDINT &= ~(1 << WAKEUPI);
|
||||
_usbSuspendState = (_usbSuspendState & ~(1 << SUSPI)) | (1 << WAKEUPI);
|
||||
}
|
||||
else if (udint & (1 << SUSPI)) // only one of the WAKEUPI / SUSPI bits can be active at time
|
||||
{
|
||||
UDIEN = (UDIEN & ~(1 << SUSPE)) | (1 << WAKEUPE); // Disable interrupts for SUSPEND and enable interrupts for WAKEUP
|
||||
|
||||
//TODO
|
||||
//USB_ClockDisable();
|
||||
|
||||
UDINT &= ~((1 << WAKEUPI) | (1 << SUSPI)); // clear any already pending WAKEUP IRQs and the SUSPI request
|
||||
_usbSuspendState = (_usbSuspendState & ~(1 << WAKEUPI)) | (1 << SUSPI);
|
||||
}
|
||||
}
|
||||
|
||||
// VBUS or counting frames
|
||||
|
|
@ -676,66 +827,16 @@ USBDevice_::USBDevice_()
|
|||
{
|
||||
}
|
||||
|
||||
// edit by NicoHood
|
||||
// added from teensy definition by paul stoffregen
|
||||
#if defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
|
||||
#define HW_CONFIG()
|
||||
#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
|
||||
#define USB_CONFIG() (USBCON = (1<<USBE))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)
|
||||
#define HW_CONFIG() (UHWCON = 0x01)
|
||||
#define PLL_CONFIG() (PLLCSR = 0x12)
|
||||
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#elif defined(__AVR_AT90USB646__)
|
||||
#define HW_CONFIG() (UHWCON = 0x81)
|
||||
#define PLL_CONFIG() (PLLCSR = 0x1A)
|
||||
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#elif defined(__AVR_AT90USB1286__)
|
||||
#define HW_CONFIG() (UHWCON = 0x81)
|
||||
#define PLL_CONFIG() (PLLCSR = 0x16)
|
||||
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
|
||||
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
|
||||
#endif
|
||||
|
||||
void USBDevice_::attach()
|
||||
{
|
||||
// edit by NicoHood
|
||||
// added support for u2 and u4 Series. Other MCUs not tested, but they could work.
|
||||
_usbConfiguration = 0;
|
||||
HW_CONFIG(); // power internal reg
|
||||
_usbCurrentStatus = 0;
|
||||
_usbSuspendState = 0;
|
||||
USB_ClockEnable();
|
||||
|
||||
// from Paul Brook source, TODO needed? (we might change this in a future commit with the USB wakeup)
|
||||
//USBCON = 0; // Reset controller
|
||||
USB_FREEZE(); // clock frozen, usb enabled
|
||||
|
||||
#if F_CPU == 16000000UL
|
||||
// Need 16 MHz xtal
|
||||
#ifdef PINDIV
|
||||
PLLCSR = (1 << PINDIV) | (1 << PLLE);
|
||||
#else
|
||||
// added from Paul Brook source, no idea for what board this is used for
|
||||
PLLCSR = (1 << PLLP0) | (1 << PLLE);
|
||||
#endif
|
||||
|
||||
#elif F_CPU == 8000000UL
|
||||
// Need 8 MHz xtal
|
||||
PLLCSR = (1 << PLLE);
|
||||
#endif
|
||||
|
||||
while (!(PLLCSR & (1 << PLOCK))) // wait for lock pll
|
||||
;
|
||||
|
||||
// Some tests on specific versions of macosx (10.7.3), reported some
|
||||
// strange behaviuors when the board is reset using the serial
|
||||
// port touch at 1200 bps. This delay fixes this behaviour.
|
||||
delay(1);
|
||||
|
||||
USB_CONFIG(); // start USB clock
|
||||
UDIEN = (1 << EORSTE) | (1 << SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
|
||||
UDCON = 0; // enable attach resistor
|
||||
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;
|
||||
}
|
||||
|
|
@ -755,4 +856,24 @@ void USBDevice_::poll()
|
|||
{
|
||||
}
|
||||
|
||||
bool USBDevice_::wakeupHost()
|
||||
{
|
||||
// clear any previous wakeup request which might have been set but could be processed at that time
|
||||
// e.g. because the host was not suspended at that time
|
||||
UDCON &= ~(1 << RMWKUP);
|
||||
|
||||
if (!(UDCON & (1 << RMWKUP))
|
||||
&& (_usbSuspendState & (1 << SUSPI))
|
||||
&& (_usbCurrentStatus & FEATURE_REMOTE_WAKEUP_ENABLED))
|
||||
{
|
||||
// This short version will only work, when the device has not been suspended. Currently the
|
||||
// Arduino core doesn't handle SUSPEND at all, so this is ok.
|
||||
USB_ClockEnable();
|
||||
UDCON |= (1 << RMWKUP); // send the wakeup request
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif /* if defined(USBCON) */
|
||||
|
|
|
|||
10
USBCore.h
10
USBCore.h
|
|
@ -82,6 +82,14 @@
|
|||
#define USB_INTERFACE_DESCRIPTOR_TYPE 4
|
||||
#define USB_ENDPOINT_DESCRIPTOR_TYPE 5
|
||||
|
||||
// usb_20.pdf Table 9.6 Standard Feature Selectors
|
||||
#define DEVICE_REMOTE_WAKEUP 1
|
||||
#define ENDPOINT_HALT 2
|
||||
#define TEST_MODE 3
|
||||
// usb_20.pdf Figure 9-4. Information Returned by a GetStatus() Request to a Device
|
||||
#define FEATURE_SELFPOWERED_ENABLED (1 << 0)
|
||||
#define FEATURE_REMOTE_WAKEUP_ENABLED (1 << 1)
|
||||
|
||||
#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
|
||||
#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
|
||||
#define USB_DEVICE_CLASS_STORAGE 0x08
|
||||
|
|
@ -300,7 +308,7 @@ typedef struct
|
|||
{ 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
|
||||
|
||||
#define D_CONFIG(_totalLength,_interfaces) \
|
||||
{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
|
||||
{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED | USB_CONFIG_REMOTE_WAKEUP, USB_CONFIG_POWER_MA(500) }
|
||||
|
||||
#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
|
||||
{ 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
|
||||
|
|
|
|||
Loading…
Reference in a new issue