79c21f24ed6356466e77a6929617959cdb555791
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
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_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 uint8_t UPCFG0XTemp[PIPE_TOTAL_PIPES];
48 uint8_t UPCFG1XTemp[PIPE_TOTAL_PIPES];
49 uint8_t UPCFG2XTemp[PIPE_TOTAL_PIPES];
50 uint8_t UPCONXTemp[PIPE_TOTAL_PIPES];
51 uint8_t UPINRQXTemp[PIPE_TOTAL_PIPES];
52
53 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
54 {
55 Pipe_SelectPipe(PNum);
56 UPCFG0XTemp[PNum] = UPCFG0X;
57 UPCFG1XTemp[PNum] = UPCFG1X;
58 UPCFG2XTemp[PNum] = UPCFG2X;
59 UPCONXTemp[PNum] = UPCONX;
60 UPINRQXTemp[PNum] = UPINRQX;
61 }
62
63 UPCFG0XTemp[Number] = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
64 UPCFG1XTemp[Number] = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
65 UPCFG2XTemp[Number] = 0;
66 UPCONXTemp[Number] = (1 << INMODE);
67 UPINRQXTemp[Number] = 0;
68
69 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
70 {
71 Pipe_SelectPipe(PNum);
72 UPIENX = 0;
73 UPINTX = 0;
74 UPCFG1X = 0;
75 Pipe_DisablePipe();
76 }
77
78 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
79 {
80 if (!(UPCFG1XTemp[PNum] & (1 << ALLOC)))
81 continue;
82
83 Pipe_SelectPipe(PNum);
84 Pipe_EnablePipe();
85
86 UPCFG0X = UPCFG0XTemp[PNum];
87 UPCFG1X = UPCFG1XTemp[PNum];
88 UPCFG2X = UPCFG2XTemp[PNum];
89 UPCONX |= UPCONXTemp[PNum];
90 UPINRQX = UPINRQXTemp[PNum];
91
92 if (!(Pipe_IsConfigured()))
93 return false;
94 }
95
96 Pipe_SelectPipe(Number);
97 return true;
98 }
99
100 void Pipe_ClearPipes(void)
101 {
102 UPINT = 0;
103
104 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
105 {
106 Pipe_SelectPipe(PNum);
107 UPIENX = 0;
108 UPINTX = 0;
109 UPCFG1X = 0;
110 Pipe_DisablePipe();
111 }
112 }
113
114 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
115 {
116 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
117
118 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
119 {
120 Pipe_SelectPipe(PNum);
121
122 if (!(Pipe_IsConfigured()))
123 continue;
124
125 uint8_t PipeToken = Pipe_GetPipeToken();
126 bool PipeTokenCorrect = true;
127
128 if (PipeToken != PIPE_TOKEN_SETUP)
129 PipeTokenCorrect = (PipeToken == ((EndpointAddress & PIPE_EPDIR_MASK) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT));
130
131 if (PipeTokenCorrect && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
132 return true;
133 }
134
135 Pipe_SelectPipe(PrevPipeNumber);
136 return false;
137 }
138
139 uint8_t Pipe_WaitUntilReady(void)
140 {
141 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
142 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
143 #else
144 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
145 #endif
146
147 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
148
149 for (;;)
150 {
151 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
152 {
153 if (Pipe_IsINReceived())
154 return PIPE_READYWAIT_NoError;
155 }
156 else
157 {
158 if (Pipe_IsOUTReady())
159 return PIPE_READYWAIT_NoError;
160 }
161
162 if (Pipe_IsStalled())
163 return PIPE_READYWAIT_PipeStalled;
164 else if (USB_HostState == HOST_STATE_Unattached)
165 return PIPE_READYWAIT_DeviceDisconnected;
166
167 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
168
169 if (CurrentFrameNumber != PreviousFrameNumber)
170 {
171 PreviousFrameNumber = CurrentFrameNumber;
172
173 if (!(TimeoutMSRem--))
174 return PIPE_READYWAIT_Timeout;
175 }
176 }
177 }
178
179 #endif