3 Copyright (C) Dean Camera, 2012.
\r
5 dean [at] fourwalledcubicle [dot] com
\r
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
\r
12 Permission to use, copy, modify, distribute, and sell this
\r
13 software and its documentation for any purpose is hereby granted
\r
14 without fee, provided that the above copyright notice appear in
\r
15 all copies and that both that the copyright notice and this
\r
16 permission notice and warranty disclaimer appear in supporting
\r
17 documentation, and that the name of the author not be used in
\r
18 advertising or publicity pertaining to distribution of the
\r
19 software without specific, written prior permission.
\r
21 The author disclaim all warranties with regard to this
\r
22 software, including all implied warranties of merchantability
\r
23 and fitness. In no event shall the author be liable for any
\r
24 special, indirect or consequential damages or any damages
\r
25 whatsoever resulting from loss of use, data or profits, whether
\r
26 in an action of contract, negligence or other tortious action,
\r
27 arising out of or in connection with the use or performance of
\r
31 #include "../../../../Common/Common.h"
\r
32 #if (ARCH == ARCH_AVR8)
\r
34 #define __INCLUDE_FROM_USB_DRIVER
\r
35 #define __INCLUDE_FROM_USB_CONTROLLER_C
\r
36 #include "../USBController.h"
\r
38 #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY))
\r
39 volatile uint8_t USB_CurrentMode = USB_MODE_None;
\r
42 #if !defined(USE_STATIC_OPTIONS)
\r
43 volatile uint8_t USB_Options;
\r
47 #if defined(USB_CAN_BE_BOTH)
\r
51 #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
\r
53 #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
\r
57 #if !defined(USE_STATIC_OPTIONS)
\r
58 const uint8_t Options
\r
62 #if !defined(USE_STATIC_OPTIONS)
\r
63 USB_Options = Options;
\r
66 if (!(USB_Options & USB_OPT_REG_DISABLED))
\r
71 if (!(USB_Options & USB_OPT_MANUAL_PLL))
\r
73 #if defined(USB_SERIES_4_AVR)
\r
74 PLLFRQ = (1 << PDIV2);
\r
78 #if defined(USB_CAN_BE_BOTH)
\r
79 if (Mode == USB_MODE_UID)
\r
81 UHWCON |= (1 << UIDE);
\r
82 USB_INT_Enable(USB_INT_IDTI);
\r
83 USB_CurrentMode = USB_GetUSBModeFromUID();
\r
87 UHWCON &= ~(1 << UIDE);
\r
88 USB_CurrentMode = Mode;
\r
92 USB_IsInitialized = true;
\r
94 USB_ResetInterface();
\r
97 void USB_Disable(void)
\r
99 USB_INT_DisableAllInterrupts();
\r
100 USB_INT_ClearAllInterrupts();
\r
103 USB_Controller_Disable();
\r
105 if (!(USB_Options & USB_OPT_MANUAL_PLL))
\r
110 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
\r
114 #if defined(USB_CAN_BE_BOTH)
\r
115 USB_CurrentMode = USB_MODE_None;
\r
118 USB_IsInitialized = false;
\r
121 void USB_ResetInterface(void)
\r
123 #if defined(USB_CAN_BE_BOTH)
\r
124 bool UIDModeSelectEnabled = ((UHWCON & (1 << UIDE)) != 0);
\r
127 USB_INT_DisableAllInterrupts();
\r
128 USB_INT_ClearAllInterrupts();
\r
130 USB_Controller_Reset();
\r
132 #if defined(USB_CAN_BE_BOTH)
\r
133 if (UIDModeSelectEnabled)
\r
134 USB_INT_Enable(USB_INT_IDTI);
\r
137 USB_CLK_Unfreeze();
\r
139 if (USB_CurrentMode == USB_MODE_Device)
\r
141 #if defined(USB_CAN_BE_DEVICE)
\r
142 #if (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
\r
143 UHWCON |= (1 << UIMOD);
\r
146 if (!(USB_Options & USB_OPT_MANUAL_PLL))
\r
148 #if defined(USB_SERIES_2_AVR)
\r
150 while (!(USB_PLL_IsReady()));
\r
159 else if (USB_CurrentMode == USB_MODE_Host)
\r
161 #if defined(USB_CAN_BE_HOST)
\r
162 UHWCON &= ~(1 << UIMOD);
\r
164 if (!(USB_Options & USB_OPT_MANUAL_PLL))
\r
166 #if defined(USB_CAN_BE_HOST)
\r
168 while (!(USB_PLL_IsReady()));
\r
176 #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
\r
181 #if defined(USB_CAN_BE_DEVICE)
\r
182 static void USB_Init_Device(void)
\r
184 USB_DeviceState = DEVICE_STATE_Unattached;
\r
185 USB_Device_ConfigurationNumber = 0;
\r
187 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
\r
188 USB_Device_RemoteWakeupEnabled = false;
\r
191 #if !defined(NO_DEVICE_SELF_POWER)
\r
192 USB_Device_CurrentlySelfPowered = false;
\r
195 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
\r
196 USB_Descriptor_Device_t* DeviceDescriptorPtr;
\r
198 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
\r
199 !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
\r
200 uint8_t DescriptorAddressSpace;
\r
202 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr, &DescriptorAddressSpace) != NO_DESCRIPTOR)
\r
204 if (DescriptorAddressSpace == MEMSPACE_FLASH)
\r
205 USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
206 else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
\r
207 USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
209 USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
\r
212 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
\r
214 #if defined(USE_RAM_DESCRIPTORS)
\r
215 USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
\r
216 #elif defined(USE_EEPROM_DESCRIPTORS)
\r
217 USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
219 USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
225 #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
\r
226 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
\r
227 USB_Device_SetLowSpeed();
\r
229 USB_Device_SetFullSpeed();
\r
231 USB_INT_Enable(USB_INT_VBUSTI);
\r
234 Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
\r
235 USB_Device_ControlEndpointSize, 1);
\r
237 USB_INT_Clear(USB_INT_SUSPI);
\r
238 USB_INT_Enable(USB_INT_SUSPI);
\r
239 USB_INT_Enable(USB_INT_EORSTI);
\r
245 #if defined(USB_CAN_BE_HOST)
\r
246 static void USB_Init_Host(void)
\r
248 USB_HostState = HOST_STATE_Unattached;
\r
249 USB_Host_ConfigurationNumber = 0;
\r
250 USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
\r
252 USB_Host_HostMode_On();
\r
254 USB_Host_VBUS_Auto_Off();
\r
255 USB_Host_VBUS_Manual_Enable();
\r
256 USB_Host_VBUS_Manual_On();
\r
258 USB_INT_Enable(USB_INT_SRPI);
\r
259 USB_INT_Enable(USB_INT_BCERRI);
\r