34
34
// MACRO CONSTANT TYPEDEF PROTOTYPES
35
35
//--------------------------------------------------------------------+
36
36
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
+
37
44
/* Blink pattern
38
45
* - 250 ms : button is not pressed
39
46
* - 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
83
90
case PD_DATA_SOURCE_CAP : {
84
91
printf ("PD Source Capabilities\r\n" );
85
92
// 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 ;
87
94
88
95
for (size_t i = 0 ; i < header -> n_data_obj ; i ++ ) {
89
96
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
92
99
switch ((pdo >> 30 ) & 0x03ul ) {
93
100
case PD_PDO_TYPE_FIXED : {
94
101
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
+
96
111
break ;
97
112
}
98
113
@@ -110,7 +125,10 @@ bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t
110
125
}
111
126
112
127
//------------- 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 );
114
132
115
133
// Send request with selected PDO position as response to Source Cap
116
134
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
123
141
.usb_comm_capable = 1 ,
124
142
.capability_mismatch = 0 ,
125
143
.give_back_flag = 0 , // exteremum is max
126
- .object_position = obj_pos ,
144
+ .object_position = selected_pos ,
127
145
};
128
-
129
146
tuc_msg_request (rhport , & rdo );
130
147
131
148
break ;
0 commit comments