Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ports/analog/boards/apard32690/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ USB_VID=0x0456
USB_PID=0x003C
USB_MANUFACTURER="Analog Devices, Inc."
USB_PRODUCT="MAX32690 APARD"
USB_HIGHSPEED=1

# NOTE: MAX32 devices do not support IN/OUT pairs on the same EP
USB_NUM_ENDPOINT_PAIRS=12
Expand Down
1 change: 0 additions & 1 deletion ports/analog/boards/max32690evkit/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ USB_VID=0x0456
USB_PID=0x003D
USB_MANUFACTURER="Analog Devices, Inc."
USB_PRODUCT="MAX32690 EvKit"
USB_HIGHSPEED=1

# NOTE: MAX32 devices do not support IN/OUT pairs on the same EP
USB_NUM_ENDPOINT_PAIRS=12
Expand Down
2 changes: 2 additions & 0 deletions ports/analog/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// 24KiB stack
#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000

#define CIRCUITPY_USB_DEVICE_HIGH_SPEED (1)

// Also includes mpconfigboard.h
#include "py/circuitpy_mpconfig.h"

Expand Down
2 changes: 2 additions & 0 deletions ports/broadcom/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define MICROPY_FATFS_EXFAT (1)
#define MICROPY_FATFS_MKFS_FAT32 (1)

#define CIRCUITPY_USB_DEVICE_HIGH_SPEED (1)

////////////////////////////////////////////////////////////////////////////////////////////////////

// This also includes mpconfigboard.h.
Expand Down
1 change: 0 additions & 1 deletion ports/broadcom/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ CIRCUITPY_VIDEOCORE = 1
INTERNAL_FLASH_FILESYSTEM = 1

USB_NUM_ENDPOINT_PAIRS = 8
USB_HIGHSPEED = 1

CIRCUITPY_BUILD_EXTENSIONS ?= disk.img.zip,kernel8.img

Expand Down
2 changes: 2 additions & 0 deletions ports/cxd56/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#define USB_MSC_EP_NUM_OUT (5)
#define USB_MSC_EP_NUM_IN (4)

#define CIRCUITPY_USB_DEVICE_HIGH_SPEED (1)

#include "py/circuitpy_mpconfig.h"

#define MICROPY_BYTES_PER_GC_BLOCK (32)
2 changes: 0 additions & 2 deletions ports/cxd56/mpconfigport.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
USB_HIGHSPEED = 1

# Number of USB endpoint pairs.
USB_NUM_ENDPOINT_PAIRS = 6

Expand Down
3 changes: 2 additions & 1 deletion ports/espressif/boards/m5stack_tab5/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
#define DEFAULT_SPI_BUS_MISO (&pin_GPIO19)

// Use the second USB device (numbered 0 and 1)
#define CIRCUITPY_USB_DEVICE_INSTANCE 1
#define CIRCUITPY_USB_DEVICE_INSTANCE 0
#define CIRCUITPY_ESP32P4_SWAP_LSFS (1)
24 changes: 17 additions & 7 deletions ports/espressif/common-hal/microcontroller/Pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,28 @@ static const uint64_t pin_mask_reset_forbidden =
#endif // ESP32H2

#if defined(CONFIG_IDF_TARGET_ESP32P4)
// Never ever reset pins used to communicate with the SPI flash.
GPIO_SEL_28 |
GPIO_SEL_29 |
GPIO_SEL_30 |
GPIO_SEL_32 |
GPIO_SEL_33 |
GPIO_SEL_34 |
// SPI flash is on dedicated pins.

// USB is on the FS OTG
#if CIRCUITPY_USB_DEVICE_INSTANCE == 0
#if CIRCUITPY_ESP32P4_SWAP_LSFS == 1
// We leave 24 and 25 alone in addition to 26 and 27 when swapped. It doesn't work otherwise. (Not sure why.)
GPIO_SEL_24 | // USB D-
GPIO_SEL_25 | // USB D+
#endif
GPIO_SEL_26 | // USB D-
GPIO_SEL_27 | // USB D+
#endif
#if CIRCUITPY_ESP_USB_SERIAL_JTAG || (defined(CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG) && CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
// Never ever reset serial/JTAG communication pins.
#if CIRCUITPY_ESP32P4_SWAP_LSFS == 1
GPIO_SEL_26 | // USB D-
GPIO_SEL_27 | // USB D+
#else
GPIO_SEL_24 | // USB D-
GPIO_SEL_25 | // USB D+
#endif
#endif
#if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) && CONFIG_ESP_CONSOLE_UART_DEFAULT && CONFIG_ESP_CONSOLE_UART_NUM == 0
// Never reset debug UART/console pins.
GPIO_SEL_37 |
Expand Down
34 changes: 34 additions & 0 deletions ports/espressif/common-hal/mipidsi/Display.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,40 @@ void common_hal_mipidsi_display_refresh(mipidsi_display_obj_t *self) {
// sends data from the framebuffer to the display
}

mp_float_t common_hal_mipidsi_display_get_brightness(mipidsi_display_obj_t *self) {
return self->current_brightness;
}

bool common_hal_mipidsi_display_set_brightness(mipidsi_display_obj_t *self, mp_float_t brightness) {
if (!self->backlight_on_high) {
brightness = 1.0 - brightness;
}
bool ok = false;

// Avoid PWM types and functions when the module isn't enabled
#if (CIRCUITPY_PWMIO)
bool ispwm = (self->backlight_pwm.base.type == &pwmio_pwmout_type) ? true : false;
#else
bool ispwm = false;
#endif

if (ispwm) {
#if (CIRCUITPY_PWMIO)
common_hal_pwmio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t)(0xffff * brightness));
ok = true;
#else
ok = false;
#endif
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99);
ok = true;
}
if (ok) {
self->current_brightness = brightness;
}
return ok;
}

int common_hal_mipidsi_display_get_width(mipidsi_display_obj_t *self) {
return self->width;
}
Expand Down
1 change: 1 addition & 0 deletions ports/espressif/common-hal/mipidsi/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ typedef struct {
#endif
};
bool backlight_on_high;
mp_float_t current_brightness;
} mipidsi_display_obj_t;
12 changes: 12 additions & 0 deletions ports/espressif/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,15 @@ extern portMUX_TYPE background_task_mutex;
#ifndef CIRCUITPY_WIFI_DEFAULT_TX_POWER
#define CIRCUITPY_WIFI_DEFAULT_TX_POWER (20)
#endif

#ifndef CIRCUITPY_USB_DEVICE_HIGH_SPEED
#if defined(CONFIG_IDF_TARGET_ESP32P4) && CIRCUITPY_USB_DEVICE_INSTANCE == 1
#define CIRCUITPY_USB_DEVICE_HIGH_SPEED (1)
#else
#define CIRCUITPY_USB_DEVICE_HIGH_SPEED (0)
#endif
#endif

#ifndef CIRCUITPY_ESP32P4_SWAP_LSFS
#define CIRCUITPY_ESP32P4_SWAP_LSFS (0)
#endif
1 change: 0 additions & 1 deletion ports/espressif/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0
# Second stage bootloader doesn't work when the factory partition is empty due to
# UF2 missing.
UF2_BOOTLOADER = 0
USB_HIGHSPEED = 1
CIRCUITPY_USB_HID = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_TUSB_MEM_ALIGN = 64
Expand Down
38 changes: 35 additions & 3 deletions ports/espressif/supervisor/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-FileCopyrightText: Copyright (c) 2018 hathach for Adafruit Industries
// SPDX-FileCopyrightText: Copyright (c) 2019 Lucian Copeland for Adafruit Industries
// SPDX-FileContributor: 2025 Nicolai Electronics
//
// SPDX-License-Identifier: MIT

Expand All @@ -27,6 +28,10 @@

#include "tusb.h"

#ifdef CONFIG_IDF_TARGET_ESP32P4
#include "hal/usb_serial_jtag_ll.h"
#endif

#if CIRCUITPY_USB_DEVICE
#ifdef CFG_TUSB_DEBUG
#define USBD_STACK_SIZE (3 * configMINIMAL_STACK_SIZE)
Expand All @@ -37,7 +42,7 @@
StackType_t usb_device_stack[USBD_STACK_SIZE];
StaticTask_t usb_device_taskdef;

static usb_phy_handle_t phy_hdl;
static usb_phy_handle_t device_phy_hdl;

// USB Device Driver task
// This top level thread process all usb events and invoke callbacks
Expand All @@ -62,13 +67,40 @@ void init_usb_hardware(void) {
usb_phy_config_t phy_conf = {
.controller = USB_PHY_CTRL_OTG,
.target = USB_PHY_TARGET_INT,

.otg_mode = USB_OTG_MODE_DEVICE,
#if defined(CONFIG_IDF_TARGET_ESP32P4) && CIRCUITPY_USB_DEVICE_INSTANCE == 0
.otg_speed = USB_PHY_SPEED_FULL,
#else
// https://github.com/hathach/tinyusb/issues/2943#issuecomment-2601888322
// Set speed to undefined (auto-detect) to avoid timing/race issue with S3 with host such as macOS
.otg_speed = USB_PHY_SPEED_UNDEFINED,
#endif
};
usb_new_phy(&phy_conf, &device_phy_hdl);

#if CIRCUITPY_ESP32P4_SWAP_LSFS == 1
#ifndef CONFIG_IDF_TARGET_ESP32P4
#error "LSFS swap is only supported on ESP32P4"
#endif
// Switch the USB PHY
const usb_serial_jtag_pull_override_vals_t override_disable_usb = {
.dm_pd = true, .dm_pu = false, .dp_pd = true, .dp_pu = false
};
usb_new_phy(&phy_conf, &phy_hdl);
const usb_serial_jtag_pull_override_vals_t override_enable_usb = {
.dm_pd = false, .dm_pu = false, .dp_pd = false, .dp_pu = true
};

// Drop off the bus by removing the pull-up on USB DP
usb_serial_jtag_ll_phy_enable_pull_override(&override_disable_usb);

// Select USB mode by swapping and un-swapping the two PHYs
vTaskDelay(pdMS_TO_TICKS(500)); // Wait for disconnect before switching to device
usb_serial_jtag_ll_phy_select(1);

// Put the device back onto the bus by re-enabling the pull-up on USB DP
usb_serial_jtag_ll_phy_enable_pull_override(&override_enable_usb);
usb_serial_jtag_ll_phy_disable_pull_override();
#endif

// Pin the USB task to the same core as CircuitPython. This way we leave
// the other core for networking.
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt10xx/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extern uint8_t _ld_default_stack_size;
// are aligned to cache lines.
#define MICROPY_BYTES_PER_GC_BLOCK (32)

#define CIRCUITPY_USB_DEVICE_HIGH_SPEED (1)

#include "py/circuitpy_mpconfig.h"

// TODO:
Expand Down
2 changes: 0 additions & 2 deletions ports/mimxrt10xx/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ LD_FILE = $(FLASH).ld $(CHIP_FAMILY).ld imxrt10xx.ld

INTERNAL_LIBM = 1

USB_HIGHSPEED = 1

# Number of USB endpoint pairs.
USB_NUM_ENDPOINT_PAIRS = 8
# Align buffers on the cache boundary so we don't inadvertently load them early.
Expand Down
8 changes: 8 additions & 0 deletions py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,18 @@ void background_callback_run_all(void);
#define CIRCUITPY_USB_DEVICE_INSTANCE 0
#endif

#ifndef CIRCUITPY_USB_DEVICE_HIGH_SPEED
#define CIRCUITPY_USB_DEVICE_HIGH_SPEED 0
#endif

#ifndef CIRCUITPY_USB_HOST_INSTANCE
#define CIRCUITPY_USB_HOST_INSTANCE -1
#endif

#ifndef CIRCUITPY_USB_HOST_HIGH_SPEED
#define CIRCUITPY_USB_HOST_HIGH_SPEED 0
#endif

// If the port requires certain USB endpoint numbers, define these in mpconfigport.h.

#ifndef USB_CDC_EP_NUM_NOTIFICATION
Expand Down
11 changes: 11 additions & 0 deletions shared-bindings/mipidsi/Display.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ static void mipidsi_display_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufi
common_hal_mipidsi_display_get_buffer(self_in, bufinfo, 0);
}

static float mipidsi_display_get_brightness_proto(mp_obj_t self_in) {
return common_hal_mipidsi_display_get_brightness(self_in);
}

static bool mipidsi_display_set_brightness_proto(mp_obj_t self_in, mp_float_t value) {
common_hal_mipidsi_display_set_brightness(self_in, value);
return true;
}

// These versions exist so that the prototype matches the protocol,
// avoiding a type cast that can hide errors
static void mipidsi_display_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmap) {
Expand Down Expand Up @@ -266,6 +275,8 @@ static int mipidsi_display_get_row_stride_proto(mp_obj_t self_in) {
static const framebuffer_p_t mipidsi_display_proto = {
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer)
.get_bufinfo = mipidsi_display_get_bufinfo,
.set_brightness = mipidsi_display_set_brightness_proto,
.get_brightness = mipidsi_display_get_brightness_proto,
.get_width = mipidsi_display_get_width_proto,
.get_height = mipidsi_display_get_height_proto,
.get_color_depth = mipidsi_display_get_color_depth_proto,
Expand Down
2 changes: 2 additions & 0 deletions shared-bindings/mipidsi/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ void common_hal_mipidsi_display_construct(mipidsi_display_obj_t *self,
void common_hal_mipidsi_display_deinit(mipidsi_display_obj_t *self);
bool common_hal_mipidsi_display_deinited(mipidsi_display_obj_t *self);
void common_hal_mipidsi_display_refresh(mipidsi_display_obj_t *self);
mp_float_t common_hal_mipidsi_display_get_brightness(mipidsi_display_obj_t *self);
bool common_hal_mipidsi_display_set_brightness(mipidsi_display_obj_t *self, mp_float_t brightness);
int common_hal_mipidsi_display_get_width(mipidsi_display_obj_t *self);
int common_hal_mipidsi_display_get_height(mipidsi_display_obj_t *self);
int common_hal_mipidsi_display_get_row_stride(mipidsi_display_obj_t *self);
Expand Down
6 changes: 3 additions & 3 deletions shared-module/usb_cdc/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static const uint8_t usb_cdc_descriptor_template[] = {
0xFF, // 54 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
#define CDC_DATA_OUT_ENDPOINT_INDEX 54
0x02, // 55 bmAttributes (Bulk)
#if USB_HIGHSPEED
#if CIRCUITPY_USB_DEVICE_HIGH_SPEED == 1
0x00, 0x02, // 56,57 wMaxPacketSize 512
#else
0x40, 0x00, // 56,57 wMaxPacketSize 64
Expand All @@ -118,7 +118,7 @@ static const uint8_t usb_cdc_descriptor_template[] = {
0xFF, // 61 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x80 | number]
#define CDC_DATA_IN_ENDPOINT_INDEX 61
0x02, // 62 bmAttributes (Bulk)
#if USB_HIGHSPEED
#if CIRCUITPY_USB_DEVICE_HIGH_SPEED == 1
0x00, 0x02, // 63,64 wMaxPacketSize 512
#else
0x40, 0x00, // 63,64 wMaxPacketSize 64
Expand Down Expand Up @@ -326,7 +326,7 @@ static const uint8_t usb_vendor_descriptor_template[] = {
0xFF, // 11 bEndpointAddress (IN/D2H) [SET AT RUNTIME: number]
#define VENDOR_OUT_ENDPOINT_INDEX 11
0x02, // 12 bmAttributes (Bulk)
#if USB_HIGHSPEED
#if CIRCUITPY_USB_DEVICE_HIGH_SPEED == 1
0x00, 0x02, // 13,14 wMaxPacketSize 512
#else
0x40, 0x00, // 13,14 wMaxPacketSize 64
Expand Down
4 changes: 2 additions & 2 deletions shared-module/usb_midi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static const uint8_t usb_midi_descriptor_template[] = {
0xFF, // 66 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
#define MIDI_STREAMING_OUT_ENDPOINT_INDEX (66)
0x02, // 67 bmAttributes (Bulk)
#if USB_HIGHSPEED
#if CIRCUITPY_USB_DEVICE_HIGH_SPEED == 1
0x00, 0x02, // 68,69 wMaxPacketSize (512)
#else
0x40, 0x00, // 68,69 wMaxPacketSize (64)
Expand All @@ -126,7 +126,7 @@ static const uint8_t usb_midi_descriptor_template[] = {
0xFF, // 78 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x80 | number]
#define MIDI_STREAMING_IN_ENDPOINT_INDEX (78)
0x02, // 79 bmAttributes (Bulk)
#if USB_HIGHSPEED
#if CIRCUITPY_USB_DEVICE_HIGH_SPEED == 1
0x00, 0x02, // 80, 81 wMaxPacketSize (512)
#else
0x40, 0x00, // 80, 81 wMaxPacketSize (64)
Expand Down
Loading