Add architecture guards to all architecture-specific files, so that they can be bulk...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / Pipe_AVR8.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 #include "../../../../Common/Common.h"
32 #if (ARCH == ARCH_AVR8)
33
34 #define __INCLUDE_FROM_USB_DRIVER
35 #include "../USBMode.h"
36
37 #if defined(USB_CAN_BE_HOST)
38
39 #include "../Pipe.h"
40
41 uint8_t USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
42
43 bool Pipe_ConfigurePipe(const uint8_t Number,
44 const uint8_t Type,
45 const uint8_t Token,
46 const uint8_t EndpointNumber,
47 const uint16_t Size,
48 const uint8_t Banks)
49 {
50 #if defined(ORDERED_EP_CONFIG)
51 Pipe_SelectPipe(Number);
52 Pipe_EnablePipe();
53
54 UPCFG1X = 0;
55
56 UPCFG0X = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
57 UPCFG1X = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
58
59 Pipe_SetInfiniteINRequests();
60
61 return Pipe_IsConfigured();
62 #else
63 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
64 {
65 uint8_t UPCFG0XTemp;
66 uint8_t UPCFG1XTemp;
67 uint8_t UPCFG2XTemp;
68 uint8_t UPIENXTemp;
69
70 Pipe_SelectPipe(PNum);
71
72 if (PNum == Number)
73 {
74 UPCFG0XTemp = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
75 UPCFG1XTemp = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
76 UPCFG2XTemp = 0;
77 UPIENXTemp = 0;
78 }
79 else
80 {
81 UPCFG0XTemp = UPCFG0X;
82 UPCFG1XTemp = UPCFG1X;
83 UPCFG2XTemp = UPCFG2X;
84 UPIENXTemp = UPIENX;
85 }
86
87 if (!(UPCFG1XTemp & (1 << ALLOC)))
88 continue;
89
90 Pipe_DisablePipe();
91 UPCFG1X &= ~(1 << ALLOC);
92
93 Pipe_EnablePipe();
94 UPCFG0X = UPCFG0XTemp;
95 UPCFG1X = UPCFG1XTemp;
96 UPCFG2X = UPCFG2XTemp;
97 UPIENX = UPIENXTemp;
98
99 Pipe_SetInfiniteINRequests();
100
101 if (!(Pipe_IsConfigured()))
102 return false;
103 }
104
105 Pipe_SelectPipe(Number);
106 return true;
107 #endif
108 }
109
110 void Pipe_ClearPipes(void)
111 {
112 UPINT = 0;
113
114 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
115 {
116 Pipe_SelectPipe(PNum);
117 UPIENX = 0;
118 UPINTX = 0;
119 UPCFG1X = 0;
120 Pipe_DisablePipe();
121 }
122 }
123
124 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
125 {
126 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
127
128 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
129 {
130 Pipe_SelectPipe(PNum);
131
132 if (!(Pipe_IsConfigured()))
133 continue;
134
135 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
136 return true;
137 }
138
139 Pipe_SelectPipe(PrevPipeNumber);
140 return false;
141 }
142
143 uint8_t Pipe_WaitUntilReady(void)
144 {
145 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
146 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
147 #else
148 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
149 #endif
150
151 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
152
153 for (;;)
154 {
155 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
156 {
157 if (Pipe_IsINReceived())
158 return PIPE_READYWAIT_NoError;
159 }
160 else
161 {
162 if (Pipe_IsOUTReady())
163 return PIPE_READYWAIT_NoError;
164 }
165
166 if (Pipe_IsStalled())
167 return PIPE_READYWAIT_PipeStalled;
168 else if (USB_HostState == HOST_STATE_Unattached)
169 return PIPE_READYWAIT_DeviceDisconnected;
170
171 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
172
173 if (CurrentFrameNumber != PreviousFrameNumber)
174 {
175 PreviousFrameNumber = CurrentFrameNumber;
176
177 if (!(TimeoutMSRem--))
178 return PIPE_READYWAIT_Timeout;
179 }
180 }
181 }
182
183 #endif
184
185 #endif