3 Copyright (C) Dean Camera, 2012.
\r
5 dean [at] fourwalledcubicle [dot] com
\r
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
\r
12 Permission to use, copy, modify, distribute, and sell this
\r
13 software and its documentation for any purpose is hereby granted
\r
14 without fee, provided that the above copyright notice appear in
\r
15 all copies and that both that the copyright notice and this
\r
16 permission notice and warranty disclaimer appear in supporting
\r
17 documentation, and that the name of the author not be used in
\r
18 advertising or publicity pertaining to distribution of the
\r
19 software without specific, written prior permission.
\r
21 The author disclaim all warranties with regard to this
\r
22 software, including all implied warranties of merchantability
\r
23 and fitness. In no event shall the author be liable for any
\r
24 special, indirect or consequential damages or any damages
\r
25 whatsoever resulting from loss of use, data or profits, whether
\r
26 in an action of contract, negligence or other tortious action,
\r
27 arising out of or in connection with the use or performance of
\r
31 #include "../../../../Common/Common.h"
\r
32 #if (ARCH == ARCH_AVR8)
\r
34 #define __INCLUDE_FROM_USB_DRIVER
\r
35 #include "../USBMode.h"
\r
37 #if defined(USB_CAN_BE_HOST)
\r
39 #include "../Pipe.h"
\r
41 uint8_t USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
\r
43 bool Pipe_ConfigurePipeTable(const USB_Pipe_Table_t* const Table,
\r
44 const uint8_t Entries)
\r
46 for (uint8_t i = 0; i < Entries; i++)
\r
48 if (!(Table[i].Address))
\r
51 if (!(Pipe_ConfigurePipe(Table[i].Address, Table[i].Type, Table[i].EndpointAddress, Table[i].Size, Table[i].Banks)))
\r
60 bool Pipe_ConfigurePipe(const uint8_t Address,
\r
62 const uint8_t EndpointAddress,
\r
63 const uint16_t Size,
\r
64 const uint8_t Banks)
\r
66 uint8_t Number = (Address & PIPE_EPNUM_MASK);
\r
67 uint8_t Token = (Address & PIPE_DIR_IN) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT;
\r
69 if (Number >= PIPE_TOTAL_PIPES)
\r
72 if (Type == EP_TYPE_CONTROL)
\r
73 Token = PIPE_TOKEN_SETUP;
\r
75 #if defined(ORDERED_EP_CONFIG)
\r
76 Pipe_SelectPipe(Number);
\r
81 UPCFG0X = ((Type << EPTYPE0) | Token | ((EndpointAddress & PIPE_EPNUM_MASK) << PEPNUM0));
\r
82 UPCFG1X = ((1 << ALLOC) | ((Banks > 1) ? (1 << EPBK0) : 0) | Pipe_BytesToEPSizeMask(Size));
\r
84 Pipe_SetInfiniteINRequests();
\r
86 return Pipe_IsConfigured();
\r
88 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
\r
90 uint8_t UPCFG0XTemp;
\r
91 uint8_t UPCFG1XTemp;
\r
92 uint8_t UPCFG2XTemp;
\r
95 Pipe_SelectPipe(PNum);
\r
99 UPCFG0XTemp = ((Type << EPTYPE0) | Token | ((EndpointAddress & PIPE_EPNUM_MASK) << PEPNUM0));
\r
100 UPCFG1XTemp = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
\r
106 UPCFG0XTemp = UPCFG0X;
\r
107 UPCFG1XTemp = UPCFG1X;
\r
108 UPCFG2XTemp = UPCFG2X;
\r
109 UPIENXTemp = UPIENX;
\r
112 if (!(UPCFG1XTemp & (1 << ALLOC)))
\r
115 Pipe_DisablePipe();
\r
116 UPCFG1X &= ~(1 << ALLOC);
\r
119 UPCFG0X = UPCFG0XTemp;
\r
120 UPCFG1X = UPCFG1XTemp;
\r
121 UPCFG2X = UPCFG2XTemp;
\r
122 UPIENX = UPIENXTemp;
\r
124 Pipe_SetInfiniteINRequests();
\r
126 if (!(Pipe_IsConfigured()))
\r
130 Pipe_SelectPipe(Number);
\r
135 void Pipe_ClearPipes(void)
\r
139 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
\r
141 Pipe_SelectPipe(PNum);
\r
145 Pipe_DisablePipe();
\r
149 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
\r
151 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
\r
153 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
\r
155 Pipe_SelectPipe(PNum);
\r
157 if (!(Pipe_IsConfigured()))
\r
160 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
\r
164 Pipe_SelectPipe(PrevPipeNumber);
\r
168 uint8_t Pipe_WaitUntilReady(void)
\r
170 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
\r
171 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
\r
173 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
\r
176 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
\r
180 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
\r
182 if (Pipe_IsINReceived())
\r
183 return PIPE_READYWAIT_NoError;
\r
187 if (Pipe_IsOUTReady())
\r
188 return PIPE_READYWAIT_NoError;
\r
191 if (Pipe_IsStalled())
\r
192 return PIPE_READYWAIT_PipeStalled;
\r
193 else if (USB_HostState == HOST_STATE_Unattached)
\r
194 return PIPE_READYWAIT_DeviceDisconnected;
\r
196 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
\r
198 if (CurrentFrameNumber != PreviousFrameNumber)
\r
200 PreviousFrameNumber = CurrentFrameNumber;
\r
202 if (!(TimeoutMSRem--))
\r
203 return PIPE_READYWAIT_Timeout;
\r