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_XMEGA)
\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
46 /* Ugly workaround to ensure an aligned table, since __BIGGEST_ALIGNMENT__ == 1 for 8-bit AVR-GCC */
\r
47 uint8_t USB_EndpointTable[sizeof(USB_EndpointTable_t) + 1];
\r
50 #if defined(USB_CAN_BE_BOTH)
\r
54 #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
\r
56 #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
\r
60 #if !defined(USE_STATIC_OPTIONS)
\r
61 const uint8_t Options
\r
65 #if !defined(USE_STATIC_OPTIONS)
\r
66 USB_Options = Options;
\r
69 USB_IsInitialized = true;
\r
71 uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
\r
72 GlobalInterruptDisable();
\r
74 NVM.CMD = NVM_CMD_READ_CALIB_ROW_gc;
\r
75 USB.CAL0 = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBCAL0));
\r
76 USB.CAL1 = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBCAL1));
\r
79 /* Ugly workaround to ensure an aligned table, since __BIGGEST_ALIGNMENT__ == 1 for the 8-bit AVR-GCC toochain */
\r
80 USB.EPPTR = ((intptr_t)&USB_EndpointTable[1] & ~(1 << 0));
\r
81 USB.CTRLA = (USB_STFRNUM_bm | ((ENDPOINT_TOTAL_ENDPOINTS - 1) << USB_MAXEP_gp));
\r
83 if ((USB_Options & USB_OPT_BUSEVENT_PRIHIGH) == USB_OPT_BUSEVENT_PRIHIGH)
\r
84 USB.INTCTRLA = (3 << USB_INTLVL_gp);
\r
85 else if ((USB_Options & USB_OPT_BUSEVENT_PRIMED) == USB_OPT_BUSEVENT_PRIMED)
\r
86 USB.INTCTRLA = (2 << USB_INTLVL_gp);
\r
88 USB.INTCTRLA = (1 << USB_INTLVL_gp);
\r
90 SetGlobalInterruptMask(CurrentGlobalInt);
\r
92 USB_ResetInterface();
\r
95 void USB_Disable(void)
\r
97 USB_INT_DisableAllInterrupts();
\r
98 USB_INT_ClearAllInterrupts();
\r
101 USB_Controller_Disable();
\r
103 USB_IsInitialized = false;
\r
106 void USB_ResetInterface(void)
\r
108 #if defined(USB_DEVICE_OPT_FULLSPEED)
\r
109 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
\r
110 CLK.USBCTRL = (((F_USB / 6000000) - 1) << CLK_USBPSDIV_gp);
\r
112 CLK.USBCTRL = (((F_USB / 48000000) - 1) << CLK_USBPSDIV_gp);
\r
114 CLK.USBCTRL = (((F_USB / 6000000) - 1) << CLK_USBPSDIV_gp);
\r
117 if (USB_Options & USB_OPT_PLLCLKSRC)
\r
118 CLK.USBCTRL |= (CLK_USBSRC_PLL_gc | CLK_USBSEN_bm);
\r
120 CLK.USBCTRL |= (CLK_USBSRC_RC32M_gc | CLK_USBSEN_bm);
\r
122 USB_Device_SetDeviceAddress(0);
\r
124 USB_INT_DisableAllInterrupts();
\r
125 USB_INT_ClearAllInterrupts();
\r
127 USB_Controller_Reset();
\r
131 #if defined(USB_CAN_BE_DEVICE)
\r
132 static void USB_Init_Device(void)
\r
134 USB_DeviceState = DEVICE_STATE_Unattached;
\r
135 USB_Device_ConfigurationNumber = 0;
\r
137 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
\r
138 USB_Device_RemoteWakeupEnabled = false;
\r
141 #if !defined(NO_DEVICE_SELF_POWER)
\r
142 USB_Device_CurrentlySelfPowered = false;
\r
145 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
\r
146 USB_Descriptor_Device_t* DeviceDescriptorPtr;
\r
148 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
\r
149 !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
\r
150 uint8_t DescriptorAddressSpace;
\r
152 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr, &DescriptorAddressSpace) != NO_DESCRIPTOR)
\r
154 if (DescriptorAddressSpace == MEMSPACE_FLASH)
\r
155 USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
156 else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
\r
157 USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
159 USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
\r
162 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
\r
164 #if defined(USE_RAM_DESCRIPTORS)
\r
165 USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
\r
166 #elif defined(USE_EEPROM_DESCRIPTORS)
\r
167 USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
169 USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
\r
175 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
\r
176 USB_Device_SetLowSpeed();
\r
178 USB_Device_SetFullSpeed();
\r
180 Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
\r
181 USB_Device_ControlEndpointSize, 1);
\r
183 USB_INT_Enable(USB_INT_BUSEVENTI);
\r