Skip to content

Commit

Permalink
rp2: Allow enabling USB device without enabling USB-CDC.
Browse files Browse the repository at this point in the history
Requires changing the USB-CDC stdin/stdout guards from
MICROPY_HW_ENABLE_USBDEV to the new (in this port)
MICROPY_HW_USB_CDC.
  • Loading branch information
projectgus committed Nov 2, 2022
1 parent d518a1b commit 0ee4ccb
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 32 deletions.
3 changes: 0 additions & 3 deletions ports/rp2/boards/PICO/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Board and hardware specific configuration
#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico"
#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024)

// Enable USB Mass Storage with FatFS filesystem.
// #define MICROPY_HW_USB_MSC (1)
2 changes: 2 additions & 0 deletions ports/rp2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ int main(int argc, char **argv) {
#endif

#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
bi_decl(bi_program_feature("USB REPL"))
#endif
tusb_init();
#endif

Expand Down
22 changes: 17 additions & 5 deletions ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,26 @@
#include "pico/binary_info.h"
#include "pico/multicore.h"
#include "mpconfigboard.h"
#if MICROPY_HW_USB_MSC
#include "hardware/flash.h"
#endif

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

#if MICROPY_HW_ENABLE_USBDEV
// Enable USB-CDC serial port
#ifndef MICROPY_HW_USB_CDC
#define MICROPY_HW_USB_CDC (1)
#endif
// Enable USB Mass Storage with FatFS filesystem.
#ifndef MICROPY_HW_USB_MSC
#define MICROPY_HW_USB_MSC (0)
#endif
#endif

#if MICROPY_HW_USB_MSC
#include "hardware/flash.h"
#endif

#ifndef MICROPY_CONFIG_ROM_LEVEL
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
Expand Down
12 changes: 6 additions & 6 deletions ports/rp2/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "lib/cyw43-driver/src/cyw43.h"
#endif

#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC

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

#endif

#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC

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

Expand Down Expand Up @@ -91,10 +91,10 @@ void tud_cdc_rx_cb(uint8_t itf) {

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
poll_cdc_interfaces();
#endif
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
ret |= MP_STREAM_POLL_RD;
}
Expand All @@ -108,7 +108,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
// Receive single character
int mp_hal_stdin_rx_chr(void) {
for (;;) {
#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
poll_cdc_interfaces();
#endif

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

#if MICROPY_HW_ENABLE_USBDEV
#if MICROPY_HW_USB_CDC
if (tud_cdc_connected()) {
for (size_t i = 0; i < len;) {
uint32_t n = len - i;
Expand Down
65 changes: 47 additions & 18 deletions shared/tinyusb/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <py/mpconfig.h>
#include "mpconfigport.h"

#if MICROPY_HW_ENABLE_USBDEV

#ifndef MICROPY_HW_USB_STR_MANUF
#define MICROPY_HW_USB_STR_MANUF "MicroPython"
#endif
Expand All @@ -44,28 +46,41 @@

#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)

#if MICROPY_HW_USB_CDC
#define CFG_TUD_CDC (1)
#else
#define CFG_TUD_CDC (0)
#endif

#if MICROPY_HW_USB_MSC
#define CFG_TUD_MSC (1)
#else
#define CFG_TUD_MSC (0)
#endif

// CDC Configuration
#if CFG_TUD_CDC
#define CFG_TUD_CDC_EP_BUFSIZE (256)
#define CFG_TUD_CDC_RX_BUFSIZE (256)
#define CFG_TUD_CDC_TX_BUFSIZE (256)
#endif

#if MICROPY_HW_USB_MSC
// Board and hardware specific configuration
// MSC Configuration
#if CFG_TUD_MSC
#ifndef MICROPY_HW_USB_STR_MSC
#define MICROPY_HW_USB_STR_MSC "Board MSC"
#endif
#define CFG_TUD_MSC (1)
// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg).
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
#endif


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

#if CFG_TUD_MSC
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
#else
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
#endif
#define USBD_STATIC_DESC_LEN (TUD_CONFIG_DESC_LEN + \
(CFG_TUD_CDC ? (TUD_CDC_DESC_LEN) : 0) + \
(CFG_TUD_MSC ? (TUD_MSC_DESC_LEN) : 0) \
)

#define USBD_STR_0 (0x00)
#define USBD_STR_MANUF (0x01)
Expand All @@ -78,27 +93,41 @@

#define USBD_DESC_STR_MAX (20)

#if CFG_TUD_CDC
#define USBD_ITF_CDC (0) // needs 2 interfaces

#define USBD_CDC_EP_CMD (0x81)
#define USBD_CDC_EP_OUT (0x02)
#define USBD_CDC_EP_IN (0x82)
#endif // CFG_TUD_CDC

#if CFG_TUD_MSC
// Interface & Endpoint numbers for MSC come after CDC, if it is enabled
#if CFG_TUD_CDC
#define USBD_ITF_MSC (2)
#define EPNUM_MSC_OUT (0x03)
#define EPNUM_MSC_IN (0x83)
#endif
#define EPNUM_MSC_OUT (0x03)
#define EPNUM_MSC_IN (0x83)
#else
#define USBD_ITF_MSC (0)
#define EPNUM_MSC_OUT (0x01)
#define EPNUM_MSC_IN (0x81)
#endif // CFG_TUD_CDC
#endif // CFG_TUD_MSC

/* Limits of statically defined USB interfaces, endpoints, strings */
#if CFG_TUD_MSC
#define USBD_ITF_STATIC_MAX (3)
#define USBD_ITF_STATIC_MAX (USBD_ITF_MSC + 1)
#define USBD_STR_STATIC_MAX (USBD_STR_MSC + 1)
#define USBD_EP_STATIC_MAX (0x04) // ignoring the IN EP flag 0x80
#else
#define USBD_ITF_STATIC_MAX (2)
#define USBD_EP_STATIC_MAX (EPNUM_MSC_OUT + 1)
#elif CFG_TUD_CDC
#define USBD_ITF_STATIC_MAX (USBD_ITF_CDC + 1)
#define USBD_STR_STATIC_MAX (USBD_STR_CDC + 1)
#define USBD_EP_STATIC_MAX (0x03) // ignoring the IN EP flag 0x80
#define USBD_EP_STATIC_MAX (((EPNUM_CDC_EP_IN)&~TUSB_DIR_IN_MASK) + 1)
#else // !CFG_TUD_MSC && !CFG_TUD_CDC
#define USBD_ITF_STATIC_MAX (0)
#define USBD_STR_STATIC_MAX (0)
#define USBD_EP_STATIC_MAX (0)
#endif

#endif // MICROPY_HW_ENABLE_USBDEV

#endif // MICROPY_INCLUDED_LIB_UTILS_TUSB_CONFIG_H
4 changes: 4 additions & 0 deletions shared/tinyusb/usbd_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ const uint8_t usbd_desc_cfg_static[USBD_STATIC_DESC_LEN] = {
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_STATIC_MAX, USBD_STR_0, USBD_STATIC_DESC_LEN,
0, USBD_MAX_POWER_MA),

#if CFG_TUD_CDC
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
#endif
#if CFG_TUD_MSC
TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
#endif
Expand Down Expand Up @@ -83,9 +85,11 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
case USBD_STR_PRODUCT:
desc_str = MICROPY_HW_USB_STR_PRODUCT;
break;
#if CFG_TUD_CDC
case USBD_STR_CDC:
desc_str = MICROPY_HW_USB_STR_CDC;
break;
#endif
#if CFG_TUD_MSC
case USBD_STR_MSC:
desc_str = MICROPY_HW_USB_STR_MSC;
Expand Down

0 comments on commit 0ee4ccb

Please sign in to comment.