]> mj.ucw.cz Git - home-hw.git/blob - Src/usb_device.c
Experiments with USB
[home-hw.git] / Src / usb_device.c
1 /**
2   ******************************************************************************
3   * @file           : usb_device.c
4   * @version        : v2.0_Cube
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.
12   *
13   * Copyright (c) 2018 STMicroelectronics International N.V. 
14   * All rights reserved.
15   *
16   * Redistribution and use in source and binary forms, with or without 
17   * modification, are permitted, provided that the following conditions are met:
18   *
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 
32   *    this license. 
33   *
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.
46   *
47   ******************************************************************************
48   */
49
50 /* Includes ------------------------------------------------------------------*/
51
52 #include "usb_device.h"
53 #include "usbd_core.h"
54 #include "usbd_desc.h"
55 #include "usbd_dfu.h"
56 #include "usbd_dfu_if.h"
57
58 /* USER CODE BEGIN Includes */
59
60 /* USER CODE END Includes */
61
62 /* USER CODE BEGIN PV */
63 /* Private variables ---------------------------------------------------------*/
64
65 /* USER CODE END PV */
66
67 /* USER CODE BEGIN PFP */
68 /* Private function prototypes -----------------------------------------------*/
69
70 /* USER CODE END PFP */
71
72 /* USB Device Core handle declaration. */
73 USBD_HandleTypeDef hUsbDeviceFS;
74
75 /*
76  * -- Insert your variables declaration here --
77  */
78 /* USER CODE BEGIN 0 */
79
80 /* USER CODE END 0 */
81
82 /*
83  * -- Insert your external function declaration here --
84  */
85 /* USER CODE BEGIN 1 */
86
87 static uint8_t xxx_rx_buf[64];
88
89 static uint8_t xxx_init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
90 {
91   USBD_LL_OpenEP(pdev, 0x01, USBD_EP_TYPE_BULK, 64);
92   pdev->ep_out[1].maxpacket = 64;
93
94   USBD_LL_OpenEP(pdev, 0x81, USBD_EP_TYPE_BULK, 64);
95   pdev->ep_in[1].maxpacket = 64;
96
97   // USBD_LL_PrepareReceive(pdev, 0x01, xxx_rx_buf, 64);
98   USBD_LL_Transmit(pdev, 0x81, "brum", 4);
99
100   return USBD_OK;
101 }
102
103 static uint8_t xxx_deinit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
104 {
105   USBD_LL_CloseEP(pdev, 0x01);
106   USBD_LL_CloseEP(pdev, 0x81);
107   return USBD_OK;
108 }
109
110 static uint8_t xxx_zero;
111
112 static uint8_t xxx_setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
113 {
114   switch (req->bmRequest & USB_REQ_TYPE_MASK)
115     {
116     case USB_REQ_TYPE_STANDARD:
117       switch (req->bRequest)
118         {
119         case USB_REQ_GET_DESCRIPTOR:
120           {
121             USBD_CtlSendData(pdev, NULL, 0);
122             return USBD_OK;
123           }
124         case USB_REQ_GET_INTERFACE:
125           {
126             USBD_CtlSendData(pdev, &xxx_zero, 1);
127             return USBD_OK;
128           }
129         case USB_REQ_SET_INTERFACE:
130           if ((uint8_t) req->wValue == 0)
131             return USBD_OK;
132           break;
133         case USB_REQ_SET_FEATURE:
134         case USB_REQ_CLEAR_FEATURE:
135           // Handled by USBD, we are only notified
136           return USBD_OK;
137         }
138       break;
139     }
140
141   USBD_CtlError(pdev, req);
142   return USBD_FAIL;
143 }
144
145 static uint8_t xxx_ep0_rx_ready(USBD_HandleTypeDef *pdev)
146 {
147   return USBD_OK;
148 }
149
150 static uint8_t xxx_ep0_tx_ready(USBD_HandleTypeDef *pdev)
151 {
152   return USBD_OK;
153 }
154
155 static uint8_t xxx_data_in(USBD_HandleTypeDef *pdev, uint8_t epnum)
156 {
157   USBD_EndpointTypeDef *pep = &pdev->ep_in[1];
158   return USBD_OK;
159 }
160
161 static uint8_t xxx_data_out(USBD_HandleTypeDef *pdev, uint8_t epnum)
162 {
163   USBD_EndpointTypeDef *pep = &pdev->ep_out[1];
164   unsigned int len = USBD_LL_GetRxDataSize(pdev, 0x01);
165   return USBD_OK;
166 }
167
168 static uint8_t xxx_sof(USBD_HandleTypeDef *pdev)
169 {
170   return USBD_OK;
171 }
172
173 static uint8_t xxx_iso_in_incomplete(USBD_HandleTypeDef *pdev, uint8_t epnum)
174 {
175   return USBD_OK;
176 }
177
178 static uint8_t xxx_iso_out_incomplete(USBD_HandleTypeDef *pdev, uint8_t epnum)
179 {
180   return USBD_OK;
181 }
182
183 // FIXME: constify
184 static uint8_t xxx_cfg_desc[] = {
185   // Configuration descriptor
186   9,                            // bLength
187   USB_DESC_TYPE_CONFIGURATION,  // bDescriptorType
188   32,                           // wTotalLength
189   0,
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
196   9,                            // bLength
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
206   7,                            // bLength
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
213   7,                            // bLength
214   USB_DESC_TYPE_ENDPOINT,       // bDescriptorType
215   0x81,                         // bEndpointAddress
216   USBD_EP_TYPE_BULK,            // bmAttributes
217   0x40, 0x00,                   // wMaxPacketSize
218   0x00,                         // bInterval: unused
219 };
220
221 static uint8_t *xxx_get_cfg_desc(uint16_t *len)
222 {
223   *len = sizeof(xxx_cfg_desc);
224   return xxx_cfg_desc;
225 }
226
227 // FIXME: constify
228 static uint8_t xxx_qual_desc[] = {
229   10,                           // bLength
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
237   0x00,                         // bReserved
238 };
239
240 static uint8_t *xxx_get_dev_qual_desc(uint16_t *len)
241 {
242   *len = sizeof(xxx_qual_desc);
243   return xxx_qual_desc;
244 }
245
246 static uint8_t *xxx_get_usr_string_desc(USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *len)
247 {
248   *len = 0;
249   return NULL;
250 }
251
252 USBD_ClassTypeDef xxx_class = {
253   xxx_init,
254   xxx_deinit,
255   xxx_setup,
256   xxx_ep0_tx_ready,
257   xxx_ep0_rx_ready,
258   xxx_data_in,
259   xxx_data_out,
260   xxx_sof,
261   xxx_iso_in_incomplete,
262   xxx_iso_out_incomplete,
263   xxx_get_cfg_desc,
264   xxx_get_cfg_desc,
265   xxx_get_cfg_desc,
266   xxx_get_dev_qual_desc,
267 #if USBD_SUPPORT_USER_STRING == 1
268   xxx_get_usr_string_desc,
269 #endif
270 };
271
272 /* USER CODE END 1 */
273
274 /**
275   * Init USB device Library, add supported class and start the library
276   * @retval None
277   */
278 void MX_USB_DEVICE_Init(void)
279 {
280   /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
281   
282   /* USER CODE END USB_DEVICE_Init_PreTreatment */
283   
284   /* Init Device Library, add supported class and start the library. */
285   USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
286
287 #if 0
288   USBD_RegisterClass(&hUsbDeviceFS, &USBD_DFU);
289
290   USBD_DFU_RegisterMedia(&hUsbDeviceFS, &USBD_DFU_fops_FS);
291 #else
292   USBD_RegisterClass(&hUsbDeviceFS, &xxx_class);
293 #endif
294
295   USBD_Start(&hUsbDeviceFS);
296
297   /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
298   
299   /* USER CODE END USB_DEVICE_Init_PostTreatment */
300 }
301
302 /**
303   * @}
304   */
305
306 /**
307   * @}
308   */
309
310 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/