Skip to content

Commit eed4eb2

Browse files
Moloriusdpgeorge
authored andcommitted
shared/tinyusb: Create common TinyUSB code for reuse by ports.
This code originates from the rp2 port, and the rp2 port has been updated to use this common code.
1 parent a513558 commit eed4eb2

File tree

8 files changed

+160
-21
lines changed

8 files changed

+160
-21
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ set(MICROPY_SOURCE_LIB
9696
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
9797
${MICROPY_DIR}/shared/runtime/tinyusb_helpers.c
9898
${MICROPY_DIR}/shared/timeutils/timeutils.c
99+
${MICROPY_DIR}/shared/tinyusb/usbd.c
100+
${MICROPY_DIR}/shared/tinyusb/usbd_descriptor.c
99101
)
100102

101103
set(MICROPY_SOURCE_DRIVERS
@@ -125,8 +127,8 @@ set(MICROPY_SOURCE_PORT
125127
pendsv.c
126128
rp2_flash.c
127129
rp2_pio.c
128-
tusb_port.c
129130
uart.c
131+
usbd.c
130132
msc_disk.c
131133
mbedtls/mbedtls_port.c
132134
)
@@ -233,6 +235,11 @@ if(MICROPY_BLUETOOTH_NIMBLE)
233235
list(APPEND MICROPY_INC_CORE ${NIMBLE_INCLUDE})
234236
endif()
235237

238+
# tinyusb helper
239+
target_include_directories(${MICROPY_TARGET} PRIVATE
240+
${MICROPY_DIR}/shared/tinyusb/
241+
)
242+
236243
if (MICROPY_PY_NETWORK_CYW43)
237244
string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/cyw43-driver)
238245
if((NOT (${ECHO_SUBMODULES})) AND NOT EXISTS ${MICROPY_DIR}/lib/cyw43-driver/src/cyw43.h)

ports/rp2/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "modrp2.h"
4444
#include "mpbthciport.h"
4545
#include "genhdr/mpversion.h"
46+
#include "usbd.h"
4647

4748
#include "pico/stdlib.h"
4849
#include "pico/binary_info.h"
@@ -87,6 +88,7 @@ int main(int argc, char **argv) {
8788
#if MICROPY_HW_ENABLE_USBDEV
8889
bi_decl(bi_program_feature("USB REPL"))
8990
tusb_init();
91+
usbd_reset_all(); // run now just in case usb initialization occurs early
9092
#endif
9193

9294
#if MICROPY_PY_THREAD
@@ -159,6 +161,9 @@ int main(int argc, char **argv) {
159161
machine_pin_init();
160162
rp2_pio_init();
161163
machine_i2s_init0();
164+
#if MICROPY_HW_ENABLE_USBDEV
165+
usbd_reset_all();
166+
#endif
162167

163168
#if MICROPY_PY_BLUETOOTH
164169
mp_bluetooth_hci_init();

ports/rp2/mpconfigport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k;
198198

199199
// Miscellaneous settings
200200

201+
#ifndef MICROPY_HW_USB_VID
202+
#define MICROPY_HW_USB_VID (0x2E8A) // Raspberry Pi
203+
#endif
204+
#ifndef MICROPY_HW_USB_PID
205+
#define MICROPY_HW_USB_PID (0x0005) // RP2 MicroPython
206+
#endif
207+
201208
// Entering a critical section.
202209
extern uint32_t mp_thread_begin_atomic_section(void);
203210
extern void mp_thread_end_atomic_section(uint32_t);

ports/rp2/usbd.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Blake W. Felt
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 "usbd.h"
28+
#include "string.h"
29+
#include "pico/unique_id.h"
30+
31+
int usbd_serialnumber(uint8_t *buf) {
32+
pico_unique_board_id_t id;
33+
const int len = 8;
34+
35+
pico_get_unique_board_id(&id);
36+
memcpy(buf, id.id, len);
37+
38+
return len;
39+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
* THE SOFTWARE.
2323
*
2424
*/
25-
#ifndef MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
26-
#define MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
2725

26+
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H
27+
#define MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H
28+
29+
#include <py/mpconfig.h>
2830
#include "mpconfigport.h"
2931

3032
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
@@ -41,4 +43,4 @@
4143
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
4244
#endif
4345

44-
#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
46+
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H

shared/tinyusb/usbd.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Blake W. Felt
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 "py/runtime.h"
28+
#include "usbd.h"
29+
30+
void usbd_reset_all(void) {
31+
usbd_reset_descriptor();
32+
}

shared/tinyusb/usbd.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Blake W. Felt
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+
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
28+
#define MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
29+
30+
#include "py/obj.h"
31+
32+
// defined externally (needed per port)
33+
34+
int usbd_serialnumber(uint8_t *buf);
35+
36+
// external use
37+
38+
void usbd_reset_all(void);
39+
void usbd_reset_descriptor(void);
40+
41+
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2019 Damien P. George
7+
* Copyright (c) 2022 Blake W. Felt
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -24,15 +25,9 @@
2425
* THE SOFTWARE.
2526
*/
2627

28+
#include "mpconfigport.h"
2729
#include "tusb.h"
28-
#include "pico/unique_id.h"
29-
30-
#ifndef MICROPY_HW_USB_VID
31-
#define MICROPY_HW_USB_VID (0x2E8A) // Raspberry Pi
32-
#endif
33-
#ifndef MICROPY_HW_USB_PID
34-
#define MICROPY_HW_USB_PID (0x0005) // RP2 MicroPython
35-
#endif
30+
#include "usbd.h"
3631

3732
#if CFG_TUD_MSC
3833
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
@@ -65,6 +60,9 @@
6560
#define USBD_STR_CDC (0x04)
6661
#define USBD_STR_MSC (0x05)
6762

63+
#define USBD_DESC_STR_MAX (20)
64+
#define USBD_DESC_SERIAL_MAX (32)
65+
6866
// Note: descriptors returned from callbacks must exist long enough for transfer to complete
6967

7068
static const tusb_desc_device_t usbd_desc_device = {
@@ -115,8 +113,7 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
115113
}
116114

117115
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
118-
#define DESC_STR_MAX (20)
119-
static uint16_t desc_str[DESC_STR_MAX];
116+
static uint16_t desc_str[USBD_DESC_STR_MAX];
120117

121118
uint8_t len;
122119
if (index == 0) {
@@ -128,17 +125,22 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
128125
}
129126
// check, if serial is requested
130127
if (index == USBD_STR_SERIAL) {
131-
pico_unique_board_id_t id;
132-
pico_get_unique_board_id(&id);
128+
uint8_t buffer[USBD_DESC_SERIAL_MAX] = {0};
129+
int buflen;
130+
const char *hexdig = "0123456789abcdef";
131+
132+
buflen = usbd_serialnumber(buffer);
133133
// byte by byte conversion
134-
for (len = 0; len < 16; len += 2) {
135-
const char *hexdig = "0123456789abcdef";
136-
desc_str[1 + len] = hexdig[id.id[len >> 1] >> 4];
137-
desc_str[1 + len + 1] = hexdig[id.id[len >> 1] & 0x0f];
134+
len = 0;
135+
for (int i=0; i<buflen; i++) {
136+
uint8_t val = buffer[i];
137+
desc_str[1 + len] = hexdig[val >> 4];
138+
desc_str[2 + len] = hexdig[val & 0x0F];
139+
len += 2;
138140
}
139141
} else {
140142
const char *str = usbd_desc_str[index];
141-
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
143+
for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) {
142144
desc_str[1 + len] = str[len];
143145
}
144146
}
@@ -149,3 +151,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
149151

150152
return desc_str;
151153
}
154+
155+
void usbd_reset_descriptor(void) {
156+
// not used yet
157+
}

0 commit comments

Comments
 (0)