Skip to content

Commit 7a1179a

Browse files
committed
tested with 9V request
1 parent 6941a5a commit 7a1179a

File tree

1 file changed

+22
-5
lines changed
  • examples/typec/power_delivery/src

1 file changed

+22
-5
lines changed

examples/typec/power_delivery/src/main.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
// MACRO CONSTANT TYPEDEF PROTOTYPES
3535
//--------------------------------------------------------------------+
3636

37+
// Voltage and current for selecting PDO
38+
// DANGEROUS: Please make sure your board can withstand the voltage and current
39+
// defined here. Otherwise, you may damage your board, smoke can come out
40+
#define VOLTAGE_MAX_MV 5000 // maximum voltage in mV
41+
#define CURRENT_MAX_MA 500 // maximum current in mA
42+
#define CURRENT_OPERATING_MA 100 // operating current in mA
43+
3744
/* Blink pattern
3845
* - 250 ms : button is not pressed
3946
* - 1000 ms : button is pressed (and hold)
@@ -83,7 +90,7 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t
8390
case PD_DATA_SOURCE_CAP: {
8491
printf("PD Source Capabilities\r\n");
8592
// Examine source capability and select a suitable PDO (starting from 1 with safe5v)
86-
uint8_t obj_pos = 1;
93+
uint8_t selected_pos = 1;
8794

8895
for(size_t i=0; i<header->n_data_obj; i++) {
8996
TU_VERIFY(dobj < p_end);
@@ -92,7 +99,15 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t
9299
switch ((pdo >> 30) & 0x03ul) {
93100
case PD_PDO_TYPE_FIXED: {
94101
pd_pdo_fixed_t const* fixed = (pd_pdo_fixed_t const*) &pdo;
95-
printf("[Fixed] %u mV %u mA\r\n", fixed->voltage_50mv*50, fixed->current_max_10ma*10);
102+
uint32_t const voltage_mv = fixed->voltage_50mv*50;
103+
uint32_t const current_ma = fixed->current_max_10ma*10;
104+
printf("[Fixed] %lu mV %lu mA\r\n", voltage_mv, current_ma);
105+
106+
if (voltage_mv <= VOLTAGE_MAX_MV && current_ma >= CURRENT_MAX_MA) {
107+
// Found a suitable PDO
108+
selected_pos = i+1;
109+
}
110+
96111
break;
97112
}
98113

@@ -110,7 +125,10 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t
110125
}
111126

112127
//------------- Response with selected PDO -------------//
113-
// Be careful and make sure your board can withstand the selected PDO voltage other than safe5v e.g 12v or 20v
128+
// Be careful and make sure your board can withstand the selected PDO
129+
// voltage other than safe5v e.g 12v or 20v
130+
131+
printf("Selected PDO %u\r\n", selected_pos);
114132

115133
// Send request with selected PDO position as response to Source Cap
116134
pd_rdo_fixed_variable_t rdo = {
@@ -123,9 +141,8 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t
123141
.usb_comm_capable = 1,
124142
.capability_mismatch = 0,
125143
.give_back_flag = 0, // exteremum is max
126-
.object_position = obj_pos,
144+
.object_position = selected_pos,
127145
};
128-
129146
tuc_msg_request(rhport, &rdo);
130147

131148
break;

0 commit comments

Comments
 (0)