Fix XMEGA USB prescaler calculation
[pub/USBasp.git] / LUFA / Drivers / USB / Core / XMEGA / USBController_XMEGA.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #include "../../../../Common/Common.h"
32 #if (ARCH == ARCH_XMEGA)
33
34 #define __INCLUDE_FROM_USB_DRIVER
35 #define __INCLUDE_FROM_USB_CONTROLLER_C
36 #include "../USBController.h"
37
38 #if defined(USB_CAN_BE_BOTH)
39 volatile uint8_t USB_CurrentMode = USB_MODE_None;
40 #endif
41
42 #if !defined(USE_STATIC_OPTIONS)
43 volatile uint8_t USB_Options;
44 #endif
45
46 /* Ugly workaround to ensure an aligned table, since __BIGGEST_ALIGNMENT__ == 1 for the 8-bit AVR-GCC toolchain */
47 uint8_t USB_EndpointTable[sizeof(USB_EndpointTable_t) + 1];
48
49 void USB_Init(
50 #if defined(USB_CAN_BE_BOTH)
51 const uint8_t Mode
52 #endif
53
54 #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
55 ,
56 #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
57 void
58 #endif
59
60 #if !defined(USE_STATIC_OPTIONS)
61 const uint8_t Options
62 #endif
63 )
64 {
65 #if !defined(USE_STATIC_OPTIONS)
66 USB_Options = Options;
67 #endif
68
69 uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
70 GlobalInterruptDisable();
71
72 NVM.CMD = NVM_CMD_READ_CALIB_ROW_gc;
73 USB.CAL0 = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBCAL0));
74 USB.CAL1 = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBCAL1));
75 NVM.CMD = NVM_CMD_NO_OPERATION_gc;
76
77 /* Ugly workaround to ensure an aligned table, since __BIGGEST_ALIGNMENT__ == 1 for the 8-bit AVR-GCC toolchain */
78 USB.EPPTR = ((intptr_t)&USB_EndpointTable[1] & ~(1 << 0));
79 USB.CTRLA = (USB_STFRNUM_bm | ((ENDPOINT_TOTAL_ENDPOINTS - 1) << USB_MAXEP_gp));
80
81 if ((USB_Options & USB_OPT_BUSEVENT_PRIHIGH) == USB_OPT_BUSEVENT_PRIHIGH)
82 USB.INTCTRLA = (3 << USB_INTLVL_gp);
83 else if ((USB_Options & USB_OPT_BUSEVENT_PRIMED) == USB_OPT_BUSEVENT_PRIMED)
84 USB.INTCTRLA = (2 << USB_INTLVL_gp);
85 else
86 USB.INTCTRLA = (1 << USB_INTLVL_gp);
87
88 SetGlobalInterruptMask(CurrentGlobalInt);
89
90 #if defined(USB_CAN_BE_BOTH)
91 USB_CurrentMode = Mode;
92 #endif
93
94 USB_IsInitialized = true;
95
96 USB_ResetInterface();
97 }
98
99 void USB_Disable(void)
100 {
101 USB_INT_DisableAllInterrupts();
102 USB_INT_ClearAllInterrupts();
103
104 USB_Detach();
105 USB_Controller_Disable();
106
107 USB_IsInitialized = false;
108 }
109
110 void USB_ResetInterface(void)
111 {
112 uint8_t PrescalerNeeded;
113 uint8_t nbit = 0;
114
115 #if defined(USB_DEVICE_OPT_FULLSPEED)
116 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
117 PrescalerNeeded = F_USB / 6000000;
118 else
119 PrescalerNeeded = F_USB / 48000000;
120 #else
121 PrescalerNeeded = F_USB / 6000000;
122 #endif
123
124 while (PrescalerNeeded && nbit < 7) {
125 PrescalerNeeded >>= 1;
126 nbit++;
127 }
128
129 CLK.USBCTRL = (nbit - 1) << CLK_USBPSDIV_gp;
130
131 if (USB_Options & USB_OPT_PLLCLKSRC)
132 CLK.USBCTRL |= (CLK_USBSRC_PLL_gc | CLK_USBSEN_bm);
133 else
134 CLK.USBCTRL |= (CLK_USBSRC_RC32M_gc | CLK_USBSEN_bm);
135
136 USB_Device_SetDeviceAddress(0);
137
138 USB_INT_DisableAllInterrupts();
139 USB_INT_ClearAllInterrupts();
140
141 USB_Controller_Reset();
142 USB_Init_Device();
143 }
144
145 #if defined(USB_CAN_BE_DEVICE)
146 static void USB_Init_Device(void)
147 {
148 USB_DeviceState = DEVICE_STATE_Unattached;
149 USB_Device_ConfigurationNumber = 0;
150
151 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
152 USB_Device_RemoteWakeupEnabled = false;
153 #endif
154
155 #if !defined(NO_DEVICE_SELF_POWER)
156 USB_Device_CurrentlySelfPowered = false;
157 #endif
158
159 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
160 USB_Descriptor_Device_t* DeviceDescriptorPtr;
161
162 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
163 !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
164 uint8_t DescriptorAddressSpace;
165
166 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr, &DescriptorAddressSpace) != NO_DESCRIPTOR)
167 {
168 if (DescriptorAddressSpace == MEMSPACE_FLASH)
169 USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
170 else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
171 USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
172 else
173 USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
174 }
175 #else
176 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
177 {
178 #if defined(USE_RAM_DESCRIPTORS)
179 USB_Device_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
180 #elif defined(USE_EEPROM_DESCRIPTORS)
181 USB_Device_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
182 #else
183 USB_Device_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
184 #endif
185 }
186 #endif
187 #endif
188
189 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
190 USB_Device_SetLowSpeed();
191 else
192 USB_Device_SetFullSpeed();
193
194 Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
195 USB_Device_ControlEndpointSize, 1);
196
197 USB_INT_Enable(USB_INT_BUSEVENTI);
198
199 USB_Attach();
200 }
201 #endif
202
203 #endif