Add architecture guards to all architecture-specific files, so that they can be bulk...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3 / Pipe_UC3.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_UC3)
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 volatile uint32_t USB_Pipe_SelectedPipe = PIPE_CONTROLPIPE;
44 volatile uint8_t* USB_Pipe_FIFOPos[PIPE_TOTAL_PIPES];
45
46 bool Pipe_ConfigurePipe(const uint8_t Number,
47 const uint8_t Type,
48 const uint8_t Token,
49 const uint8_t EndpointNumber,
50 const uint16_t Size,
51 const uint8_t Banks)
52 {
53 USB_Pipe_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * 0x10000];
54
55 #if defined(ORDERED_EP_CONFIG)
56 Pipe_SelectPipe(Number);
57 Pipe_EnablePipe();
58
59 (&AVR32_USBB.upcfg0)[Number] = 0;
60 (&AVR32_USBB.upcfg0)[Number] = (AVR32_USBB_ALLOC_MASK |
61 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
62 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
63 ((uint32_t)Banks << AVR32_USBB_PBK_OFFSET) |
64 Pipe_BytesToEPSizeMask(Size) |
65 ((EndpointNumber & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
66
67 Pipe_SetInfiniteINRequests();
68
69 return Pipe_IsConfigured();
70 #else
71 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
72 {
73 uint32_t UPCFG0Temp;
74
75 Pipe_SelectPipe(PNum);
76
77 if (PNum == Number)
78 {
79 UPCFG0Temp = (AVR32_USBB_ALLOC_MASK |
80 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
81 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
82 ((uint32_t)Banks << AVR32_USBB_PBK_OFFSET) |
83 Pipe_BytesToEPSizeMask(Size) |
84 ((EndpointNumber & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
85 }
86 else
87 {
88 UPCFG0Temp = (&AVR32_USBB.upcfg0)[PNum];
89 }
90
91 if (!(UPCFG0Temp & AVR32_USBB_ALLOC_MASK))
92 continue;
93
94 Pipe_DisablePipe();
95 (&AVR32_USBB.upcfg0)[PNum] &= ~AVR32_USBB_ALLOC_MASK;
96
97 Pipe_EnablePipe();
98 (&AVR32_USBB.upcfg0)[PNum] = UPCFG0Temp;
99
100 Pipe_SetInfiniteINRequests();
101
102 if (!(Pipe_IsConfigured()))
103 return false;
104 }
105
106 Pipe_SelectPipe(Number);
107 return true;
108 #endif
109 }
110
111 void Pipe_ClearPipes(void)
112 {
113 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
114 {
115 Pipe_SelectPipe(PNum);
116 (&AVR32_USBB.upcfg0)[PNum] = 0;
117 (&AVR32_USBB.upcon0clr)[PNum] = -1;
118 USB_Pipe_FIFOPos[PNum] = &AVR32_USBB_SLAVE[PNum * 0x10000];
119 Pipe_DisablePipe();
120 }
121 }
122
123 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
124 {
125 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
126
127 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
128 {
129 Pipe_SelectPipe(PNum);
130
131 if (!(Pipe_IsConfigured()))
132 continue;
133
134 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
135 return true;
136 }
137
138 Pipe_SelectPipe(PrevPipeNumber);
139 return false;
140 }
141
142 uint8_t Pipe_WaitUntilReady(void)
143 {
144 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
145 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
146 #else
147 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
148 #endif
149
150 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
151
152 for (;;)
153 {
154 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
155 {
156 if (Pipe_IsINReceived())
157 return PIPE_READYWAIT_NoError;
158 }
159 else
160 {
161 if (Pipe_IsOUTReady())
162 return PIPE_READYWAIT_NoError;
163 }
164
165 if (Pipe_IsStalled())
166 return PIPE_READYWAIT_PipeStalled;
167 else if (USB_HostState == HOST_STATE_Unattached)
168 return PIPE_READYWAIT_DeviceDisconnected;
169
170 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
171
172 if (CurrentFrameNumber != PreviousFrameNumber)
173 {
174 PreviousFrameNumber = CurrentFrameNumber;
175
176 if (!(TimeoutMSRem--))
177 return PIPE_READYWAIT_Timeout;
178 }
179 }
180 }
181
182 #endif
183
184 #endif