Removed two-step endpoint/pipe bank clear and switch sequence for smaller, faster...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / LowLevel.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "LowLevel.h"
33
34 #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY))
35 volatile uint8_t USB_CurrentMode = USB_MODE_NONE;
36 #endif
37
38 #if !defined(USE_STATIC_OPTIONS)
39 volatile uint8_t USB_Options;
40 #endif
41
42 void USB_Init(
43 #if defined(USB_CAN_BE_BOTH)
44 const uint8_t Mode
45 #endif
46
47 #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
48 ,
49 #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
50 void
51 #endif
52
53 #if !defined(USE_STATIC_OPTIONS)
54 const uint8_t Options
55 #endif
56 )
57 {
58 #if defined(USB_CAN_BE_BOTH)
59 USB_CurrentMode = Mode;
60 #endif
61
62 #if !defined(USE_STATIC_OPTIONS)
63 USB_Options = Options;
64 #endif
65
66 #if defined(USB_CAN_BE_HOST)
67 USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
68 #endif
69
70 #if defined(USB_DEVICE_ONLY) && (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
71 UHWCON |= (1 << UIMOD);
72 #elif defined(USB_HOST_ONLY)
73 UHWCON &= ~(1 << UIMOD);
74 #elif defined(USB_CAN_BE_BOTH)
75 if (Mode == USB_MODE_UID)
76 {
77 UHWCON |= (1 << UIDE);
78
79 USB_INT_Clear(USB_INT_IDTI);
80 USB_INT_Enable(USB_INT_IDTI);
81
82 USB_CurrentMode = USB_GetUSBModeFromUID();
83 }
84 else if (Mode == USB_MODE_DEVICE)
85 {
86 UHWCON |= (1 << UIMOD);
87 }
88 else if (Mode == USB_MODE_HOST)
89 {
90 UHWCON &= ~(1 << UIMOD);
91 }
92 else
93 {
94 EVENT_USB_InitFailure(USB_INITERROR_NoUSBModeSpecified);
95 return;
96 }
97 #endif
98
99 USB_ResetInterface();
100
101 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
102 USB_OTGPAD_On();
103 #endif
104
105 USB_IsInitialized = true;
106
107 sei();
108 }
109
110 void USB_ShutDown(void)
111 {
112 USB_ResetInterface();
113 USB_Detach();
114 USB_Controller_Disable();
115
116 USB_INT_DisableAllInterrupts();
117 USB_INT_ClearAllInterrupts();
118
119 #if defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
120 UHWCON &= ~(1 << UIMOD);
121 #endif
122
123 if (!(USB_Options & USB_OPT_MANUAL_PLL))
124 USB_PLL_Off();
125
126 USB_REG_Off();
127
128 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
129 USB_OTGPAD_Off();
130 #endif
131
132 #if defined(USB_CAN_BE_BOTH)
133 UHWCON &= ~(1 << UIDE);
134 #endif
135
136 USB_IsInitialized = false;
137
138 #if defined(USB_CAN_BE_BOTH)
139 USB_CurrentMode = USB_MODE_NONE;
140 #endif
141 }
142
143 void USB_ResetInterface(void)
144 {
145 USB_INT_DisableAllInterrupts();
146 USB_INT_ClearAllInterrupts();
147
148 #if defined(USB_CAN_BE_HOST)
149 USB_HostState = HOST_STATE_Unattached;
150 #endif
151
152 #if defined(USB_CAN_BE_DEVICE)
153 USB_DeviceState = DEVICE_STATE_Unattached;
154 USB_ConfigurationNumber = 0;
155
156 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
157 USB_RemoteWakeupEnabled = false;
158 #endif
159
160 #if !defined(NO_DEVICE_SELF_POWER)
161 USB_CurrentlySelfPowered = false;
162 #endif
163 #endif
164
165 if (!(USB_Options & USB_OPT_MANUAL_PLL))
166 {
167 #if defined(USB_SERIES_4_AVR)
168 PLLFRQ = ((1 << PLLUSB) | (1 << PDIV3) | (1 << PDIV1));
169 #endif
170
171 USB_PLL_On();
172 while (!(USB_PLL_IsReady()));
173 }
174
175 USB_Controller_Reset();
176
177 #if defined(USB_CAN_BE_BOTH)
178 if (UHWCON & (1 << UIDE))
179 {
180 USB_INT_Clear(USB_INT_IDTI);
181 USB_INT_Enable(USB_INT_IDTI);
182 USB_CurrentMode = USB_GetUSBModeFromUID();
183 }
184 #endif
185
186 if (!(USB_Options & USB_OPT_REG_DISABLED))
187 USB_REG_On();
188 else
189 USB_REG_Off();
190
191 USB_CLK_Unfreeze();
192
193 #if (defined(USB_CAN_BE_DEVICE) && (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)))
194 if (USB_CurrentMode == USB_MODE_DEVICE)
195 {
196 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
197 USB_Device_SetLowSpeed();
198 else
199 USB_Device_SetFullSpeed();
200 }
201 #endif
202
203 #if (defined(USB_CAN_BE_DEVICE) && !defined(FIXED_CONTROL_ENDPOINT_SIZE))
204 if (USB_CurrentMode == USB_MODE_DEVICE)
205 {
206 USB_Descriptor_Device_t* DeviceDescriptorPtr;
207
208 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
209 {
210 #if defined(USE_RAM_DESCRIPTORS)
211 USB_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
212 #elif defined(USE_EEPROM_DESCRIPTORS)
213 USB_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
214 #else
215 USB_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
216 #endif
217 }
218 }
219 #endif
220
221 USB_Attach();
222
223 #if defined(USB_DEVICE_ONLY)
224 USB_INT_Clear(USB_INT_SUSPEND);
225 USB_INT_Enable(USB_INT_SUSPEND);
226 USB_INT_Clear(USB_INT_EORSTI);
227 USB_INT_Enable(USB_INT_EORSTI);
228
229 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
230 USB_INT_Enable(USB_INT_VBUS);
231 #endif
232 #elif defined(USB_HOST_ONLY)
233 USB_Host_HostMode_On();
234
235 USB_Host_VBUS_Auto_Off();
236 USB_OTGPAD_Off();
237
238 USB_Host_VBUS_Manual_Enable();
239 USB_Host_VBUS_Manual_On();
240
241 USB_INT_Enable(USB_INT_SRPI);
242 USB_INT_Enable(USB_INT_BCERRI);
243 #else
244 if (USB_CurrentMode == USB_MODE_DEVICE)
245 {
246 USB_INT_Clear(USB_INT_SUSPEND);
247 USB_INT_Enable(USB_INT_SUSPEND);
248 USB_INT_Clear(USB_INT_EORSTI);
249 USB_INT_Enable(USB_INT_EORSTI);
250
251 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
252 USB_INT_Enable(USB_INT_VBUS);
253 #endif
254
255 #if defined(CONTROL_ONLY_DEVICE)
256 UENUM = ENDPOINT_CONTROLEP;
257 #endif
258 }
259 else if (USB_CurrentMode == USB_MODE_HOST)
260 {
261 USB_Host_HostMode_On();
262
263 USB_Host_VBUS_Auto_Off();
264 USB_OTGPAD_Off();
265
266 USB_Host_VBUS_Manual_Enable();
267 USB_Host_VBUS_Manual_On();
268
269 USB_INT_Enable(USB_INT_SRPI);
270 USB_INT_Enable(USB_INT_BCERRI);
271 }
272 #endif
273 }