3 Copyright (C) Dean Camera, 2018.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2018 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 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
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 "../USBController.h"
54 #include "../StdDescriptors.h"
55 #include "../USBInterrupt.h"
56 #include "../Endpoint.h"
58 /* Enable C linkage for C++ Compilers: */
59 #if defined(__cplusplus)
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.
68 #if (defined(USE_RAM_DESCRIPTORS) && defined(USE_EEPROM_DESCRIPTORS))
69 #error USE_RAM_DESCRIPTORS and USE_EEPROM_DESCRIPTORS are mutually exclusive.
72 #if (defined(USE_FLASH_DESCRIPTORS) && defined(USE_EEPROM_DESCRIPTORS))
73 #error USE_FLASH_DESCRIPTORS and USE_EEPROM_DESCRIPTORS are mutually exclusive.
76 #if (defined(USE_FLASH_DESCRIPTORS) && defined(USE_RAM_DESCRIPTORS))
77 #error USE_FLASH_DESCRIPTORS and USE_RAM_DESCRIPTORS are mutually exclusive.
80 /* Public Interface - May be used in end-application: */
82 /** \name USB Device Mode Option Masks */
84 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__)
85 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
86 * USB interface should be initialized in low speed (1.5Mb/s) mode.
88 * \note Low Speed mode is not available on all USB AVR models.
91 * \note Restrictions apply on the number, size and type of endpoints which can be used
92 * when running in low speed mode - please refer to the USB 2.0 specification.
94 #define USB_DEVICE_OPT_LOWSPEED (1 << 0)
97 /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
98 * USB interface should be initialized in full speed (12Mb/s) mode.
100 #define USB_DEVICE_OPT_FULLSPEED (0 << 0)
103 #if (!defined(NO_INTERNAL_SERIAL) && \
104 (defined(USB_SERIES_7_AVR) || defined(USB_SERIES_6_AVR) || \
105 defined(USB_SERIES_4_AVR) || defined(USB_SERIES_2_AVR) || \
106 defined(__DOXYGEN__)))
107 /** String descriptor index for the device's unique serial number string descriptor within the device.
108 * This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
109 * number allocations) to a device regardless of the port it is plugged in to on the host. Some microcontrollers contain
110 * a unique serial number internally, and setting the device descriptors serial number string index to this value
111 * will cause it to use the internal serial number.
113 * On unsupported devices, this will evaluate to \ref NO_DESCRIPTOR and so will force the host to create a pseudo-serial
114 * number for the device.
116 #ifndef USE_INTERNAL_SERIAL
117 #define USE_INTERNAL_SERIAL 0xDC
120 /** Length of the device's unique internal serial number, in bits, if present on the selected microcontroller
123 #define INTERNAL_SERIAL_LENGTH_BITS 80
125 /** Start address of the internal serial number, in the appropriate address space, if present on the selected microcontroller
128 #define INTERNAL_SERIAL_START_ADDRESS 0x0E
130 #undef USE_INTERNAL_SERIAL
131 #define USE_INTERNAL_SERIAL NO_DESCRIPTOR
133 #define INTERNAL_SERIAL_LENGTH_BITS 0
134 #define INTERNAL_SERIAL_START_ADDRESS 0
137 /* Function Prototypes: */
138 /** Sends a Remote Wakeup request to the host. This signals to the host that the device should
139 * be taken out of suspended mode, and communications should resume.
141 * Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the
142 * host computer when the host has suspended all USB devices to enter a low power state.
144 * \attention This function should only be used if the device has indicated to the host that it
145 * supports the Remote Wakeup feature in the device descriptors, and should only be
146 * issued if the host is currently allowing remote wakeup events from the device (i.e.,
147 * the \ref USB_Device_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP
148 * compile time option is used, this function is unavailable.
151 * \attention The USB clock must be running for this function to operate. If the stack is initialized with
152 * the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running
153 * before attempting to call this function.
155 * \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors.
157 void USB_Device_SendRemoteWakeup(void);
159 /* Inline Functions: */
160 /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host)
161 * the frame number is incremented by one.
163 * \return Current USB frame number from the USB controller.
165 static inline uint16_t USB_Device_GetFrameNumber(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
166 static inline uint16_t USB_Device_GetFrameNumber(void)
171 #if !defined(NO_SOF_EVENTS)
172 /** Enables the device mode Start Of Frame events. When enabled, this causes the
173 * \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
174 * at the start of each USB frame when enumerated in device mode.
176 * \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.
178 static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE
;
179 static inline void USB_Device_EnableSOFEvents(void)
181 USB_INT_Enable(USB_INT_SOFI
);
184 /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the
185 * \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode.
187 * \note This function is not available when the \c NO_SOF_EVENTS compile time token is defined.
189 static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE
;
190 static inline void USB_Device_DisableSOFEvents(void)
192 USB_INT_Disable(USB_INT_SOFI
);
196 /* Private Interface - For use in library only: */
197 #if !defined(__DOXYGEN__)
198 /* Inline Functions: */
199 #if defined(USB_DEVICE_OPT_LOWSPEED)
200 static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE
;
201 static inline void USB_Device_SetLowSpeed(void)
206 static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE
;
207 static inline void USB_Device_SetFullSpeed(void)
209 UDCON
&= ~(1 << LSM
);
213 static inline void USB_Device_SetDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
;
214 static inline void USB_Device_SetDeviceAddress(const uint8_t Address
)
216 UDADDR
= (UDADDR
& (1 << ADDEN
)) | (Address
& 0x7F);
219 static inline void USB_Device_EnableDeviceAddress(const uint8_t Address
) ATTR_ALWAYS_INLINE
;
220 static inline void USB_Device_EnableDeviceAddress(const uint8_t Address
)
224 UDADDR
|= (1 << ADDEN
);
227 static inline bool USB_Device_IsAddressSet(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
228 static inline bool USB_Device_IsAddressSet(void)
230 return (UDADDR
& (1 << ADDEN
));
233 #if (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
234 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString
) ATTR_NON_NULL_PTR_ARG(1);
235 static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString
)
237 uint_reg_t CurrentGlobalInt
= GetGlobalInterruptMask();
238 GlobalInterruptDisable();
240 uint8_t SigReadAddress
= INTERNAL_SERIAL_START_ADDRESS
;
242 for (uint8_t SerialCharNum
= 0; SerialCharNum
< (INTERNAL_SERIAL_LENGTH_BITS
/ 4); SerialCharNum
++)
244 uint8_t SerialByte
= boot_signature_byte_get(SigReadAddress
);
246 if (SerialCharNum
& 0x01)
254 UnicodeString
[SerialCharNum
] = cpu_to_le16((SerialByte
>= 10) ?
255 (('A' - 10) + SerialByte
) : ('0' + SerialByte
));
258 SetGlobalInterruptMask(CurrentGlobalInt
);
264 /* Disable C linkage for C++ Compilers: */
265 #if defined(__cplusplus)