LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 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 
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 
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  {
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 */
73  }
74  else
75  {
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 
89 
90  if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
91  goto End_Of_Control_Send;
92 
95 
96  Pipe_Unfreeze();
97 
98  Pipe_Write_8(USB_ControlRequest.bmRequestType);
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 
115  {
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 
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  {
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 
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)
193 
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--))
218  }
219 
221 }
222 #endif
223 
224 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t corenum, const uint8_t ConfigNumber)
225 {
226  uint8_t ErrorCode;
227 
229  {
230  .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
232  .wValue = ConfigNumber,
233  .wIndex = 0,
234  .wLength = 0,
235  };
236 
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 {
251  {
252  .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
254  .wValue = (DTYPE_Device << 8),
255  .wIndex = 0,
256  .wLength = sizeof(USB_Descriptor_Device_t),
257  };
258 
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 {
270  {
271  .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
273  .wValue = (DTYPE_String << 8) | Index,
274  .wIndex = 0,
275  .wLength = BufferLength,
276  };
277 
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 {
286  {
287  .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
289  .wValue = 0,
290  .wIndex = 0,
291  .wLength = 0,
292  };
293 
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 {
302  {
306  .wIndex = EndpointAddress,
307  .wLength = 0,
308  };
309 
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 {
320  {
323  .wValue = AltSetting,
324  .wIndex = InterfaceIndex,
325  .wLength = 0,
326  };
327 
329 
330  return USB_Host_SendControlRequest(corenum,NULL);
331 }
332 
333 #endif
334