|
@@ -27,14 +27,6 @@
|
27
|
27
|
#include "pd.h"
|
28
|
28
|
|
29
|
29
|
|
30
|
|
-bool pdb_dpm_output_enabled = true;
|
31
|
|
-bool pdb_dpm_led_pd_status = true;
|
32
|
|
-bool pdb_dpm_usb_comms = false;
|
33
|
|
-
|
34
|
|
-const union pd_msg *pdb_dpm_capabilities = NULL;
|
35
|
|
-enum fusb_typec_current pdb_dpm_typec_current = None;
|
36
|
|
-
|
37
|
|
-
|
38
|
30
|
/* The current draw when the output is disabled */
|
39
|
31
|
#define DPM_MIN_CURRENT PD_MA2PDI(100)
|
40
|
32
|
|
|
@@ -53,15 +45,18 @@ static bool dpm_capability_match;
|
53
|
45
|
bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
|
54
|
46
|
const union pd_msg *capabilities, union pd_msg *request)
|
55
|
47
|
{
|
|
48
|
+ /* Cast the dpm_data to the right type */
|
|
49
|
+ struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
|
|
50
|
+
|
56
|
51
|
/* Update the stored Source_Capabilities */
|
57
|
52
|
if (capabilities != NULL) {
|
58
|
|
- if (pdb_dpm_capabilities != NULL) {
|
59
|
|
- chPoolFree(&pdb_msg_pool, (union pd_msg *) pdb_dpm_capabilities);
|
|
53
|
+ if (dpm_data->capabilities != NULL) {
|
|
54
|
+ chPoolFree(&pdb_msg_pool, (union pd_msg *) dpm_data->capabilities);
|
60
|
55
|
}
|
61
|
|
- pdb_dpm_capabilities = capabilities;
|
|
56
|
+ dpm_data->capabilities = capabilities;
|
62
|
57
|
} else {
|
63
|
58
|
/* No new capabilities; use a shorter name for the stored ones. */
|
64
|
|
- capabilities = pdb_dpm_capabilities;
|
|
59
|
+ capabilities = dpm_data->capabilities;
|
65
|
60
|
}
|
66
|
61
|
|
67
|
62
|
/* Get the current configuration */
|
|
@@ -70,7 +65,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
|
70
|
65
|
uint8_t numobj = PD_NUMOBJ_GET(capabilities);
|
71
|
66
|
|
72
|
67
|
/* Make the LED blink to indicate ongoing power negotiations */
|
73
|
|
- if (pdb_dpm_led_pd_status) {
|
|
68
|
+ if (dpm_data->led_pd_status) {
|
74
|
69
|
chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
|
75
|
70
|
}
|
76
|
71
|
|
|
@@ -78,7 +73,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
|
78
|
73
|
dpm_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
|
79
|
74
|
|
80
|
75
|
/* Make sure we have configuration */
|
81
|
|
- if (scfg != NULL && pdb_dpm_output_enabled) {
|
|
76
|
+ if (scfg != NULL && dpm_data->output_enabled) {
|
82
|
77
|
/* Look at the PDOs to see if one matches our desires */
|
83
|
78
|
for (uint8_t i = 0; i < numobj; i++) {
|
84
|
79
|
/* Fixed Supply PDOs come first, so when we see a PDO that isn't a
|
|
@@ -105,7 +100,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
|
105
|
100
|
| PD_RDO_FV_CURRENT_SET(scfg->i)
|
106
|
101
|
| PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
|
107
|
102
|
}
|
108
|
|
- if (pdb_dpm_usb_comms) {
|
|
103
|
+ if (dpm_data->usb_comms) {
|
109
|
104
|
request->obj[0] |= PD_RDO_USB_COMMS;
|
110
|
105
|
}
|
111
|
106
|
|
|
@@ -126,11 +121,11 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
|
126
|
121
|
| PD_RDO_OBJPOS_SET(1);
|
127
|
122
|
/* If the output is enabled and we got here, it must be a capability
|
128
|
123
|
* mismatch. */
|
129
|
|
- if (pdb_dpm_output_enabled) {
|
|
124
|
+ if (dpm_data->output_enabled) {
|
130
|
125
|
request->obj[0] |= PD_RDO_CAP_MISMATCH;
|
131
|
126
|
}
|
132
|
127
|
/* If we can do USB communications, tell the power supply */
|
133
|
|
- if (pdb_dpm_usb_comms) {
|
|
128
|
+ if (dpm_data->usb_comms) {
|
134
|
129
|
request->obj[0] |= PD_RDO_USB_COMMS;
|
135
|
130
|
}
|
136
|
131
|
|
|
@@ -138,8 +133,8 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
|
138
|
133
|
dpm_requested_voltage = PD_MV2PDV(5000);
|
139
|
134
|
|
140
|
135
|
/* At this point, we have a capability match iff the output is disabled */
|
141
|
|
- dpm_capability_match = !pdb_dpm_output_enabled;
|
142
|
|
- return !pdb_dpm_output_enabled;
|
|
136
|
+ dpm_capability_match = !dpm_data->output_enabled;
|
|
137
|
+ return !dpm_data->output_enabled;
|
143
|
138
|
}
|
144
|
139
|
|
145
|
140
|
void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
|
|
@@ -148,6 +143,8 @@ void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
|
148
|
143
|
int numobj = 0;
|
149
|
144
|
/* Get the current configuration */
|
150
|
145
|
struct pdbs_config *scfg = pdbs_config_flash_read();
|
|
146
|
+ /* Cast the dpm_data to the right type */
|
|
147
|
+ struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
|
151
|
148
|
|
152
|
149
|
/* If we have no configuration or want something other than 5 V, add a PDO
|
153
|
150
|
* for vSafe5V */
|
|
@@ -175,7 +172,7 @@ void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
|
175
|
172
|
}
|
176
|
173
|
|
177
|
174
|
/* Set the USB communications capable flag. */
|
178
|
|
- if (pdb_dpm_usb_comms) {
|
|
175
|
+ if (dpm_data->usb_comms) {
|
179
|
176
|
cap->obj[0] |= PD_PDO_SNK_FIXED_USB_COMMS;
|
180
|
177
|
}
|
181
|
178
|
|
|
@@ -195,13 +192,15 @@ bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
|
195
|
192
|
enum fusb_typec_current tcc)
|
196
|
193
|
{
|
197
|
194
|
struct pdbs_config *scfg = pdbs_config_flash_read();
|
|
195
|
+ /* Cast the dpm_data to the right type */
|
|
196
|
+ struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
|
198
|
197
|
|
199
|
198
|
/* We don't control the voltage anymore; it will always be 5 V. */
|
200
|
199
|
dpm_requested_voltage = PD_MV2PDV(5000);
|
201
|
200
|
|
202
|
201
|
/* Make the present Type-C Current advertisement available to the rest of
|
203
|
202
|
* the DPM */
|
204
|
|
- pdb_dpm_typec_current = tcc;
|
|
203
|
+ dpm_data->typec_current = tcc;
|
205
|
204
|
|
206
|
205
|
/* If we have no configuration or don't want 5 V, Type-C Current can't
|
207
|
206
|
* possibly satisfy our needs */
|
|
@@ -231,7 +230,10 @@ bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
|
231
|
230
|
|
232
|
231
|
void pdbs_dpm_pd_start(struct pdb_config *cfg)
|
233
|
232
|
{
|
234
|
|
- if (pdb_dpm_led_pd_status) {
|
|
233
|
+ /* Cast the dpm_data to the right type */
|
|
234
|
+ struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
|
|
235
|
+
|
|
236
|
+ if (dpm_data->led_pd_status) {
|
235
|
237
|
chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
|
236
|
238
|
}
|
237
|
239
|
}
|
|
@@ -239,21 +241,21 @@ void pdbs_dpm_pd_start(struct pdb_config *cfg)
|
239
|
241
|
/*
|
240
|
242
|
* Set the output state, with LED indication.
|
241
|
243
|
*/
|
242
|
|
-static void dpm_output_set(bool state)
|
|
244
|
+static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
|
243
|
245
|
{
|
244
|
246
|
/* Update the present voltage */
|
245
|
247
|
dpm_present_voltage = dpm_requested_voltage;
|
246
|
248
|
|
247
|
249
|
/* Set the power output */
|
248
|
|
- if (state && pdb_dpm_output_enabled) {
|
|
250
|
+ if (state && dpm_data->output_enabled) {
|
249
|
251
|
/* Turn the output on */
|
250
|
|
- if (pdb_dpm_led_pd_status) {
|
|
252
|
+ if (dpm_data->led_pd_status) {
|
251
|
253
|
chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_ON);
|
252
|
254
|
}
|
253
|
255
|
palSetLine(LINE_OUT_CTRL);
|
254
|
256
|
} else {
|
255
|
257
|
/* Turn the output off */
|
256
|
|
- if (pdb_dpm_led_pd_status) {
|
|
258
|
+ if (dpm_data->led_pd_status) {
|
257
|
259
|
chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_OFF);
|
258
|
260
|
}
|
259
|
261
|
palClearLine(LINE_OUT_CTRL);
|
|
@@ -265,12 +267,12 @@ void pdbs_dpm_transition_default(struct pdb_config *cfg)
|
265
|
267
|
/* Pretend we requested 5 V */
|
266
|
268
|
dpm_requested_voltage = PD_MV2PDV(5000);
|
267
|
269
|
/* Turn the output off */
|
268
|
|
- dpm_output_set(false);
|
|
270
|
+ dpm_output_set(cfg->dpm_data, false);
|
269
|
271
|
}
|
270
|
272
|
|
271
|
273
|
void pdbs_dpm_transition_min(struct pdb_config *cfg)
|
272
|
274
|
{
|
273
|
|
- dpm_output_set(false);
|
|
275
|
+ dpm_output_set(cfg->dpm_data, false);
|
274
|
276
|
}
|
275
|
277
|
|
276
|
278
|
void pdbs_dpm_transition_standby(struct pdb_config *cfg)
|
|
@@ -286,5 +288,5 @@ void pdbs_dpm_transition_standby(struct pdb_config *cfg)
|
286
|
288
|
|
287
|
289
|
void pdbs_dpm_transition_requested(struct pdb_config *cfg)
|
288
|
290
|
{
|
289
|
|
- dpm_output_set(dpm_capability_match);
|
|
291
|
+ dpm_output_set(cfg->dpm_data, dpm_capability_match);
|
290
|
292
|
}
|