3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  32  *  \brief USB Device definitions for the AVR8 microcontrollers. 
  33  *  \copydetails Group_Device_AVR8 
  35  *  \note This file should not be included directly. It is automatically included as needed by the USB driver 
  36  *        dispatch header located in LUFA/Drivers/USB/USB.h. 
  39 /** \ingroup Group_Device 
  40  *  \defgroup Group_Device_AVR8 Device Management (AVR8) 
  41  *  \brief USB Device definitions for the AVR8 microcontrollers. 
  43  *  Architecture specific USB Device definitions for the Atmel 8-bit AVR microcontrollers. 
  48 #ifndef __USBDEVICE_AVR8_H__ 
  49 #define __USBDEVICE_AVR8_H__ 
  52                 #include "../../../../Common/Common.h" 
  53                 #include "../StdDescriptors.h" 
  54                 #include "../USBInterrupt.h" 
  55                 #include "../Endpoint.h" 
  57         /* Preprocessor Checks: */ 
  58                 #if !defined(__INCLUDE_FROM_USB_DRIVER) 
  59                         #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead. 
  62                 #if (defined(USE_RAM_DESCRIPTORS) && defined(USE_EEPROM_DESCRIPTORS)) 
  63                         #error USE_RAM_DESCRIPTORS and USE_EEPROM_DESCRIPTORS are mutually exclusive. 
  66         /* Public Interface - May be used in end-application: */ 
  68                         /** \name USB Device Mode Option Masks */ 
  70                         #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__) 
  71                                 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the 
  72                                  *  USB interface should be initialized in low speed (1.5Mb/s) mode. 
  74                                  *  \note Low Speed mode is not available on all USB AVR models. 
  77                                  *  \note Restrictions apply on the number, size and type of endpoints which can be used 
  78                                  *        when running in low speed mode - refer to the USB 2.0 specification. 
  80                                 #define USB_DEVICE_OPT_LOWSPEED            (1 << 0) 
  83                         /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the 
  84                          *  USB interface should be initialized in full speed (12Mb/s) mode. 
  86                         #define USB_DEVICE_OPT_FULLSPEED               (0 << 0) 
  89                         #if (!defined(NO_INTERNAL_SERIAL) && \ 
  90                              (defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1287__) || \ 
  91                               defined(__AVR_ATmega32U6__) || defined(__AVR_AT90USB646__)  || defined(__AVR_AT90USB1286__) ||  \ 
  92                               defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__)  || defined(__AVR_ATmega8U2__))) 
  93                                 /** String descriptor index for the device's unique serial number string descriptor within the device. 
  94                                  *  This unique serial number is used by the host to associate resources to the device (such as drivers or COM port 
  95                                  *  number allocations) to a device regardless of the port it is plugged in to on the host. Some USB AVRs contain 
  96                                  *  a unique serial number internally, and setting the device descriptors serial number string index to this value 
  97                                  *  will cause it to use the internal serial number. 
  99                                  *  On unsupported devices, this will evaluate to NO_DESCRIPTOR and so will force the host to create a pseudo-serial 
 100                                  *  number for the device. 
 102                                 #define USE_INTERNAL_SERIAL           0xDC 
 104                                 #define USE_INTERNAL_SERIAL           NO_DESCRIPTOR 
 107                 /* Function Prototypes: */ 
 108                         /** Sends a Remote Wakeup request to the host. This signals to the host that the device should 
 109                          *  be taken out of suspended mode, and communications should resume. 
 111                          *  Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the 
 112                          *  host computer when the host has suspended all USB devices to enter a low power state. 
 114                          *  \note This macro should only be used if the device has indicated to the host that it 
 115                          *        supports the Remote Wakeup feature in the device descriptors, and should only be 
 116                          *        issued if the host is currently allowing remote wakeup events from the device (i.e., 
 117                          *        the \ref USB_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP compile 
 118                          *        time option is used, this macro is unavailable. 
 121                          *  \note The USB clock must be running for this function to operate. If the stack is initialized with 
 122                          *        the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running 
 123                          *        before attempting to call this function. 
 125                          *  \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors. 
 127                         void USB_Device_SendRemoteWakeup(void); 
 130                         enum USB_Device_States_t
 
 132                                 DEVICE_STATE_Unattached                   
= 0, /**< Internally implemented by the library. This state indicates 
 133                                                                                 *   that the device is not currently connected to a host. 
 135                                 DEVICE_STATE_Powered                      
= 1, /**< Internally implemented by the library. This state indicates 
 136                                                                                 *   that the device is connected to a host, but enumeration has not 
 139                                 DEVICE_STATE_Default                      
= 2, /**< Internally implemented by the library. This state indicates 
 140                                                                                 *   that the device's USB bus has been reset by the host and it is 
 141                                                                                 *   now waiting for the host to begin the enumeration process. 
 143                                 DEVICE_STATE_Addressed                    
= 3, /**< Internally implemented by the library. This state indicates 
 144                                                                                 *   that the device has been addressed by the USB Host, but is not 
 147                                 DEVICE_STATE_Configured                   
= 4, /**< May be implemented by the user project. This state indicates 
 148                                                                                 *   that the device has been enumerated by the host and is ready 
 149                                                                                 *   for USB communications to begin. 
 151                                 DEVICE_STATE_Suspended                    
= 5, /**< May be implemented by the user project. This state indicates 
 152                                                                                 *   that the USB bus has been suspended by the host, and the device 
 153                                                                                 *   should power down to a minimal power level until the bus is 
 158                 /* Inline Functions: */ 
 159                         /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host) 
 160                          *  the frame number is incremented by one. 
 162                         static inline uint16_t USB_Device_GetFrameNumber(void) 
 167                         #if !defined(NO_SOF_EVENTS) 
 168                                 /** Enables the device mode Start Of Frame events. When enabled, this causes the 
 169                                  *  \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus, 
 170                                  *  at the start of each USB frame when enumerated in device mode. 
 172                                  *  \note Not available when the \c NO_SOF_EVENTS compile time token is defined. 
 174                                 static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 175                                 static inline void USB_Device_EnableSOFEvents(void) 
 177                                         USB_INT_Enable(USB_INT_SOFI
); 
 180                                 /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the 
 181                                  *  \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode. 
 183                                  *  \note Not available when the \c NO_SOF_EVENTS compile time token is defined. 
 185                                 static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE
; 
 186                                 static inline void USB_Device_DisableSOFEvents(void) 
 188                                         USB_INT_Disable(USB_INT_SOFI
); 
 192         /* Private Interface - For use in library only: */ 
 193         #if !defined(__DOXYGEN__) 
 194                 /* Inline Functions: */ 
 195                         #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)) 
 196                         static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE
; 
 197                         static inline void USB_Device_SetLowSpeed(void) 
 202                         static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE
; 
 203                         static inline void USB_Device_SetFullSpeed(void) 
 205                                 UDCON 
&= ~(1 << LSM
); 
 209                         static inline void USB_Device_SetDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
; 
 210                         static inline void USB_Device_SetDeviceAddress(const uint8_t Address
) 
 212                                 UDADDR  
= ((UDADDR 
& (1 << ADDEN
)) | (Address 
& 0x7F)); 
 213                                 UDADDR 
|= (1 << ADDEN
); 
 216                         static inline bool USB_Device_IsAddressSet(void) ATTR_ALWAYS_INLINE
; 
 217                         static inline bool USB_Device_IsAddressSet(void) 
 219                                 return (UDADDR 
& (1 << ADDEN
)); 
 222                         static inline uint8_t USB_Device_GetSerialString(wchar_t* UnicodeString
, const uint8_t MaxLen
) 
 224                                 uint8_t SerialCharNum 
= 0; 
 226                                 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
) 
 228                                         uint8_t SigReadAddress 
= 0x0E; 
 230                                         for (SerialCharNum 
= 0; SerialCharNum 
< MIN(MaxLen
, 20); SerialCharNum
++) 
 232                                                 if (SerialCharNum 
== MaxLen
) 
 235                                                 uint8_t SerialByte 
= boot_signature_byte_get(SigReadAddress
); 
 237                                                 if (SerialCharNum 
& 0x01) 
 245                                                 UnicodeString
[SerialCharNum
] = (SerialByte 
>= 10) ?
 
 246                                                                                (('A' - 10) + SerialByte
) : ('0' + SerialByte
); 
 250                                 return SerialCharNum
;