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 #include "../../../../Common/Common.h"
32 #if (ARCH == ARCH_XMEGA)
34 #define __INCLUDE_FROM_USB_DRIVER
35 #include "../USBMode.h"
37 #if defined(USB_CAN_BE_DEVICE)
39 #include "../Endpoint.h"
41 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
42 uint8_t USB_Device_ControlEndpointSize
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
;
45 Endpoint_FIFOPair_t USB_Endpoint_FIFOs
[ENDPOINT_TOTAL_ENDPOINTS
];
47 volatile uint8_t USB_Endpoint_SelectedEndpoint
;
48 volatile USB_EP_t
* USB_Endpoint_SelectedHandle
;
49 volatile Endpoint_FIFO_t
* USB_Endpoint_SelectedFIFO
;
51 bool Endpoint_ConfigureEndpoint_PRV(const uint8_t Number
,
52 const uint8_t Direction
,
56 Endpoint_SelectEndpoint(Number
| Direction
);
58 USB_Endpoint_SelectedHandle
->CTRL
= 0;
59 USB_Endpoint_SelectedHandle
->STATUS
= (Direction
== ENDPOINT_DIR_IN
) ? USB_EP_BUSNACK0_bm
: 0;
60 USB_Endpoint_SelectedHandle
->CTRL
= Config
;
61 USB_Endpoint_SelectedHandle
->CNT
= 0;
62 USB_Endpoint_SelectedHandle
->DATAPTR
= (intptr_t)USB_Endpoint_SelectedFIFO
->Data
;
64 USB_Endpoint_SelectedFIFO
->Length
= (Direction
== ENDPOINT_DIR_IN
) ? Size
: 0;
65 USB_Endpoint_SelectedFIFO
->Position
= 0;
70 void Endpoint_ClearEndpoints(void)
72 for (uint8_t EPNum
= 0; EPNum
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++)
74 USB_EndpointTable
.Endpoints
[EPNum
].IN
.CTRL
= 0;
75 USB_EndpointTable
.Endpoints
[EPNum
].OUT
.CTRL
= 0;
79 void Endpoint_ClearStatusStage(void)
81 if (USB_ControlRequest
.bmRequestType
& REQDIR_DEVICETOHOST
)
83 while (!(Endpoint_IsOUTReceived()))
85 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
93 while (!(Endpoint_IsINReady()))
95 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
103 #if !defined(CONTROL_ONLY_DEVICE)
104 uint8_t Endpoint_WaitUntilReady(void)
106 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
107 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
109 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
112 uint16_t PreviousFrameNumber
= USB_Device_GetFrameNumber();
116 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
)
118 if (Endpoint_IsINReady())
119 return ENDPOINT_READYWAIT_NoError
;
123 if (Endpoint_IsOUTReceived())
124 return ENDPOINT_READYWAIT_NoError
;
127 uint8_t USB_DeviceState_LCL
= USB_DeviceState
;
129 if (USB_DeviceState_LCL
== DEVICE_STATE_Unattached
)
130 return ENDPOINT_READYWAIT_DeviceDisconnected
;
131 else if (USB_DeviceState_LCL
== DEVICE_STATE_Suspended
)
132 return ENDPOINT_READYWAIT_BusSuspended
;
133 else if (Endpoint_IsStalled())
134 return ENDPOINT_READYWAIT_EndpointStalled
;
136 uint16_t CurrentFrameNumber
= USB_Device_GetFrameNumber();
138 if (CurrentFrameNumber
!= PreviousFrameNumber
)
140 PreviousFrameNumber
= CurrentFrameNumber
;
142 if (!(TimeoutMSRem
--))
143 return ENDPOINT_READYWAIT_Timeout
;