Changed over www.fourwalledcubicle.com links to the new www.lufa-lib.org redirect...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 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 "../HighLevel/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 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
43 const uint8_t UECFG0XData,
44 const uint8_t UECFG1XData)
45 {
46 Endpoint_SelectEndpoint(Number);
47 Endpoint_EnableEndpoint();
48
49 UECFG1X = 0;
50 UECFG0X = UECFG0XData;
51 UECFG1X = UECFG1XData;
52
53 return Endpoint_IsConfigured();
54 }
55
56 void Endpoint_ClearEndpoints(void)
57 {
58 UEINT = 0;
59
60 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
61 {
62 Endpoint_SelectEndpoint(EPNum);
63 UEIENX = 0;
64 UEINTX = 0;
65 UECFG1X = 0;
66 Endpoint_DisableEndpoint();
67 }
68 }
69
70 void Endpoint_ClearStatusStage(void)
71 {
72 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
73 {
74 while (!(Endpoint_IsOUTReceived()))
75 {
76 if (USB_DeviceState == DEVICE_STATE_Unattached)
77 return;
78 }
79
80 Endpoint_ClearOUT();
81 }
82 else
83 {
84 while (!(Endpoint_IsINReady()))
85 {
86 if (USB_DeviceState == DEVICE_STATE_Unattached)
87 return;
88 }
89
90 Endpoint_ClearIN();
91 }
92 }
93
94 #if !defined(CONTROL_ONLY_DEVICE)
95 uint8_t Endpoint_WaitUntilReady(void)
96 {
97 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
98 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
99 #else
100 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
101 #endif
102
103 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
104
105 for (;;)
106 {
107 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
108 {
109 if (Endpoint_IsINReady())
110 return ENDPOINT_READYWAIT_NoError;
111 }
112 else
113 {
114 if (Endpoint_IsOUTReceived())
115 return ENDPOINT_READYWAIT_NoError;
116 }
117
118 uint8_t USB_DeviceState_LCL = USB_DeviceState;
119
120 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
121 return ENDPOINT_READYWAIT_DeviceDisconnected;
122 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
123 return ENDPOINT_READYWAIT_BusSuspended;
124 else if (Endpoint_IsStalled())
125 return ENDPOINT_READYWAIT_EndpointStalled;
126
127 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
128
129 if (CurrentFrameNumber != PreviousFrameNumber)
130 {
131 PreviousFrameNumber = CurrentFrameNumber;
132
133 if (!(TimeoutMSRem--))
134 return ENDPOINT_READYWAIT_Timeout;
135 }
136 }
137 }
138 #endif
139
140 #endif
141