Skip to content

Commit c8913fd

Browse files
projectgusdpgeorge
authored andcommitted
rp2: Allow enabling USB device without enabling USB-CDC.
Requires changing the USB-CDC stdin/stdout guards from MICROPY_HW_ENABLE_USBDEV to the new (in this port) MICROPY_HW_USB_CDC.
1 parent 2e6e530 commit c8913fd

File tree

6 files changed

+66
-28
lines changed

6 files changed

+66
-28
lines changed

ports/rp2/boards/PICO/mpconfigboard.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
// Board and hardware specific configuration
22
#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico"
33
#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024)
4-
5-
// Enable USB Mass Storage with FatFS filesystem.
6-
// #define MICROPY_HW_USB_MSC (1)

ports/rp2/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ int main(int argc, char **argv) {
8686
#endif
8787

8888
#if MICROPY_HW_ENABLE_USBDEV
89+
#if MICROPY_HW_USB_CDC
8990
bi_decl(bi_program_feature("USB REPL"))
91+
#endif
9092
tusb_init();
9193
#endif
9294

ports/rp2/mpconfigport.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,29 @@
2727
// Options controlling how MicroPython is built, overriding defaults in py/mpconfig.h
2828

2929
#include <stdint.h>
30+
#include "hardware/flash.h"
3031
#include "hardware/spi.h"
3132
#include "hardware/sync.h"
3233
#include "pico/binary_info.h"
3334
#include "pico/multicore.h"
3435
#include "mpconfigboard.h"
35-
#if MICROPY_HW_USB_MSC
36-
#include "hardware/flash.h"
37-
#endif
3836

3937
// Board and hardware specific configuration
4038
#define MICROPY_HW_MCU_NAME "RP2040"
4139
#define MICROPY_HW_ENABLE_UART_REPL (0) // useful if there is no USB
4240
#define MICROPY_HW_ENABLE_USBDEV (1)
4341

42+
#if MICROPY_HW_ENABLE_USBDEV
43+
// Enable USB-CDC serial port
44+
#ifndef MICROPY_HW_USB_CDC
45+
#define MICROPY_HW_USB_CDC (1)
46+
#endif
47+
// Enable USB Mass Storage with FatFS filesystem.
48+
#ifndef MICROPY_HW_USB_MSC
49+
#define MICROPY_HW_USB_MSC (0)
50+
#endif
51+
#endif
52+
4453
#ifndef MICROPY_CONFIG_ROM_LEVEL
4554
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
4655
#endif

ports/rp2/mphalport.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "lib/cyw43-driver/src/cyw43.h"
4040
#endif
4141

42-
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV
42+
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
4343

4444
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
4545
#define MICROPY_HW_STDIN_BUFFER_LEN 512
@@ -50,7 +50,7 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) };
5050

5151
#endif
5252

53-
#if MICROPY_HW_ENABLE_USBDEV
53+
#if MICROPY_HW_USB_CDC
5454

5555
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
5656

@@ -91,10 +91,10 @@ void tud_cdc_rx_cb(uint8_t itf) {
9191

9292
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
9393
uintptr_t ret = 0;
94-
#if MICROPY_HW_ENABLE_USBDEV
94+
#if MICROPY_HW_USB_CDC
9595
poll_cdc_interfaces();
9696
#endif
97-
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV
97+
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
9898
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
9999
ret |= MP_STREAM_POLL_RD;
100100
}
@@ -108,7 +108,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
108108
// Receive single character
109109
int mp_hal_stdin_rx_chr(void) {
110110
for (;;) {
111-
#if MICROPY_HW_ENABLE_USBDEV
111+
#if MICROPY_HW_USB_CDC
112112
poll_cdc_interfaces();
113113
#endif
114114

@@ -132,7 +132,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
132132
mp_uart_write_strn(str, len);
133133
#endif
134134

135-
#if MICROPY_HW_ENABLE_USBDEV
135+
#if MICROPY_HW_USB_CDC
136136
if (tud_cdc_connected()) {
137137
for (size_t i = 0; i < len;) {
138138
uint32_t n = len - i;

shared/tinyusb/mp_usbd_descriptor.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ const uint8_t mp_usbd_desc_cfg_static[USBD_STATIC_DESC_LEN] = {
5454
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_STATIC_MAX, USBD_STR_0, USBD_STATIC_DESC_LEN,
5555
0, USBD_MAX_POWER_MA),
5656

57+
#if CFG_TUD_CDC
5758
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
5859
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
60+
#endif
5961
#if CFG_TUD_MSC
6062
TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
6163
#endif
@@ -83,9 +85,11 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
8385
case USBD_STR_PRODUCT:
8486
desc_str = MICROPY_HW_USB_PRODUCT_FS_STRING;
8587
break;
88+
#if CFG_TUD_CDC
8689
case USBD_STR_CDC:
8790
desc_str = MICROPY_HW_USB_CDC_INTERFACE_STRING;
8891
break;
92+
#endif
8993
#if CFG_TUD_MSC
9094
case USBD_STR_MSC:
9195
desc_str = MICROPY_HW_USB_MSC_INTERFACE_STRING;

shared/tinyusb/tusb_config.h

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,40 @@
4646

4747
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
4848

49+
#if MICROPY_HW_USB_CDC
4950
#define CFG_TUD_CDC (1)
51+
#else
52+
#define CFG_TUD_CDC (0)
53+
#endif
54+
55+
#if MICROPY_HW_USB_MSC
56+
#define CFG_TUD_MSC (1)
57+
#else
58+
#define CFG_TUD_MSC (0)
59+
#endif
60+
61+
// CDC Configuration
62+
#if CFG_TUD_CDC
5063
#define CFG_TUD_CDC_EP_BUFSIZE (256)
5164
#define CFG_TUD_CDC_RX_BUFSIZE (256)
5265
#define CFG_TUD_CDC_TX_BUFSIZE (256)
66+
#endif
5367

5468
// MSC Configuration
5569
#if CFG_TUD_MSC
5670
#ifndef MICROPY_HW_USB_MSC_INTERFACE_STRING
5771
#define MICROPY_HW_USB_MSC_INTERFACE_STRING "Board MSC"
5872
#endif
59-
#define CFG_TUD_MSC (1)
6073
// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg).
61-
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
74+
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
6275
#endif
6376

6477
// Define static descriptor size and interface count based on the above config
6578

66-
#if CFG_TUD_MSC
67-
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
68-
#else
69-
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
70-
#endif
79+
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + \
80+
(CFG_TUD_CDC ? (TUD_CDC_DESC_LEN) : 0) + \
81+
(CFG_TUD_MSC ? (TUD_MSC_DESC_LEN) : 0) \
82+
)
7183

7284
#define USBD_STR_0 (0x00)
7385
#define USBD_STR_MANUF (0x01)
@@ -80,27 +92,41 @@
8092

8193
#define USBD_DESC_STR_MAX (20)
8294

95+
#if CFG_TUD_CDC
8396
#define USBD_ITF_CDC (0) // needs 2 interfaces
84-
8597
#define USBD_CDC_EP_CMD (0x81)
8698
#define USBD_CDC_EP_OUT (0x02)
8799
#define USBD_CDC_EP_IN (0x82)
100+
#endif // CFG_TUD_CDC
88101

89102
#if CFG_TUD_MSC
103+
// Interface & Endpoint numbers for MSC come after CDC, if it is enabled
104+
#if CFG_TUD_CDC
90105
#define USBD_ITF_MSC (2)
91-
#define EPNUM_MSC_OUT (0x03)
92-
#define EPNUM_MSC_IN (0x83)
93-
#endif
106+
#define EPNUM_MSC_OUT (0x03)
107+
#define EPNUM_MSC_IN (0x83)
108+
#else
109+
#define USBD_ITF_MSC (0)
110+
#define EPNUM_MSC_OUT (0x01)
111+
#define EPNUM_MSC_IN (0x81)
112+
#endif // CFG_TUD_CDC
113+
#endif // CFG_TUD_MSC
94114

95115
/* Limits of statically defined USB interfaces, endpoints, strings */
96116
#if CFG_TUD_MSC
97-
#define USBD_ITF_STATIC_MAX (3)
117+
#define USBD_ITF_STATIC_MAX (USBD_ITF_MSC + 1)
98118
#define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1)
99-
#define USBD_EP_STATIC_MAX (0x04) // ignoring the IN EP flag 0x80
100-
#else
101-
#define USBD_ITF_STATIC_MAX (2)
119+
#define USBD_EP_STATIC_MAX (EPNUM_MSC_OUT + 1)
120+
#elif CFG_TUD_CDC
121+
#define USBD_ITF_STATIC_MAX (USBD_ITF_CDC + 1)
102122
#define USBD_STR_STATIC_MAX (USBD_STR_CDC + 1)
103-
#define USBD_EP_STATIC_MAX (0x03) // ignoring the IN EP flag 0x80
123+
#define USBD_EP_STATIC_MAX (((EPNUM_CDC_EP_IN)&~TUSB_DIR_IN_MASK) + 1)
124+
#else // !CFG_TUD_MSC && !CFG_TUD_CDC
125+
#define USBD_ITF_STATIC_MAX (0)
126+
#define USBD_STR_STATIC_MAX (0)
127+
#define USBD_EP_STATIC_MAX (0)
104128
#endif
105129

130+
#endif // MICROPY_HW_ENABLE_USBDEV
131+
106132
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy