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 AVR XMEGA microcontrollers.
33 * \copydetails Group_Device_XMEGA
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_XMEGA Device Management (XMEGA)
41 * \brief USB Device definitions for the AVR XMEGA microcontrollers.
43 * Architecture specific USB Device definitions for the Atmel AVR XMEGA microcontrollers.
48 #ifndef __USBDEVICE_XMEGA_H__
49 #define __USBDEVICE_XMEGA_H__
52 #include "../../../../Common/Common.h"
53 #include "../StdDescriptors.h"
54 #include "../USBInterrupt.h"
55 #include "../Endpoint.h"
57 /* Enable C linkage for C++ Compilers: */
58 #if defined(__cplusplus)
62 /* Preprocessor Checks: */
63 #if !defined(__INCLUDE_FROM_USB_DRIVER)
64 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
67 #if (defined(USE_RAM_DESCRIPTORS) && defined(USE_EEPROM_DESCRIPTORS))
68 #error USE_RAM_DESCRIPTORS and USE_EEPROM_DESCRIPTORS are mutually exclusive.
71 /* Public Interface - May be used in end-application: */
73 /** \name USB Device Mode Option Masks */
75 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
76 * USB interface should be initialized in low speed (1.5Mb/s) mode.
78 * \note Low Speed mode is not available on all USB AVR models.
81 * \note Restrictions apply on the number, size and type of endpoints which can be used
82 * when running in low speed mode - refer to the USB 2.0 specification.
84 #define USB_DEVICE_OPT_LOWSPEED (1 << 0)
86 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
87 * USB interface should be initialized in full speed (12Mb/s) mode.
89 #define USB_DEVICE_OPT_FULLSPEED (0 << 0)
92 /** String descriptor index for the device's unique serial number string descriptor within the device.
93 * This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
94 * number allocations) to a device regardless of the port it is plugged in to on the host. Some microcontrollers contain
95 * a unique serial number internally, and setting the device descriptors serial number string index to this value
96 * will cause it to use the internal serial number.
98 * On unsupported devices, this will evaluate to \ref NO_DESCRIPTOR and so will force the host to create a pseudo-serial
99 * number for the device.
101 #define USE_INTERNAL_SERIAL 0xDC
103 /** Length of the device's unique internal serial number, in bits, if present on the selected microcontroller
106 #define INTERNAL_SERIAL_LENGTH_BITS 112
108 /** Start address of the internal serial number, in the appropriate address space, if present on the selected microcontroller
111 #define INTERNAL_SERIAL_START_ADDRESS 0x08
113 /* Function Prototypes: */
114 /** Sends a Remote Wakeup request to the host. This signals to the host that the device should
115 * be taken out of suspended mode, and communications should resume.
117 * Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the
118 * host computer when the host has suspended all USB devices to enter a low power state.
120 * \note This macro should only be used if the device has indicated to the host that it
121 * supports the Remote Wakeup feature in the device descriptors, and should only be
122 * issued if the host is currently allowing remote wakeup events from the device (i.e.,
123 * the \ref USB_Device_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP
124 * compile time option is used, this macro is unavailable.
127 * \note The USB clock must be running for this function to operate. If the stack is initialized with
128 * the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running
129 * before attempting to call this function.
131 * \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors.
133 void USB_Device_SendRemoteWakeup(void);
135 /* Inline Functions: */
136 /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host)
137 * the frame number is incremented by one.
139 static inline uint16_t USB_Device_GetFrameNumber(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
140 static inline uint16_t USB_Device_GetFrameNumber(void)
145 #if !defined(NO_SOF_EVENTS)
146 /** Enables the device mode Start Of Frame events. When enabled, this causes the
147 * \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
148 * at the start of each USB frame when enumerated in device mode.
150 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
152 static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE
;
153 static inline void USB_Device_EnableSOFEvents(void)
158 /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the
159 * \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode.
161 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
163 static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE
;
164 static inline void USB_Device_DisableSOFEvents(void)
170 /* Private Interface - For use in library only: */
171 #if !defined(__DOXYGEN__)
172 /* Inline Functions: */
173 static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE
;
174 static inline void USB_Device_SetLowSpeed(void)
176 USB
.CTRLA
&= ~USB_SPEED_bm
;
179 static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE
;
180 static inline void USB_Device_SetFullSpeed(void)
182 USB
.CTRLA
|= USB_SPEED_bm
;
185 static inline void USB_Device_SetDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
;
186 static inline void USB_Device_SetDeviceAddress(const uint8_t Address
)
191 static inline bool USB_Device_IsAddressSet(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
192 static inline bool USB_Device_IsAddressSet(void)
194 return ((USB
.ADDR
!= 0) ?
true : false);
197 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString
) ATTR_NON_NULL_PTR_ARG(1);
198 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString
)
200 uint_reg_t CurrentGlobalInt
= GetGlobalInterruptMask();
201 GlobalInterruptDisable();
203 uint8_t SigReadAddress
= INTERNAL_SERIAL_START_ADDRESS
;
205 for (uint8_t SerialCharNum
= 0; SerialCharNum
< (INTERNAL_SERIAL_LENGTH_BITS
/ 4); SerialCharNum
++)
209 NVM
.CMD
= NVM_CMD_READ_CALIB_ROW_gc
;
210 SerialByte
= pgm_read_byte(SigReadAddress
);
212 if (SerialCharNum
& 0x01)
220 UnicodeString
[SerialCharNum
] = cpu_to_le16((SerialByte
>= 10) ?
221 (('A' - 10) + SerialByte
) : ('0' + SerialByte
));
224 SetGlobalInterruptMask(CurrentGlobalInt
);
231 /* Disable C linkage for C++ Compilers: */
232 #if defined(__cplusplus)