Removed Pipe_ClearErrorFlags(), pipe error flags are now automatically cleared when...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3B / Endpoint_UC3B.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_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
40 #endif
41
42 uint8_t USB_SelectedEndpoint = ENDPOINT_CONTROLEP;
43
44
45 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
46 const uint32_t UECFGXData)
47 {
48 Endpoint_SelectEndpoint(Number);
49 Endpoint_EnableEndpoint();
50
51 *((uint32_t*)AVR32_USBB_UECFG0)[USB_SelectedEndpoint] = 0;
52 *((uint32_t*)AVR32_USBB_UECFG0)[USB_SelectedEndpoint] = UECFGXData;
53
54 return Endpoint_IsConfigured();
55 }
56
57 void Endpoint_ClearEndpoints(void)
58 {
59 UEINT = 0;
60
61 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
62 {
63 Endpoint_SelectEndpoint(EPNum);
64 *((uint32_t*)AVR32_USBB_UECFG0)[USB_SelectedEndpoint] = 0;
65 Endpoint_DisableEndpoint();
66 }
67 }
68
69 void Endpoint_ClearStatusStage(void)
70 {
71 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
72 {
73 while (!(Endpoint_IsOUTReceived()))
74 {
75 if (USB_DeviceState == DEVICE_STATE_Unattached)
76 return;
77 }
78
79 Endpoint_ClearOUT();
80 }
81 else
82 {
83 while (!(Endpoint_IsINReady()))
84 {
85 if (USB_DeviceState == DEVICE_STATE_Unattached)
86 return;
87 }
88
89 Endpoint_ClearIN();
90 }
91 }
92
93 #if !defined(CONTROL_ONLY_DEVICE)
94 uint8_t Endpoint_WaitUntilReady(void)
95 {
96 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
97 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
98 #else
99 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
100 #endif
101
102 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
103
104 for (;;)
105 {
106 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
107 {
108 if (Endpoint_IsINReady())
109 return ENDPOINT_READYWAIT_NoError;
110 }
111 else
112 {
113 if (Endpoint_IsOUTReceived())
114 return ENDPOINT_READYWAIT_NoError;
115 }
116
117 uint8_t USB_DeviceState_LCL = USB_DeviceState;
118
119 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
120 return ENDPOINT_READYWAIT_DeviceDisconnected;
121 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
122 return ENDPOINT_READYWAIT_BusSuspended;
123 else if (Endpoint_IsStalled())
124 return ENDPOINT_READYWAIT_EndpointStalled;
125
126 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
127
128 if (CurrentFrameNumber != PreviousFrameNumber)
129 {
130 PreviousFrameNumber = CurrentFrameNumber;
131
132 if (!(TimeoutMSRem--))
133 return ENDPOINT_READYWAIT_Timeout;
134 }
135 }
136 }
137 #endif
138
139 #endif
140