From be686e634eede19f33612122d7254007a3d72bd0 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 18 Mar 2023 09:08:08 +0100 Subject: [PATCH] rp2: Allow disabling USB via MICROPY_HW_ENABLE_USBDEV config. Previously, setting MICROPY_HW_ENABLE_USBDEV to 0 caused build errors. The change affects the nrf and samd ports as well, so MICROPY_HW_ENABLE_USBDEV had to be explicitly enabled there. The configuration options MICROPY_HW_ENABLE_USBDEV and MICROPY_HW_ENABLE_UART_REPL are independent, and can be enabled or disabled by a board. Signed-off-by: Damien George --- ports/nrf/mpconfigport.h | 1 + ports/rp2/mpconfigport.h | 4 ++++ ports/rp2/usbd.c | 6 ++++++ ports/samd/mpconfigport.h | 1 + shared/tinyusb/mp_cdc_common.c | 2 +- shared/tinyusb/mp_usbd.c | 6 ++++++ shared/tinyusb/mp_usbd_descriptor.c | 7 ++++++- 7 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 4762693c81..8875e3faaf 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -141,6 +141,7 @@ #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_USE_INTERNAL_ERRNO (1) #if MICROPY_HW_USB_CDC_1200BPS_TOUCH +#define MICROPY_HW_ENABLE_USBDEV (1) #define MICROPY_ENABLE_SCHEDULER (1) #define MICROPY_SCHEDULER_STATIC_NODES (1) #endif diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index fc86e33e7e..d862812f9b 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -36,8 +36,12 @@ // Board and hardware specific configuration #define MICROPY_HW_MCU_NAME "RP2040" +#ifndef MICROPY_HW_ENABLE_UART_REPL #define MICROPY_HW_ENABLE_UART_REPL (0) // useful if there is no USB +#endif +#ifndef MICROPY_HW_ENABLE_USBDEV #define MICROPY_HW_ENABLE_USBDEV (1) +#endif #if MICROPY_HW_ENABLE_USBDEV // Enable USB-CDC serial port diff --git a/ports/rp2/usbd.c b/ports/rp2/usbd.c index e17c546c61..e51cef0ff7 100644 --- a/ports/rp2/usbd.c +++ b/ports/rp2/usbd.c @@ -24,6 +24,10 @@ * THE SOFTWARE. */ +#include "py/mpconfig.h" + +#if MICROPY_HW_ENABLE_USBDEV + #include "mp_usbd.h" #include "string.h" #include "tusb.h" @@ -42,3 +46,5 @@ void mp_usbd_port_get_serial_number(char *serial_buf) { } serial_buf[hexlen] = 0; } + +#endif diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index a048ad54be..153ad75820 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -60,6 +60,7 @@ #define MICROPY_ENABLE_SCHEDULER (1) #define MICROPY_SCHEDULER_STATIC_NODES (1) #define MICROPY_MODULE_WEAK_LINKS (1) +#define MICROPY_HW_ENABLE_USBDEV (1) #define MICROPY_HW_USB_CDC_1200BPS_TOUCH (1) // Control over Python builtins diff --git a/shared/tinyusb/mp_cdc_common.c b/shared/tinyusb/mp_cdc_common.c index 00b72e74b7..cd4f5d1013 100644 --- a/shared/tinyusb/mp_cdc_common.c +++ b/shared/tinyusb/mp_cdc_common.c @@ -28,7 +28,7 @@ #include "py/mphal.h" #include "modmachine.h" -#if MICROPY_HW_USB_CDC_1200BPS_TOUCH +#if MICROPY_HW_USB_CDC_1200BPS_TOUCH && MICROPY_HW_ENABLE_USBDEV #include "tusb.h" diff --git a/shared/tinyusb/mp_usbd.c b/shared/tinyusb/mp_usbd.c index 91d35a2b4a..0edc489881 100644 --- a/shared/tinyusb/mp_usbd.c +++ b/shared/tinyusb/mp_usbd.c @@ -26,6 +26,10 @@ #include +#include "py/mpconfig.h" + +#if MICROPY_HW_ENABLE_USBDEV + #ifndef NO_QSTR #include "tusb.h" // TinyUSB is not avaiable when running the string preprocessor #include "device/usbd.h" @@ -35,3 +39,5 @@ void usbd_task(void) { tud_task_ext(0, false); } + +#endif diff --git a/shared/tinyusb/mp_usbd_descriptor.c b/shared/tinyusb/mp_usbd_descriptor.c index 3a21baf5bf..c6d205c921 100644 --- a/shared/tinyusb/mp_usbd_descriptor.c +++ b/shared/tinyusb/mp_usbd_descriptor.c @@ -25,7 +25,10 @@ * THE SOFTWARE. */ -#include "mpconfigport.h" +#include "py/mpconfig.h" + +#if MICROPY_HW_ENABLE_USBDEV + #include "tusb.h" #include "mp_usbd.h" #include "mp_usbd_internal.h" @@ -126,3 +129,5 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { (void)index; return mp_usbd_desc_cfg_static; } + +#endif