Add LUFA-111009-BETA tag.
[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 volatile uint8_t Endpoint_SelectedEndpoint;
43 volatile USB_EP_t* Endpoint_SelectedEndpointHandle;
44
45 void Endpoint_ClearEndpoints(void)
46 {
47 for (uint8_t EPNum = 0; EPNum < (ENDPOINT_TOTAL_ENDPOINTS * 2); EPNum++)
48 ((USB_EP_t*)&USB_EndpointTable)[EPNum].CTRL = 0;
49 }
50
51 void Endpoint_ClearStatusStage(void)
52 {
53 while (!(Endpoint_IsOUTReceived()))
54 {
55 if (USB_DeviceState == DEVICE_STATE_Unattached)
56 return;
57 }
58
59 Endpoint_ClearOUT();
60 }
61
62 #if !defined(CONTROL_ONLY_DEVICE)
63 uint8_t Endpoint_WaitUntilReady(void)
64 {
65 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
66 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
67 #else
68 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
69 #endif
70
71 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
72
73 for (;;)
74 {
75 if (Endpoint_IsOUTReceived())
76 return ENDPOINT_READYWAIT_NoError;
77
78 uint8_t USB_DeviceState_LCL = USB_DeviceState;
79
80 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
81 return ENDPOINT_READYWAIT_DeviceDisconnected;
82 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
83 return ENDPOINT_READYWAIT_BusSuspended;
84 else if (Endpoint_IsStalled())
85 return ENDPOINT_READYWAIT_EndpointStalled;
86
87 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
88
89 if (CurrentFrameNumber != PreviousFrameNumber)
90 {
91 PreviousFrameNumber = CurrentFrameNumber;
92
93 if (!(TimeoutMSRem--))
94 return ENDPOINT_READYWAIT_Timeout;
95 }
96 }
97 }
98 #endif
99
100 #endif
101