Skip to content

Commit c285030

Browse files
committed
ad pd_types.h
1 parent debde4c commit c285030

File tree

6 files changed

+278
-217
lines changed

6 files changed

+278
-217
lines changed

src/common/tusb_types.h

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ typedef struct TU_ATTR_PACKED
480480
//--------------------------------------------------------------------+
481481
//
482482
//--------------------------------------------------------------------+
483+
483484
typedef struct TU_ATTR_PACKED{
484485
union {
485486
struct TU_ATTR_PACKED {
@@ -499,101 +500,6 @@ typedef struct TU_ATTR_PACKED{
499500

500501
TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "size is not correct");
501502

502-
//--------------------------------------------------------------------+
503-
// TYPE-C
504-
//--------------------------------------------------------------------+
505-
506-
typedef enum {
507-
TUSB_TYPEC_PORT_SRC,
508-
TUSB_TYPEC_PORT_SNK,
509-
TUSB_TYPEC_PORT_DRP
510-
} tusb_typec_port_type_t;
511-
512-
typedef enum {
513-
TUSB_PD_CTRL_RESERVED = 0, // 0b00000: 0
514-
TUSB_PD_CTRL_GOOD_CRC, // 0b00001: 1
515-
TUSB_PD_CTRL_GO_TO_MIN, // 0b00010: 2
516-
TUSB_PD_CTRL_ACCEPT, // 0b00011: 3
517-
TUSB_PD_CTRL_REJECT, // 0b00100: 4
518-
TUSB_PD_CTRL_PING, // 0b00101: 5
519-
TUSB_PD_CTRL_PS_RDY, // 0b00110: 6
520-
TUSB_PD_CTRL_GET_SOURCE_CAP, // 0b00111: 7
521-
TUSB_PD_CTRL_GET_SINK_CAP, // 0b01000: 8
522-
TUSB_PD_CTRL_DR_SWAP, // 0b01001: 9
523-
TUSB_PD_CTRL_PR_SWAP, // 0b01010: 10
524-
TUSB_PD_CTRL_VCONN_SWAP, // 0b01011: 11
525-
TUSB_PD_CTRL_WAIT, // 0b01100: 12
526-
TUSB_PD_CTRL_SOFT_RESET, // 0b01101: 13
527-
TUSB_PD_CTRL_DATA_RESET, // 0b01110: 14
528-
TUSB_PD_CTRL_DATA_RESET_COMPLETE, // 0b01111: 15
529-
TUSB_PD_CTRL_NOT_SUPPORTED, // 0b10000: 16
530-
TUSB_PD_CTRL_GET_SOURCE_CAP_EXTENDED, // 0b10001: 17
531-
TUSB_PD_CTRL_GET_STATUS, // 0b10010: 18
532-
TUSB_PD_CTRL_FR_SWAP, // 0b10011: 19
533-
TUSB_PD_CTRL_GET_PPS_STATUS, // 0b10100: 20
534-
TUSB_PD_CTRL_GET_COUNTRY_CODES, // 0b10101: 21
535-
TUSB_PD_CTRL_GET_SINK_CAP_EXTENDED, // 0b10110: 22
536-
TUSB_PD_CTRL_GET_SOURCE_INFO, // 0b10111: 23
537-
TUSB_PD_CTRL_REVISION, // 0b11000: 24
538-
} tusb_pd_ctrl_type_t;
539-
540-
typedef enum {
541-
TUSB_PD_DATA_RESERVED = 0, // 0b00000: 0
542-
TUSB_PD_DATA_SOURCE_CAP, // 0b00001: 1
543-
TUSB_PD_DATA_REQUEST, // 0b00010: 2
544-
TUSB_PD_DATA_BIST, // 0b00011: 3
545-
TUSB_PD_DATA_SINK_CAP, // 0b00100: 4
546-
TUSB_PD_DATA_BATTERY_STATUS, // 0b00101: 5
547-
TUSB_PD_DATA_ALERT, // 0b00110: 6
548-
TUSB_PD_DATA_GET_COUNTRY_INFO, // 0b00111: 7
549-
TUSB_PD_DATA_ENTER_USB, // 0b01000: 8
550-
TUSB_PD_DATA_EPR_REQUEST, // 0b01001: 9
551-
TUSB_PD_DATA_EPR_MODE, // 0b01010: 10
552-
TUSB_PD_DATA_SRC_INFO, // 0b01011: 11
553-
TUSB_PD_DATA_REVISION, // 0b01100: 12
554-
TUSB_PD_DATA_RESERVED_13, // 0b01101: 13
555-
TUSB_PD_DATA_RESERVED_14, // 0b01110: 14
556-
TUSB_PD_DATA_VENDOR_DEFINED, // 0b01111: 15
557-
} tusb_pd_data_type_t;
558-
559-
enum {
560-
TUSB_PD_REV10 = 0x0,
561-
TUSB_PD_REV20 = 0x1,
562-
TUSB_PD_REV30 = 0x2,
563-
};
564-
565-
enum {
566-
TUSB_PD_DATA_ROLE_UFP = 0x0,
567-
TUSB_PD_DATA_ROLE_DFP = 0x1,
568-
};
569-
570-
enum {
571-
TUSB_PD_POWER_ROLE_SINK = 0x0,
572-
TUSB_PD_POWER_ROLE_SOURCE = 0x1,
573-
};
574-
575-
typedef struct TU_ATTR_PACKED {
576-
uint16_t msg_type : 5; // [0:4]
577-
uint16_t data_role : 1; // [5] SOP only: 0 UFP, 1 DFP
578-
uint16_t specs_rev : 2; // [6:7]
579-
uint16_t power_role : 1; // [8] SOP only: 0 Sink, 1 Source
580-
uint16_t msg_id : 3; // [9:11]
581-
uint16_t n_data_obj : 3; // [12:14]
582-
uint16_t extended : 1; // [15]
583-
} tusb_pd_header_t;
584-
585-
TU_VERIFY_STATIC( sizeof(tusb_pd_header_t) == 2, "size is not correct");
586-
587-
typedef struct TU_ATTR_PACKED {
588-
uint16_t data_size : 9; // [0:8]
589-
uint16_t reserved : 1; // [9]
590-
uint16_t request_chunk : 1; // [10]
591-
uint16_t chunk_number : 4; // [11:14]
592-
uint16_t chunked : 1; // [15]
593-
} tusb_pd_header_extended_t;
594-
595-
TU_VERIFY_STATIC( sizeof(tusb_pd_header_extended_t) == 2, "size is not correct");
596-
597503

598504
TU_ATTR_PACKED_END // End of all packed definitions
599505
TU_ATTR_BIT_FIELD_ORDER_END

src/portable/st/typec/typec_stm32.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ static uint8_t const* _tx_pending_buf;
6767
static uint16_t _tx_pending_bytes;
6868
static uint16_t _tx_xferring_bytes;
6969

70-
static tusb_pd_header_t _good_crc = {
71-
.msg_type = TUSB_PD_CTRL_GOOD_CRC,
70+
static pd_header_t _good_crc = {
71+
.msg_type = PD_CTRL_GOOD_CRC,
7272
.data_role = 0, // UFP
73-
.specs_rev = TUSB_PD_REV20,
73+
.specs_rev = PD_REV_20,
7474
.power_role = 0, // Sink
7575
.msg_id = 0,
7676
.n_data_obj = 0,
@@ -149,7 +149,6 @@ TU_ATTR_ALWAYS_INLINE static inline bool dma_enabled(uint8_t rhport, bool is_rx)
149149
return dma_ch->CCR & DMA_CCR_EN;
150150
}
151151

152-
153152
TU_ATTR_ALWAYS_INLINE static inline void dma_tx_start(uint8_t rhport, void const* buf, uint16_t len) {
154153
UCPD1->TX_ORDSET = PHY_ORDERED_SET_SOP;
155154
UCPD1->TX_PAYSZ = len;
@@ -165,7 +164,7 @@ TU_ATTR_ALWAYS_INLINE static inline void dma_tx_stop(uint8_t rhport) {
165164
//
166165
//--------------------------------------------------------------------+
167166

168-
bool tcd_init(uint8_t rhport, tusb_typec_port_type_t port_type) {
167+
bool tcd_init(uint8_t rhport, uint32_t port_type) {
169168
(void) rhport;
170169

171170
// Init DMA for RX, TX
@@ -211,7 +210,7 @@ void tcd_int_disable(uint8_t rhport) {
211210
NVIC_DisableIRQ(UCPD1_IRQn);
212211
}
213212

214-
bool tcd_rx_start(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes) {
213+
bool tcd_msg_receive(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes) {
215214
_rx_buf = buffer;
216215
dma_start(rhport, true, buffer, total_bytes);
217216
return true;
@@ -309,7 +308,7 @@ void tcd_int_handler(uint8_t rhport) {
309308

310309
if (!(sr & UCPD_SR_RXERR)) {
311310
// response with good crc
312-
_good_crc.msg_id = ((tusb_pd_header_t const*) _rx_buf)->msg_id;
311+
_good_crc.msg_id = ((pd_header_t const*) _rx_buf)->msg_id;
313312
dma_tx_start(rhport, &_good_crc, 2);
314313

315314
result = XFER_RESULT_SUCCESS;

src/typec/pd_types.h

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2023 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#ifndef _TUSB_PD_TYPES_H_
28+
#define _TUSB_PD_TYPES_H_
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
#include <stdbool.h>
35+
#include <stdint.h>
36+
#include "common/tusb_compiler.h"
37+
38+
// Start of all packed definitions for compiler without per-type packed
39+
TU_ATTR_PACKED_BEGIN
40+
TU_ATTR_BIT_FIELD_ORDER_BEGIN
41+
42+
//--------------------------------------------------------------------+
43+
// TYPE-C
44+
//--------------------------------------------------------------------+
45+
46+
typedef enum {
47+
TUSB_TYPEC_PORT_SRC,
48+
TUSB_TYPEC_PORT_SNK,
49+
TUSB_TYPEC_PORT_DRP
50+
} tusb_typec_port_type_t;
51+
52+
enum {
53+
PD_CTRL_RESERVED = 0, // 0b00000: 0
54+
PD_CTRL_GOOD_CRC, // 0b00001: 1
55+
PD_CTRL_GO_TO_MIN, // 0b00010: 2
56+
PD_CTRL_ACCEPT, // 0b00011: 3
57+
PD_CTRL_REJECT, // 0b00100: 4
58+
PD_CTRL_PING, // 0b00101: 5
59+
PD_CTRL_PS_READY, // 0b00110: 6
60+
PD_CTRL_GET_SOURCE_CAP, // 0b00111: 7
61+
PD_CTRL_GET_SINK_CAP, // 0b01000: 8
62+
PD_CTRL_DR_SWAP, // 0b01001: 9
63+
PD_CTRL_PR_SWAP, // 0b01010: 10
64+
PD_CTRL_VCONN_SWAP, // 0b01011: 11
65+
PD_CTRL_WAIT, // 0b01100: 12
66+
PD_CTRL_SOFT_RESET, // 0b01101: 13
67+
PD_CTRL_DATA_RESET, // 0b01110: 14
68+
PD_CTRL_DATA_RESET_COMPLETE, // 0b01111: 15
69+
PD_CTRL_NOT_SUPPORTED, // 0b10000: 16
70+
PD_CTRL_GET_SOURCE_CAP_EXTENDED, // 0b10001: 17
71+
PD_CTRL_GET_STATUS, // 0b10010: 18
72+
PD_CTRL_FR_SWAP, // 0b10011: 19
73+
PD_CTRL_GET_PPS_STATUS, // 0b10100: 20
74+
PD_CTRL_GET_COUNTRY_CODES, // 0b10101: 21
75+
PD_CTRL_GET_SINK_CAP_EXTENDED, // 0b10110: 22
76+
PD_CTRL_GET_SOURCE_INFO, // 0b10111: 23
77+
PD_CTRL_REVISION, // 0b11000: 24
78+
};
79+
80+
enum {
81+
PD_DATA_RESERVED = 0, // 0b00000: 0
82+
PD_DATA_SOURCE_CAP, // 0b00001: 1
83+
PD_DATA_REQUEST, // 0b00010: 2
84+
PD_DATA_BIST, // 0b00011: 3
85+
PD_DATA_SINK_CAP, // 0b00100: 4
86+
PD_DATA_BATTERY_STATUS, // 0b00101: 5
87+
PD_DATA_ALERT, // 0b00110: 6
88+
PD_DATA_GET_COUNTRY_INFO, // 0b00111: 7
89+
PD_DATA_ENTER_USB, // 0b01000: 8
90+
PD_DATA_EPR_REQUEST, // 0b01001: 9
91+
PD_DATA_EPR_MODE, // 0b01010: 10
92+
PD_DATA_SRC_INFO, // 0b01011: 11
93+
PD_DATA_REVISION, // 0b01100: 12
94+
PD_DATA_RESERVED_13, // 0b01101: 13
95+
PD_DATA_RESERVED_14, // 0b01110: 14
96+
PD_DATA_VENDOR_DEFINED, // 0b01111: 15
97+
};
98+
99+
enum {
100+
PD_REV_10 = 0x0,
101+
PD_REV_20 = 0x1,
102+
PD_REV_30 = 0x2,
103+
};
104+
105+
enum {
106+
PD_DATA_ROLE_UFP = 0x0,
107+
PD_DATA_ROLE_DFP = 0x1,
108+
};
109+
110+
enum {
111+
PD_POWER_ROLE_SINK = 0x0,
112+
PD_POWER_ROLE_SOURCE = 0x1,
113+
};
114+
115+
typedef struct TU_ATTR_PACKED {
116+
uint16_t msg_type : 5; // [0:4]
117+
uint16_t data_role : 1; // [5] SOP only: 0 UFP, 1 DFP
118+
uint16_t specs_rev : 2; // [6:7]
119+
uint16_t power_role : 1; // [8] SOP only: 0 Sink, 1 Source
120+
uint16_t msg_id : 3; // [9:11]
121+
uint16_t n_data_obj : 3; // [12:14]
122+
uint16_t extended : 1; // [15]
123+
} pd_header_t;
124+
TU_VERIFY_STATIC(sizeof(pd_header_t) == 2, "size is not correct");
125+
126+
typedef struct TU_ATTR_PACKED {
127+
uint16_t data_size : 9; // [0:8]
128+
uint16_t reserved : 1; // [9]
129+
uint16_t request_chunk : 1; // [10]
130+
uint16_t chunk_number : 4; // [11:14]
131+
uint16_t chunked : 1; // [15]
132+
} pd_header_extended_t;
133+
TU_VERIFY_STATIC(sizeof(pd_header_extended_t) == 2, "size is not correct");
134+
135+
//--------------------------------------------------------------------+
136+
// Source Capability
137+
//--------------------------------------------------------------------+
138+
139+
// All table references are from USBPD Specification rev3.1 version 1.8
140+
enum {
141+
PD_PDO_TYPE_FIXED = 0, // Vmin = Vmax
142+
PD_PDO_TYPE_BATTERY,
143+
PD_PDO_TYPE_VARIABLE, // non-battery
144+
PD_PDO_TYPE_APDO, // Augmented Power Data Object
145+
};
146+
147+
// Fixed Power Data Object (PDO) table 6-9
148+
typedef struct TU_ATTR_PACKED {
149+
uint32_t current_max_10ma : 10; // [9..0] Max current in 10mA unit
150+
uint32_t voltage_50mv : 10; // [19..10] Voltage in 50mV unit
151+
uint32_t current_peak : 2; // [21..20] Peak current
152+
uint32_t reserved : 1; // [22] Reserved
153+
uint32_t epr_mode_capable : 1; // [23] epr_mode_capable
154+
uint32_t unchunked_ext_msg_support : 1; // [24] UnChunked Extended Message Supported
155+
uint32_t dual_role_data : 1; // [25] Dual Role Data
156+
uint32_t usb_comm_capable : 1; // [26] USB Communications Capable
157+
uint32_t unconstrained_power : 1; // [27] Unconstrained Power
158+
uint32_t usb_suspend_supported : 1; // [28] USB Suspend Supported
159+
uint32_t dual_role_power : 1; // [29] Dual Role Power
160+
uint32_t type : 2; // [30] Fixed Supply type = PD_PDO_TYPE_FIXED
161+
} pd_pdo_fixed_t;
162+
TU_VERIFY_STATIC(sizeof(pd_pdo_fixed_t) == 4, "Invalid size");
163+
164+
// Battery Power Data Object (PDO) table 6-12
165+
typedef struct TU_ATTR_PACKED {
166+
uint32_t power_max_250mw : 10; // [9..0] Max allowable power in 250mW unit
167+
uint32_t voltage_min_50mv : 10; // [19..10] Minimum voltage in 50mV unit
168+
uint32_t voltage_max_50mv : 10; // [29..20] Maximum voltage in 50mV unit
169+
uint32_t type : 2; // [31..30] Battery type = PD_PDO_TYPE_BATTERY
170+
} pd_pdo_battery_t;
171+
TU_VERIFY_STATIC(sizeof(pd_pdo_battery_t) == 4, "Invalid size");
172+
173+
// Variable Power Data Object (PDO) table 6-11
174+
typedef struct TU_ATTR_PACKED {
175+
uint32_t current_max_10ma : 10; // [9..0] Max current in 10mA unit
176+
uint32_t voltage_min_50mv : 10; // [19..10] Minimum voltage in 50mV unit
177+
uint32_t voltage_max_50mv : 10; // [29..20] Maximum voltage in 50mV unit
178+
uint32_t type : 2; // [31..30] Variable Supply type = PD_PDO_TYPE_VARIABLE
179+
} pd_pdo_variable_t;
180+
TU_VERIFY_STATIC(sizeof(pd_pdo_variable_t) == 4, "Invalid size");
181+
182+
// Augmented Power Data Object (PDO) table 6-13
183+
typedef struct TU_ATTR_PACKED {
184+
uint32_t current_max_50ma : 7; // [6..0] Max current in 50mA unit
185+
uint32_t reserved1 : 1; // [7] Reserved
186+
uint32_t voltage_min_100mv : 8; // [15..8] Minimum Voltage in 100mV unit
187+
uint32_t reserved2 : 1; // [16] Reserved
188+
uint32_t voltage_max_100mv : 8; // [24..17] Maximum Voltage in 100mV unit
189+
uint32_t reserved3 : 2; // [26..25] Reserved
190+
uint32_t pps_power_limited : 1; // [27] PPS Power Limited
191+
uint32_t spr_programmable : 2; // [29..28] SPR Programmable Power Supply
192+
uint32_t type : 2; // [31..30] Augmented Power Data Object = PD_PDO_TYPE_APDO
193+
} pd_pdo_apdo_t;
194+
TU_VERIFY_STATIC(sizeof(pd_pdo_apdo_t) == 4, "Invalid size");
195+
196+
//--------------------------------------------------------------------+
197+
// Request
198+
//--------------------------------------------------------------------+
199+
200+
typedef struct TU_ATTR_PACKED {
201+
uint32_t current_extremum_10ma : 10; // [9..0] Max (give back = 0) or Min (give back = 1) current in 10mA unit
202+
uint32_t current_operate_10ma : 10; // [19..10] Operating current in 10mA unit
203+
uint32_t reserved : 2; // [21..20] Reserved
204+
uint32_t epr_mode_capable : 1; // [22] EPR mode capable
205+
uint32_t unchunked_ext_msg_support : 1; // [23] UnChunked Extended Message Supported
206+
uint32_t no_usb_suspend : 1; // [24] No USB Suspend
207+
uint32_t usb_comm_capable : 1; // [25] USB Communications Capable
208+
uint32_t capability_mismatch : 1; // [26] Capability Mismatch
209+
uint32_t give_back_flag : 1; // [27] GiveBack Flag: 0 = Max, 1 = Min
210+
uint32_t object_position : 4; // [31..28] Object Position
211+
} pd_rdo_fixed_variable_t;
212+
TU_VERIFY_STATIC(sizeof(pd_rdo_fixed_variable_t) == 4, "Invalid size");
213+
214+
typedef struct TU_ATTR_PACKED {
215+
uint32_t power_extremum_250mw : 10; // [9..0] Max (give back = 0) or Min (give back = 1) operating power in 250mW unit
216+
uint32_t power_operate_250mw : 10; // [19..10] Operating power in 250mW unit
217+
uint32_t reserved : 2; // [21..20] Reserved
218+
uint32_t epr_mode_capable : 1; // [22] EPR mode capable
219+
uint32_t unchunked_ext_msg_support : 1; // [23] UnChunked Extended Message Supported
220+
uint32_t no_usb_suspend : 1; // [24] No USB Suspend
221+
uint32_t usb_comm_capable : 1; // [25] USB Communications Capable
222+
uint32_t capability_mismatch : 1; // [26] Capability Mismatch
223+
uint32_t give_back_flag : 1; // [27] GiveBack Flag: 0 = Max, 1 = Min
224+
uint32_t object_position : 4; // [31..28] Object Position
225+
} pd_rdo_battery_t;
226+
TU_VERIFY_STATIC(sizeof(pd_rdo_battery_t) == 4, "Invalid size");
227+
228+
229+
TU_ATTR_PACKED_END // End of all packed definitions
230+
TU_ATTR_BIT_FIELD_ORDER_END
231+
232+
233+
#ifdef __cplusplus
234+
}
235+
#endif
236+
237+
#endif

0 commit comments

Comments
 (0)