inital support for usb typec and pd example

This commit is contained in:
hathach
2023-06-07 15:10:40 +07:00
parent 914e82b25d
commit b893f1d541
16 changed files with 651 additions and 145 deletions

View File

@@ -32,6 +32,8 @@ function(add_tinyusb TARGET)
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/hid/hid_host.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/msc/msc_host.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/vendor/vendor_host.c
# typec
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/typec/utcd.c
)
target_include_directories(${TARGET} PUBLIC
${CMAKE_CURRENT_FUNCTION_LIST_DIR}

View File

@@ -199,6 +199,8 @@
#elif TU_CHECK_MCU(OPT_MCU_STM32G4)
#define TUP_USBIP_FSDEV
#define TUP_USBIP_FSDEV_STM32
#define TUP_USBIP_TYPEC_STM32
#define TUP_DCD_ENDPOINT_MAX 8
#elif TU_CHECK_MCU(OPT_MCU_STM32G0)

View File

@@ -232,16 +232,6 @@ enum {
#define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2)
//--------------------------------------------------------------------+
// TYPE-C
//--------------------------------------------------------------------+
typedef enum {
TUSB_TYPEC_PORT_SRC,
TUSB_TYPEC_PORT_SNK,
TUSB_TYPEC_PORT_DRP
} tusb_typec_port_type_t;
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
@@ -487,9 +477,9 @@ typedef struct TU_ATTR_PACKED
uint16_t bcdDFUVersion;
} tusb_desc_dfu_functional_t;
/*------------------------------------------------------------------*/
/* Types
*------------------------------------------------------------------*/
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
typedef struct TU_ATTR_PACKED{
union {
struct TU_ATTR_PACKED {
@@ -509,6 +499,81 @@ typedef struct TU_ATTR_PACKED{
TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "size is not correct");
//--------------------------------------------------------------------+
// TYPE-C
//--------------------------------------------------------------------+
typedef enum {
TYPEC_PORT_SRC,
TYPEC_PORT_SNK,
TYPEC_PORT_DRP
} typec_port_type_t;
typedef enum {
TYPEC_MSG_CTRL_RESERVED = 0, // 0b00000: 0
TYPEC_MSG_CTRL_GOOD_CRC, // 0b00001: 1
TYPEC_MSG_CTRL_GO_TO_MIN, // 0b00010: 2
TYPEC_MSG_CTRL_ACCEPT, // 0b00011: 3
TYPEC_MSG_CTRL_REJECT, // 0b00100: 4
TYPEC_MSG_CTRL_PING, // 0b00101: 5
TYPEC_MSG_CTRL_PS_RDY, // 0b00110: 6
TYPEC_MSG_CTRL_GET_SOURCE_CAP, // 0b00111: 7
TYPEC_MSG_CTRL_GET_SINK_CAP, // 0b01000: 8
TYPEC_MSG_CTRL_DR_SWAP, // 0b01001: 9
TYPEC_MSG_CTRL_PR_SWAP, // 0b01010: 10
TYPEC_MSG_CTRL_VCONN_SWAP, // 0b01011: 11
TYPEC_MSG_CTRL_WAIT, // 0b01100: 12
TYPEC_MSG_CTRL_SOFT_RESET, // 0b01101: 13
TYPEC_MSG_CTRL_DATA_RESET, // 0b01110: 14
TYPEC_MSG_CTRL_DATA_RESET_COMPLETE, // 0b01111: 15
TYPEC_MSG_CTRL_NOT_SUPPORTED, // 0b10000: 16
TYPEC_MSG_CTRL_GET_SOURCE_CAP_EXTENDED, // 0b10001: 17
TYPEC_MSG_CTRL_GET_STATUS, // 0b10010: 18
TYPEC_MSG_CTRL_FR_SWAP, // 0b10011: 19
TYPEC_MSG_CTRL_GET_PPS_STATUS, // 0b10100: 20
TYPEC_MSG_CTRL_GET_COUNTRY_CODES, // 0b10101: 21
TYPEC_MSG_CTRL_GET_SINK_CAP_EXTENDED, // 0b10110: 22
TYPEC_MSG_CTRL_GET_SOURCE_INFO, // 0b10111: 23
TYPEC_MSG_CTRL_REVISION, // 0b11000: 24
} typec_msg_ctrl_type_t;
typedef enum {
TYPEC_MSG_DATA_RESERVED = 0, // 0b00000: 0
TYPEC_MSG_DATA_SOURCE_CAP, // 0b00001: 1
TYPEC_MSG_DATA_REQUEST, // 0b00010: 2
TYPEC_MSG_DATA_BIST, // 0b00011: 3
TYPEC_MSG_DATA_SINK_CAP, // 0b00100: 4
TYPEC_MSG_DATA_BATTERY_STATUS, // 0b00101: 5
TYPEC_MSG_DATA_ALERT, // 0b00110: 6
TYPEC_MSG_DATA_GET_COUNTRY_INFO, // 0b00111: 7
TYPEC_MSG_DATA_ENTER_USB, // 0b01000: 8
TYPEC_MSG_DATA_EPR_REQUEST, // 0b01001: 9
TYPEC_MSG_DATA_EPR_MODE, // 0b01010: 10
TYPEC_MSG_DATA_SRC_INFO, // 0b01011: 11
TYPEC_MSG_DATA_REVISION, // 0b01100: 12
TYPEC_MSG_DATA_RESERVED_13, // 0b01101: 13
TYPEC_MSG_DATA_RESERVED_14, // 0b01110: 14
TYPEC_MSG_DATA_VENDOR_DEFINED, // 0b01111: 15
} typec_msg_data_type_t;
typedef struct TU_ATTR_PACKED {
uint16_t msg_type : 5; // [0:4]
uint16_t data_role : 1; // [5] SOP only
uint16_t specs_rev : 2; // [6:7]
uint16_t power_role : 1; // [8] SOP only
uint16_t msg_id : 3; // [9:11]
uint16_t n_data_obj : 3; // [12:14]
uint16_t extended : 1; // [15]
} tusb_typec_message_header_t;
typedef struct TU_ATTR_PACKED {
uint16_t data_size : 9; // [0:8]
uint16_t reserved : 1; // [9]
uint16_t request_chunk : 1; // [10]
uint16_t chunk_number : 4; // [11:14]
uint16_t chunked : 1; // [15]
} tusb_typec_message_header_extended_t;
TU_ATTR_PACKED_END // End of all packed definitions
TU_ATTR_BIT_FIELD_ORDER_END

View File

@@ -0,0 +1,181 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "tusb_option.h"
#include "typec/tcd.h"
#if CFG_TUC_ENABLED && defined(TUP_USBIP_TYPEC_STM32)
#include "common/tusb_common.h"
#include "stm32g4xx.h"
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
uint8_t pd_rx_buf[262];
uint32_t pd_rx_count = 0;
uint8_t pd_rx_order_set;
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
bool tcd_init(uint8_t rhport, typec_port_type_t port_type) {
(void) rhport;
// Initialization phase: CFG1
UCPD1->CFG1 = (0x0d << UCPD_CFG1_HBITCLKDIV_Pos) | (0x10 << UCPD_CFG1_IFRGAP_Pos) | (0x07 << UCPD_CFG1_TRANSWIN_Pos) |
(0x01 << UCPD_CFG1_PSC_UCPDCLK_Pos) | (0x1f << UCPD_CFG1_RXORDSETEN_Pos) |
(0 << UCPD_CFG1_TXDMAEN_Pos) | (0 << UCPD_CFG1_RXDMAEN_Pos);
UCPD1->CFG1 |= UCPD_CFG1_UCPDEN;
// General programming sequence (with UCPD configured then enabled)
if (port_type == TYPEC_PORT_SNK) {
// Enable both CC Phy
UCPD1->CR = (0x01 << UCPD_CR_ANAMODE_Pos) | (0x03 << UCPD_CR_CCENABLE_Pos);
// Read Voltage State on CC1 & CC2 fore initial state
uint32_t vstate_cc[2];
vstate_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
vstate_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
TU_LOG1_INT(vstate_cc[0]);
TU_LOG1_INT(vstate_cc[1]);
// Enable CC1 & CC2 Interrupt
UCPD1->IMR = UCPD_IMR_TYPECEVT1IE | UCPD_IMR_TYPECEVT2IE;
}
return true;
}
// Enable interrupt
void tcd_int_enable (uint8_t rhport) {
(void) rhport;
NVIC_EnableIRQ(UCPD1_IRQn);
}
// Disable interrupt
void tcd_int_disable(uint8_t rhport) {
(void) rhport;
NVIC_DisableIRQ(UCPD1_IRQn);
}
void tcd_int_handler(uint8_t rhport) {
(void) rhport;
uint32_t sr = UCPD1->SR;
sr &= UCPD1->IMR;
// TU_LOG1("UCPD1_IRQHandler: sr = 0x%08X\n", sr);
if (sr & (UCPD_SR_TYPECEVT1 | UCPD_SR_TYPECEVT2)) {
uint32_t vstate_cc[2];
vstate_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
vstate_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
TU_LOG1("VState CC1 = %u, CC2 = %u\n", vstate_cc[0], vstate_cc[1]);
uint32_t cr = UCPD1->CR;
// TODO only support SNK for now, required highest voltage for now
if ((sr & UCPD_SR_TYPECEVT1) && (vstate_cc[0] == 3)) {
TU_LOG1("Attach CC1\n");
cr &= ~UCPD_CR_PHYCCSEL;
cr |= UCPD_CR_PHYRXEN;
} else if ((sr & UCPD_SR_TYPECEVT2) && (vstate_cc[1] == 3)) {
TU_LOG1("Attach CC2\n");
cr |= UCPD_CR_PHYCCSEL;
cr |= UCPD_CR_PHYRXEN;
} else {
TU_LOG1("Detach\n");
cr &= ~UCPD_CR_PHYRXEN;
}
if (cr & UCPD_CR_PHYRXEN) {
// Enable Interrupt
UCPD1->IMR |= UCPD_IMR_TXMSGDISCIE | UCPD_IMR_TXMSGSENTIE | UCPD_IMR_TXMSGABTIE | UCPD_IMR_TXUNDIE |
UCPD_IMR_RXNEIE | UCPD_IMR_RXORDDETIE | UCPD_IMR_RXHRSTDETIE | UCPD_IMR_RXOVRIE |
UCPD_IMR_RXMSGENDIE | UCPD_IMR_HRSTDISCIE | UCPD_IMR_HRSTSENTIE;
}
// Enable PD RX
UCPD1->CR = cr;
// ack
UCPD1->ICR = UCPD_ICR_TYPECEVT1CF | UCPD_ICR_TYPECEVT2CF;
}
//------------- Receive -------------//
if (sr & UCPD_SR_RXORDDET) {
// SOP: Start of Packet.
pd_rx_order_set = UCPD1->RX_ORDSET & UCPD_RX_ORDSET_RXORDSET_Msk;
// reset count when received SOP
pd_rx_count = 0;
// ack
UCPD1->ICR = UCPD_ICR_RXORDDETCF;
}
if (sr & UCPD_SR_RXNE) {
// TODO DMA later
do {
pd_rx_buf[pd_rx_count++] = UCPD1->RXDR;
} while (UCPD1->SR & UCPD_SR_RXNE);
}
if (sr & UCPD_SR_RXMSGEND) {
// End of message
// Skip if CRC failed
if (!(sr & UCPD_SR_RXERR)) {
uint32_t payload_size = UCPD1->RX_PAYSZ;
TU_LOG1("RXMSGEND: payload_size = %u, rx count = %u\n", payload_size, pd_rx_count);
}
// ack
UCPD1->ICR = UCPD_ICR_RXMSGENDCF;
}
if (sr & UCPD_SR_RXOVR) {
TU_LOG1("RXOVR\n");
TU_LOG1_HEX(pd_rx_count);
// ack
UCPD1->ICR = UCPD_ICR_RXOVRCF;
}
// if (sr & UCPD_SR_RXNE) {
// uint8_t data = UCPD1->RXDR;
// pd_rx_buf[pd_rx_count++] = data;
// TU_LOG1_HEX(data);
// }
// else {
// TU_LOG_LOCATION();
// }
}
#endif

View File

@@ -40,6 +40,11 @@
#include "class/hid/hid.h"
//------------- TypeC -------------//
#if CFG_TUC_ENABLED
#include "typec/utcd.h"
#endif
//------------- HOST -------------//
#if CFG_TUH_ENABLED
#include "host/usbh.h"

58
src/typec/tcd.h Normal file
View File

@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Ha Thach (thach@tinyusb.org) for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#ifndef _TUSB_TCD_H_
#define _TUSB_TCD_H_
#include "common/tusb_common.h"
#include "osal/osal.h"
#include "common/tusb_fifo.h"
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
// Initialize controller
bool tcd_init(uint8_t rhport, typec_port_type_t port_type);
// Enable interrupt
void tcd_int_enable (uint8_t rhport);
// Disable interrupt
void tcd_int_disable(uint8_t rhport);
// Interrupt Handler
void tcd_int_handler(uint8_t rhport);
#endif

48
src/typec/utcd.c Normal file
View File

@@ -0,0 +1,48 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Ha Thach (thach@tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#include "tusb_option.h"
#if CFG_TUC_ENABLED
#include "tcd.h"
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
bool tuc_init(uint8_t rhport, typec_port_type_t port_type) {
TU_ASSERT(tcd_init(rhport, port_type));
tcd_int_enable(rhport);
return true;
}
#endif

54
src/typec/utcd.h Normal file
View File

@@ -0,0 +1,54 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#ifndef _TUSB_UTCD_H_
#define _TUSB_UTCD_H_
#include "common/tusb_common.h"
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------+
// Application API
//--------------------------------------------------------------------+
// init typec stack
bool tuc_init(uint8_t rhport, typec_port_type_t port_type);
#ifndef _TUSB_TCD_H_
extern void tcd_int_handler(uint8_t rhport);
#endif
// Interrupt handler, name alias to TCD
#define tuc_int_handler tcd_int_handler
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
#endif