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