Added new USB_Device_GetFrameNumber() and USB_Host_GetFrameNumber() functions to...
[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
50 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
51 {
52 Pipe_SelectPipe(PNum);
53 UPCFG0XTemp[PNum] = UPCFG0X;
54 UPCFG1XTemp[PNum] = UPCFG1X;
55 }
56
57 UPCFG0XTemp[Number] = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
58 UPCFG1XTemp[Number] = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
59
60 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
61 {
62 Pipe_SelectPipe(PNum);
63 UPIENX = 0;
64 UPINTX = 0;
65 UPCFG1X = 0;
66 Pipe_DisablePipe();
67 }
68
69 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
70 {
71 if (!(UPCFG1XTemp[PNum] & (1 << ALLOC)))
72 continue;
73
74 Pipe_SelectPipe(PNum);
75 Pipe_EnablePipe();
76
77 UPCFG0X = UPCFG0XTemp[PNum];
78 UPCFG1X = UPCFG1XTemp[PNum];
79
80 if (!(Pipe_IsConfigured()))
81 return false;
82 }
83
84 Pipe_SelectPipe(Number);
85 Pipe_SetInfiniteINRequests();
86
87 return true;
88 }
89
90 void Pipe_ClearPipes(void)
91 {
92 UPINT = 0;
93
94 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
95 {
96 Pipe_SelectPipe(PNum);
97 UPIENX = 0;
98 UPINTX = 0;
99 UPCFG1X = 0;
100 Pipe_DisablePipe();
101 }
102 }
103
104 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
105 {
106 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
107
108 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
109 {
110 Pipe_SelectPipe(PNum);
111
112 if (!(Pipe_IsConfigured()))
113 continue;
114
115 uint8_t PipeToken = Pipe_GetPipeToken();
116 bool PipeTokenCorrect = true;
117
118 if (PipeToken != PIPE_TOKEN_SETUP)
119 PipeTokenCorrect = (PipeToken == ((EndpointAddress & PIPE_EPDIR_MASK) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT));
120
121 if (PipeTokenCorrect && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
122 return true;
123 }
124
125 Pipe_SelectPipe(PrevPipeNumber);
126 return false;
127 }
128
129 uint8_t Pipe_WaitUntilReady(void)
130 {
131 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
132 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
133 #else
134 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
135 #endif
136
137 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
138
139 for (;;)
140 {
141 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
142 {
143 if (Pipe_IsINReceived())
144 return PIPE_READYWAIT_NoError;
145 }
146 else
147 {
148 if (Pipe_IsOUTReady())
149 return PIPE_READYWAIT_NoError;
150 }
151
152 if (Pipe_IsStalled())
153 return PIPE_READYWAIT_PipeStalled;
154 else if (USB_HostState == HOST_STATE_Unattached)
155 return PIPE_READYWAIT_DeviceDisconnected;
156
157 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
158
159 if (CurrentFrameNumber != PreviousFrameNumber)
160 {
161 PreviousFrameNumber = CurrentFrameNumber;
162
163 if (!(TimeoutMSRem--))
164 return PIPE_READYWAIT_Timeout;
165 }
166 }
167 }
168
169 #endif