3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
35 #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY))
36 volatile uint8_t USB_CurrentMode
= USB_MODE_NONE
;
39 #if !defined(USE_STATIC_OPTIONS)
40 volatile uint8_t USB_Options
;
44 #if defined(USB_CAN_BE_BOTH)
48 #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
50 #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
54 #if !defined(USE_STATIC_OPTIONS)
61 #if defined(USB_CAN_BE_BOTH)
62 USB_CurrentMode
= Mode
;
65 #if !defined(USE_STATIC_OPTIONS)
66 USB_Options
= Options
;
69 #if defined(USB_CAN_BE_HOST)
70 USB_ControlPipeSize
= PIPE_CONTROLPIPE_DEFAULT_SIZE
;
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
)
80 UHWCON
|= (1 << UIDE
);
82 USB_INT_Clear(USB_INT_IDTI
);
83 USB_INT_Enable(USB_INT_IDTI
);
85 USB_CurrentMode
= USB_GetUSBModeFromUID();
87 else if (Mode
== USB_MODE_DEVICE
)
89 UHWCON
|= (1 << UIMOD
);
91 else if (Mode
== USB_MODE_HOST
)
93 UHWCON
&= ~(1 << UIMOD
);
97 RAISE_EVENT(USB_PowerOnFail
, POWERON_ERROR_NoUSBModeSpecified
);
102 USB_ResetInterface();
104 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
108 USB_IsInitialized
= true;
113 void USB_ShutDown(void)
116 RAISE_EVENT(USB_Disconnect
);
120 USB_INT_DisableAllInterrupts();
121 USB_INT_ClearAllInterrupts();
123 USB_IsConnected
= false;
124 USB_IsInitialized
= false;
126 #if defined(USB_CAN_BE_HOST)
127 USB_HostState
= HOST_STATE_Unattached
;
130 #if defined(USB_CAN_BE_DEVICE)
131 USB_ConfigurationNumber
= 0;
134 #if defined(CAN_BE_BOTH)
135 USB_CurrentMode
= USB_MODE_NONE
;
138 USB_Interface_Disable();
141 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
145 #if defined(USB_CAN_BE_BOTH)
146 UHWCON
&= ~(1 << UIDE
);
150 void USB_ResetInterface(void)
152 USB_INT_DisableAllInterrupts();
153 USB_INT_ClearAllInterrupts();
155 USB_IsConnected
= false;
157 #if defined(USB_CAN_BE_HOST)
158 USB_HostState
= HOST_STATE_Unattached
;
161 #if defined(USB_CAN_BE_DEVICE)
162 USB_ConfigurationNumber
= 0;
163 USB_IsSuspended
= false;
164 USB_RemoteWakeupEnabled
= false;
165 USB_CurrentlySelfPowered
= false;
168 if (!(USB_Options
& USB_OPT_MANUAL_PLL
))
170 #if defined(USB_MODIFIED_FULL_CONTROLLER)
171 PLLFRQ
= ((1 << PLLUSB
) | (1 << PDIV3
) | (1 << PDIV1
));
175 while (!(USB_PLL_IsReady()));
178 USB_Interface_Reset();
180 #if defined(USB_CAN_BE_BOTH)
181 if (UHWCON
& (1 << UIDE
))
183 USB_INT_Clear(USB_INT_IDTI
);
184 USB_INT_Enable(USB_INT_IDTI
);
185 USB_CurrentMode
= USB_GetUSBModeFromUID();
189 if (!(USB_Options
& USB_OPT_REG_DISABLED
))
194 #if (defined(USB_CAN_BE_DEVICE) && (defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)))
195 if (USB_CurrentMode
== USB_MODE_DEVICE
)
197 if (USB_Options
& USB_DEVICE_OPT_LOWSPEED
)
198 USB_Device_SetLowSpeed();
200 USB_Device_SetHighSpeed();
202 USB_INT_Enable(USB_INT_VBUS
);
206 #if (defined(USB_CAN_BE_DEVICE) && !defined(FIXED_CONTROL_ENDPOINT_SIZE))
207 if (USB_CurrentMode
== USB_MODE_DEVICE
)
209 USB_Descriptor_Device_t
* DeviceDescriptorPtr
;
211 if (USB_GetDescriptor((DTYPE_Device
<< 8), 0, (void*)&DeviceDescriptorPtr
) != NO_DESCRIPTOR
)
213 #if defined(USE_RAM_DESCRIPTORS)
214 USB_ControlEndpointSize
= DeviceDescriptorPtr
->Endpoint0Size
;
215 #elif defined(USE_EEPROM_DESCRIPTORS)
216 USB_ControlEndpointSize
= eeprom_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
);
218 USB_ControlEndpointSize
= pgm_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
);
226 #if defined(USB_DEVICE_ONLY)
227 USB_INT_Enable(USB_INT_SUSPEND
);
228 USB_INT_Enable(USB_INT_EORSTI
);
229 #elif defined(USB_HOST_ONLY)
230 USB_Host_HostMode_On();
232 USB_Host_VBUS_Auto_Off();
235 USB_Host_VBUS_Manual_Enable();
236 USB_Host_VBUS_Manual_On();
238 USB_INT_Enable(USB_INT_SRPI
);
239 USB_INT_Enable(USB_INT_BCERRI
);
241 if (USB_CurrentMode
== USB_MODE_DEVICE
)
243 USB_INT_Enable(USB_INT_SUSPEND
);
244 USB_INT_Enable(USB_INT_EORSTI
);
246 else if (USB_CurrentMode
== USB_MODE_HOST
)
248 USB_Host_HostMode_On();
250 USB_Host_VBUS_Auto_Off();
253 USB_Host_VBUS_Manual_Enable();
254 USB_Host_VBUS_Manual_On();
256 USB_INT_Enable(USB_INT_SRPI
);
257 USB_INT_Enable(USB_INT_BCERRI
);