Skip to content

Commit ba2556a

Browse files
committed
rp2350 native tinyusb host mode
1 parent a54ddb3 commit ba2556a

File tree

11 files changed

+256
-6
lines changed

11 files changed

+256
-6
lines changed

ports/raspberrypi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ SRC_C += \
549549
ifeq ($(CIRCUITPY_USB_HOST), 1)
550550
SRC_C += \
551551
lib/tinyusb/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c \
552+
lib/tinyusb/src/portable/raspberrypi/rp2040/hcd_rp2040.c \
552553
lib/Pico-PIO-USB/src/pio_usb.c \
553554
lib/Pico-PIO-USB/src/pio_usb_host.c \
554555
lib/Pico-PIO-USB/src/usb_crc.c \
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
16MB SPI Flash
2+
x4 USB hosts
3+
Audio codec with stereo audio amplifier
4+
PWM Stereo Audio generation
5+
Two JST2.0 connectors for left and right speaker
6+
Audio 3.5mm Line-in
7+
Audio 3.5mm headphones connector
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "common-hal/microcontroller/Pin.h"
8+
#include "hardware/gpio.h"
9+
#include "shared-bindings/usb_host/Port.h"
10+
#include "supervisor/board.h"
11+
12+
#include "common-hal/picodvi/__init__.h"
13+
14+
bool board_reset_pin_number(uint8_t pin_number) {
15+
#if defined(DEFAULT_USB_HOST_5V_POWER)
16+
if (pin_number == DEFAULT_USB_HOST_5V_POWER->number) {
17+
// doing this (rather than gpio_init) in this specific order ensures no
18+
// glitch if pin was already configured as a high output. gpio_init() temporarily
19+
// configures the pin as an input, so the power enable value would potentially
20+
// glitch.
21+
gpio_put(pin_number, 1);
22+
gpio_set_dir(pin_number, GPIO_OUT);
23+
gpio_set_function(pin_number, GPIO_FUNC_SIO);
24+
25+
return true;
26+
}
27+
#endif
28+
// Set I2S out of reset.
29+
/*
30+
if (pin_number == I2S_RESET_PIN_NUMBER) {
31+
gpio_put(pin_number, 1);
32+
gpio_set_dir(pin_number, GPIO_OUT);
33+
gpio_set_function(pin_number, GPIO_FUNC_SIO);
34+
35+
return true;
36+
}
37+
*/
38+
39+
return false;
40+
}
41+
42+
void board_init(void) {
43+
common_hal_usb_host_port_construct(0,0);
44+
picodvi_autoconstruct();
45+
}
46+
47+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#define MICROPY_HW_BOARD_NAME "Olimex RP2350pc"
8+
#define MICROPY_HW_MCU_NAME "rp2350b"
9+
10+
#define MICROPY_HW_LED_STATUS (&pin_GPIO25)
11+
12+
// Internal I2C bus (DVI, I2S), no user connection pins
13+
#define DEFAULT_I2C_BUS_SCL (&pin_GPIO33)
14+
#define DEFAULT_I2C_BUS_SDA (&pin_GPIO32)
15+
16+
#define DEFAULT_SPI_BUS_SCK (&pin_GPIO6)
17+
#define DEFAULT_SPI_BUS_MOSI (&pin_GPIO7)
18+
#define DEFAULT_SPI_BUS_MISO (&pin_GPIO4)
19+
20+
#define DEFAULT_UART_BUS_RX (&pin_GPIO1)
21+
#define DEFAULT_UART_BUS_TX (&pin_GPIO0)
22+
23+
#define CIRCUITPY_CONSOLE_UART_RX DEFAULT_UART_BUS_RX
24+
#define CIRCUITPY_CONSOLE_UART_TX DEFAULT_UART_BUS_TX
25+
26+
#define CIRCUITPY_PSRAM_CHIP_SELECT (&pin_GPIO8)
27+
28+
#define DEFAULT_DVI_BUS_CLK_DN (&pin_GPIO15)
29+
#define DEFAULT_DVI_BUS_CLK_DP (&pin_GPIO14)
30+
#define DEFAULT_DVI_BUS_RED_DN (&pin_GPIO13)
31+
#define DEFAULT_DVI_BUS_RED_DP (&pin_GPIO12)
32+
#define DEFAULT_DVI_BUS_GREEN_DN (&pin_GPIO19)
33+
#define DEFAULT_DVI_BUS_GREEN_DP (&pin_GPIO18)
34+
#define DEFAULT_DVI_BUS_BLUE_DN (&pin_GPIO17)
35+
#define DEFAULT_DVI_BUS_BLUE_DP (&pin_GPIO16)
36+
37+
#define DEFAULT_SD_SCK (&pin_GPIO10)
38+
#define DEFAULT_SD_MOSI (&pin_GPIO11)
39+
#define DEFAULT_SD_MISO (&pin_GPIO24)
40+
#define DEFAULT_SD_CS (&pin_GPIO9)
41+
#define DEFAULT_SD_CARD_DETECT (&pin_GPIO41)
42+
#define DEFAULT_SD_CARD_INSERTED true
43+
44+
#define CIRCUITPY_DEBUG_TINYUSB 1
45+
46+
// #define CIRCUITPY_SAVES_PARTITION_SIZE (2 * 1024 * 1024)
47+
48+
49+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#USB_VID = 0x15BA
2+
#USB_PID = 0x0054
3+
CIRCUITPY_CREATOR_ID = 0x000c15BA
4+
CIRCUITPY_CREATION_ID = 0x00000054
5+
USB_PRODUCT = "RP2350pc"
6+
USB_MANUFACTURER = "Olimex"
7+
8+
CHIP_VARIANT = RP2350
9+
CHIP_PACKAGE = B
10+
CHIP_FAMILY = rp2
11+
12+
EXTERNAL_FLASH_DEVICES = "W25Q128JVxQ, P25Q16H"
13+
14+
CIRCUITPY_USB_DEVICE=0
15+
CIRCUITPY_USB_MIDI=0
16+
CIRCUITPY_USB_IDENTIFICATION = 0
17+
CIRCUITPY_USB_CDC = 0
18+
CIRCUITPY_USB_HID = 0
19+
CIRCUITPY_USB_MSC = 0
20+
CIRCUITPY_USB_VIDEO=0
21+
22+
CIRCUITPY_USB_HOST=1
23+
CIRCUITPY_TINYUSB_HOST=1
24+
CIRCUITPY_USB_HOST_INSTANCE = 0
25+
26+
CIRCUITPY_BUILD_EXTENSIONS = uf2
27+
UF2_BOOTLOADER = 1
28+
#OPTIMIZATION_FLAGS = -Os
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
// Put board-specific pico-sdk definitions here. This file must exist.
8+
9+
// Allow extra time for xosc to start.
10+
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "shared-bindings/board/__init__.h"
8+
9+
static const mp_rom_map_elem_t board_module_globals_table[] = {
10+
CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS
11+
12+
// UEXT2 HEADER Pin 1 - 3.3V, 2 - Gnd
13+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D36), MP_ROM_PTR(&pin_GPIO36) }, // PIN 6
14+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D37), MP_ROM_PTR(&pin_GPIO37) }, // 5
15+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D38), MP_ROM_PTR(&pin_GPIO38) }, // 3
16+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D39), MP_ROM_PTR(&pin_GPIO39) }, // 4
17+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D42), MP_ROM_PTR(&pin_GPIO42) }, // 9
18+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D43), MP_ROM_PTR(&pin_GPIO43) }, // 8
19+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D44), MP_ROM_PTR(&pin_GPIO44) }, // 7
20+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D45), MP_ROM_PTR(&pin_GPIO45) }, // 10
21+
22+
// { MP_OBJ_NEW_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_GPIO32) },
23+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SDA0), MP_ROM_PTR(&pin_GPIO36) },
24+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D36), MP_ROM_PTR(&pin_GPIO36) },
25+
// { MP_OBJ_NEW_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_GPIO33) },
26+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SCL0), MP_ROM_PTR(&pin_GPIO37) },
27+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D37), MP_ROM_PTR(&pin_GPIO37) },
28+
29+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SDA1), MP_ROM_PTR(&pin_GPIO2) },
30+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_GPIO2) },
31+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SCL1), MP_ROM_PTR(&pin_GPIO3) },
32+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_GPIO3) },
33+
34+
{ MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_GPIO5) },
35+
{ MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_GPIO6) },
36+
{ MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_GPIO7) },
37+
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_GPIO4) },
38+
39+
{ MP_ROM_QSTR(MP_QSTR_SD_CS), MP_ROM_PTR(&pin_GPIO9) },
40+
{ MP_ROM_QSTR(MP_QSTR_SD_SCK), MP_ROM_PTR(&pin_GPIO10) },
41+
{ MP_ROM_QSTR(MP_QSTR_SD_MOSI), MP_ROM_PTR(&pin_GPIO11) },
42+
{ MP_ROM_QSTR(MP_QSTR_SD_MISO), MP_ROM_PTR(&pin_GPIO24) },
43+
44+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD_CARD_DETECT), MP_ROM_PTR(&pin_GPIO41) },
45+
46+
{ MP_ROM_QSTR(MP_QSTR_CKN), MP_ROM_PTR(&pin_GPIO15) },
47+
{ MP_ROM_QSTR(MP_QSTR_CKP), MP_ROM_PTR(&pin_GPIO14) },
48+
{ MP_ROM_QSTR(MP_QSTR_D0N), MP_ROM_PTR(&pin_GPIO13) }, // Red
49+
{ MP_ROM_QSTR(MP_QSTR_D0P), MP_ROM_PTR(&pin_GPIO12) },
50+
{ MP_ROM_QSTR(MP_QSTR_D1N), MP_ROM_PTR(&pin_GPIO19) }, // Green
51+
{ MP_ROM_QSTR(MP_QSTR_D1P), MP_ROM_PTR(&pin_GPIO18) },
52+
{ MP_ROM_QSTR(MP_QSTR_D2N), MP_ROM_PTR(&pin_GPIO17) }, // Blue
53+
{ MP_ROM_QSTR(MP_QSTR_D2P), MP_ROM_PTR(&pin_GPIO16) },
54+
55+
{ MP_OBJ_NEW_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO25) },
56+
{ MP_OBJ_NEW_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_GPIO25) },
57+
58+
{ MP_ROM_QSTR(MP_QSTR_PWM_SPK_L), MP_ROM_PTR(&pin_GPIO26) },
59+
{ MP_ROM_QSTR(MP_QSTR_PWM_SPK_R), MP_ROM_PTR(&pin_GPIO27) },
60+
61+
{ MP_ROM_QSTR(MP_QSTR_I2S_PWR), MP_ROM_PTR(&pin_GPIO22) },
62+
63+
{ MP_ROM_QSTR(MP_QSTR_I2S_DIN), MP_ROM_PTR(&pin_GPIO31) },
64+
{ MP_ROM_QSTR(MP_QSTR_I2S_MCLK), MP_ROM_PTR(&pin_GPIO23) },
65+
{ MP_ROM_QSTR(MP_QSTR_I2S_BCLK), MP_ROM_PTR(&pin_GPIO30) },
66+
{ MP_ROM_QSTR(MP_QSTR_I2S_WS), MP_ROM_PTR(&pin_GPIO29) },
67+
{ MP_ROM_QSTR(MP_QSTR_I2S_ASDOUT), MP_ROM_PTR(&pin_GPIO28) },
68+
69+
// AMP_VOL must be set high and AMP_SHDN set low to enable the PAM8003 amplifier
70+
// There is an unpopulated potentiometer but without it AMP_VOL is only off/on
71+
{ MP_ROM_QSTR(MP_QSTR_AMP_VOL), MP_ROM_PTR(&pin_GPIO34) },
72+
{ MP_ROM_QSTR(MP_QSTR_AMP_SHDN), MP_ROM_PTR(&pin_GPIO35) },
73+
74+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
75+
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
76+
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
77+
};
78+
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);

ports/raspberrypi/common-hal/usb_host/Port.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
usb_host_port_obj_t usb_host_instance;
3434

35+
#if CIRCUITPY_USB_DEVICE
3536
volatile bool _core1_ready = false;
3637

3738
static void __not_in_flash_func(core1_main)(void) {
@@ -109,16 +110,19 @@ static size_t get_usb_pio(void) {
109110
}
110111
mp_raise_RuntimeError(MP_ERROR_TEXT("All state machines in use"));
111112
}
112-
113+
#endif
113114

114115
usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) {
116+
#if CIRCUITPY_USB_DEVICE
115117
if ((dp->number + 1 != dm->number)
116118
&& (dp->number - 1 != dm->number)) {
117119
raise_ValueError_invalid_pins();
118120
}
121+
#endif
119122
usb_host_port_obj_t *self = &usb_host_instance;
120123

121124
// Return the singleton if given the same pins.
125+
#if CIRCUITPY_USB_DEVICE
122126
if (self->dp != NULL) {
123127
if (self->dp != dp || self->dm != dm) {
124128
mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("%q in use"), MP_QSTR_usb_host);
@@ -170,6 +174,18 @@ usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp,
170174

171175
tuh_configure(TUH_OPT_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg);
172176
tuh_init(TUH_OPT_RHPORT);
177+
#else
178+
#if CIRCUITPY_USB_HOST
179+
// init host stack on configured roothub port
180+
tusb_rhport_init_t host_init = {
181+
.role = TUSB_ROLE_HOST,
182+
.speed = TUSB_SPEED_AUTO
183+
};
184+
tusb_init(0, &host_init);
185+
186+
//tuh_init(0);
187+
#endif
188+
#endif
173189

174190
return self;
175191
}

ports/raspberrypi/supervisor/port.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,15 @@ void port_interrupt_after_ticks(uint32_t ticks) {
552552
void port_idle_until_interrupt(void) {
553553
#ifdef PICO_RP2040
554554
common_hal_mcu_disable_interrupts();
555-
#if CIRCUITPY_USB_HOST
555+
#if CIRCUITPY_USB_HOST && CIRCUITPY_USB_DEVICE
556556
if (!background_callback_pending() && !tud_task_event_ready() && !tuh_task_event_ready() && !_woken_up) {
557557
#else
558+
#if CIRCUITPY_USB_HOST
559+
if (!background_callback_pending() && !tuh_task_event_ready() && !_woken_up) {
560+
#endif
561+
#if CIRCUITPY_USB_DEVICE
558562
if (!background_callback_pending() && !tud_task_event_ready() && !_woken_up) {
563+
#endif
559564
#endif
560565
__DSB();
561566
__WFI();
@@ -571,10 +576,15 @@ void port_idle_until_interrupt(void) {
571576
uint32_t oldBasePri = __get_BASEPRI();
572577
__set_BASEPRI(0);
573578
__isb();
574-
#if CIRCUITPY_USB_HOST
579+
#if CIRCUITPY_USB_HOST && CIRCUITPY_USB_DEVICE
575580
if (!background_callback_pending() && !tud_task_event_ready() && !tuh_task_event_ready() && !_woken_up) {
576581
#else
582+
#if CIRCUITPY_USB_HOST
583+
if (!background_callback_pending() && !tuh_task_event_ready() && !_woken_up) {
584+
#endif
585+
#if CIRCUITPY_USB_DEVICE
577586
if (!background_callback_pending() && !tud_task_event_ready() && !_woken_up) {
587+
#endif
578588
#endif
579589
__DSB();
580590
__WFI();

supervisor/shared/usb/tusb_config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ extern "C" {
150150
#if CIRCUITPY_USB_HOST || CIRCUITPY_MAX3421E
151151
#define CFG_TUH_ENABLED 1
152152

153-
// Always use PIO to do host on RP2.
154-
#if !CIRCUITPY_MAX3421E
155-
#define CFG_TUH_RPI_PIO_USB 1
153+
// Don't use RP2 PIO to do host on MAX3421E or when USB device is disabled
154+
#if CIRCUITPY_MAX3421E || (CIRCUITPY_USB_HOST && !CIRCUITPY_USB_DEVICE)
155+
#define CFG_TUH_RPI_PIO_USB 0
156156
#else
157157
#define CFG_TUH_RPI_PIO_USB 1
158158
#endif

0 commit comments

Comments
 (0)