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