Skip to content
Open
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: 1 addition & 0 deletions examples/dual/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if (FAMILY STREQUAL "rp2040" AND NOT TARGET tinyusb_pico_pio_usb)
message("Skipping dual host/device mode examples as Pico-PIO-USB is not available")
else ()
# family_add_subdirectory will filter what to actually add based on selected FAMILY
family_add_subdirectory(dynamic_switch)
family_add_subdirectory(host_hid_to_device_cdc)
family_add_subdirectory(host_info_to_device_cdc)
endif ()
33 changes: 33 additions & 0 deletions examples/dual/dynamic_switch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.20)

include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)

# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})

project(${PROJECT} C CXX ASM)

# Checks this example is valid for the family and initializes the project
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})

# Espressif has its own cmake build system
if(FAMILY STREQUAL "espressif")
return()
endif()

add_executable(${PROJECT})

# Example source
target_sources(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
)

# Example include
target_include_directories(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Configure compilation flags and libraries for the example without RTOS.
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
family_configure_dual_usb_example(${PROJECT} noos)
6 changes: 6 additions & 0 deletions examples/dual/dynamic_switch/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 6,
"include": [
"../../../hw/bsp/BoardPresets.json"
]
}
16 changes: 16 additions & 0 deletions examples/dual/dynamic_switch/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include ../../../hw/bsp/family_support.mk

INC += \
src \

# Example source
EXAMPLE_SOURCE += $(wildcard src/*.c)
SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE))

# Include device and host stack
SRC_C += \
src/class/cdc/cdc_device.c \
src/host/hub.c \
src/host/usbh.c

include ../../../hw/bsp/family_rules.mk
60 changes: 60 additions & 0 deletions examples/dual/dynamic_switch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Dynamic Switch Example

This example demonstrates TinyUSB's dual-role capability by allowing runtime switching between USB device and host modes.

## Features

- **Button-triggered mode switching**: Press the board button to switch between device and host modes
- **Device Mode**: Acts as a USB CDC (Virtual Serial Port) that echoes all received data
- **Host Mode**: Enumerates connected USB devices and prints device information
- **Dynamic switching**: Deinitializes the current stack and reinitializes in the new mode

## Usage

1. **Build and flash** the example to your board
2. **Default behavior**: The board starts in **Device mode**
3. **Device mode**:
- Connect the board to a PC
- Open a serial terminal (e.g., `screen /dev/ttyACM0` or PuTTY)
- Type characters - they will be echoed back to you
4. **Switch to Host mode**:
- Press the board button
- Connect a USB device to the board
- The board will enumerate the device and print its descriptors to the debug console
5. **Switch back to Device mode**: Press the button again

## LED Patterns

The onboard LED indicates the USB connection status:

- **Fast blink (250ms)**: Not mounted/connected
- **Slow blink (1000ms)**: Successfully mounted/connected
- **Very slow blink (2500ms)**: Suspended (device mode only)

## Serial Output

The example prints status messages to the debug UART:

```
======================================
TinyUSB Dynamic Switch Example
Press button to switch between device and host modes
Starting in DEVICE mode...
======================================

[DEVICE] Mounted

--- Switching USB mode ---
Stopping DEVICE mode...
Starting HOST mode...
Mode switch complete!

[HOST] Device attached, address = 1
Device 1: ID 1234:5678 SN ABC123
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 0200
bDeviceClass 239
...
```
7 changes: 7 additions & 0 deletions examples/dual/dynamic_switch/only.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
family:espressif
mcu:STM32F2
mcu:STM32F4
mcu:STM32U5
mcu:STM32F7
mcu:STM32H7
mcu:STM32H7RS
1 change: 1 addition & 0 deletions examples/dual/dynamic_switch/skip.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
board:stm32u545nucleo
4 changes: 4 additions & 0 deletions examples/dual/dynamic_switch/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is for ESP-IDF only
idf_component_register(SRCS "main.c" "usb_descriptors.c"
INCLUDE_DIRS "."
REQUIRES boards tinyusb_src)
Loading
Loading