8a054da61861f2e33d6cb73ee1a34e861d7e9f54
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3B / Pipe_UC3B.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
39 uint8_t USB_SelectedPipe = PIPE_CONTROLPIPE;
40
41 bool Pipe_ConfigurePipe(const uint8_t Number,
42 const uint8_t Type,
43 const uint8_t Token,
44 const uint8_t EndpointNumber,
45 const uint16_t Size,
46 const uint8_t Banks)
47 {
48 Pipe_SelectPipe(Number);
49 Pipe_EnablePipe();
50
51 *((uint32_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe] = 0;
52 *((uint32_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe] = (AVR32_USBB_ALLOC_MASK |
53 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
54 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
55 ((uint32_t)Banks << AVR32_USBB_PBK_OFFSET) |
56 ((EndpointNumber & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
57
58 Pipe_SetInfiniteINRequests();
59
60 return Pipe_IsConfigured();
61 }
62
63 void Pipe_ClearPipes(void)
64 {
65 UPINT = 0;
66
67 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
68 {
69 Pipe_SelectPipe(PNum);
70 *((uint32_t*)AVR32_USBB_UPCFG0)[USB_SelectedPipe] = 0;
71 Pipe_DisablePipe();
72 }
73 }
74
75 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
76 {
77 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
78
79 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
80 {
81 Pipe_SelectPipe(PNum);
82
83 if (!(Pipe_IsConfigured()))
84 continue;
85
86 uint8_t PipeToken = Pipe_GetPipeToken();
87 bool PipeTokenCorrect = true;
88
89 if (PipeToken != PIPE_TOKEN_SETUP)
90 PipeTokenCorrect = (PipeToken == ((EndpointAddress & PIPE_EPDIR_MASK) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT));
91
92 if (PipeTokenCorrect && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
93 return true;
94 }
95
96 Pipe_SelectPipe(PrevPipeNumber);
97 return false;
98 }
99
100 uint8_t Pipe_WaitUntilReady(void)
101 {
102 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
103 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
104 #else
105 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
106 #endif
107
108 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
109
110 for (;;)
111 {
112 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
113 {
114 if (Pipe_IsINReceived())
115 return PIPE_READYWAIT_NoError;
116 }
117 else
118 {
119 if (Pipe_IsOUTReady())
120 return PIPE_READYWAIT_NoError;
121 }
122
123 if (Pipe_IsStalled())
124 return PIPE_READYWAIT_PipeStalled;
125 else if (USB_HostState == HOST_STATE_Unattached)
126 return PIPE_READYWAIT_DeviceDisconnected;
127
128 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
129
130 if (CurrentFrameNumber != PreviousFrameNumber)
131 {
132 PreviousFrameNumber = CurrentFrameNumber;
133
134 if (!(TimeoutMSRem--))
135 return PIPE_READYWAIT_Timeout;
136 }
137 }
138 }
139
140 #endif
141