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