2 ******************************************************************************
4 * @author MCD Application Team
7 * @brief This file handles the issuing of the USB transactions
8 ******************************************************************************
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
21 /* Includes ------------------------------------------------------------------*/
23 #include "usbh_ioreq.h"
25 /** @addtogroup USBH_LIB
29 /** @addtogroup USBH_LIB_CORE
33 /** @defgroup USBH_IOREQ
34 * @brief This file handles the standard protocol processing (USB v2.0)
39 /** @defgroup USBH_IOREQ_Private_Defines
47 /** @defgroup USBH_IOREQ_Private_TypesDefinitions
56 /** @defgroup USBH_IOREQ_Private_Macros
64 /** @defgroup USBH_IOREQ_Private_Variables
72 /** @defgroup USBH_IOREQ_Private_FunctionPrototypes
75 static USBH_Status USBH_SubmitSetupRequest(USBH_HOST *phost,
84 /** @defgroup USBH_IOREQ_Private_Functions
91 * USBH_CtlReq sends a control request and provide the status after
92 * completion of the request
93 * @param pdev: Selected device
94 * @param req: Setup Request Structure
95 * @param buff: data buffer address to store the response
96 * @param length: length of the response
99 USBH_Status USBH_CtlReq (USB_OTG_CORE_HANDLE *pdev,
105 URB_STATE URB_Status = URB_IDLE;
107 URB_Status = HCD_GetURB_State(pdev, phost->Control.hc_num_out);
111 switch (phost->RequestState)
114 /* Start a SETUP transfer */
115 USBH_SubmitSetupRequest(phost, buff, length);
116 phost->RequestState = CMD_WAIT;
121 if (URB_Status == URB_DONE)
123 /* Commands successfully sent and Response Received */
124 phost->RequestState = CMD_SEND;
127 else if (URB_Status == URB_ERROR)
130 phost->RequestState = CMD_SEND;
133 else if (URB_Status == URB_STALL)
135 /* Commands successfully sent and Response Received */
136 phost->RequestState = CMD_SEND;
137 status = USBH_NOT_SUPPORTED;
148 * @brief USBH_CtlSendSetup
149 * Sends the Setup Packet to the Device
150 * @param pdev: Selected device
151 * @param buff: Buffer pointer from which the Data will be send to Device
152 * @param hc_num: Host channel Number
155 USBH_Status USBH_CtlSendSetup ( USB_OTG_CORE_HANDLE *pdev,
158 pdev->host.hc[hc_num].ep_is_in = 0;
159 pdev->host.hc[hc_num].data_pid = HC_PID_SETUP;
160 pdev->host.hc[hc_num].xfer_buff = buff;
161 pdev->host.hc[hc_num].xfer_len = USBH_SETUP_PKT_SIZE;
163 return (USBH_Status)HCD_SubmitRequest (pdev , hc_num);
168 * @brief USBH_CtlSendData
169 * Sends a data Packet to the Device
170 * @param pdev: Selected device
171 * @param buff: Buffer pointer from which the Data will be sent to Device
172 * @param length: Length of the data to be sent
173 * @param hc_num: Host channel Number
176 USBH_Status USBH_CtlSendData ( USB_OTG_CORE_HANDLE *pdev,
181 pdev->host.hc[hc_num].ep_is_in = 0;
182 pdev->host.hc[hc_num].xfer_buff = buff;
183 pdev->host.hc[hc_num].xfer_len = length;
186 { /* For Status OUT stage, Length==0, Status Out PID = 1 */
187 pdev->host.hc[hc_num].toggle_out = 1;
190 /* Set the Data Toggle bit as per the Flag */
191 if ( pdev->host.hc[hc_num].toggle_out == 0)
192 { /* Put the PID 0 */
193 pdev->host.hc[hc_num].data_pid = HC_PID_DATA0;
196 { /* Put the PID 1 */
197 pdev->host.hc[hc_num].data_pid = HC_PID_DATA1 ;
200 HCD_SubmitRequest (pdev , hc_num);
207 * @brief USBH_CtlReceiveData
208 * Receives the Device Response to the Setup Packet
209 * @param pdev: Selected device
210 * @param buff: Buffer pointer in which the response needs to be copied
211 * @param length: Length of the data to be received
212 * @param hc_num: Host channel Number
215 USBH_Status USBH_CtlReceiveData(USB_OTG_CORE_HANDLE *pdev,
221 pdev->host.hc[hc_num].ep_is_in = 1;
222 pdev->host.hc[hc_num].data_pid = HC_PID_DATA1;
223 pdev->host.hc[hc_num].xfer_buff = buff;
224 pdev->host.hc[hc_num].xfer_len = length;
226 HCD_SubmitRequest (pdev , hc_num);
234 * @brief USBH_BulkSendData
235 * Sends the Bulk Packet to the device
236 * @param pdev: Selected device
237 * @param buff: Buffer pointer from which the Data will be sent to Device
238 * @param length: Length of the data to be sent
239 * @param hc_num: Host channel Number
242 USBH_Status USBH_BulkSendData ( USB_OTG_CORE_HANDLE *pdev,
247 pdev->host.hc[hc_num].ep_is_in = 0;
248 pdev->host.hc[hc_num].xfer_buff = buff;
249 pdev->host.hc[hc_num].xfer_len = length;
251 /* Set the Data Toggle bit as per the Flag */
252 if ( pdev->host.hc[hc_num].toggle_out == 0)
253 { /* Put the PID 0 */
254 pdev->host.hc[hc_num].data_pid = HC_PID_DATA0;
257 { /* Put the PID 1 */
258 pdev->host.hc[hc_num].data_pid = HC_PID_DATA1 ;
261 HCD_SubmitRequest (pdev , hc_num);
267 * @brief USBH_BulkReceiveData
268 * Receives IN bulk packet from device
269 * @param pdev: Selected device
270 * @param buff: Buffer pointer in which the received data packet to be copied
271 * @param length: Length of the data to be received
272 * @param hc_num: Host channel Number
275 USBH_Status USBH_BulkReceiveData( USB_OTG_CORE_HANDLE *pdev,
280 pdev->host.hc[hc_num].ep_is_in = 1;
281 pdev->host.hc[hc_num].xfer_buff = buff;
282 pdev->host.hc[hc_num].xfer_len = length;
285 if( pdev->host.hc[hc_num].toggle_in == 0)
287 pdev->host.hc[hc_num].data_pid = HC_PID_DATA0;
291 pdev->host.hc[hc_num].data_pid = HC_PID_DATA1;
294 HCD_SubmitRequest (pdev , hc_num);
300 * @brief USBH_InterruptReceiveData
301 * Receives the Device Response to the Interrupt IN token
302 * @param pdev: Selected device
303 * @param buff: Buffer pointer in which the response needs to be copied
304 * @param length: Length of the data to be received
305 * @param hc_num: Host channel Number
308 USBH_Status USBH_InterruptReceiveData( USB_OTG_CORE_HANDLE *pdev,
314 pdev->host.hc[hc_num].ep_is_in = 1;
315 pdev->host.hc[hc_num].xfer_buff = buff;
316 pdev->host.hc[hc_num].xfer_len = length;
320 if(pdev->host.hc[hc_num].toggle_in == 0)
322 pdev->host.hc[hc_num].data_pid = HC_PID_DATA0;
326 pdev->host.hc[hc_num].data_pid = HC_PID_DATA1;
329 /* toggle DATA PID */
330 pdev->host.hc[hc_num].toggle_in ^= 1;
332 HCD_SubmitRequest (pdev , hc_num);
338 * @brief USBH_InterruptSendData
339 * Sends the data on Interrupt OUT Endpoint
340 * @param pdev: Selected device
341 * @param buff: Buffer pointer from where the data needs to be copied
342 * @param length: Length of the data to be sent
343 * @param hc_num: Host channel Number
346 USBH_Status USBH_InterruptSendData( USB_OTG_CORE_HANDLE *pdev,
352 pdev->host.hc[hc_num].ep_is_in = 0;
353 pdev->host.hc[hc_num].xfer_buff = buff;
354 pdev->host.hc[hc_num].xfer_len = length;
356 if(pdev->host.hc[hc_num].toggle_in == 0)
358 pdev->host.hc[hc_num].data_pid = HC_PID_DATA0;
362 pdev->host.hc[hc_num].data_pid = HC_PID_DATA1;
365 pdev->host.hc[hc_num].toggle_in ^= 1;
367 HCD_SubmitRequest (pdev , hc_num);
374 * @brief USBH_SubmitSetupRequest
375 * Start a setup transfer by changing the state-machine and
376 * initializing the required variables needed for the Control Transfer
377 * @param pdev: Selected device
378 * @param setup: Setup Request Structure
379 * @param buff: Buffer used for setup request
380 * @param length: Length of the data
383 static USBH_Status USBH_SubmitSetupRequest(USBH_HOST *phost,
388 /* Save Global State */
389 phost->gStateBkp = phost->gState;
391 /* Prepare the Transactions */
392 phost->gState = HOST_CTRL_XFER;
393 phost->Control.buff = buff;
394 phost->Control.length = length;
395 phost->Control.state = CTRL_SETUP;
416 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/