Make USE_INTERNAL_SERIAL configureable as makefile define
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3 / Device_UC3.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2018.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2018 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 disclaims 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 "../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 /* Public Interface - May be used in end-application: */
69 /* Macros: */
70 /** \name USB Device Mode Option Masks */
71 //@{
72 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
73 * USB interface should be initialized in low speed (1.5Mb/s) mode.
74 *
75 * \note Restrictions apply on the number, size and type of endpoints which can be used
76 * when running in low speed mode - please refer to the USB 2.0 specification.
77 */
78 #define USB_DEVICE_OPT_LOWSPEED (1 << 0)
79
80 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
81 * USB interface should be initialized in full speed (12Mb/s) mode.
82 */
83 #define USB_DEVICE_OPT_FULLSPEED (0 << 0)
84
85 #if defined(USB_SERIES_UC3A3_AVR32) || defined(USB_SERIES_UC3A4_AVR32) || defined(__DOXYGEN__)
86 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
87 * USB interface should be initialized in high speed (480Mb/s) mode.
88 */
89 #define USB_DEVICE_OPT_HIGHSPEED (1 << 1)
90 #endif
91 //@}
92
93 #if (!defined(NO_INTERNAL_SERIAL) && \
94 (defined(USB_SERIES_UC3A3_AVR32) || defined(USB_SERIES_UC3A4_AVR32) || \
95 defined(__DOXYGEN__)))
96 /** String descriptor index for the device's unique serial number string descriptor within the device.
97 * This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
98 * number allocations) to a device regardless of the port it is plugged in to on the host. Some microcontrollers contain
99 * a unique serial number internally, and setting the device descriptors serial number string index to this value
100 * will cause it to use the internal serial number.
101 *
102 * On unsupported devices, this will evaluate to \ref NO_DESCRIPTOR and so will force the host to create a pseudo-serial
103 * number for the device.
104 */
105 #ifndef USE_INTERNAL_SERIAL
106 #define USE_INTERNAL_SERIAL 0xDC
107 #endif
108
109 /** Length of the device's unique internal serial number, in bits, if present on the selected microcontroller
110 * model.
111 */
112 #define INTERNAL_SERIAL_LENGTH_BITS 120
113
114 /** Start address of the internal serial number, in the appropriate address space, if present on the selected microcontroller
115 * model.
116 */
117 #define INTERNAL_SERIAL_START_ADDRESS 0x80800204
118 #else
119 #define USE_INTERNAL_SERIAL NO_DESCRIPTOR
120
121 #define INTERNAL_SERIAL_LENGTH_BITS 0
122 #define INTERNAL_SERIAL_START_ADDRESS 0
123 #endif
124
125 /* Function Prototypes: */
126 /** Sends a Remote Wakeup request to the host. This signals to the host that the device should
127 * be taken out of suspended mode, and communications should resume.
128 *
129 * Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the
130 * host computer when the host has suspended all USB devices to enter a low power state.
131 *
132 * \note This function should only be used if the device has indicated to the host that it
133 * supports the Remote Wakeup feature in the device descriptors, and should only be
134 * issued if the host is currently allowing remote wakeup events from the device (i.e.,
135 * the \ref USB_Device_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP
136 * compile time option is used, this function is unavailable.
137 *
138 * \note The USB clock must be running for this function to operate. If the stack is initialized with
139 * the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running
140 * before attempting to call this function.
141 *
142 * \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors.
143 */
144 void USB_Device_SendRemoteWakeup(void);
145
146 /* Inline Functions: */
147 /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host)
148 * the frame number is incremented by one.
149 *
150 * \return Current USB frame number from the USB controller.
151 */
152 static inline uint16_t USB_Device_GetFrameNumber(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
153 static inline uint16_t USB_Device_GetFrameNumber(void)
154 {
155 return AVR32_USBB.UDFNUM.fnum;
156 }
157
158 #if !defined(NO_SOF_EVENTS)
159 /** Enables the device mode Start Of Frame events. When enabled, this causes the
160 * \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
161 * at the start of each USB frame when enumerated in device mode.
162 *
163 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
164 */
165 static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
166 static inline void USB_Device_EnableSOFEvents(void)
167 {
168 USB_INT_Enable(USB_INT_SOFI);
169 }
170
171 /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the
172 * \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode.
173 *
174 * \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
175 */
176 static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
177 static inline void USB_Device_DisableSOFEvents(void)
178 {
179 USB_INT_Disable(USB_INT_SOFI);
180 }
181 #endif
182
183 /* Private Interface - For use in library only: */
184 #if !defined(__DOXYGEN__)
185 /* Inline Functions: */
186 static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE;
187 static inline void USB_Device_SetLowSpeed(void)
188 {
189 AVR32_USBB.UDCON.ls = true;
190 }
191
192 static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE;
193 static inline void USB_Device_SetFullSpeed(void)
194 {
195 AVR32_USBB.UDCON.ls = false;
196 #if defined(USB_DEVICE_OPT_HIGHSPEED)
197 AVR32_USBB.UDCON.spdconf = 3;
198 #endif
199 }
200
201 #if defined(USB_DEVICE_OPT_HIGHSPEED)
202 static inline void USB_Device_SetHighSpeed(void) ATTR_ALWAYS_INLINE;
203 static inline void USB_Device_SetHighSpeed(void)
204 {
205 AVR32_USBB.UDCON.ls = false;
206 AVR32_USBB.UDCON.spdconf = 0;
207 }
208 #endif
209
210 static inline void USB_Device_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
211 static inline void USB_Device_SetDeviceAddress(const uint8_t Address)
212 {
213 AVR32_USBB.UDCON.uadd = Address;
214 }
215
216 static inline void USB_Device_EnableDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
217 static inline void USB_Device_EnableDeviceAddress(const uint8_t Address)
218 {
219 (void)Address;
220
221 AVR32_USBB.UDCON.adden = true;
222 }
223
224 static inline bool USB_Device_IsAddressSet(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
225 static inline bool USB_Device_IsAddressSet(void)
226 {
227 return AVR32_USBB.UDCON.adden;
228 }
229
230 #if (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
231 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString) ATTR_NON_NULL_PTR_ARG(1);
232 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString)
233 {
234 uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
235 GlobalInterruptDisable();
236
237 uint8_t* SigReadAddress = (uint8_t*)INTERNAL_SERIAL_START_ADDRESS;
238
239 for (uint8_t SerialCharNum = 0; SerialCharNum < (INTERNAL_SERIAL_LENGTH_BITS / 4); SerialCharNum++)
240 {
241 uint8_t SerialByte = *SigReadAddress;
242
243 if (SerialCharNum & 0x01)
244 {
245 SerialByte >>= 4;
246 SigReadAddress++;
247 }
248
249 SerialByte &= 0x0F;
250
251 UnicodeString[SerialCharNum] = cpu_to_le16((SerialByte >= 10) ?
252 (('A' - 10) + SerialByte) : ('0' + SerialByte));
253 }
254
255 SetGlobalInterruptMask(CurrentGlobalInt);
256 }
257 #endif
258
259 #endif
260
261 /* Disable C linkage for C++ Compilers: */
262 #if defined(__cplusplus)
263 }
264 #endif
265
266 #endif
267
268 /** @} */
269