From dc7d58b943a890e07cda04018349b18e0c5f8366 Mon Sep 17 00:00:00 2001 From: Nico Date: Sat, 27 Dec 2014 15:57:58 +0100 Subject: [PATCH] Added USB wakeup support Had no chance to test it yet. Not sure if u2 Series will work as well. --- Readme.md | 1 + USBAPI.h | 1 + USBCore.cpp | 237 +++++++++++++++++++++++++++++++++++++++------------- USBCore.h | 10 ++- 4 files changed, 190 insertions(+), 59 deletions(-) diff --git a/Readme.md b/Readme.md index abb703c..3d01056 100644 --- a/Readme.md +++ b/Readme.md @@ -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 ``` ``` diff --git a/USBAPI.h b/USBAPI.h index 79cbc3c..a49669c 100644 --- a/USBAPI.h +++ b/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; diff --git a/USBCore.cpp b/USBCore.cpp index be38718..242c6dc 100644 --- a/USBCore.cpp +++ b/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<