3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../USBMode.h"
34 #if defined(USB_CAN_BE_DEVICE)
36 #include "../Endpoint.h"
38 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
39 uint8_t USB_Device_ControlEndpointSize
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
;
42 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
,
43 const uint8_t UECFG0XData
,
44 const uint8_t UECFG1XData
)
46 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
47 Endpoint_SelectEndpoint(Number
);
48 Endpoint_EnableEndpoint();
51 UECFG0X
= UECFG0XData
;
52 UECFG1X
= UECFG1XData
;
54 return Endpoint_IsConfigured();
56 for (uint8_t EPNum
= Number
; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
62 Endpoint_SelectEndpoint(EPNum
);
66 UECFG0XTemp
= UECFG0XData
;
67 UECFG1XTemp
= UECFG1XData
;
72 UECFG0XTemp
= UECFG0X
;
73 UECFG1XTemp
= UECFG1X
;
77 if (!(UECFG1XTemp
& (1 << ALLOC
)))
80 Endpoint_DisableEndpoint();
81 UECFG1X
&= ~(1 << ALLOC
);
83 Endpoint_EnableEndpoint();
84 UECFG0X
= UECFG0XTemp
;
85 UECFG1X
= UECFG1XTemp
;
88 if (!(Endpoint_IsConfigured()))
92 Endpoint_SelectEndpoint(Number
);
97 void Endpoint_ClearEndpoints(void)
101 for (uint8_t EPNum
= 0; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
103 Endpoint_SelectEndpoint(EPNum
);
107 Endpoint_DisableEndpoint();
111 void Endpoint_ClearStatusStage(void)
113 if (USB_ControlRequest
.bmRequestType
& REQDIR_DEVICETOHOST
)
115 while (!(Endpoint_IsOUTReceived()))
117 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
125 while (!(Endpoint_IsINReady()))
127 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
135 #if !defined(CONTROL_ONLY_DEVICE)
136 uint8_t Endpoint_WaitUntilReady(void)
138 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
139 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
141 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
144 uint16_t PreviousFrameNumber
= USB_Device_GetFrameNumber();
148 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
150 if (Endpoint_IsINReady())
151 return ENDPOINT_READYWAIT_NoError
;
155 if (Endpoint_IsOUTReceived())
156 return ENDPOINT_READYWAIT_NoError
;
159 uint8_t USB_DeviceState_LCL
= USB_DeviceState
;
161 if (USB_DeviceState_LCL
== DEVICE_STATE_Unattached
)
162 return ENDPOINT_READYWAIT_DeviceDisconnected
;
163 else if (USB_DeviceState_LCL
== DEVICE_STATE_Suspended
)
164 return ENDPOINT_READYWAIT_BusSuspended
;
165 else if (Endpoint_IsStalled())
166 return ENDPOINT_READYWAIT_EndpointStalled
;
168 uint16_t CurrentFrameNumber
= USB_Device_GetFrameNumber();
170 if (CurrentFrameNumber
!= PreviousFrameNumber
)
172 PreviousFrameNumber
= CurrentFrameNumber
;
174 if (!(TimeoutMSRem
--))
175 return ENDPOINT_READYWAIT_Timeout
;