diff options
| author | Zixun LI <[email protected]> | 2026-06-25 10:24:39 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-06-25 10:24:39 +0200 |
| commit | 0a25cc27d7d3699536f6df01e80af3eb0423ce58 (patch) | |
| tree | 62b61da1555d58bf44e8ee7eaae22472df9c5629 /examples | |
| parent | ff86dfe41f6407f430413f2519226af835a21665 (diff) | |
| parent | c374de17d9cd40c8631a10d9fb9251a4a06fc70f (diff) | |
Merge pull request #3616 from michaelajax/fix-stm32-usbc
typec/stm32: fix UCPD sink bugs affecting compatibility with newer PD sources
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/typec/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | examples/typec/power_delivery/only.txt | 1 | ||||
| -rw-r--r-- | examples/typec/power_delivery/skip.txt | 1 | ||||
| -rw-r--r-- | examples/typec/power_delivery/src/main.c | 67 |
4 files changed, 56 insertions, 17 deletions
diff --git a/examples/typec/CMakeLists.txt b/examples/typec/CMakeLists.txt index 24c25908a..cb6c5b5a7 100644 --- a/examples/typec/CMakeLists.txt +++ b/examples/typec/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.20) include(${CMAKE_CURRENT_SOURCE_DIR}/../../hw/bsp/family_support.cmake) -project(tinyusb_host_examples C CXX ASM) -family_initialize_project(tinyusb_host_examples ${CMAKE_CURRENT_LIST_DIR}) +project(tinyusb_typec_examples C CXX ASM) +family_initialize_project(tinyusb_typec_examples ${CMAKE_CURRENT_LIST_DIR}) # family_add_subdirectory will filter what to actually add based on selected FAMILY family_add_subdirectory(power_delivery) diff --git a/examples/typec/power_delivery/only.txt b/examples/typec/power_delivery/only.txt index 657aeaac5..fc458560d 100644 --- a/examples/typec/power_delivery/only.txt +++ b/examples/typec/power_delivery/only.txt @@ -1 +1,2 @@ mcu:STM32G4 +mcu:STM32U5 diff --git a/examples/typec/power_delivery/skip.txt b/examples/typec/power_delivery/skip.txt new file mode 100644 index 000000000..f25559549 --- /dev/null +++ b/examples/typec/power_delivery/skip.txt @@ -0,0 +1 @@ +board:stm32u545nucleo diff --git a/examples/typec/power_delivery/src/main.c b/examples/typec/power_delivery/src/main.c index f6191bfe8..78e29d166 100644 --- a/examples/typec/power_delivery/src/main.c +++ b/examples/typec/power_delivery/src/main.c @@ -39,31 +39,31 @@ // defined here. Otherwise, you may damage your board, smoke can come out #define VOLTAGE_MAX_MV 5000 // maximum voltage in mV #define CURRENT_MAX_MA 500 // maximum current in mA -#define CURRENT_OPERATING_MA 100 // operating current in mA +#define CURRENT_OPERATING_MA 300 // operating current in mA /* Blink pattern - * - 250 ms : button is not pressed - * - 1000 ms : button is pressed (and hold) + * - 250 ms : Type-C disconnected + * - 1000 ms : Type-C connected */ enum { - BLINK_PRESSED = 250, - BLINK_UNPRESSED = 1000 + BLINK_DISCONNECTED = 250, + BLINK_CONNECTED = 1000 }; -static uint32_t blink_interval_ms = BLINK_UNPRESSED; +static uint32_t blink_interval_ms = BLINK_CONNECTED; +void typec_connect_task(void); void led_blinking_task(void); -#define HELLO_STR "Hello from TinyUSB\r\n" - int main(void) { board_init(); board_led_write(true); - tuc_init(0, TUSB_TYPEC_PORT_SNK); + tuc_init(0, TUSB_TYPEC_PORT_DISCONNECTED); while (1) { + typec_connect_task(); led_blinking_task(); // tinyusb typec task @@ -87,6 +87,7 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t printf("PD Source Capabilities\r\n"); // Examine source capability and select a suitable PDO (starting from 1 with safe5v) uint8_t selected_pos = 1; + bool voltage_available = false; for(size_t i=0; i<header->n_data_obj; i++) { TU_VERIFY(dobj < p_end); @@ -97,11 +98,15 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t pd_pdo_fixed_t const* fixed = (pd_pdo_fixed_t const*) &pdo; uint32_t const voltage_mv = fixed->voltage_50mv*50; uint32_t const current_ma = fixed->current_max_10ma*10; - printf("[Fixed] %"PRIu32" mV %"PRIu32" mA\r\n", voltage_mv, current_ma); + printf("[Fixed] PDO%"PRIu8" %"PRIu32" mV %"PRIu32" mA\r\n", i+1, voltage_mv, current_ma); + + if (voltage_mv <= VOLTAGE_MAX_MV) { + voltage_available = true; - if (voltage_mv <= VOLTAGE_MAX_MV && current_ma >= CURRENT_MAX_MA) { - // Found a suitable PDO - selected_pos = i+1; + if (current_ma >= CURRENT_MAX_MA) { + // Found a suitable PDO + selected_pos = i+1; + } } break; @@ -124,12 +129,17 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t // Be careful and make sure your board can withstand the selected PDO // voltage other than safe5v e.g 12v or 20v + if (!voltage_available) { + printf("No PDO within %"PRIu32" mV, skip request\r\n", (uint32_t) VOLTAGE_MAX_MV); + break; + } + printf("Selected PDO %u\r\n", selected_pos); // Send request with selected PDO position as response to Source Cap pd_rdo_fixed_variable_t rdo = { - .current_extremum_10ma = 50, // max 500mA - .current_operate_10ma = 30, // 300mA + .current_extremum_10ma = CURRENT_MAX_MA / 10, + .current_operate_10ma = CURRENT_OPERATING_MA / 10, .reserved = 0, .epr_mode_capable = 0, .unchunked_ext_msg_support = 0, @@ -176,6 +186,33 @@ bool tuc_pd_control_received_cb(uint8_t rhport, pd_header_t const* header) { } //--------------------------------------------------------------------+ +// TypeC CONNECT TASK +//--------------------------------------------------------------------+ +void typec_connect_task(void) +{ + static uint32_t btn_prev = 0; + static bool connected = false; + + uint32_t const btn = board_button_read(); + + if ((btn_prev == 0u) && (btn != 0u)) { + bool const connect = !connected; + + if (connect && tuc_connect(0)) { + connected = true; + blink_interval_ms = BLINK_CONNECTED; + printf("Type-C connect\r\n"); + } else if (!connect && tuc_disconnect(0)) { + connected = false; + blink_interval_ms = BLINK_DISCONNECTED; + printf("Type-C disconnect\r\n"); + } + } + + btn_prev = btn; +} + +//--------------------------------------------------------------------+ // BLINKING TASK //--------------------------------------------------------------------+ void led_blinking_task(void) |
