From 3a1ced7e1333de7ddf57f8592a41cbf0605512ed Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Wed, 24 Jun 2026 20:48:42 +0200 Subject: typec: add stm32u5 support Signed-off-by: HiFiPhile --- examples/typec/CMakeLists.txt | 4 ++-- examples/typec/power_delivery/only.txt | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'examples') 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 -- cgit v1.3.1 From f35af01db58ba76bef1a65a56458bc815497ebbe Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Wed, 24 Jun 2026 21:15:33 +0200 Subject: typec: add tuc_connect/tuc_disconnect api Signed-off-by: HiFiPhile --- examples/typec/power_delivery/src/main.c | 41 ++++++++++++++++---- src/portable/st/typec/typec_stm32.c | 65 ++++++++++++++++++++++++++------ src/typec/pd_types.h | 3 +- src/typec/tcd.h | 6 +++ src/typec/usbc.c | 22 +++++++++++ src/typec/usbc.h | 8 ++++ 6 files changed, 125 insertions(+), 20 deletions(-) (limited to 'examples') diff --git a/examples/typec/power_delivery/src/main.c b/examples/typec/power_delivery/src/main.c index f6191bfe8..7c1a0bcaf 100644 --- a/examples/typec/power_delivery/src/main.c +++ b/examples/typec/power_delivery/src/main.c @@ -42,20 +42,19 @@ #define CURRENT_OPERATING_MA 100 // 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(); @@ -64,6 +63,7 @@ int main(void) tuc_init(0, TUSB_TYPEC_PORT_SNK); while (1) { + typec_connect_task(); led_blinking_task(); // tinyusb typec task @@ -175,6 +175,33 @@ bool tuc_pd_control_received_cb(uint8_t rhport, pd_header_t const* header) { return true; } +//--------------------------------------------------------------------+ +// TypeC CONNECT TASK +//--------------------------------------------------------------------+ +void typec_connect_task(void) +{ + static uint32_t btn_prev = 0; + static bool connected = true; + + 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 //--------------------------------------------------------------------+ diff --git a/src/portable/st/typec/typec_stm32.c b/src/portable/st/typec/typec_stm32.c index bd222fcd1..5fa53fe42 100644 --- a/src/portable/st/typec/typec_stm32.c +++ b/src/portable/st/typec/typec_stm32.c @@ -71,6 +71,7 @@ static uint16_t _rx_buf_len; static uint8_t const* _tx_pending_buf; static uint16_t _tx_pending_bytes; static uint16_t _tx_xferring_bytes; +static bool _cc_enabled[TUP_TYPEC_RHPORTS_NUM]; static pd_header_t _good_crc = { .msg_type = PD_CTRL_GOOD_CRC, @@ -250,17 +251,7 @@ bool tcd_init(uint8_t rhport, uint32_t port_type) { // General programming sequence (with UCPD configured then enabled) if (port_type == TUSB_TYPEC_PORT_SNK) { - // Set analog mode enable both CC Phy - UCPD1->CR = (0x01 << UCPD_CR_ANAMODE_Pos) | (UCPD_CR_CCENABLE_0 | UCPD_CR_CCENABLE_1); - - // Read Voltage State on CC1 & CC2 fore initial state - uint32_t v_cc[2]; - v_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03; - v_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03; - TU_LOG1("Initial VState CC1 = %lu, CC2 = %lu\r\n", v_cc[0], v_cc[1]); - - // Enable CC1 & CC2 Interrupt - UCPD1->IMR = UCPD_IMR_TYPECEVT1IE | UCPD_IMR_TYPECEVT2IE; + tcd_connect(rhport); } #if CFG_TUSB_MCU == OPT_MCU_STM32G4 @@ -286,6 +277,56 @@ void tcd_int_disable(uint8_t rhport) { NVIC_DisableIRQ(UCPD1_IRQn); } +void tcd_connect(uint8_t rhport) { + if (_cc_enabled[rhport]) { + return; + } + + _cc_enabled[rhport] = true; + + // Set analog mode and enable both CC Phy as sink. + uint32_t cr = UCPD1->CR; + cr &= ~(UCPD_CR_PHYRXEN | UCPD_CR_PHYCCSEL | UCPD_CR_CCENABLE); + cr |= (0x01 << UCPD_CR_ANAMODE_Pos) | UCPD_CR_CCENABLE_0 | UCPD_CR_CCENABLE_1; + UCPD1->CR = cr; + + UCPD1->ICR = UCPD_ICR_TYPECEVT1CF | UCPD_ICR_TYPECEVT2CF; + UCPD1->IMR |= UCPD_IMR_TYPECEVT1IE | UCPD_IMR_TYPECEVT2IE; +} + +void tcd_disconnect(uint8_t rhport) { + if (!_cc_enabled[rhport]) { + return; + } + + _cc_enabled[rhport] = false; + + if (dma_enabled(rhport, true)) { + dma_stop(rhport, true); + } + + if (dma_enabled(rhport, false)) { + dma_tx_stop(rhport); + } + + _rx_buf = NULL; + _rx_buf_len = 0; + _tx_pending_buf = NULL; + _tx_pending_bytes = 0; + _tx_xferring_bytes = 0; + + UCPD1->CFG1 &= ~(UCPD_CFG1_RXDMAEN | UCPD_CFG1_TXDMAEN); + UCPD1->IMR &= ~(UCPD_IMR_TYPECEVT1IE | UCPD_IMR_TYPECEVT2IE | IMR_ATTACHED); + + uint32_t cr = UCPD1->CR; + cr &= ~(UCPD_CR_PHYRXEN | UCPD_CR_PHYCCSEL | UCPD_CR_CCENABLE); + UCPD1->CR = cr; + + UCPD1->ICR = UCPD_ICR_TYPECEVT1CF | UCPD_ICR_TYPECEVT2CF; + + tcd_event_cc_changed(rhport, 0, 0, false); +} + bool tcd_msg_receive(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes) { _rx_buf = buffer; _rx_buf_len = total_bytes; @@ -347,7 +388,7 @@ void tcd_int_handler(uint8_t rhport) { // Attached UCPD1->IMR |= IMR_ATTACHED; UCPD1->CFG1 |= UCPD_CFG1_RXDMAEN | UCPD_CFG1_TXDMAEN; - }else { + } else { // Detached UCPD1->CFG1 &= ~(UCPD_CFG1_RXDMAEN | UCPD_CFG1_TXDMAEN); UCPD1->IMR &= ~IMR_ATTACHED; diff --git a/src/typec/pd_types.h b/src/typec/pd_types.h index 950f4d488..eebcec632 100644 --- a/src/typec/pd_types.h +++ b/src/typec/pd_types.h @@ -46,7 +46,8 @@ TU_ATTR_BIT_FIELD_ORDER_BEGIN typedef enum { TUSB_TYPEC_PORT_SRC, TUSB_TYPEC_PORT_SNK, - TUSB_TYPEC_PORT_DRP + TUSB_TYPEC_PORT_DRP, + TUSB_TYPEC_PORT_DISCONNECTED, } tusb_typec_port_type_t; enum { diff --git a/src/typec/tcd.h b/src/typec/tcd.h index da7ab4b13..6e18cd063 100644 --- a/src/typec/tcd.h +++ b/src/typec/tcd.h @@ -78,6 +78,12 @@ void tcd_int_enable (uint8_t rhport); // Disable interrupt void tcd_int_disable(uint8_t rhport); +// Enable Type-C port terminations +void tcd_connect(uint8_t rhport); + +// Disable Type-C port terminations +void tcd_disconnect(uint8_t rhport); + // Interrupt Handler void tcd_int_handler(uint8_t rhport); diff --git a/src/typec/usbc.c b/src/typec/usbc.c index 20abd1700..8071e93c3 100644 --- a/src/typec/usbc.c +++ b/src/typec/usbc.c @@ -76,6 +76,14 @@ TU_ATTR_WEAK bool tuc_pd_control_received_cb(uint8_t rhport, pd_header_t const* return false; } +TU_ATTR_WEAK void tcd_connect(uint8_t rhport) { + (void) rhport; +} + +TU_ATTR_WEAK void tcd_disconnect(uint8_t rhport) { + (void) rhport; +} + //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ @@ -83,6 +91,20 @@ bool tuc_inited(uint8_t rhport) { return _usbc_inited && _port_inited[rhport]; } +bool tuc_connect(uint8_t rhport) { + TU_VERIFY(rhport < TUP_TYPEC_RHPORTS_NUM && tuc_inited(rhport)); + + tcd_connect(rhport); + return true; +} + +bool tuc_disconnect(uint8_t rhport) { + TU_VERIFY(rhport < TUP_TYPEC_RHPORTS_NUM && tuc_inited(rhport)); + + tcd_disconnect(rhport); + return true; +} + bool tuc_init(uint8_t rhport, uint32_t port_type) { // Initialize stack if (!_usbc_inited) { diff --git a/src/typec/usbc.h b/src/typec/usbc.h index 711119596..ed8595d27 100644 --- a/src/typec/usbc.h +++ b/src/typec/usbc.h @@ -52,6 +52,14 @@ bool tuc_init(uint8_t rhport, uint32_t port_type); // Check if typec port is initialized bool tuc_inited(uint8_t rhport); +// Enable Type-C port terminations +// Return false if port is not initialized +bool tuc_connect(uint8_t rhport); + +// Disable Type-C port terminations +// Return false if port is not initialized +bool tuc_disconnect(uint8_t rhport); + // Task function should be called in main/rtos loop, extended version of tud_task() // - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever // - in_isr: if function is called in ISR -- cgit v1.3.1 From 87a98641e4e9315577ec8f83827474e243983a55 Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Wed, 24 Jun 2026 21:28:40 +0200 Subject: fix ci Signed-off-by: HiFiPhile --- examples/typec/power_delivery/skip.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/typec/power_delivery/skip.txt (limited to 'examples') 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 -- cgit v1.3.1 From 430efad690e7038339550020b51e8f1c7fa235ed Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Wed, 24 Jun 2026 21:59:41 +0200 Subject: skip pdo request if no suitable voltage Signed-off-by: HiFiPhile --- examples/typec/power_delivery/src/main.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/typec/power_delivery/src/main.c b/examples/typec/power_delivery/src/main.c index 7c1a0bcaf..78e29d166 100644 --- a/examples/typec/power_delivery/src/main.c +++ b/examples/typec/power_delivery/src/main.c @@ -39,7 +39,7 @@ // 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 : Type-C disconnected @@ -60,7 +60,7 @@ 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(); @@ -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; in_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 && current_ma >= CURRENT_MAX_MA) { - // Found a suitable PDO - selected_pos = i+1; + if (voltage_mv <= VOLTAGE_MAX_MV) { + voltage_available = true; + + 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, @@ -181,7 +191,7 @@ bool tuc_pd_control_received_cb(uint8_t rhport, pd_header_t const* header) { void typec_connect_task(void) { static uint32_t btn_prev = 0; - static bool connected = true; + static bool connected = false; uint32_t const btn = board_button_read(); -- cgit v1.3.1