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