Simplify the internal serial extraction routines for each architecture.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3 / Device_UC3.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 AVR32 UC3 microcontrollers.
33 * \copydetails Group_Device_UC3
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_UC3 Device Management (UC3)
41 * \brief USB Device definitions for the AVR32 UC3 microcontrollers.
42 *
43 * Architecture specific USB Device definitions for the Atmel 32-bit UC3 AVR microcontrollers.
44 *
45 * @{
46 */
47
48 #ifndef __USBDEVICE_UC3_H__
49 #define __USBDEVICE_UC3_H__
50
51 /* Includes: */
52 #include "../../../../Common/Common.h"
53 #include "../StdDescriptors.h"
54 #include "../USBInterrupt.h"
55 #include "../Endpoint.h"
56
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.
60 #endif
61
62 /* Public Interface - May be used in end-application: */
63 /* Macros: */
64 /** \name USB Device Mode Option Masks */
65 //@{
66 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
67 * USB interface should be initialized in low speed (1.5Mb/s) mode.
68 *
69 * \note Restrictions apply on the number, size and type of endpoints which can be used
70 * when running in low speed mode - refer to the USB 2.0 specification.
71 */
72 #define USB_DEVICE_OPT_LOWSPEED (1 << 0)
73
74 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
75 * USB interface should be initialized in full speed (12Mb/s) mode.
76 */
77 #define USB_DEVICE_OPT_FULLSPEED (0 << 0)
78 //@}
79
80 #if (!defined(NO_INTERNAL_SERIAL) && \
81 (defined(USB_SERIES_UC3A3_AVR) || defined(USB_SERIES_UC3A4_AVR) || \
82 defined(__DOXYGEN__)))
83 /** String descriptor index for the device's unique serial number string descriptor within the device.
84 * This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
85 * number allocations) to a device regardless of the port it is plugged in to on the host. Some microcontrollers contain
86 * a unique serial number internally, and setting the device descriptors serial number string index to this value
87 * will cause it to use the internal serial number.
88 *
89 * On unsupported devices, this will evaluate to \ref NO_DESCRIPTOR and so will force the host to create a pseudo-serial
90 * number for the device.
91 */
92 #define USE_INTERNAL_SERIAL 0xDC
93
94 /** Length of the device's unique internal serial number, in bits, if present on the selected microcontroller
95 * model.
96 */
97 #define INTERNAL_SERIAL_LENGTH_BITS 120
98 #else
99 #define USE_INTERNAL_SERIAL NO_DESCRIPTOR
100
101 #define INTERNAL_SERIAL_LENGTH_BITS 0
102 #endif
103
104 /* Function Prototypes: */
105 /** Sends a Remote Wakeup request to the host. This signals to the host that the device should
106 * be taken out of suspended mode, and communications should resume.
107 *
108 * Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the
109 * host computer when the host has suspended all USB devices to enter a low power state.
110 *
111 * \note This macro should only be used if the device has indicated to the host that it
112 * supports the Remote Wakeup feature in the device descriptors, and should only be
113 * issued if the host is currently allowing remote wakeup events from the device (i.e.,
114 * the \ref USB_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP compile
115 * time option is used, this macro is unavailable.
116 * \n\n
117 *
118 * \note The USB clock must be running for this function to operate. If the stack is initialized with
119 * the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running
120 * before attempting to call this function.
121 *
122 * \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors.
123 */
124 void USB_Device_SendRemoteWakeup(void);
125
126 /* Inline Functions: */
127 /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host)
128 * the frame number is incremented by one.
129 */
130 static inline uint16_t USB_Device_GetFrameNumber(void)
131 {
132 return AVR32_USBB.UDFNUM.fnum;
133 }
134
135 #if !defined(NO_SOF_EVENTS)
136 /** Enables the device mode Start Of Frame events. When enabled, this causes the
137 * \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
138 * at the start of each USB frame when enumerated in device mode.
139 *
140 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
141 */
142 static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
143 static inline void USB_Device_EnableSOFEvents(void)
144 {
145 USB_INT_Enable(USB_INT_SOFI);
146 }
147
148 /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the
149 * \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode.
150 *
151 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
152 */
153 static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
154 static inline void USB_Device_DisableSOFEvents(void)
155 {
156 USB_INT_Disable(USB_INT_SOFI);
157 }
158 #endif
159
160 /* Private Interface - For use in library only: */
161 #if !defined(__DOXYGEN__)
162 /* Inline Functions: */
163 static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE;
164 static inline void USB_Device_SetLowSpeed(void)
165 {
166 AVR32_USBB.UDCON.ls = true;
167 }
168
169 static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE;
170 static inline void USB_Device_SetFullSpeed(void)
171 {
172 AVR32_USBB.UDCON.ls = false;
173 }
174
175 static inline void USB_Device_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
176 static inline void USB_Device_SetDeviceAddress(const uint8_t Address)
177 {
178 AVR32_USBB.UDCON.uadd = Address;
179 AVR32_USBB.UDCON.adden = true;
180 }
181
182 static inline bool USB_Device_IsAddressSet(void) ATTR_ALWAYS_INLINE;
183 static inline bool USB_Device_IsAddressSet(void)
184 {
185 return AVR32_USBB.UDCON.adden;
186 }
187
188 static inline void USB_Device_GetSerialString(uint16_t* UnicodeString)
189 {
190 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
191 {
192 uint8_t* SigReadAddress = (uint8_t*)0x80800204;
193
194 for (uint8_t SerialCharNum = 0; SerialCharNum < (INTERNAL_SERIAL_LENGTH_BITS / 4); SerialCharNum++)
195 {
196 uint8_t SerialByte = *SigReadAddress;
197
198 if (SerialCharNum & 0x01)
199 {
200 SerialByte >>= 4;
201 SigReadAddress++;
202 }
203
204 SerialByte &= 0x0F;
205
206 UnicodeString[SerialCharNum] = cpu_to_le16((SerialByte >= 10) ?
207 (('A' - 10) + SerialByte) : ('0' + SerialByte));
208 }
209 }
210 }
211 #endif
212
213 #endif
214
215 /** @} */
216