2 ******************************************************************************
5 * @brief : This file implements the USB Device
6 ******************************************************************************
7 * This notice applies to any and all portions of this file
8 * that are not between comment pairs USER CODE BEGIN and
9 * USER CODE END. Other portions of this file, whether
10 * inserted by the user or by software development tools
11 * are owned by their respective copyright owners.
13 * Copyright (c) 2018 STMicroelectronics International N.V.
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted, provided that the following conditions are met:
19 * 1. Redistribution of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 * 3. Neither the name of STMicroelectronics nor the names of other
25 * contributors to this software may be used to endorse or promote products
26 * derived from this software without specific written permission.
27 * 4. This software, including modifications and/or derivative works of this
28 * software, must execute solely and exclusively on microcontroller or
29 * microprocessor devices manufactured by or for STMicroelectronics.
30 * 5. Redistribution and use of this software other than as permitted under
31 * this license is void and will automatically terminate your rights under
34 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
35 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
37 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
38 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
39 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
42 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 ******************************************************************************
50 /* Includes ------------------------------------------------------------------*/
52 #include "usb_device.h"
53 #include "usbd_core.h"
54 #include "usbd_desc.h"
56 #include "usbd_dfu_if.h"
58 /* USER CODE BEGIN Includes */
60 /* USER CODE END Includes */
62 /* USER CODE BEGIN PV */
63 /* Private variables ---------------------------------------------------------*/
65 /* USER CODE END PV */
67 /* USER CODE BEGIN PFP */
68 /* Private function prototypes -----------------------------------------------*/
70 /* USER CODE END PFP */
72 /* USB Device Core handle declaration. */
73 USBD_HandleTypeDef hUsbDeviceFS;
76 * -- Insert your variables declaration here --
78 /* USER CODE BEGIN 0 */
83 * -- Insert your external function declaration here --
85 /* USER CODE BEGIN 1 */
87 static uint8_t xxx_rx_buf[64];
89 static uint8_t xxx_init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
91 USBD_LL_OpenEP(pdev, 0x01, USBD_EP_TYPE_BULK, 64);
92 pdev->ep_out[1].maxpacket = 64;
94 USBD_LL_OpenEP(pdev, 0x81, USBD_EP_TYPE_BULK, 64);
95 pdev->ep_in[1].maxpacket = 64;
97 // USBD_LL_PrepareReceive(pdev, 0x01, xxx_rx_buf, 64);
98 USBD_LL_Transmit(pdev, 0x81, "brum", 4);
103 static uint8_t xxx_deinit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
105 USBD_LL_CloseEP(pdev, 0x01);
106 USBD_LL_CloseEP(pdev, 0x81);
110 static uint8_t xxx_zero;
112 static uint8_t xxx_setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
114 switch (req->bmRequest & USB_REQ_TYPE_MASK)
116 case USB_REQ_TYPE_STANDARD:
117 switch (req->bRequest)
119 case USB_REQ_GET_DESCRIPTOR:
121 USBD_CtlSendData(pdev, NULL, 0);
124 case USB_REQ_GET_INTERFACE:
126 USBD_CtlSendData(pdev, &xxx_zero, 1);
129 case USB_REQ_SET_INTERFACE:
130 if ((uint8_t) req->wValue == 0)
133 case USB_REQ_SET_FEATURE:
134 case USB_REQ_CLEAR_FEATURE:
135 // Handled by USBD, we are only notified
141 USBD_CtlError(pdev, req);
145 static uint8_t xxx_ep0_rx_ready(USBD_HandleTypeDef *pdev)
150 static uint8_t xxx_ep0_tx_ready(USBD_HandleTypeDef *pdev)
155 static uint8_t xxx_data_in(USBD_HandleTypeDef *pdev, uint8_t epnum)
157 USBD_EndpointTypeDef *pep = &pdev->ep_in[1];
161 static uint8_t xxx_data_out(USBD_HandleTypeDef *pdev, uint8_t epnum)
163 USBD_EndpointTypeDef *pep = &pdev->ep_out[1];
164 unsigned int len = USBD_LL_GetRxDataSize(pdev, 0x01);
168 static uint8_t xxx_sof(USBD_HandleTypeDef *pdev)
173 static uint8_t xxx_iso_in_incomplete(USBD_HandleTypeDef *pdev, uint8_t epnum)
178 static uint8_t xxx_iso_out_incomplete(USBD_HandleTypeDef *pdev, uint8_t epnum)
184 static uint8_t xxx_cfg_desc[] = {
185 // Configuration descriptor
187 USB_DESC_TYPE_CONFIGURATION, // bDescriptorType
190 0x01, // bNumInterfaces
191 0x01, // bConfigurationValue
192 USBD_IDX_CONFIG_STR, // iConfiguration: index of string descriptor
193 0xc0, // bmAttributes: bus-powered, supports remote wakeup
194 0x32, // Max power: 100 mA
195 // Interface descriptor
197 USB_DESC_TYPE_INTERFACE, // bDescriptorType
198 0x00, // bInterfaceNumber
199 0x00, // bAlternateSetting
200 0x01, // bNumEndpoints
201 0xff, // bInterfaceClass: vendor-defined
202 0x00, // bInterfaceSubClass
203 0x00, // nInterfaceProtocol
204 USBD_IDX_INTERFACE_STR, // iInterface: index of string descriptor
205 // End-point descriptor
207 USB_DESC_TYPE_ENDPOINT, // bDescriptorType
208 0x01, // bEndpointAddress
209 USBD_EP_TYPE_BULK, // bmAttributes
210 0x40, 0x00, // wMaxPacketSize
211 0x00, // bInterval: unused
212 // End-point descriptor
214 USB_DESC_TYPE_ENDPOINT, // bDescriptorType
215 0x81, // bEndpointAddress
216 USBD_EP_TYPE_BULK, // bmAttributes
217 0x40, 0x00, // wMaxPacketSize
218 0x00, // bInterval: unused
221 static uint8_t *xxx_get_cfg_desc(uint16_t *len)
223 *len = sizeof(xxx_cfg_desc);
228 static uint8_t xxx_qual_desc[] = {
230 USB_DESC_TYPE_DEVICE_QUALIFIER, // bDescriptorType
231 0x00, 0x02, // bcdUSB
232 0x00, // bDeviceClass
233 0x00, // bDeviceSubClass
234 0x00, // bDeviceProtocol
235 0x40, // bMaxPacketSize0
236 0x01, // bNumConfigurations
240 static uint8_t *xxx_get_dev_qual_desc(uint16_t *len)
242 *len = sizeof(xxx_qual_desc);
243 return xxx_qual_desc;
246 static uint8_t *xxx_get_usr_string_desc(USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *len)
252 USBD_ClassTypeDef xxx_class = {
261 xxx_iso_in_incomplete,
262 xxx_iso_out_incomplete,
266 xxx_get_dev_qual_desc,
267 #if USBD_SUPPORT_USER_STRING == 1
268 xxx_get_usr_string_desc,
272 /* USER CODE END 1 */
275 * Init USB device Library, add supported class and start the library
278 void MX_USB_DEVICE_Init(void)
280 /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
282 /* USER CODE END USB_DEVICE_Init_PreTreatment */
284 /* Init Device Library, add supported class and start the library. */
285 USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
288 USBD_RegisterClass(&hUsbDeviceFS, &USBD_DFU);
290 USBD_DFU_RegisterMedia(&hUsbDeviceFS, &USBD_DFU_fops_FS);
292 USBD_RegisterClass(&hUsbDeviceFS, &xxx_class);
295 USBD_Start(&hUsbDeviceFS);
297 /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
299 /* USER CODE END USB_DEVICE_Init_PostTreatment */
310 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/