Conditionally add available address spaces to the address space enum, for multiple...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / Pipe_AVR8.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_HOST)
35
36 #include "../Pipe.h"
37
38 uint8_t USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
39
40 bool Pipe_ConfigurePipe(const uint8_t Number,
41 const uint8_t Type,
42 const uint8_t Token,
43 const uint8_t EndpointNumber,
44 const uint16_t Size,
45 const uint8_t Banks)
46 {
47 #if defined(ORDERED_EP_CONFIG)
48 Pipe_SelectPipe(Number);
49 Pipe_EnablePipe();
50
51 UPCFG1X = 0;
52
53 UPCFG0X = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
54 UPCFG1X = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
55
56 Pipe_SetInfiniteINRequests();
57
58 return Pipe_IsConfigured();
59 #else
60 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
61 {
62 uint8_t UPCFG0XTemp;
63 uint8_t UPCFG1XTemp;
64 uint8_t UPCFG2XTemp;
65 uint8_t UPCONXTemp;
66 uint8_t UPINRQXTemp;
67 uint8_t UPIENXTemp;
68
69 Pipe_SelectPipe(PNum);
70
71 if (PNum == Number)
72 {
73 UPCFG0XTemp = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
74 UPCFG1XTemp = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
75 UPCFG2XTemp = 0;
76 UPCONXTemp = ((1 << PEN) | (1 << INMODE));
77 UPINRQXTemp = 0;
78 UPIENXTemp = 0;
79 }
80 else
81 {
82 UPCFG0XTemp = UPCFG0X;
83 UPCFG1XTemp = UPCFG1X;
84 UPCFG2XTemp = UPCFG2X;
85 UPCONXTemp = UPCONX;
86 UPINRQXTemp = UPINRQX;
87 UPIENXTemp = UPIENX;
88 }
89
90 Pipe_SetInfiniteINRequests();
91
92 if (!(UPCFG1XTemp & (1 << ALLOC)))
93 continue;
94
95 Pipe_DisablePipe();
96 UPCFG1X &= (1 << ALLOC);
97
98 Pipe_EnablePipe();
99 UPCFG0X = UPCFG0XTemp;
100 UPCFG1X = UPCFG1XTemp;
101 UPCFG2X = UPCFG2XTemp;
102 UPCONX = UPCONXTemp;
103 UPINRQX = UPINRQXTemp;
104 UPIENX = UPIENXTemp;
105
106 if (!(Pipe_IsConfigured()))
107 return false;
108 }
109
110 Pipe_SelectPipe(Number);
111 return true;
112 #endif
113 }
114
115 void Pipe_ClearPipes(void)
116 {
117 UPINT = 0;
118
119 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
120 {
121 Pipe_SelectPipe(PNum);
122 UPIENX = 0;
123 UPINTX = 0;
124 UPCFG1X = 0;
125 Pipe_DisablePipe();
126 }
127 }
128
129 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
130 {
131 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
132
133 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
134 {
135 Pipe_SelectPipe(PNum);
136
137 if (!(Pipe_IsConfigured()))
138 continue;
139
140 uint8_t PipeToken = Pipe_GetPipeToken();
141 bool PipeTokenCorrect = true;
142
143 if (PipeToken != PIPE_TOKEN_SETUP)
144 PipeTokenCorrect = (PipeToken == ((EndpointAddress & PIPE_EPDIR_MASK) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT));
145
146 if (PipeTokenCorrect && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
147 return true;
148 }
149
150 Pipe_SelectPipe(PrevPipeNumber);
151 return false;
152 }
153
154 uint8_t Pipe_WaitUntilReady(void)
155 {
156 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
157 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
158 #else
159 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
160 #endif
161
162 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
163
164 for (;;)
165 {
166 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
167 {
168 if (Pipe_IsINReceived())
169 return PIPE_READYWAIT_NoError;
170 }
171 else
172 {
173 if (Pipe_IsOUTReady())
174 return PIPE_READYWAIT_NoError;
175 }
176
177 if (Pipe_IsStalled())
178 return PIPE_READYWAIT_PipeStalled;
179 else if (USB_HostState == HOST_STATE_Unattached)
180 return PIPE_READYWAIT_DeviceDisconnected;
181
182 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
183
184 if (CurrentFrameNumber != PreviousFrameNumber)
185 {
186 PreviousFrameNumber = CurrentFrameNumber;
187
188 if (!(TimeoutMSRem--))
189 return PIPE_READYWAIT_Timeout;
190 }
191 }
192 }
193
194 #endif
195