Browse Source

Use a struct pdbs_dpm_data for DPM globals

Not the static globals yet, just the public ones.  I suppose this change
isn't technically necessary since the PD Buddy Sink is a single-port
device, but I want its firmware to be a shining example of how to use
the library.
Clara Hobbs 7 years ago
parent
commit
ccc28c900b
5 changed files with 72 additions and 58 deletions
  1. 1
    1
      lib/include/pdb_pe.h
  2. 31
    29
      src/device_policy_manager.c
  3. 12
    14
      src/device_policy_manager.h
  4. 16
    4
      src/main.c
  5. 12
    10
      src/shell.c

+ 1
- 1
lib/include/pdb_pe.h View File

@@ -26,7 +26,7 @@
26 26
 
27 27
 
28 28
 struct pdb_pe {
29
-    THD_WORKING_AREA(_wa, 128);
29
+    THD_WORKING_AREA(_wa, 256);
30 30
     thread_t *thread;
31 31
 
32 32
     /* PE mailbox for received PD messages */

+ 31
- 29
src/device_policy_manager.c View File

@@ -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
 }

+ 12
- 14
src/device_policy_manager.h View File

@@ -26,20 +26,18 @@
26 26
 #include "messages.h"
27 27
 
28 28
 
29
-/* Whether the DPM is able to turn on the output */
30
-extern bool pdb_dpm_output_enabled;
31
-
32
-/* Whether the DPM sets the LED to indicate the PD status */
33
-extern bool pdb_dpm_led_pd_status;
34
-
35
-/* Whether the device is capable of USB communications */
36
-extern bool pdb_dpm_usb_comms;
37
-
38
-/* The most recently received Source_Capabilities message */
39
-extern const union pd_msg *pdb_dpm_capabilities;
40
-
41
-/* The most recently received Type-C Current advertisement */
42
-extern enum fusb_typec_current pdb_dpm_typec_current;
29
+struct pdbs_dpm_data {
30
+    /* The most recently received Source_Capabilities message */
31
+    const union pd_msg *capabilities;
32
+    /* The most recently received Type-C Current advertisement */
33
+    enum fusb_typec_current typec_current;
34
+    /* Whether the DPM is able to turn on the output */
35
+    bool output_enabled;
36
+    /* Whether the DPM sets the LED to indicate the PD status */
37
+    bool led_pd_status;
38
+    /* Whether the device is capable of USB communications */
39
+    bool usb_comms;
40
+};
43 41
 
44 42
 
45 43
 /*

+ 16
- 4
src/main.c View File

@@ -53,6 +53,17 @@ static const I2CConfig i2c2config = {
53 53
     0
54 54
 };
55 55
 
56
+/*
57
+ * PD Buddy Sink DPM data
58
+ */
59
+static struct pdbs_dpm_data dpm_data = {
60
+    NULL,
61
+    None,
62
+    true,
63
+    true,
64
+    false
65
+};
66
+
56 67
 /*
57 68
  * PD Buddy firmware library configuration object
58 69
  */
@@ -72,7 +83,8 @@ static struct pdb_config pdb_config = {
72 83
         pdbs_dpm_transition_standby,
73 84
         pdbs_dpm_transition_requested,
74 85
         pdbs_dpm_transition_requested /* XXX type-c current */
75
-    }
86
+    },
87
+    .dpm_data = &dpm_data
76 88
 };
77 89
 
78 90
 /*
@@ -81,9 +93,9 @@ static struct pdb_config pdb_config = {
81 93
 static void setup(void)
82 94
 {
83 95
     /* Configure the DPM to play nice with the shell */
84
-    pdb_dpm_output_enabled = false;
85
-    pdb_dpm_led_pd_status = false;
86
-    pdb_dpm_usb_comms = true;
96
+    dpm_data.output_enabled = false;
97
+    dpm_data.led_pd_status = false;
98
+    dpm_data.usb_comms = true;
87 99
 
88 100
     /* Start the USB Power Delivery threads */
89 101
     pdb_init(&pdb_config);

+ 12
- 10
src/shell.c View File

@@ -54,6 +54,7 @@ static struct pdbs_config tmpcfg = {
54 54
 
55 55
 /* Pointer to the PD Buddy firmware library configuration */
56 56
 static struct pdb_config *pdb_config;
57
+static struct pdbs_dpm_data *pdbs_dpm_data;
57 58
 
58 59
 /*
59 60
  * Helper functions for printing PDOs
@@ -323,7 +324,7 @@ static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
323 324
 {
324 325
     if (argc == 0) {
325 326
         /* With no arguments, print the output status */
326
-        if (pdb_dpm_output_enabled) {
327
+        if (pdbs_dpm_data->output_enabled) {
327 328
             chprintf(chp, "enabled\r\n");
328 329
         } else {
329 330
             chprintf(chp, "disabled\r\n");
@@ -331,10 +332,10 @@ static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
331 332
     } else if (argc == 1) {
332 333
         /* Set the output status and re-negotiate power */
333 334
         if (strcmp(argv[0], "enable") == 0) {
334
-            pdb_dpm_output_enabled = true;
335
+            pdbs_dpm_data->output_enabled = true;
335 336
             chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
336 337
         } else if (strcmp(argv[0], "disable") == 0) {
337
-            pdb_dpm_output_enabled = false;
338
+            pdbs_dpm_data->output_enabled = false;
338 339
             chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
339 340
         } else {
340 341
             /* Or, if the argument was invalid, print a usage message */
@@ -355,16 +356,16 @@ static void cmd_get_source_cap(BaseSequentialStream *chp, int argc, char *argv[]
355 356
     }
356 357
 
357 358
     /* If we haven't seen any Source_Capabilities */
358
-    if (pdb_dpm_capabilities == NULL) {
359
+    if (pdbs_dpm_data->capabilities == NULL) {
359 360
         /* Have we started reading Type-C Current advertisements? */
360
-        if (pdb_dpm_typec_current != None) {
361
+        if (pdbs_dpm_data->typec_current != None) {
361 362
             /* Type-C Current is available, so report it */
362 363
             chprintf(chp, "PDO 1: typec_virtual\r\n");
363
-            if (pdb_dpm_typec_current == Default) {
364
+            if (pdbs_dpm_data->typec_current == Default) {
364 365
                 chprintf(chp, "\ti: 0.50 A\r\n");
365
-            } else if (pdb_dpm_typec_current == OnePointFiveAmps) {
366
+            } else if (pdbs_dpm_data->typec_current == OnePointFiveAmps) {
366 367
                 chprintf(chp, "\ti: 1.50 A\r\n");
367
-            } else if (pdb_dpm_typec_current == ThreePointZeroAmps) {
368
+            } else if (pdbs_dpm_data->typec_current == ThreePointZeroAmps) {
368 369
                 chprintf(chp, "\ti: 3.00 A\r\n");
369 370
             }
370 371
             return;
@@ -376,9 +377,9 @@ static void cmd_get_source_cap(BaseSequentialStream *chp, int argc, char *argv[]
376 377
     }
377 378
 
378 379
     /* Print all the PDOs */
379
-    uint8_t numobj = PD_NUMOBJ_GET(pdb_dpm_capabilities);
380
+    uint8_t numobj = PD_NUMOBJ_GET(pdbs_dpm_data->capabilities);
380 381
     for (uint8_t i = 0; i < numobj; i++) {
381
-        print_src_pdo(chp, pdb_dpm_capabilities->obj[i], i+1);
382
+        print_src_pdo(chp, pdbs_dpm_data->capabilities->obj[i], i+1);
382 383
     }
383 384
 }
384 385
 
@@ -466,6 +467,7 @@ void pdbs_shell(struct pdb_config *cfg)
466 467
     char *args[PDB_SHELL_MAX_ARGUMENTS + 1];
467 468
 
468 469
     pdb_config = cfg;
470
+    pdbs_dpm_data = cfg->dpm_data;
469 471
 
470 472
     while (true) {
471 473
         /* Print the prompt */

Loading…
Cancel
Save