3 Copyright (C) Dean Camera, 2018.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
21 The author disclaims 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
31 #include "../../../../Common/Common.h"
32 #if (ARCH == ARCH_AVR8)
34 #define __INCLUDE_FROM_USB_DRIVER
35 #define __INCLUDE_FROM_USB_CONTROLLER_C
36 #include "../USBController.h"
38 #if defined(USB_CAN_BE_BOTH)
39 volatile uint8_t USB_CurrentMode
= USB_MODE_None
;
42 #if !defined(USE_STATIC_OPTIONS)
43 volatile uint8_t USB_Options
;
47 #if defined(USB_CAN_BE_BOTH)
51 #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
53 #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
57 #if !defined(USE_STATIC_OPTIONS)
62 #if !defined(USE_STATIC_OPTIONS)
63 USB_Options
= Options
;
66 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
67 /* Workaround for AVR8 bootloaders that fail to turn off the OTG pad before running
68 * the loaded application. This causes VBUS detection to fail unless we first force
69 * it off to reset it. */
73 if (!(USB_Options
& USB_OPT_REG_DISABLED
))
78 if (!(USB_Options
& USB_OPT_MANUAL_PLL
))
80 #if defined(USB_SERIES_4_AVR)
81 PLLFRQ
= (1 << PDIV2
);
85 #if defined(USB_CAN_BE_BOTH)
86 if (Mode
== USB_MODE_UID
)
88 UHWCON
|= (1 << UIDE
);
89 USB_INT_Enable(USB_INT_IDTI
);
90 USB_CurrentMode
= USB_GetUSBModeFromUID();
94 UHWCON
&= ~(1 << UIDE
);
95 USB_CurrentMode
= Mode
;
99 USB_IsInitialized
= true;
101 USB_ResetInterface();
104 void USB_Disable(void)
106 USB_INT_DisableAllInterrupts();
107 USB_INT_ClearAllInterrupts();
110 USB_Controller_Disable();
112 if (!(USB_Options
& USB_OPT_MANUAL_PLL
))
115 if (!(USB_Options
& USB_OPT_REG_KEEP_ENABLED
))
118 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
122 #if defined(USB_CAN_BE_BOTH)
123 USB_CurrentMode
= USB_MODE_None
;
126 USB_IsInitialized
= false;
129 void USB_ResetInterface(void)
131 #if defined(USB_CAN_BE_BOTH)
132 bool UIDModeSelectEnabled
= ((UHWCON
& (1 << UIDE
)) != 0);
135 USB_INT_DisableAllInterrupts();
136 USB_INT_ClearAllInterrupts();
138 USB_Controller_Reset();
140 #if defined(USB_CAN_BE_BOTH)
141 if (UIDModeSelectEnabled
)
142 USB_INT_Enable(USB_INT_IDTI
);
147 if (USB_CurrentMode
== USB_MODE_Device
)
149 #if defined(USB_CAN_BE_DEVICE)
150 #if (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
151 UHWCON
|= (1 << UIMOD
);
154 if (!(USB_Options
& USB_OPT_MANUAL_PLL
))
156 #if defined(USB_SERIES_2_AVR)
158 while (!(USB_PLL_IsReady()));
167 else if (USB_CurrentMode
== USB_MODE_Host
)
169 #if defined(USB_CAN_BE_HOST)
170 UHWCON
&= ~(1 << UIMOD
);
172 if (!(USB_Options
& USB_OPT_MANUAL_PLL
))
174 #if defined(USB_CAN_BE_HOST)
176 while (!(USB_PLL_IsReady()));
184 #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
189 #if defined(USB_CAN_BE_DEVICE)
190 static void USB_Init_Device(void)
192 USB_DeviceState
= DEVICE_STATE_Unattached
;
193 USB_Device_ConfigurationNumber
= 0;
195 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
196 USB_Device_RemoteWakeupEnabled
= false;
199 #if !defined(NO_DEVICE_SELF_POWER)
200 USB_Device_CurrentlySelfPowered
= false;
203 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
204 USB_Descriptor_Device_t
* DeviceDescriptorPtr
;
206 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
207 !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
208 uint8_t DescriptorAddressSpace
;
210 if (CALLBACK_USB_GetDescriptor((DTYPE_Device
<< 8), 0, (void*)&DeviceDescriptorPtr
, &DescriptorAddressSpace
) != NO_DESCRIPTOR
)
212 if (DescriptorAddressSpace
== MEMSPACE_FLASH
)
213 USB_Device_ControlEndpointSize
= pgm_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
);
214 else if (DescriptorAddressSpace
== MEMSPACE_EEPROM
)
215 USB_Device_ControlEndpointSize
= eeprom_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
);
217 USB_Device_ControlEndpointSize
= DeviceDescriptorPtr
->Endpoint0Size
;
220 if (CALLBACK_USB_GetDescriptor((DTYPE_Device
<< 8), 0, (void*)&DeviceDescriptorPtr
) != NO_DESCRIPTOR
)
222 #if defined(USE_RAM_DESCRIPTORS)
223 USB_Device_ControlEndpointSize
= DeviceDescriptorPtr
->Endpoint0Size
;
224 #elif defined(USE_EEPROM_DESCRIPTORS)
225 USB_Device_ControlEndpointSize
= eeprom_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
);
227 USB_Device_ControlEndpointSize
= pgm_read_byte(&DeviceDescriptorPtr
->Endpoint0Size
);
233 #if (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
234 if (USB_Options
& USB_DEVICE_OPT_LOWSPEED
)
235 USB_Device_SetLowSpeed();
237 USB_Device_SetFullSpeed();
239 USB_INT_Enable(USB_INT_VBUSTI
);
242 Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP
, EP_TYPE_CONTROL
,
243 USB_Device_ControlEndpointSize
, 1);
245 USB_INT_Clear(USB_INT_SUSPI
);
246 USB_INT_Enable(USB_INT_SUSPI
);
247 USB_INT_Enable(USB_INT_EORSTI
);
253 #if defined(USB_CAN_BE_HOST)
254 static void USB_Init_Host(void)
256 USB_HostState
= HOST_STATE_Unattached
;
257 USB_Host_ConfigurationNumber
= 0;
258 USB_Host_ControlPipeSize
= PIPE_CONTROLPIPE_DEFAULT_SIZE
;
260 USB_Host_HostMode_On();
262 USB_Host_VBUS_Auto_Off();
263 USB_Host_VBUS_Manual_Enable();
264 USB_Host_VBUS_Manual_On();
266 USB_INT_Enable(USB_INT_SRPI
);
267 USB_INT_Enable(USB_INT_BCERRI
);