1
1
//! Handles USB PD negotiation.
2
- use defmt:: { error , info, warn, Format } ;
2
+ use defmt:: { info, warn, Format } ;
3
3
use embassy_futures:: select:: { select, Either } ;
4
4
use embassy_stm32:: gpio:: Output ;
5
5
use embassy_stm32:: ucpd:: { self , CcPhy , CcPull , CcSel , CcVState , PdPhy , Ucpd } ;
@@ -119,20 +119,32 @@ impl SinkTimer for EmbassySinkTimer {
119
119
}
120
120
}
121
121
122
+ /// Capabilities that are tested (cycled through each second).
123
+ #[ derive( Format ) ]
124
+ enum TestCapabilities {
125
+ Safe5V ,
126
+ Pps3V6 ,
127
+ Pps5V5 ,
128
+ Fixed9V ,
129
+ Max ,
130
+ }
131
+
122
132
struct Device {
123
- target_voltage : ElectricPotential ,
133
+ test_capabilities : TestCapabilities ,
124
134
}
125
135
126
136
impl Default for Device {
127
137
fn default ( ) -> Self {
128
138
Self {
129
- target_voltage : ElectricPotential :: new :: < electric_potential :: volt > ( 5 ) ,
139
+ test_capabilities : TestCapabilities :: Safe5V ,
130
140
}
131
141
}
132
142
}
133
143
134
144
impl DevicePolicyManager for Device {
135
145
async fn request ( & mut self , source_capabilities : & SourceCapabilities ) -> request:: PowerSource {
146
+ info ! ( "Found capabilities: {}" , source_capabilities) ;
147
+
136
148
request:: PowerSource :: new_fixed (
137
149
request:: CurrentRequest :: Highest ,
138
150
request:: VoltageRequest :: Safe5V ,
@@ -143,32 +155,54 @@ impl DevicePolicyManager for Device {
143
155
144
156
async fn get_event ( & mut self , source_capabilities : & SourceCapabilities ) -> Event {
145
157
// Periodically request another power level.
146
- Timer :: after_secs ( 5 ) . await ;
147
-
148
- let power_source = match request:: PowerSource :: new_pps (
149
- request:: CurrentRequest :: Highest ,
150
- self . target_voltage ,
151
- source_capabilities,
152
- ) {
153
- Ok ( power_source) => {
154
- self . target_voltage += ElectricPotential :: new :: < electric_potential:: millivolt > ( 100 ) ;
155
- info ! ( "Requesting {}" , power_source, ) ;
158
+ Timer :: after_secs ( 1 ) . await ;
159
+
160
+ info ! ( "Test capabilities: {}" , self . test_capabilities) ;
161
+ let ( power_source, new_test_capabilities) = match self . test_capabilities {
162
+ TestCapabilities :: Safe5V => (
163
+ request:: PowerSource :: new_fixed ( CurrentRequest :: Highest , VoltageRequest :: Safe5V , source_capabilities) ,
164
+ TestCapabilities :: Fixed9V ,
165
+ ) ,
166
+ TestCapabilities :: Fixed9V => (
167
+ request:: PowerSource :: new_fixed (
168
+ CurrentRequest :: Highest ,
169
+ VoltageRequest :: Specific ( ElectricPotential :: new :: < electric_potential:: volt > ( 9 ) ) ,
170
+ source_capabilities,
171
+ ) ,
172
+ TestCapabilities :: Pps3V6 ,
173
+ ) ,
174
+ TestCapabilities :: Pps3V6 => (
175
+ request:: PowerSource :: new_pps (
176
+ request:: CurrentRequest :: Highest ,
177
+ ElectricPotential :: new :: < electric_potential:: millivolt > ( 3600 ) ,
178
+ source_capabilities,
179
+ ) ,
180
+ TestCapabilities :: Pps5V5 ,
181
+ ) ,
182
+ TestCapabilities :: Pps5V5 => (
183
+ request:: PowerSource :: new_pps (
184
+ request:: CurrentRequest :: Highest ,
185
+ ElectricPotential :: new :: < electric_potential:: millivolt > ( 5500 ) ,
186
+ source_capabilities,
187
+ ) ,
188
+ TestCapabilities :: Max ,
189
+ ) ,
190
+ TestCapabilities :: Max => (
191
+ request:: PowerSource :: new_fixed ( CurrentRequest :: Highest , VoltageRequest :: Highest , source_capabilities) ,
192
+ TestCapabilities :: Safe5V ,
193
+ ) ,
194
+ } ;
156
195
157
- power_source
158
- }
159
- Err ( _) => {
160
- error ! (
161
- "Cannot request {} mV - fall back to default voltage" ,
162
- self . target_voltage. get:: <electric_potential:: millivolt>( )
163
- ) ;
164
- self . target_voltage = ElectricPotential :: new :: < electric_potential:: volt > ( 5 ) ;
165
-
166
- request:: PowerSource :: new_fixed ( CurrentRequest :: Highest , VoltageRequest :: Safe5V , source_capabilities)
167
- . unwrap ( )
168
- }
196
+ let event = if let Ok ( power_source) = power_source {
197
+ info ! ( "Requesting power source {}" , power_source) ;
198
+ Event :: RequestPower ( power_source)
199
+ } else {
200
+ warn ! ( "Capabilities not available {}" , self . test_capabilities) ;
201
+ Event :: None
169
202
} ;
170
203
171
- Event :: RequestPower ( power_source)
204
+ self . test_capabilities = new_test_capabilities;
205
+ event
172
206
}
173
207
}
174
208
0 commit comments