rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
usbd_bulk.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file usbd_bulk.c
4 * @author MCD Application Team
5 * @version V2.4.2
6 * @date 11-December-2015
7 * @brief This file provides the high layer firmware functions to manage a
8 * USB bulk device.
9 *
10 ******************************************************************************
11 * @attention
12 *
13 * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
14 *
15 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
16 * You may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at:
18 *
19 * http://www.st.com/software_license_agreement_liberty_v2
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *
27 ******************************************************************************
28 */
29
30/* Includes ------------------------------------------------------------------*/
31#include "usbd_bulk.h"
32#include "usbd_desc.h"
33#include "usbd_ctlreq.h"
34
35
36/** @addtogroup STM32_USB_DEVICE_LIBRARY
37 * @{
38 */
39
40
41/** @defgroup USBD_Bulk
42 * @brief usbd core module
43 * @{
44 */
45
46/** @defgroup USBD_Bulk_Private_TypesDefinitions
47 * @{
48 */
49/**
50 * @}
51 */
52
53
54/** @defgroup USBD_Bulk_Private_Defines
55 * @{
56 */
57/**
58 * @}
59 */
60
61
62/** @defgroup USBD_Bulk_Private_Macros
63 * @{
64 */
65
66/**
67 * @}
68 */
69
70
71/** @defgroup USBD_Bulk_Private_FunctionPrototypes
72 * @{
73 */
74extern void UsbReceivePipeBulkOUT(uint8_t epnum);
75extern void UsbTransmitPipeBulkIN(void);
76
77
78static uint8_t USBD_Bulk_Init (USBD_HandleTypeDef *pdev,
79 uint8_t cfgidx);
80
81static uint8_t USBD_Bulk_DeInit (USBD_HandleTypeDef *pdev,
82 uint8_t cfgidx);
83
84static uint8_t USBD_Bulk_Setup (USBD_HandleTypeDef *pdev,
85 USBD_SetupReqTypedef *req);
86
87static uint8_t USBD_Bulk_DataIn (USBD_HandleTypeDef *pdev,
88 uint8_t epnum);
89
90static uint8_t USBD_Bulk_DataOut (USBD_HandleTypeDef *pdev,
91 uint8_t epnum);
92
93static uint8_t USBD_Bulk_SOF (USBD_HandleTypeDef *pdev);
94
95static uint8_t *USBD_Bulk_GetFSCfgDesc (uint16_t *length);
96
97uint8_t *USBD_Bulk_GetDeviceQualifierDescriptor (uint16_t *length);
98
99/* USB Standard Device Descriptor */
100__ALIGN_BEGIN static uint8_t USBD_Bulk_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
101{
102 USB_LEN_DEV_QUALIFIER_DESC,
103 USB_DESC_TYPE_DEVICE_QUALIFIER,
104 0x10,
105 0x01,
106 0x00,
107 0x00,
108 0x00,
109 0x40,
110 0x01,
111 0x00,
112};
113
114/**
115 * @}
116 */
117
118/** @defgroup USBD_Bulk_Private_Variables
119 * @{
120 */
121#if defined ( __ICCARM__ ) /* IAR Compiler */
122 #pragma data_alignment=4
123#endif /* defined ( __ICCARM__ ) */
124__ALIGN_BEGIN static volatile uint32_t usbd_bulk_AltSet __ALIGN_END = 0;
125__ALIGN_BEGIN uint8_t USB_Rx_Buffer[BULK_DATA_MAX_PACKET_SIZE] __ALIGN_END ;
126
127
128/* Bulk interface class callbacks structure */
129USBD_ClassTypeDef USBD_Bulk =
130{
134 NULL,
135 NULL,
139 NULL,
140 NULL,
141 NULL,
143 NULL,
145};
146
147/* USB Bulk device Configuration Descriptor */
148__ALIGN_BEGIN uint8_t USBD_Bulk_CfgFSDesc[USB_BULK_CONFIG_DESC_SIZ] __ALIGN_END =
149{
150 /*Configuration Descriptor*/
151 0x09, /* bLength: Configuration Descriptor size */
152 USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
153 USB_BULK_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */
154 0x00,
155 0x01, /* bNumInterfaces: 1 interface */
156 0x01, /* bConfigurationValue: Configuration value */
157 0x00, /* iConfiguration: Index of string descriptor describing the configuration */
158 0xC0, /* bmAttributes: self powered */
159 0x32, /* MaxPower 100 mA */
160
161 /*---------------------------------------------------------------------------*/
162
163 /*Interface Descriptor */
164 0x09, /* bLength: Interface Descriptor size */
165 USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
166 /* Interface descriptor type */
167 0x00, /* bInterfaceNumber: Number of Interface */
168 0x00, /* bAlternateSetting: Alternate setting */
169 0x02, /* bNumEndpoints: One endpoints used */
170 0xFF, /* bInterfaceClass: vendor specific */
171 0x00, /* bInterfaceSubClass */
172 0x00, /* bInterfaceProtocol */
173 0x00, /* iInterface: */
174
175 /*Endpoint IN Descriptor*/
176 0x07, /* bLength: Endpoint Descriptor size */
177 USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
178 BULK_IN_EP, /* bEndpointAddress */
179 0x02, /* bmAttributes: Bulk */
180 LOBYTE(BULK_DATA_MAX_PACKET_SIZE), /* wMaxPacketSize: */
181 HIBYTE(BULK_DATA_MAX_PACKET_SIZE),
182 0xFF, /* bInterval: ignore for Bulk transfer */
183
184 /*Endpoint OUT Descriptor*/
185 0x07, /* bLength: Endpoint Descriptor size */
186 USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
187 BULK_OUT_EP, /* bEndpointAddress */
188 0x02, /* bmAttributes: Bulk */
189 LOBYTE(BULK_DATA_MAX_PACKET_SIZE), /* wMaxPacketSize: */
190 HIBYTE(BULK_DATA_MAX_PACKET_SIZE),
191 0xFF /* bInterval: ignore for Bulk transfer */
192} ;
193
194
195/**
196 * @}
197 */
198
199/** @defgroup USBD_Bulk_Private_Functions
200 * @{
201 */
202
203/**
204 * @brief USBD_Bulk_Init
205 * Initialize the Bulk interface
206 * @param pdev: device instance
207 * @param cfgidx: Configuration index
208 * @retval status
209 */
210static uint8_t USBD_Bulk_Init (USBD_HandleTypeDef *pdev,
211 uint8_t cfgidx)
212{
213 uint8_t ret = 0;
214
215 /* Open EP IN */
216 USBD_LL_OpenEP(pdev,
217 BULK_IN_EP,
218 USBD_EP_TYPE_BULK,
219 BULK_DATA_FS_IN_PACKET_SIZE);
220
221 /* Open EP OUT */
222 USBD_LL_OpenEP(pdev,
223 BULK_OUT_EP,
224 USBD_EP_TYPE_BULK,
225 BULK_DATA_FS_OUT_PACKET_SIZE);
226
227 /* Prepare Out endpoint to receive next packet */
229 BULK_OUT_EP,
230 &USB_Rx_Buffer[0],
231 BULK_DATA_FS_OUT_PACKET_SIZE);
232
233 return ret;
234}
235
236/**
237 * @brief USBD_Bulk_DeInit
238 * DeInitialize the Bulk layer
239 * @param pdev: device instance
240 * @param cfgidx: Configuration index
241 * @retval status
242 */
243static uint8_t USBD_Bulk_DeInit (USBD_HandleTypeDef *pdev,
244 uint8_t cfgidx)
245{
246 uint8_t ret = 0;
247
248 /* Close EP IN */
249 USBD_LL_CloseEP(pdev, BULK_IN_EP);
250
251 /* lose EP OUT */
252 USBD_LL_CloseEP(pdev, BULK_OUT_EP);
253
254 return ret;
255}
256
257/**
258 * @brief USBD_Bulk_Setup
259 * Handle the bulk specific requests
260 * @param pdev: instance
261 * @param req: usb requests
262 * @retval status
263 */
264static uint8_t USBD_Bulk_Setup (USBD_HandleTypeDef *pdev,
265 USBD_SetupReqTypedef *req)
266{
267 uint16_t len=USB_BULK_DESC_SIZ;
268 uint8_t *pbuf=USBD_Bulk_CfgFSDesc + 9;
269
270 switch (req->bmRequest & USB_REQ_TYPE_MASK)
271 {
272 case USB_REQ_TYPE_STANDARD:
273 switch (req->bRequest)
274 {
275 case USB_REQ_GET_DESCRIPTOR :
276 if ( (req->wValue >> 8) == BULK_DESCRIPTOR_TYPE)
277 {
278 pbuf = USBD_Bulk_CfgFSDesc + 9 + (9 * USBD_MAX_NUM_INTERFACES);
279 len = MIN(USB_BULK_DESC_SIZ , req->wLength);
280
281 }
282 USBD_CtlSendData (pdev, pbuf, len);
283 break;
284
285 case USB_REQ_GET_INTERFACE :
286 USBD_CtlSendData (pdev,
287 (uint8_t *)&usbd_bulk_AltSet,
288 1);
289 break;
290
291 case USB_REQ_SET_INTERFACE :
292 if ((uint8_t)(req->wValue) < USBD_MAX_NUM_INTERFACES)
293 {
294 usbd_bulk_AltSet = (uint8_t)(req->wValue);
295 }
296 else
297 {
298 /* Call the error management function (command will be nacked */
299 USBD_CtlError (pdev, req);
300 }
301 break;
302 }
303
304 default:
305 break;
306 }
307 return USBD_OK;
308}
309
310/**
311 * @brief USBD_Bulk_DataIn
312 * Data sent on non-control IN endpoint
313 * @param pdev: device instance
314 * @param epnum: endpoint number
315 * @retval status
316 */
317static uint8_t USBD_Bulk_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum)
318{
319 /* endpoint finished the previous transmission so see if more data is left */
321 return USBD_OK;
322}
323
324/**
325 * @brief USBD_Bulk_DataOut
326 * Data received on non-control Out endpoint
327 * @param pdev: device instance
328 * @param epnum: endpoint number
329 * @retval status
330 */
331static uint8_t USBD_Bulk_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum)
332{
333 /* read the data from the bulk OUT pipe */
335 return USBD_OK;
336}
337
338/**
339 * @brief USBD_Bulk_SOF
340 * Start Of Frame event management
341 * @param pdev: device instance
342 * @retval status
343 */
344static uint8_t USBD_Bulk_SOF (USBD_HandleTypeDef *pdev)
345{
346 if (pdev->dev_state == USBD_STATE_CONFIGURED )
347 {
348 /* Check the data to be sent through IN pipe */
350 }
351 return USBD_OK;
352
353}
354
355/**
356 * @brief USBD_Bulk_GetFSCfgDesc
357 * Return configuration descriptor
358 * @param speed : current device speed
359 * @param length : pointer data length
360 * @retval pointer to descriptor buffer
361 */
362static uint8_t *USBD_Bulk_GetFSCfgDesc (uint16_t *length)
363{
364 *length = sizeof (USBD_Bulk_CfgFSDesc);
365 return USBD_Bulk_CfgFSDesc;
366}
367
368/**
369* @brief DeviceQualifierDescriptor
370* return Device Qualifier descriptor
371* @param length : pointer data length
372* @retval pointer to descriptor buffer
373*/
374uint8_t *USBD_Bulk_GetDeviceQualifierDescriptor (uint16_t *length)
375{
376 *length = sizeof (USBD_Bulk_DeviceQualifierDesc);
377 return USBD_Bulk_DeviceQualifierDesc;
378}
379
380/**
381 * @brief USBD_Bulk_GetRxBufferPtr
382 * @retval Pointer to reception buffer.
383 */
385{
386 return &USB_Rx_Buffer[0];
387}
388/**
389 * @}
390 */
391
392/**
393 * @}
394 */
395
396/**
397 * @}
398 */
399
400/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
static uint8_t USBD_Bulk_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
USBD_Bulk_DataIn Data sent on non-control IN endpoint.
Definition usbd_bulk.c:317
static uint8_t USBD_Bulk_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
USBD_Bulk_DataOut Data received on non-control Out endpoint.
Definition usbd_bulk.c:331
static uint8_t * USBD_Bulk_GetFSCfgDesc(uint16_t *length)
USBD_Bulk_GetFSCfgDesc Return configuration descriptor.
Definition usbd_bulk.c:362
static uint8_t USBD_Bulk_SOF(USBD_HandleTypeDef *pdev)
USBD_Bulk_SOF Start Of Frame event management.
Definition usbd_bulk.c:344
static uint8_t USBD_Bulk_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
USBD_Bulk_DeInit DeInitialize the Bulk layer.
Definition usbd_bulk.c:243
void UsbTransmitPipeBulkIN(void)
static uint8_t USBD_Bulk_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
USBD_Bulk_Init Initialize the Bulk interface.
Definition usbd_bulk.c:210
uint8_t * USBD_Bulk_GetDeviceQualifierDescriptor(uint16_t *length)
DeviceQualifierDescriptor return Device Qualifier descriptor.
Definition usbd_bulk.c:374
static uint8_t USBD_Bulk_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
USBD_Bulk_Setup Handle the bulk specific requests.
Definition usbd_bulk.c:264
static __ALIGN_BEGIN uint8_t USBD_Bulk_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END
Definition usbd_bulk.c:100
void UsbReceivePipeBulkOUT(uint8_t epnum)
uint8_t * USBD_Bulk_GetRxBufferPtr(void)
USBD_Bulk_GetRxBufferPtr.
Definition usbd_bulk.c:384
USBD_ClassTypeDef USBD_Bulk
Definition usbd_bulk.c:129
USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)
Prepares an endpoint for reception.
Definition usbd_conf.c:542
USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
Opens an endpoint of the low level driver.
Definition usbd_conf.c:392
USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
Closes an endpoint of the low level driver.
Definition usbd_conf.c:410
header file for the usbd_bulk.c file.
static void uint8_t epnum
Definition usbd_conf.c:104
: Header for usbd_conf.c file.