Statistics
| Branch: | Tag: | Revision:

root / LUFA / Drivers / USB / Core / AVR8 / Device_AVR8.h @ 978b99e5

History | View | Annotate | Download (9.82 KB)

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 AVR8 microcontrollers.
33
 *  \copydetails Group_Device_AVR8
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_AVR8 Device Management (AVR8)
41
 *  \brief USB Device definitions for the AVR8 microcontrollers.
42
 *
43
 *  Architecture specific USB Device definitions for the Atmel 8-bit AVR microcontrollers.
44
 *
45
 *  @{
46
 */
47

    
48
#ifndef __USBDEVICE_AVR8_H__
49
#define __USBDEVICE_AVR8_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
        /* Public Interface - May be used in end-application: */
73
                /* Macros: */
74
                        /** \name USB Device Mode Option Masks */
75
                        //@{
76
                        #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__)
77
                                /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
78
                                 *  USB interface should be initialized in low speed (1.5Mb/s) mode.
79
                                 *
80
                                 *  \note Low Speed mode is not available on all USB AVR models.
81
                                 *        \n
82
                                 *
83
                                 *  \note Restrictions apply on the number, size and type of endpoints which can be used
84
                                 *        when running in low speed mode - refer to the USB 2.0 specification.
85
                                 */
86
                                #define USB_DEVICE_OPT_LOWSPEED            (1 << 0)
87
                        #endif
88

    
89
                        /** Mask for the Options parameter of the \ref USB_Init() function. This indicates that the
90
                         *  USB interface should be initialized in full speed (12Mb/s) mode.
91
                         */
92
                        #define USB_DEVICE_OPT_FULLSPEED               (0 << 0)
93
                        //@}
94
                        
95
                        #if (!defined(NO_INTERNAL_SERIAL) && \
96
                             (defined(USB_SERIES_7_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_4_AVR) || \
97
                              (defined(USB_SERIES_2_AVR) && (!defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__))) || \
98
                                  defined(__DOXYGEN__)))
99
                                /** String descriptor index for the device's unique serial number string descriptor within the device.
100
                                 *  This unique serial number is used by the host to associate resources to the device (such as drivers or COM port
101
                                 *  number allocations) to a device regardless of the port it is plugged in to on the host. Some microcontrollers contain
102
                                 *  a unique serial number internally, and setting the device descriptors serial number string index to this value
103
                                 *  will cause it to use the internal serial number.
104
                                 *
105
                                 *  On unsupported devices, this will evaluate to \ref NO_DESCRIPTOR and so will force the host to create a pseudo-serial
106
                                 *  number for the device.
107
                                 */
108
                                #define USE_INTERNAL_SERIAL            0xDC
109

    
110
                                /** Length of the device's unique internal serial number, in bits, if present on the selected microcontroller
111
                                 *  model.
112
                                 */
113
                                #define INTERNAL_SERIAL_LENGTH_BITS    80
114
                                
115
                                /** Start address of the internal serial number, in the appropriate address space, if present on the selected microcontroller
116
                                 *  model.
117
                                 */
118
                                #define INTERNAL_SERIAL_START_ADDRESS  0x0E
119
                        #else
120
                                #define USE_INTERNAL_SERIAL            NO_DESCRIPTOR
121

    
122
                                #define INTERNAL_SERIAL_LENGTH_BITS    0
123
                                #define INTERNAL_SERIAL_START_ADDRESS  0
124
                        #endif                        
125
                        
126
                /* Function Prototypes: */
127
                        /** Sends a Remote Wakeup request to the host. This signals to the host that the device should
128
                         *  be taken out of suspended mode, and communications should resume.
129
                         *
130
                         *  Typically, this is implemented so that HID devices (mice, keyboards, etc.) can wake up the
131
                         *  host computer when the host has suspended all USB devices to enter a low power state.
132
                         *
133
                         *  \note This macro should only be used if the device has indicated to the host that it
134
                         *        supports the Remote Wakeup feature in the device descriptors, and should only be
135
                         *        issued if the host is currently allowing remote wakeup events from the device (i.e.,
136
                         *        the \ref USB_Device_RemoteWakeupEnabled flag is set). When the \c NO_DEVICE_REMOTE_WAKEUP
137
                         *        compile time option is used, this macro is unavailable.
138
                         *        \n\n
139
                         *
140
                         *  \note The USB clock must be running for this function to operate. If the stack is initialized with
141
                         *        the \ref USB_OPT_MANUAL_PLL option enabled, the user must ensure that the PLL is running
142
                         *        before attempting to call this function.
143
                         *
144
                         *  \see \ref Group_StdDescriptors for more information on the RMWAKEUP feature and device descriptors.
145
                         */
146
                        void USB_Device_SendRemoteWakeup(void);
147

    
148
                /* Inline Functions: */
149
                        /** Returns the current USB frame number, when in device mode. Every millisecond the USB bus is active (i.e. enumerated to a host)
150
                         *  the frame number is incremented by one.
151
                         *
152
                         *  \return Current USB frame number from the USB controller.
153
                         */
154
                        static inline uint16_t USB_Device_GetFrameNumber(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
155
                        static inline uint16_t USB_Device_GetFrameNumber(void)
156
                        {
157
                                return UDFNUM;
158
                        }
159

    
160
                        #if !defined(NO_SOF_EVENTS)
161
                                /** Enables the device mode Start Of Frame events. When enabled, this causes the
162
                                 *  \ref EVENT_USB_Device_StartOfFrame() event to fire once per millisecond, synchronized to the USB bus,
163
                                 *  at the start of each USB frame when enumerated in device mode.
164
                                 *
165
                                 *  \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
166
                                 */
167
                                static inline void USB_Device_EnableSOFEvents(void) ATTR_ALWAYS_INLINE;
168
                                static inline void USB_Device_EnableSOFEvents(void)
169
                                {
170
                                        USB_INT_Enable(USB_INT_SOFI);
171
                                }
172

    
173
                                /** Disables the device mode Start Of Frame events. When disabled, this stops the firing of the
174
                                 *  \ref EVENT_USB_Device_StartOfFrame() event when enumerated in device mode.
175
                                 *
176
                                 *  \note Not available when the \c NO_SOF_EVENTS compile time token is defined.
177
                                 */
178
                                static inline void USB_Device_DisableSOFEvents(void) ATTR_ALWAYS_INLINE;
179
                                static inline void USB_Device_DisableSOFEvents(void)
180
                                {
181
                                        USB_INT_Disable(USB_INT_SOFI);
182
                                }
183
                        #endif
184

    
185
        /* Private Interface - For use in library only: */
186
        #if !defined(__DOXYGEN__)
187
                /* Inline Functions: */
188
                        #if defined(USB_DEVICE_OPT_LOWSPEED)
189
                        static inline void USB_Device_SetLowSpeed(void) ATTR_ALWAYS_INLINE;
190
                        static inline void USB_Device_SetLowSpeed(void)
191
                        {
192
                                UDCON |=  (1 << LSM);
193
                        }
194

    
195
                        static inline void USB_Device_SetFullSpeed(void) ATTR_ALWAYS_INLINE;
196
                        static inline void USB_Device_SetFullSpeed(void)
197
                        {
198
                                UDCON &= ~(1 << LSM);
199
                        }
200
                        #endif
201

    
202
                        static inline void USB_Device_SetDeviceAddress(const uint8_t Address) ATTR_ALWAYS_INLINE;
203
                        static inline void USB_Device_SetDeviceAddress(const uint8_t Address)
204
                        {
205
                                uint8_t Temp = (UDADDR & (1 << ADDEN)) | (Address & 0x7F);
206

    
207
                                UDADDR = Temp;
208
                                UDADDR = Temp | (1 << ADDEN);
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 (UDADDR & (1 << ADDEN));
215
                        }
216
                
217
                        #if (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
218
                        static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString) ATTR_NON_NULL_PTR_ARG(1);
219
                        static inline void USB_Device_GetSerialString(uint16_t* const UnicodeString)
220
                        {
221
                                uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
222
                                GlobalInterruptDisable();
223
                                
224
                                uint8_t SigReadAddress = INTERNAL_SERIAL_START_ADDRESS;
225

    
226
                                for (uint8_t SerialCharNum = 0; SerialCharNum < (INTERNAL_SERIAL_LENGTH_BITS / 4); SerialCharNum++)
227
                                {
228
                                        uint8_t SerialByte = boot_signature_byte_get(SigReadAddress);
229

    
230
                                        if (SerialCharNum & 0x01)
231
                                        {
232
                                                SerialByte >>= 4;
233
                                                SigReadAddress++;
234
                                        }
235

    
236
                                        SerialByte &= 0x0F;
237

    
238
                                        UnicodeString[SerialCharNum] = cpu_to_le16((SerialByte >= 10) ?
239
                                                                                                                           (('A' - 10) + SerialByte) : ('0' + SerialByte));
240
                                }
241
                                
242
                                SetGlobalInterruptMask(CurrentGlobalInt);
243
                        }
244
                        #endif
245

    
246
        #endif
247

    
248
        /* Disable C linkage for C++ Compilers: */
249
                #if defined(__cplusplus)
250
                        }
251
                #endif
252

    
253
#endif
254

    
255
/** @} */
256