Fix errors uncovered by the new build test compile warnings; fix UC3 pipe configurati...
[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 #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_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
39
40 volatile uint32_t USB_Pipe_SelectedPipe = PIPE_CONTROLPIPE;
41 volatile uint8_t* USB_Pipe_FIFOPos[PIPE_TOTAL_PIPES];
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 USB_Pipe_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * 0x10000];
51
52 #if defined(ORDERED_EP_CONFIG)
53 Pipe_SelectPipe(Number);
54 Pipe_EnablePipe();
55
56 (&AVR32_USBB.upcfg0)[Number] = 0;
57 (&AVR32_USBB.upcfg0)[Number] = (AVR32_USBB_ALLOC_MASK |
58 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
59 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
60 ((uint32_t)Banks << AVR32_USBB_PBK_OFFSET) |
61 Pipe_BytesToEPSizeMask(Size) |
62 ((EndpointNumber & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
63
64 Pipe_SetInfiniteINRequests();
65
66 return Pipe_IsConfigured();
67 #else
68 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
69 {
70 uint32_t UPCFG0Temp;
71
72 Pipe_SelectPipe(PNum);
73
74 if (PNum == Number)
75 {
76 UPCFG0Temp = (AVR32_USBB_ALLOC_MASK |
77 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
78 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
79 ((uint32_t)Banks << AVR32_USBB_PBK_OFFSET) |
80 Pipe_BytesToEPSizeMask(Size) |
81 ((EndpointNumber & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
82 }
83 else
84 {
85 UPCFG0Temp = (&AVR32_USBB.upcfg0)[PNum];
86 }
87
88 if (!(UPCFG0Temp & AVR32_USBB_ALLOC_MASK))
89 continue;
90
91 Pipe_DisablePipe();
92 (&AVR32_USBB.upcfg0)[PNum] &= ~AVR32_USBB_ALLOC_MASK;
93
94 Pipe_EnablePipe();
95 (&AVR32_USBB.upcfg0)[PNum] = UPCFG0Temp;
96
97 Pipe_SetInfiniteINRequests();
98
99 if (!(Pipe_IsConfigured()))
100 return false;
101 }
102
103 Pipe_SelectPipe(Number);
104 return true;
105 #endif
106 }
107
108 void Pipe_ClearPipes(void)
109 {
110 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
111 {
112 Pipe_SelectPipe(PNum);
113 (&AVR32_USBB.upcfg0)[PNum] = 0;
114 (&AVR32_USBB.upcon0clr)[PNum] = -1;
115 USB_Pipe_FIFOPos[PNum] = &AVR32_USBB_SLAVE[PNum * 0x10000];
116 Pipe_DisablePipe();
117 }
118 }
119
120 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
121 {
122 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
123
124 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
125 {
126 Pipe_SelectPipe(PNum);
127
128 if (!(Pipe_IsConfigured()))
129 continue;
130
131 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
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
180