XMEGA port improvements; connection and disconnection are now detected correctly...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / XMEGA / Endpoint_XMEGA.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 disclaim 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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #include "../Endpoint.h"
37
38 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
39 uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
40 #endif
41
42 Endpoint_AuxData_t Endpoint_AuxData[ENDPOINT_DETAILS_MAXEP * 2];
43
44 volatile uint8_t Endpoint_SelectedEndpoint;
45 volatile USB_EP_t* Endpoint_SelectedEndpointHandle;
46 volatile Endpoint_AuxData_t* Endpoint_SelectedEndpointAux;
47
48 bool Endpoint_ConfigureEndpoint_PRV(const uint8_t Number,
49 const uint8_t Direction,
50 const uint8_t Config,
51 const uint8_t Size)
52 {
53 Endpoint_SelectEndpoint(Number | Direction);
54
55 Endpoint_SelectedEndpointHandle->CTRL = 0;
56 Endpoint_SelectedEndpointHandle->STATUS = (Direction == ENDPOINT_DIR_IN) ? USB_EP_BUSNACK0_bm : 0;
57 Endpoint_SelectedEndpointHandle->CTRL = Config;
58 Endpoint_SelectedEndpointHandle->CNT = 0;
59 Endpoint_SelectedEndpointHandle->DATAPTR = (intptr_t)&Endpoint_SelectedEndpointAux->Data;
60
61 Endpoint_SelectedEndpointAux->BankSize = Size;
62 Endpoint_SelectedEndpointAux->FIFOLength = (Direction == ENDPOINT_DIR_IN) ? Size : 0;
63 Endpoint_SelectedEndpointAux->FIFOPosition = 0;
64
65 return true;
66 }
67
68 void Endpoint_ClearEndpoints(void)
69 {
70 for (uint8_t EPNum = 0; EPNum < (ENDPOINT_TOTAL_ENDPOINTS * 2); EPNum++)
71 ((USB_EP_t*)&USB_EndpointTable)[EPNum].CTRL = 0;
72 }
73
74 void Endpoint_ClearStatusStage(void)
75 {
76 while (!(Endpoint_IsOUTReceived()))
77 {
78 if (USB_DeviceState == DEVICE_STATE_Unattached)
79 return;
80 }
81
82 Endpoint_ClearOUT();
83 }
84
85 #if !defined(CONTROL_ONLY_DEVICE)
86 uint8_t Endpoint_WaitUntilReady(void)
87 {
88 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
89 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
90 #else
91 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
92 #endif
93
94 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
95
96 for (;;)
97 {
98 if (Endpoint_IsOUTReceived())
99 return ENDPOINT_READYWAIT_NoError;
100
101 uint8_t USB_DeviceState_LCL = USB_DeviceState;
102
103 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
104 return ENDPOINT_READYWAIT_DeviceDisconnected;
105 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
106 return ENDPOINT_READYWAIT_BusSuspended;
107 else if (Endpoint_IsStalled())
108 return ENDPOINT_READYWAIT_EndpointStalled;
109
110 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
111
112 if (CurrentFrameNumber != PreviousFrameNumber)
113 {
114 PreviousFrameNumber = CurrentFrameNumber;
115
116 if (!(TimeoutMSRem--))
117 return ENDPOINT_READYWAIT_Timeout;
118 }
119 }
120 }
121 #endif
122
123 #endif
124