mirror of
https://github.com/hathach/tinyusb.git
synced 2026-08-18 11:02:16 +00:00
typec: add tuc_connect/tuc_disconnect api
Signed-off-by: HiFiPhile <admin@hifiphile.com>
This commit is contained in:
@ -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
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user