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_UC3)
\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 volatile uint32_t USB_Pipe_SelectedPipe = PIPE_CONTROLPIPE;
\r
44 volatile uint8_t* USB_Pipe_FIFOPos[PIPE_TOTAL_PIPES];
\r
46 bool Pipe_ConfigurePipeTable(const USB_Pipe_Table_t* const Table,
\r
47 const uint8_t Entries)
\r
49 for (uint8_t i = 0; i < Entries; i++)
\r
51 if (!(Table[i].Address))
\r
54 if (!(Pipe_ConfigurePipe(Table[i].Address, Table[i].Type, Table[i].EndpointAddress, Table[i].Size, Table[i].Banks)))
\r
63 bool Pipe_ConfigurePipe(const uint8_t Address,
\r
65 const uint8_t EndpointAddress,
\r
66 const uint16_t Size,
\r
67 const uint8_t Banks)
\r
69 uint8_t Number = (Address & PIPE_EPNUM_MASK);
\r
70 uint8_t Token = (Address & PIPE_DIR_IN) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT;
\r
72 if (Number >= PIPE_TOTAL_PIPES)
\r
75 if (Type == EP_TYPE_CONTROL)
\r
76 Token = PIPE_TOKEN_SETUP;
\r
78 USB_Pipe_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * PIPE_HSB_ADDRESS_SPACE_SIZE];
\r
80 #if defined(ORDERED_EP_CONFIG)
\r
81 Pipe_SelectPipe(Number);
\r
84 (&AVR32_USBB.upcfg0)[Number] = 0;
\r
85 (&AVR32_USBB.upcfg0)[Number] = (AVR32_USBB_ALLOC_MASK |
\r
86 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
\r
87 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
\r
88 ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0) |
\r
89 Pipe_BytesToEPSizeMask(Size) |
\r
90 ((uint32_t)Number << AVR32_USBB_PEPNUM_OFFSET));
\r
92 Pipe_SetInfiniteINRequests();
\r
94 return Pipe_IsConfigured();
\r
96 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
\r
98 uint32_t UPCFG0Temp;
\r
100 Pipe_SelectPipe(PNum);
\r
102 if (PNum == Number)
\r
104 UPCFG0Temp = (AVR32_USBB_ALLOC_MASK |
\r
105 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
\r
106 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
\r
107 ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0) |
\r
108 Pipe_BytesToEPSizeMask(Size) |
\r
109 ((EndpointAddress & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
\r
113 UPCFG0Temp = (&AVR32_USBB.upcfg0)[PNum];
\r
116 if (!(UPCFG0Temp & AVR32_USBB_ALLOC_MASK))
\r
119 Pipe_DisablePipe();
\r
120 (&AVR32_USBB.upcfg0)[PNum] &= ~AVR32_USBB_ALLOC_MASK;
\r
123 (&AVR32_USBB.upcfg0)[PNum] = UPCFG0Temp;
\r
125 Pipe_SetInfiniteINRequests();
\r
127 if (!(Pipe_IsConfigured()))
\r
131 Pipe_SelectPipe(Number);
\r
136 void Pipe_ClearPipes(void)
\r
138 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
\r
140 Pipe_SelectPipe(PNum);
\r
141 (&AVR32_USBB.upcfg0)[PNum] = 0;
\r
142 (&AVR32_USBB.upcon0clr)[PNum] = -1;
\r
143 USB_Pipe_FIFOPos[PNum] = &AVR32_USBB_SLAVE[PNum * 0x10000];
\r
144 Pipe_DisablePipe();
\r
148 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
\r
150 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
\r
152 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
\r
154 Pipe_SelectPipe(PNum);
\r
156 if (!(Pipe_IsConfigured()))
\r
159 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
\r
163 Pipe_SelectPipe(PrevPipeNumber);
\r
167 uint8_t Pipe_WaitUntilReady(void)
\r
169 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
\r
170 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
\r
172 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
\r
175 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
\r
179 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
\r
181 if (Pipe_IsINReceived())
\r
182 return PIPE_READYWAIT_NoError;
\r
186 if (Pipe_IsOUTReady())
\r
187 return PIPE_READYWAIT_NoError;
\r
190 if (Pipe_IsStalled())
\r
191 return PIPE_READYWAIT_PipeStalled;
\r
192 else if (USB_HostState == HOST_STATE_Unattached)
\r
193 return PIPE_READYWAIT_DeviceDisconnected;
\r
195 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
\r
197 if (CurrentFrameNumber != PreviousFrameNumber)
\r
199 PreviousFrameNumber = CurrentFrameNumber;
\r
201 if (!(TimeoutMSRem--))
\r
202 return PIPE_READYWAIT_Timeout;
\r