Skip to content

Commit 0ee4ccb

Browse files
committed
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 d518a1b commit 0ee4ccb

File tree

6 files changed

+76
-32
lines changed

6 files changed

+76
-32
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: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,26 @@
3232
#include "pico/binary_info.h"
3333
#include "pico/multicore.h"
3434
#include "mpconfigboard.h"
35-
#if MICROPY_HW_USB_MSC
36-
#include "hardware/flash.h"
37-
#endif
3835

3936
// Board and hardware specific configuration
4037
#define MICROPY_HW_MCU_NAME "RP2040"
41-
#define MICROPY_HW_ENABLE_UART_REPL (0) // useful if there is no USB
42-
#define MICROPY_HW_ENABLE_USBDEV (1)
38+
#define MICROPY_HW_ENABLE_UART_REPL (1) // useful if there is no USB
39+
#define MICROPY_HW_ENABLE_USBDEV (1)
40+
41+
#if MICROPY_HW_ENABLE_USBDEV
42+
// Enable USB-CDC serial port
43+
#ifndef MICROPY_HW_USB_CDC
44+
#define MICROPY_HW_USB_CDC (1)
45+
#endif
46+
// Enable USB Mass Storage with FatFS filesystem.
47+
#ifndef MICROPY_HW_USB_MSC
48+
#define MICROPY_HW_USB_MSC (0)
49+
#endif
50+
#endif
51+
52+
#if MICROPY_HW_USB_MSC
53+
#include "hardware/flash.h"
54+
#endif
4355

4456
#ifndef MICROPY_CONFIG_ROM_LEVEL
4557
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)

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/tusb_config.h

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <py/mpconfig.h>
3131
#include "mpconfigport.h"
3232

33+
#if MICROPY_HW_ENABLE_USBDEV
34+
3335
#ifndef MICROPY_HW_USB_STR_MANUF
3436
#define MICROPY_HW_USB_STR_MANUF "MicroPython"
3537
#endif
@@ -44,28 +46,41 @@
4446

4547
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
4648

49+
#if MICROPY_HW_USB_CDC
4750
#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
4863
#define CFG_TUD_CDC_EP_BUFSIZE (256)
4964
#define CFG_TUD_CDC_RX_BUFSIZE (256)
5065
#define CFG_TUD_CDC_TX_BUFSIZE (256)
66+
#endif
5167

52-
#if MICROPY_HW_USB_MSC
53-
// Board and hardware specific configuration
68+
// MSC Configuration
69+
#if CFG_TUD_MSC
5470
#ifndef MICROPY_HW_USB_STR_MSC
5571
#define MICROPY_HW_USB_STR_MSC "Board MSC"
5672
#endif
57-
#define CFG_TUD_MSC (1)
5873
// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg).
59-
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
74+
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
6075
#endif
6176

77+
6278
// Define static descriptor size and interface count based on the above config
6379

64-
#if CFG_TUD_MSC
65-
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
66-
#else
67-
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
68-
#endif
80+
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + \
81+
(CFG_TUD_CDC ? (TUD_CDC_DESC_LEN) : 0) + \
82+
(CFG_TUD_MSC ? (TUD_MSC_DESC_LEN) : 0) \
83+
)
6984

7085
#define USBD_STR_0 (0x00)
7186
#define USBD_STR_MANUF (0x01)
@@ -78,27 +93,41 @@
7893

7994
#define USBD_DESC_STR_MAX (20)
8095

96+
#if CFG_TUD_CDC
8197
#define USBD_ITF_CDC (0) // needs 2 interfaces
82-
8398
#define USBD_CDC_EP_CMD (0x81)
8499
#define USBD_CDC_EP_OUT (0x02)
85100
#define USBD_CDC_EP_IN (0x82)
101+
#endif // CFG_TUD_CDC
86102

87103
#if CFG_TUD_MSC
104+
// Interface & Endpoint numbers for MSC come after CDC, if it is enabled
105+
#if CFG_TUD_CDC
88106
#define USBD_ITF_MSC (2)
89-
#define EPNUM_MSC_OUT (0x03)
90-
#define EPNUM_MSC_IN (0x83)
91-
#endif
107+
#define EPNUM_MSC_OUT (0x03)
108+
#define EPNUM_MSC_IN (0x83)
109+
#else
110+
#define USBD_ITF_MSC (0)
111+
#define EPNUM_MSC_OUT (0x01)
112+
#define EPNUM_MSC_IN (0x81)
113+
#endif // CFG_TUD_CDC
114+
#endif // CFG_TUD_MSC
92115

93116
/* Limits of statically defined USB interfaces, endpoints, strings */
94117
#if CFG_TUD_MSC
95-
#define USBD_ITF_STATIC_MAX (3)
118+
#define USBD_ITF_STATIC_MAX (USBD_ITF_MSC + 1)
96119
#define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1)
97-
#define USBD_EP_STATIC_MAX (0x04) // ignoring the IN EP flag 0x80
98-
#else
99-
#define USBD_ITF_STATIC_MAX (2)
120+
#define USBD_EP_STATIC_MAX (EPNUM_MSC_OUT + 1)
121+
#elif CFG_TUD_CDC
122+
#define USBD_ITF_STATIC_MAX (USBD_ITF_CDC + 1)
100123
#define USBD_STR_STATIC_MAX (USBD_STR_CDC + 1)
101-
#define USBD_EP_STATIC_MAX (0x03) // ignoring the IN EP flag 0x80
124+
#define USBD_EP_STATIC_MAX (((EPNUM_CDC_EP_IN)&~TUSB_DIR_IN_MASK) + 1)
125+
#else // !CFG_TUD_MSC && !CFG_TUD_CDC
126+
#define USBD_ITF_STATIC_MAX (0)
127+
#define USBD_STR_STATIC_MAX (0)
128+
#define USBD_EP_STATIC_MAX (0)
102129
#endif
103130

131+
#endif // MICROPY_HW_ENABLE_USBDEV
132+
104133
#endif // MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H

shared/tinyusb/usbd_descriptor.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ const uint8_t 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_STR_PRODUCT;
8587
break;
88+
#if CFG_TUD_CDC
8689
case USBD_STR_CDC:
8790
desc_str = MICROPY_HW_USB_STR_CDC;
8891
break;
92+
#endif
8993
#if CFG_TUD_MSC
9094
case USBD_STR_MSC:
9195
desc_str = MICROPY_HW_USB_STR_MSC;

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