Statistics
| Branch: | Tag: | Revision:

root / LUFA / Drivers / USB / Core / UC3 / Endpoint_UC3.c @ 978b99e5

History | View | Annotate | Download (4.47 KB)

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_DEVICE)
35

    
36
#include "../Endpoint.h"
37

    
38
#if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
39
uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
40
#endif
41

    
42
volatile uint32_t USB_SelectedEndpoint = ENDPOINT_CONTROLEP;
43
volatile uint8_t* USB_EndpointFIFOPos[ENDPOINT_TOTAL_ENDPOINTS];
44

    
45
bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
46
                                    const uint32_t UECFG0Data)
47
{
48
#if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
49
        Endpoint_SelectEndpoint(Number);
50
        Endpoint_EnableEndpoint();
51

    
52
        (&AVR32_USBB.uecfg0)[Number] = 0;
53
        (&AVR32_USBB.uecfg0)[Number] = UECFG0Data;
54
        USB_EndpointFIFOPos[Number]  = &AVR32_USBB_SLAVE[Number * 0x10000];
55

    
56
        return Endpoint_IsConfigured();
57
#else
58
        for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
59
        {
60
                uint8_t UECFG0Temp;
61

    
62
                Endpoint_SelectEndpoint(EPNum);
63
                
64
                if (EPNum == Number)
65
                {
66
                        UECFG0Temp = UECFG0Data;
67
                }
68
                else
69
                {
70
                        UECFG0Temp = (&AVR32_USBB.uecfg0)[EPNum];
71
                }
72

    
73
                if (!(UECFG0Temp & AVR32_USBB_ALLOC_MASK))
74
                  continue;
75

    
76
                Endpoint_DisableEndpoint();
77
                (&AVR32_USBB.uecfg0)[EPNum] &= ~AVR32_USBB_ALLOC_MASK;
78

    
79
                Endpoint_EnableEndpoint();
80
                (&AVR32_USBB.uecfg0)[EPNum] = UECFG0Temp;
81
                        
82
                if (!(Endpoint_IsConfigured()))
83
                  return false;                        
84
        }
85
        
86
        Endpoint_SelectEndpoint(Number);
87
        return true;
88
#endif
89
}
90

    
91
void Endpoint_ClearEndpoints(void)
92
{
93
        for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
94
        {
95
                Endpoint_SelectEndpoint(EPNum);
96
                (&AVR32_USBB.uecfg0)[EPNum]    = 0;
97
                (&AVR32_USBB.uecon0clr)[EPNum] = -1;
98
                USB_EndpointFIFOPos[EPNum]     = &AVR32_USBB_SLAVE[EPNum * 0x10000];
99
                Endpoint_DisableEndpoint();
100
        }
101
}
102

    
103
void Endpoint_ClearStatusStage(void)
104
{
105
        if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
106
        {
107
                while (!(Endpoint_IsOUTReceived()))
108
                {
109
                        if (USB_DeviceState == DEVICE_STATE_Unattached)
110
                          return;
111
                }
112

    
113
                Endpoint_ClearOUT();
114
        }
115
        else
116
        {
117
                while (!(Endpoint_IsINReady()))
118
                {
119
                        if (USB_DeviceState == DEVICE_STATE_Unattached)
120
                          return;
121
                }
122

    
123
                Endpoint_ClearIN();
124
        }
125
}
126

    
127
#if !defined(CONTROL_ONLY_DEVICE)
128
uint8_t Endpoint_WaitUntilReady(void)
129
{
130
        #if (USB_STREAM_TIMEOUT_MS < 0xFF)
131
        uint8_t  TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
132
        #else
133
        uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
134
        #endif
135

    
136
        uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
137

    
138
        for (;;)
139
        {
140
                if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
141
                {
142
                        if (Endpoint_IsINReady())
143
                          return ENDPOINT_READYWAIT_NoError;
144
                }
145
                else
146
                {
147
                        if (Endpoint_IsOUTReceived())
148
                          return ENDPOINT_READYWAIT_NoError;
149
                }
150
                
151
                uint8_t USB_DeviceState_LCL = USB_DeviceState;
152

    
153
                if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
154
                  return ENDPOINT_READYWAIT_DeviceDisconnected;
155
                else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
156
                  return ENDPOINT_READYWAIT_BusSuspended;
157
                else if (Endpoint_IsStalled())
158
                  return ENDPOINT_READYWAIT_EndpointStalled;
159

    
160
                uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
161

    
162
                if (CurrentFrameNumber != PreviousFrameNumber)
163
                {
164
                        PreviousFrameNumber = CurrentFrameNumber;
165

    
166
                        if (!(TimeoutMSRem--))
167
                          return ENDPOINT_READYWAIT_Timeout;
168
                }
169
        }
170
}
171
#endif
172

    
173
#endif
174