1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2019 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include "tusb.h"
28 
29 #ifndef MICROPY_HW_USB_VID
30 #define MICROPY_HW_USB_VID (0xf055)
31 #define MICROPY_HW_USB_PID (0x9802)
32 #endif
33 
34 #define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
35 #define USBD_MAX_POWER_MA (250)
36 
37 #define USBD_ITF_CDC (0) // needs 2 interfaces
38 #define USBD_ITF_MAX (2)
39 
40 #define USBD_CDC_EP_CMD (0x81)
41 #define USBD_CDC_EP_OUT (0x02)
42 #define USBD_CDC_EP_IN (0x82)
43 #define USBD_CDC_CMD_MAX_SIZE (8)
44 #define USBD_CDC_IN_OUT_MAX_SIZE (512)
45 
46 #define USBD_STR_0 (0x00)
47 #define USBD_STR_MANUF (0x01)
48 #define USBD_STR_PRODUCT (0x02)
49 #define USBD_STR_SERIAL (0x03)
50 #define USBD_STR_CDC (0x04)
51 
52 // Note: descriptors returned from callbacks must exist long enough for transfer to complete
53 
54 static const tusb_desc_device_t usbd_desc_device = {
55     .bLength = sizeof(tusb_desc_device_t),
56     .bDescriptorType = TUSB_DESC_DEVICE,
57     .bcdUSB = 0x0200,
58     .bDeviceClass = TUSB_CLASS_MISC,
59     .bDeviceSubClass = MISC_SUBCLASS_COMMON,
60     .bDeviceProtocol = MISC_PROTOCOL_IAD,
61     .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
62     .idVendor = MICROPY_HW_USB_VID,
63     .idProduct = MICROPY_HW_USB_PID,
64     .bcdDevice = 0x0100,
65     .iManufacturer = USBD_STR_MANUF,
66     .iProduct = USBD_STR_PRODUCT,
67     .iSerialNumber = USBD_STR_SERIAL,
68     .bNumConfigurations = 1,
69 };
70 
71 static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
72     TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN,
73         TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA),
74 
75     TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
76         USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
77 };
78 
79 static const char *const usbd_desc_str[] = {
80     [USBD_STR_MANUF] = "MicroPython",
81     [USBD_STR_PRODUCT] = "Board in FS mode", // Todo: fix string to indicate that product is running in High Speed mode
82     [USBD_STR_SERIAL] = "000000000000", // TODO
83     [USBD_STR_CDC] = "Board CDC",
84 };
85 
tud_descriptor_device_cb(void)86 const uint8_t *tud_descriptor_device_cb(void) {
87     return (const uint8_t *)&usbd_desc_device;
88 }
89 
tud_descriptor_configuration_cb(uint8_t index)90 const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
91     (void)index;
92     return usbd_desc_cfg;
93 }
94 
tud_descriptor_string_cb(uint8_t index,uint16_t langid)95 const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
96     #define DESC_STR_MAX (20)
97     static uint16_t desc_str[DESC_STR_MAX];
98 
99     uint8_t len;
100     if (index == 0) {
101         desc_str[1] = 0x0409; // supported language is English
102         len = 1;
103     } else {
104         if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
105             return NULL;
106         }
107         const char *str = usbd_desc_str[index];
108         for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
109             desc_str[1 + len] = str[len];
110         }
111     }
112 
113     // first byte is length (including header), second byte is string type
114     desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
115 
116     return desc_str;
117 }
118