Added incomplete MIDIToneGenerator project.
[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 if (!(USB_Options & USB_OPT_MANUAL_PLL))
117 USB_PLL_Off();
118
119 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
120 USB_OTGPAD_Off();
121 #endif
122
123 #if defined(USB_CAN_BE_BOTH)
124 UHWCON &= ~(1 << UIDE);
125 #endif
126
127 USB_IsInitialized = false;
128
129 #if defined(USB_CAN_BE_BOTH)
130 USB_CurrentMode = USB_MODE_NONE;
131 #endif
132 }
133
134 void USB_ResetInterface(void)
135 {
136 USB_INT_DisableAllInterrupts();
137 USB_INT_ClearAllInterrupts();
138
139 #if defined(USB_CAN_BE_HOST)
140 USB_HostState = HOST_STATE_Unattached;
141 #endif
142
143 #if defined(USB_CAN_BE_DEVICE)
144 USB_DeviceState = DEVICE_STATE_Unattached;
145 USB_ConfigurationNumber = 0;
146
147 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
148 USB_RemoteWakeupEnabled = false;
149 #endif
150
151 #if !defined(NO_DEVICE_SELF_POWER)
152 USB_CurrentlySelfPowered = false;
153 #endif
154 #endif
155
156 if (!(USB_Options & USB_OPT_MANUAL_PLL))
157 {
158 #if defined(USB_SERIES_4_AVR)
159 PLLFRQ = ((1 << PLLUSB) | (1 << PDIV3) | (1 << PDIV1));
160 #endif
161
162 USB_PLL_On();
163 while (!(USB_PLL_IsReady()));
164 }
165
166 USB_Controller_Reset();
167
168 #if defined(USB_CAN_BE_BOTH)
169 if (UHWCON & (1 << UIDE))
170 {
171 USB_INT_Clear(USB_INT_IDTI);
172 USB_INT_Enable(USB_INT_IDTI);
173 USB_CurrentMode = USB_GetUSBModeFromUID();
174 }
175 #endif
176
177 if (!(USB_Options & USB_OPT_REG_DISABLED))
178 USB_REG_On();
179 else
180 USB_REG_Off();
181
182 USB_CLK_Unfreeze();
183
184 #if (defined(USB_CAN_BE_DEVICE) && (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)))
185 if (USB_CurrentMode == USB_MODE_DEVICE)
186 {
187 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
188 USB_Device_SetLowSpeed();
189 else
190 USB_Device_SetFullSpeed();
191 }
192 #endif
193
194 #if (defined(USB_CAN_BE_DEVICE) && !defined(FIXED_CONTROL_ENDPOINT_SIZE))
195 if (USB_CurrentMode == USB_MODE_DEVICE)
196 {
197 USB_Descriptor_Device_t* DeviceDescriptorPtr;
198
199 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
200 {
201 #if defined(USE_RAM_DESCRIPTORS)
202 USB_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
203 #elif defined(USE_EEPROM_DESCRIPTORS)
204 USB_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
205 #else
206 USB_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
207 #endif
208 }
209 }
210 #endif
211
212 USB_Attach();
213
214 #if defined(USB_DEVICE_ONLY)
215 USB_INT_Clear(USB_INT_SUSPEND);
216 USB_INT_Enable(USB_INT_SUSPEND);
217 USB_INT_Clear(USB_INT_EORSTI);
218 USB_INT_Enable(USB_INT_EORSTI);
219
220 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
221 USB_INT_Enable(USB_INT_VBUS);
222 #endif
223 #elif defined(USB_HOST_ONLY)
224 USB_Host_HostMode_On();
225
226 USB_Host_VBUS_Auto_Off();
227 USB_OTGPAD_Off();
228
229 USB_Host_VBUS_Manual_Enable();
230 USB_Host_VBUS_Manual_On();
231
232 USB_INT_Enable(USB_INT_SRPI);
233 USB_INT_Enable(USB_INT_BCERRI);
234 #else
235 if (USB_CurrentMode == USB_MODE_DEVICE)
236 {
237 USB_INT_Clear(USB_INT_SUSPEND);
238 USB_INT_Enable(USB_INT_SUSPEND);
239 USB_INT_Clear(USB_INT_EORSTI);
240 USB_INT_Enable(USB_INT_EORSTI);
241
242 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
243 USB_INT_Enable(USB_INT_VBUS);
244 #endif
245
246 #if defined(CONTROL_ONLY_DEVICE)
247 UENUM = ENDPOINT_CONTROLEP;
248 #endif
249 }
250 else if (USB_CurrentMode == USB_MODE_HOST)
251 {
252 USB_Host_HostMode_On();
253
254 USB_Host_VBUS_Auto_Off();
255 USB_OTGPAD_Off();
256
257 USB_Host_VBUS_Manual_Enable();
258 USB_Host_VBUS_Manual_On();
259
260 USB_INT_Enable(USB_INT_SRPI);
261 USB_INT_Enable(USB_INT_BCERRI);
262 }
263 #endif
264 }