]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/XMEGA/USBController_XMEGA.c
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / XMEGA / USBController_XMEGA.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \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
20 \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
28   this software.\r
29 */\r
30 \r
31 #include "../../../../Common/Common.h"\r
32 #if (ARCH == ARCH_XMEGA)\r
33 \r
34 #define  __INCLUDE_FROM_USB_DRIVER\r
35 #define  __INCLUDE_FROM_USB_CONTROLLER_C\r
36 #include "../USBController.h"\r
37 \r
38 #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY))\r
39 volatile uint8_t USB_CurrentMode = USB_MODE_None;\r
40 #endif\r
41 \r
42 #if !defined(USE_STATIC_OPTIONS)\r
43 volatile uint8_t USB_Options;\r
44 #endif\r
45 \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
48 \r
49 void USB_Init(\r
50                #if defined(USB_CAN_BE_BOTH)\r
51                const uint8_t Mode\r
52                #endif\r
53 \r
54                #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))\r
55                ,\r
56                #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))\r
57                void\r
58                #endif\r
59 \r
60                #if !defined(USE_STATIC_OPTIONS)\r
61                const uint8_t Options\r
62                #endif\r
63                )\r
64 {\r
65         #if !defined(USE_STATIC_OPTIONS)\r
66         USB_Options = Options;\r
67         #endif\r
68 \r
69         USB_IsInitialized = true;\r
70 \r
71         uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();\r
72         GlobalInterruptDisable();\r
73 \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
77         NVM.CMD  = 0;\r
78 \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
82 \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
87         else\r
88           USB.INTCTRLA = (1 << USB_INTLVL_gp);\r
89 \r
90         SetGlobalInterruptMask(CurrentGlobalInt);\r
91 \r
92         USB_ResetInterface();\r
93 }\r
94 \r
95 void USB_Disable(void)\r
96 {\r
97         USB_INT_DisableAllInterrupts();\r
98         USB_INT_ClearAllInterrupts();\r
99 \r
100         USB_Detach();\r
101         USB_Controller_Disable();\r
102 \r
103         USB_IsInitialized = false;\r
104 }\r
105 \r
106 void USB_ResetInterface(void)\r
107 {\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
111         else\r
112           CLK.USBCTRL = (((F_USB / 48000000) - 1) << CLK_USBPSDIV_gp);\r
113         #else\r
114         CLK.USBCTRL = (((F_USB / 6000000) - 1) << CLK_USBPSDIV_gp);\r
115         #endif\r
116         \r
117         if (USB_Options & USB_OPT_PLLCLKSRC)\r
118           CLK.USBCTRL |= (CLK_USBSRC_PLL_gc   | CLK_USBSEN_bm);\r
119         else\r
120           CLK.USBCTRL |= (CLK_USBSRC_RC32M_gc | CLK_USBSEN_bm);\r
121 \r
122         USB_Device_SetDeviceAddress(0);\r
123 \r
124         USB_INT_DisableAllInterrupts();\r
125         USB_INT_ClearAllInterrupts();\r
126 \r
127         USB_Controller_Reset();\r
128         USB_Init_Device();\r
129 }\r
130 \r
131 #if defined(USB_CAN_BE_DEVICE)\r
132 static void USB_Init_Device(void)\r
133 {\r
134         USB_DeviceState                 = DEVICE_STATE_Unattached;\r
135         USB_Device_ConfigurationNumber  = 0;\r
136 \r
137         #if !defined(NO_DEVICE_REMOTE_WAKEUP)\r
138         USB_Device_RemoteWakeupEnabled  = false;\r
139         #endif\r
140 \r
141         #if !defined(NO_DEVICE_SELF_POWER)\r
142         USB_Device_CurrentlySelfPowered = false;\r
143         #endif\r
144 \r
145         #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)\r
146         USB_Descriptor_Device_t* DeviceDescriptorPtr;\r
147 \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
151 \r
152         if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr, &DescriptorAddressSpace) != NO_DESCRIPTOR)\r
153         {\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
158                 else\r
159                   USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;\r
160         }\r
161         #else\r
162         if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)\r
163         {\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
168                 #else\r
169                 USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);\r
170                 #endif\r
171         }\r
172         #endif\r
173         #endif\r
174 \r
175         if (USB_Options & USB_DEVICE_OPT_LOWSPEED)\r
176           USB_Device_SetLowSpeed();\r
177         else\r
178           USB_Device_SetFullSpeed();\r
179 \r
180         Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,\r
181                                                            USB_Device_ControlEndpointSize, 1);\r
182 \r
183         USB_INT_Enable(USB_INT_BUSEVENTI);\r
184 \r
185         USB_Attach();\r
186 }\r
187 #endif\r
188 \r
189 #endif\r