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