]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/XMEGA/Device_XMEGA.h
Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / XMEGA / Device_XMEGA.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 * \brief USB Device definitions for the AVR XMEGA microcontrollers.
33 * \copydetails Group_Device_XMEGA
34 *
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.
37 */
38
39 /** \ingroup Group_Device
40 * \defgroup Group_Device_XMEGA Device Management (XMEGA)
41 * \brief USB Device definitions for the AVR XMEGA microcontrollers.
42 *
43 * Architecture specific USB Device definitions for the Atmel AVR XMEGA microcontrollers.
44 *
45 * @{
46 */
47
48 #ifndef __USBDEVICE_XMEGA_H__
49 #define __USBDEVICE_XMEGA_H__
50
51 /* Includes: */
52 #include "../../../../Common/Common.h"
53 #include "../USBController.h"
54 #include "../StdDescriptors.h"
55 #include "../USBInterrupt.h"
56 #include "../Endpoint.h"
57
58 /* Enable C linkage for C++ Compilers: */
59 #if defined(__cplusplus)
60 extern "C" {
61 #endif
62
63 /* Preprocessor Checks: */
64 #if !defined(__INCLUDE_FROM_USB_DRIVER)
65 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
66 #endif
67
68 #if (defined(USE_RAM_DESCRIPTORS) && defined(USE_EEPROM_DESCRIPTORS))
69 #error USE_RAM_DESCRIPTORS and USE_EEPROM_DESCRIPTORS are mutually exclusive.
70 #endif
71
72 #if (defined(USE_FLASH_DESCRIPTORS) && defined(USE_EEPROM_DESCRIPTORS))
73 #error USE_FLASH_DESCRIPTORS and USE_EEPROM_DESCRIPTORS are mutually exclusive.
74 #endif
75
76 #if (defined(USE_FLASH_DESCRIPTORS) && defined(USE_RAM_DESCRIPTORS))
77 #error USE_FLASH_DESCRIPTORS and USE_RAM_DESCRIPTORS are mutually exclusive.
78 #endif
79
80 /* Public Interface - May be used in end-application: */
81 /* Macros: */
82 /** \name USB Device Mode Option Masks */
83 //@{
84 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
85 * USB interface should be initialized in low speed (1.5Mb/s) mode.
86 *
87 * \note Low Speed mode is not available on all USB AVR models.
88 * \n
89 *
90 * \note Restrictions apply on the number, size and type of endpoints which can be used
91 * when running in low speed mode - refer to the USB 2.0 specification.
92 */
93 #define USB_DEVICE_OPT_LOWSPEED (1 << 0)
94
95 #if (F_USB > 6000000)
96 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
97 * USB interface should be initialized in full speed (12Mb/s) mode.
98 */
99 #define USB_DEVICE_OPT_FULLSPEED (0 << 0)
100 #endif
101 //@}
102
103 #if (!defined(NO_INTERNAL_SERIAL) || defined(__DOXYGEN__))
104 /** String descriptor index for the device's unique serial number string descriptor within the device.
105 * This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
106 * number allocations) to a device regardless of the port it is plugged in to on the host. Some microcontrollers contain
107 * a unique serial number internally, and setting the device descriptors serial number string index to this value
108 * will cause it to use the internal serial number.
109 *
110 * On unsupported devices, this will evaluate to \ref NO_DESCRIPTOR and so will force the host to create a pseudo-serial
111 * number for the device.
112 */
113 #define USE_INTERNAL_SERIAL 0xDC
114
115 /** Length of the device's unique internal serial number, in bits, if present on the selected microcontroller
116 * model.
117 */
118 #define INTERNAL_SERIAL_LENGTH_BITS (8 * (1 + (offsetof(NVM_PROD_SIGNATURES_t, COORDY1) - offsetof(NVM_PROD_SIGNATURES_t, LOTNUM0))))
119
120 /** Start address of the internal serial number, in the appropriate address space, if present on the selected microcontroller
121 * model.
122 */
123 #define INTERNAL_SERIAL_START_ADDRESS offsetof(NVM_PROD_SIGNATURES_t, LOTNUM0)
124 #else
125 #define USE_INTERNAL_SERIAL NO_DESCRIPTOR
126
127 #define INTERNAL_SERIAL_LENGTH_BITS 0
128 #define INTERNAL_SERIAL_START_ADDRESS 0
129 #endif
130
131 /* Function Prototypes: */
132 /** Sends a Remote Wakeup request to the host. This signals to the host that the device should
133 * be taken out of suspended mode, and communications should resume.
134 *
135 * Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the
136 * host computer when the host has suspended all USB devices to enter a low power state.
137 *
138 * \note This function should only be used if the device has indicated to the host that it
139 * supports the Remote Wakeup feature in the device descriptors, and should only be
140 * issued if the host is currently allowing remote wakeup events from the device (i.e.,
141 * the \ref USB_Device_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP
142 * compile time option is used, this function is unavailable.
143 * \n\n
144 *
145 * \note The USB clock must be running for this function to operate. If the stack is initialized with
146 * the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running
147 * before attempting to call this function.
148 *
149 * \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors.
150 */
151 void USB_Device_SendRemoteWakeup(void);
152
153 /* Inline Functions: */
154 /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host)
155 * the frame number is incremented by one.
156 *
157 * \return Current USB frame number from the USB controller.
158 */
159 static inline uint16_t USB_Device_GetFrameNumber(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
160 static inline uint16_t USB_Device_GetFrameNumber(void)
161 {
162 return ((USB_EndpointTable_t*)USB.EPPTR)->FrameNum;
163 }
164
165 #if !defined(NO_SOF_EVENTS)
166 /** Enables the device mode Start Of Frame events. When enabled, this causes the
167 * \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
168 * at the start of each USB frame when enumerated in device mode.
169 *
170 * \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.
171 */
172 static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
173 static inline void USB_Device_EnableSOFEvents(void)
174 {
175 USB.INTCTRLA |= USB_SOFIE_bm;
176 }
177
178 /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the
179 * \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode.
180 *
181 * \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.
182 */
183 static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
184 static inline void USB_Device_DisableSOFEvents(void)
185 {
186 USB.INTCTRLA &= ~USB_SOFIE_bm;
187 }
188 #endif
189
190 /* Private Interface - For use in library only: */
191 #if !defined(__DOXYGEN__)
192 /* Inline Functions: */
193 static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE;
194 static inline void USB_Device_SetLowSpeed(void)
195 {
196 USB.CTRLA &= ~USB_SPEED_bm;
197 }
198
199 static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE;
200 static inline void USB_Device_SetFullSpeed(void)
201 {
202 USB.CTRLA |= USB_SPEED_bm;
203 }
204
205 static inline void USB_Device_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
206 static inline void USB_Device_SetDeviceAddress(const uint8_t Address)
207 {
208 USB.ADDR = Address;
209 }
210
211 static inline bool USB_Device_IsAddressSet(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
212 static inline bool USB_Device_IsAddressSet(void)
213 {
214 return ((USB.ADDR != 0) ? true : false);
215 }
216
217 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString) ATTR_NON_NULL_PTR_ARG(1);
218 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString)
219 {
220 uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
221 GlobalInterruptDisable();
222
223 uint8_t SigReadAddress = INTERNAL_SERIAL_START_ADDRESS;
224
225 for (uint8_t SerialCharNum = 0; SerialCharNum < (INTERNAL_SERIAL_LENGTH_BITS / 4); SerialCharNum++)
226 {
227 uint8_t SerialByte;
228
229 NVM.CMD = NVM_CMD_READ_CALIB_ROW_gc;
230 SerialByte = pgm_read_byte(SigReadAddress);
231 NVM.CMD = 0;
232
233 if (SerialCharNum & 0x01)
234 {
235 SerialByte >>= 4;
236 SigReadAddress++;
237 }
238
239 SerialByte &= 0x0F;
240
241 UnicodeString[SerialCharNum] = cpu_to_le16((SerialByte >= 10) ?
242 (('A' - 10) + SerialByte) : ('0' + SerialByte));
243 }
244
245 SetGlobalInterruptMask(CurrentGlobalInt);
246 }
247
248 #endif
249
250 /* Disable C linkage for C++ Compilers: */
251 #if defined(__cplusplus)
252 }
253 #endif
254
255 #endif
256
257 /** @} */
258
Imprint / Impressum