| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
HostStandardReq.c
Go to the documentation of this file.
1 /*
2 * @brief USB host standard request management
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * Copyright(C) Dean Camera, 2011, 2012
7 * All rights reserved.
8 *
9 * @par
10 * Software that is described herein is for illustrative purposes only
11 * which provides customers with programming information regarding the
12 * LPC products. This software is supplied "AS IS" without any warranties of
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
14 * all warranties, express or implied, including all implied warranties of
15 * merchantability, fitness for a particular purpose and non-infringement of
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
17 * or liability for the use of the software, conveys no license or rights under any
18 * patent, copyright, mask work right, or any other intellectual property rights in
19 * or to any products. NXP Semiconductors reserves the right to make changes
20 * in the software without notification. NXP Semiconductors also makes no
21 * representation or warranty that such application will be suitable for the
22 * specified use without further testing or modification.
23 *
24 * @par
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation is hereby granted, under NXP Semiconductors' and its
27 * licensor's relevant copyrights in the software, without fee, provided that it
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
29 * copyright, permission, and disclaimer notice must appear in all copies of
30 * this code.
31 */
32
33 #define __INCLUDE_FROM_USB_DRIVER
34 #include "USBMode.h"
35
36 #if defined(USB_CAN_BE_HOST)
37
38 #define __INCLUDE_FROM_HOSTSTDREQ_C
39 #include "HostStandardReq.h"
40
41 uint8_t USB_Host_ConfigurationNumber;
42
43 #if 1
44
45 uint8_t USB_Host_SendControlRequest(const uint8_t corenum, void* const BufferPtr)
46 {
47 uint8_t* DataStream = (uint8_t*)BufferPtr;
48 uint16_t DataLen = USB_ControlRequest.wLength;
49 uint8_t ret;
50
51 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_HOSTTODEVICE)
52 {
53 Pipe_Write_Stream_LE(corenum, BufferPtr, DataLen, NULL);
54 }
55
56 ret = (uint8_t)HcdControlTransfer(PipeInfo[corenum][pipeselected[corenum]].PipeHandle, &USB_ControlRequest,
57 PipeInfo[corenum][pipeselected[corenum]].Buffer);
58
59 if(ret == (uint8_t)HOST_SENDCONTROL_Successful)
60 {
61 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
62 {
63 PipeInfo[corenum][pipeselected[corenum]].ByteTransfered = USB_ControlRequest.wLength;
64 while(DataLen)
65 {
66 *(DataStream++) = Pipe_Read_8(corenum);
67 DataLen--;
68 }
69 /* Pipe_Read_Stream_LE(BufferPtr, DataLen, NULL); cannot use read stream as it call HcdDataTransfer*/
70 }
71 PipeInfo[corenum][pipeselected[corenum]].StartIdx = PipeInfo[corenum][pipeselected[corenum]].ByteTransfered = 0; /* Clear Control Pipe */
72 return HOST_SENDCONTROL_Successful;
73 }
74 else
75 {
76 return HOST_SENDCONTROL_PipeError;
77 }
78 }
79
80 #else // The following code is deprecated
81 uint8_t USB_Host_SendControlRequest(void* const BufferPtr)
82 {
83 uint8_t* DataStream = (uint8_t*)BufferPtr;
84 bool BusSuspended = USB_Host_IsBusSuspended();
85 uint8_t ReturnStatus = HOST_SENDCONTROL_Successful;
86 uint16_t DataLen = USB_ControlRequest.wLength;
87
88 USB_Host_ResumeBus();
89
90 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
91 goto End_Of_Control_Send;
92
93 Pipe_SetPipeToken(PIPE_TOKEN_SETUP);
94 Pipe_ClearError();
95
96 Pipe_Unfreeze();
97
98 Pipe_Write_8(USB_ControlRequest.bmRequestType);
99 Pipe_Write_8(USB_ControlRequest.bRequest);
100 Pipe_Write_16_LE(USB_ControlRequest.wValue);
101 Pipe_Write_16_LE(USB_ControlRequest.wIndex);
102 Pipe_Write_16_LE(USB_ControlRequest.wLength);
103
104 Pipe_ClearSETUP();
105
106 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
107 goto End_Of_Control_Send;
108
109 Pipe_Freeze();
110
111 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
112 goto End_Of_Control_Send;
113
114 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
115 {
116 Pipe_SetPipeToken(PIPE_TOKEN_IN);
117
118 if (DataStream != NULL)
119 {
120 while (DataLen)
121 {
122 Pipe_Unfreeze();
123
124 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
125 goto End_Of_Control_Send;
126
127 if (!(Pipe_BytesInPipe()))
128 DataLen = 0;
129
130 while (Pipe_BytesInPipe() && DataLen)
131 {
132 *(DataStream++) = Pipe_Read_8();
133 DataLen--;
134 }
135
136 Pipe_Freeze();
137 Pipe_ClearIN();
138 }
139 }
140
141 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
142 Pipe_Unfreeze();
143
144 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
145 goto End_Of_Control_Send;
146
147 Pipe_ClearOUT();
148
149 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
150 goto End_Of_Control_Send;
151 }
152 else
153 {
154 if (DataStream != NULL)
155 {
156 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
157 Pipe_Unfreeze();
158
159 while (DataLen)
160 {
161 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
162 goto End_Of_Control_Send;
163
164 while (DataLen && (Pipe_BytesInPipe() < USB_Host_ControlPipeSize))
165 {
166 Pipe_Write_8(*(DataStream++));
167 DataLen--;
168 }
169
170 Pipe_ClearOUT();
171 }
172
173 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
174 goto End_Of_Control_Send;
175
176 Pipe_Freeze();
177 }
178
179 Pipe_SetPipeToken(PIPE_TOKEN_IN);
180 Pipe_Unfreeze();
181
182 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
183 goto End_Of_Control_Send;
184
185 Pipe_ClearIN();
186 }
187
188 End_Of_Control_Send:
189 Pipe_Freeze();
190
191 if (BusSuspended)
192 USB_Host_SuspendBus();
193
194 Pipe_ResetPipe(PIPE_CONTROLPIPE);
195
196 return ReturnStatus;
197 }
198
199 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType)
200 {
201 #if (USB_HOST_TIMEOUT_MS < 0xFF)
202 uint8_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
203 #else
204 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
205 #endif
206
207 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSETUPSent()) ||
208 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsINReceived()) ||
209 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsOUTReady())))
210 {
211 uint8_t ErrorCode;
212
213 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
214 return ErrorCode;
215
216 if (!(TimeoutCounter--))
217 return HOST_SENDCONTROL_SoftwareTimeOut;
218 }
219
220 return HOST_SENDCONTROL_Successful;
221 }
222 #endif
223
224 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t corenum, const uint8_t ConfigNumber)
225 {
226 uint8_t ErrorCode;
227
228 USB_ControlRequest = (USB_Request_Header_t)
229 {
230 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
231 .bRequest = REQ_SetConfiguration,
232 .wValue = ConfigNumber,
233 .wIndex = 0,
234 .wLength = 0,
235 };
236
237 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
238
239 if ((ErrorCode = USB_Host_SendControlRequest(corenum, NULL)) == HOST_SENDCONTROL_Successful)
240 {
241 USB_Host_ConfigurationNumber = ConfigNumber;
242 USB_HostState[corenum] = (ConfigNumber) ? HOST_STATE_Configured : HOST_STATE_Addressed;
243 }
244
245 return ErrorCode;
246 }
247
248 uint8_t USB_Host_GetDeviceDescriptor(const uint8_t corenum, void* const DeviceDescriptorPtr)
249 {
250 USB_ControlRequest = (USB_Request_Header_t)
251 {
252 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
253 .bRequest = REQ_GetDescriptor,
254 .wValue = (DTYPE_Device << 8),
255 .wIndex = 0,
256 .wLength = sizeof(USB_Descriptor_Device_t),
257 };
258
259 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
260
261 return USB_Host_SendControlRequest(corenum,DeviceDescriptorPtr);
262 }
263
264 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t corenum,
265 const uint8_t Index,
266 void* const Buffer,
267 const uint8_t BufferLength)
268 {
269 USB_ControlRequest = (USB_Request_Header_t)
270 {
271 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
272 .bRequest = REQ_GetDescriptor,
273 .wValue = (DTYPE_String << 8) | Index,
274 .wIndex = 0,
275 .wLength = BufferLength,
276 };
277
278 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
279
280 return USB_Host_SendControlRequest(corenum,Buffer);
281 }
282
283 uint8_t USB_Host_GetDeviceStatus(const uint8_t corenum, uint8_t* const FeatureStatus)
284 {
285 USB_ControlRequest = (USB_Request_Header_t)
286 {
287 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
288 .bRequest = REQ_GetStatus,
289 .wValue = 0,
290 .wIndex = 0,
291 .wLength = 0,
292 };
293
294 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
295
296 return USB_Host_SendControlRequest(corenum, FeatureStatus);
297 }
298
299 uint8_t USB_Host_ClearEndpointStall(const uint8_t corenum, const uint8_t EndpointAddress)
300 {
301 USB_ControlRequest = (USB_Request_Header_t)
302 {
303 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
304 .bRequest = REQ_ClearFeature,
305 .wValue = FEATURE_SEL_EndpointHalt,
306 .wIndex = EndpointAddress,
307 .wLength = 0,
308 };
309
310 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
311
312 return USB_Host_SendControlRequest(corenum,NULL);
313 }
314
315 uint8_t USB_Host_SetInterfaceAltSetting(const uint8_t corenum,
316 const uint8_t InterfaceIndex,
317 const uint8_t AltSetting)
318 {
319 USB_ControlRequest = (USB_Request_Header_t)
320 {
321 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
322 .bRequest = REQ_SetInterface,
323 .wValue = AltSetting,
324 .wIndex = InterfaceIndex,
325 .wLength = 0,
326 };
327
328 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
329
330 return USB_Host_SendControlRequest(corenum,NULL);
331 }
332
333 #endif
334