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 6 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
 
26
 
27
 
27
 
28
 struct pdb_pe {
28
 struct pdb_pe {
29
-    THD_WORKING_AREA(_wa, 128);
29
+    THD_WORKING_AREA(_wa, 256);
30
     thread_t *thread;
30
     thread_t *thread;
31
 
31
 
32
     /* PE mailbox for received PD messages */
32
     /* PE mailbox for received PD messages */

+ 31
- 29
src/device_policy_manager.c View File

27
 #include "pd.h"
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
 /* The current draw when the output is disabled */
30
 /* The current draw when the output is disabled */
39
 #define DPM_MIN_CURRENT PD_MA2PDI(100)
31
 #define DPM_MIN_CURRENT PD_MA2PDI(100)
40
 
32
 
53
 bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
45
 bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
54
         const union pd_msg *capabilities, union pd_msg *request)
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
     /* Update the stored Source_Capabilities */
51
     /* Update the stored Source_Capabilities */
57
     if (capabilities != NULL) {
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
     } else {
57
     } else {
63
         /* No new capabilities; use a shorter name for the stored ones. */
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
     /* Get the current configuration */
62
     /* Get the current configuration */
70
     uint8_t numobj = PD_NUMOBJ_GET(capabilities);
65
     uint8_t numobj = PD_NUMOBJ_GET(capabilities);
71
 
66
 
72
     /* Make the LED blink to indicate ongoing power negotiations */
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
         chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
69
         chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
75
     }
70
     }
76
 
71
 
78
     dpm_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
73
     dpm_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
79
 
74
 
80
     /* Make sure we have configuration */
75
     /* Make sure we have configuration */
81
-    if (scfg != NULL && pdb_dpm_output_enabled) {
76
+    if (scfg != NULL && dpm_data->output_enabled) {
82
         /* Look at the PDOs to see if one matches our desires */
77
         /* Look at the PDOs to see if one matches our desires */
83
         for (uint8_t i = 0; i < numobj; i++) {
78
         for (uint8_t i = 0; i < numobj; i++) {
84
             /* Fixed Supply PDOs come first, so when we see a PDO that isn't a
79
             /* Fixed Supply PDOs come first, so when we see a PDO that isn't a
105
                         | PD_RDO_FV_CURRENT_SET(scfg->i)
100
                         | PD_RDO_FV_CURRENT_SET(scfg->i)
106
                         | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
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
                     request->obj[0] |= PD_RDO_USB_COMMS;
104
                     request->obj[0] |= PD_RDO_USB_COMMS;
110
                 }
105
                 }
111
 
106
 
126
         | PD_RDO_OBJPOS_SET(1);
121
         | PD_RDO_OBJPOS_SET(1);
127
     /* If the output is enabled and we got here, it must be a capability
122
     /* If the output is enabled and we got here, it must be a capability
128
      * mismatch. */
123
      * mismatch. */
129
-    if (pdb_dpm_output_enabled) {
124
+    if (dpm_data->output_enabled) {
130
         request->obj[0] |= PD_RDO_CAP_MISMATCH;
125
         request->obj[0] |= PD_RDO_CAP_MISMATCH;
131
     }
126
     }
132
     /* If we can do USB communications, tell the power supply */
127
     /* If we can do USB communications, tell the power supply */
133
-    if (pdb_dpm_usb_comms) {
128
+    if (dpm_data->usb_comms) {
134
         request->obj[0] |= PD_RDO_USB_COMMS;
129
         request->obj[0] |= PD_RDO_USB_COMMS;
135
     }
130
     }
136
 
131
 
138
     dpm_requested_voltage = PD_MV2PDV(5000);
133
     dpm_requested_voltage = PD_MV2PDV(5000);
139
 
134
 
140
     /* At this point, we have a capability match iff the output is disabled */
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
 void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
140
 void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
148
     int numobj = 0;
143
     int numobj = 0;
149
     /* Get the current configuration */
144
     /* Get the current configuration */
150
     struct pdbs_config *scfg = pdbs_config_flash_read();
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
     /* If we have no configuration or want something other than 5 V, add a PDO
149
     /* If we have no configuration or want something other than 5 V, add a PDO
153
      * for vSafe5V */
150
      * for vSafe5V */
175
     }
172
     }
176
 
173
 
177
     /* Set the USB communications capable flag. */
174
     /* Set the USB communications capable flag. */
178
-    if (pdb_dpm_usb_comms) {
175
+    if (dpm_data->usb_comms) {
179
         cap->obj[0] |= PD_PDO_SNK_FIXED_USB_COMMS;
176
         cap->obj[0] |= PD_PDO_SNK_FIXED_USB_COMMS;
180
     }
177
     }
181
 
178
 
195
         enum fusb_typec_current tcc)
192
         enum fusb_typec_current tcc)
196
 {
193
 {
197
     struct pdbs_config *scfg = pdbs_config_flash_read();
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
     /* We don't control the voltage anymore; it will always be 5 V. */
198
     /* We don't control the voltage anymore; it will always be 5 V. */
200
     dpm_requested_voltage = PD_MV2PDV(5000);
199
     dpm_requested_voltage = PD_MV2PDV(5000);
201
 
200
 
202
     /* Make the present Type-C Current advertisement available to the rest of
201
     /* Make the present Type-C Current advertisement available to the rest of
203
      * the DPM */
202
      * the DPM */
204
-    pdb_dpm_typec_current = tcc;
203
+    dpm_data->typec_current = tcc;
205
 
204
 
206
     /* If we have no configuration or don't want 5 V, Type-C Current can't
205
     /* If we have no configuration or don't want 5 V, Type-C Current can't
207
      * possibly satisfy our needs */
206
      * possibly satisfy our needs */
231
 
230
 
232
 void pdbs_dpm_pd_start(struct pdb_config *cfg)
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
         chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
237
         chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
236
     }
238
     }
237
 }
239
 }
239
 /*
241
 /*
240
  * Set the output state, with LED indication.
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
     /* Update the present voltage */
246
     /* Update the present voltage */
245
     dpm_present_voltage = dpm_requested_voltage;
247
     dpm_present_voltage = dpm_requested_voltage;
246
 
248
 
247
     /* Set the power output */
249
     /* Set the power output */
248
-    if (state && pdb_dpm_output_enabled) {
250
+    if (state && dpm_data->output_enabled) {
249
         /* Turn the output on */
251
         /* Turn the output on */
250
-        if (pdb_dpm_led_pd_status) {
252
+        if (dpm_data->led_pd_status) {
251
             chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_ON);
253
             chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_ON);
252
         }
254
         }
253
         palSetLine(LINE_OUT_CTRL);
255
         palSetLine(LINE_OUT_CTRL);
254
     } else {
256
     } else {
255
         /* Turn the output off */
257
         /* Turn the output off */
256
-        if (pdb_dpm_led_pd_status) {
258
+        if (dpm_data->led_pd_status) {
257
             chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_OFF);
259
             chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_OFF);
258
         }
260
         }
259
         palClearLine(LINE_OUT_CTRL);
261
         palClearLine(LINE_OUT_CTRL);
265
     /* Pretend we requested 5 V */
267
     /* Pretend we requested 5 V */
266
     dpm_requested_voltage = PD_MV2PDV(5000);
268
     dpm_requested_voltage = PD_MV2PDV(5000);
267
     /* Turn the output off */
269
     /* Turn the output off */
268
-    dpm_output_set(false);
270
+    dpm_output_set(cfg->dpm_data, false);
269
 }
271
 }
270
 
272
 
271
 void pdbs_dpm_transition_min(struct pdb_config *cfg)
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
 void pdbs_dpm_transition_standby(struct pdb_config *cfg)
278
 void pdbs_dpm_transition_standby(struct pdb_config *cfg)
286
 
288
 
287
 void pdbs_dpm_transition_requested(struct pdb_config *cfg)
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
 #include "messages.h"
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
     0
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
  * PD Buddy firmware library configuration object
68
  * PD Buddy firmware library configuration object
58
  */
69
  */
72
         pdbs_dpm_transition_standby,
83
         pdbs_dpm_transition_standby,
73
         pdbs_dpm_transition_requested,
84
         pdbs_dpm_transition_requested,
74
         pdbs_dpm_transition_requested /* XXX type-c current */
85
         pdbs_dpm_transition_requested /* XXX type-c current */
75
-    }
86
+    },
87
+    .dpm_data = &dpm_data
76
 };
88
 };
77
 
89
 
78
 /*
90
 /*
81
 static void setup(void)
93
 static void setup(void)
82
 {
94
 {
83
     /* Configure the DPM to play nice with the shell */
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
     /* Start the USB Power Delivery threads */
100
     /* Start the USB Power Delivery threads */
89
     pdb_init(&pdb_config);
101
     pdb_init(&pdb_config);

+ 12
- 10
src/shell.c View File

54
 
54
 
55
 /* Pointer to the PD Buddy firmware library configuration */
55
 /* Pointer to the PD Buddy firmware library configuration */
56
 static struct pdb_config *pdb_config;
56
 static struct pdb_config *pdb_config;
57
+static struct pdbs_dpm_data *pdbs_dpm_data;
57
 
58
 
58
 /*
59
 /*
59
  * Helper functions for printing PDOs
60
  * Helper functions for printing PDOs
323
 {
324
 {
324
     if (argc == 0) {
325
     if (argc == 0) {
325
         /* With no arguments, print the output status */
326
         /* With no arguments, print the output status */
326
-        if (pdb_dpm_output_enabled) {
327
+        if (pdbs_dpm_data->output_enabled) {
327
             chprintf(chp, "enabled\r\n");
328
             chprintf(chp, "enabled\r\n");
328
         } else {
329
         } else {
329
             chprintf(chp, "disabled\r\n");
330
             chprintf(chp, "disabled\r\n");
331
     } else if (argc == 1) {
332
     } else if (argc == 1) {
332
         /* Set the output status and re-negotiate power */
333
         /* Set the output status and re-negotiate power */
333
         if (strcmp(argv[0], "enable") == 0) {
334
         if (strcmp(argv[0], "enable") == 0) {
334
-            pdb_dpm_output_enabled = true;
335
+            pdbs_dpm_data->output_enabled = true;
335
             chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
336
             chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
336
         } else if (strcmp(argv[0], "disable") == 0) {
337
         } else if (strcmp(argv[0], "disable") == 0) {
337
-            pdb_dpm_output_enabled = false;
338
+            pdbs_dpm_data->output_enabled = false;
338
             chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
339
             chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
339
         } else {
340
         } else {
340
             /* Or, if the argument was invalid, print a usage message */
341
             /* Or, if the argument was invalid, print a usage message */
355
     }
356
     }
356
 
357
 
357
     /* If we haven't seen any Source_Capabilities */
358
     /* If we haven't seen any Source_Capabilities */
358
-    if (pdb_dpm_capabilities == NULL) {
359
+    if (pdbs_dpm_data->capabilities == NULL) {
359
         /* Have we started reading Type-C Current advertisements? */
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
             /* Type-C Current is available, so report it */
362
             /* Type-C Current is available, so report it */
362
             chprintf(chp, "PDO 1: typec_virtual\r\n");
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
                 chprintf(chp, "\ti: 0.50 A\r\n");
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
                 chprintf(chp, "\ti: 1.50 A\r\n");
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
                 chprintf(chp, "\ti: 3.00 A\r\n");
369
                 chprintf(chp, "\ti: 3.00 A\r\n");
369
             }
370
             }
370
             return;
371
             return;
376
     }
377
     }
377
 
378
 
378
     /* Print all the PDOs */
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
     for (uint8_t i = 0; i < numobj; i++) {
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
     char *args[PDB_SHELL_MAX_ARGUMENTS + 1];
467
     char *args[PDB_SHELL_MAX_ARGUMENTS + 1];
467
 
468
 
468
     pdb_config = cfg;
469
     pdb_config = cfg;
470
+    pdbs_dpm_data = cfg->dpm_data;
469
 
471
 
470
     while (true) {
472
     while (true) {
471
         /* Print the prompt */
473
         /* Print the prompt */

Loading…
Cancel
Save