Browse Source

Use millivolts internally instead of PDV

This commit breaks old configuration by interpreting stored voltages as
millivolts, rather than 50 mV.  This will allow the finer voltage
increments used by programmable power supplies to be specified.
Clara Hobbs 7 years ago
parent
commit
2eda450aff
7 changed files with 28 additions and 26 deletions
  1. 4
    1
      lib/include/pd.h
  2. 5
    5
      src/config.c
  3. 6
    6
      src/config.h
  4. 9
    9
      src/device_policy_manager.c
  5. 2
    2
      src/device_policy_manager.h
  6. 1
    1
      src/main.c
  7. 1
    2
      src/shell.c

+ 4
- 1
lib/include/pd.h View File

@@ -248,7 +248,10 @@
248 248
 #define PD_MA2PDI(ma) ((ma) / 10)
249 249
 #define PD_PDI2MA(pdi) ((pdi) * 10)
250 250
 
251
-/* Get portions of a PD voltage in more normal units */
251
+/* Get portions of a voltage in more normal units */
252
+#define PD_MV_V(mv) ((mv) / 1000)
253
+#define PD_MV_MV(mv) ((mv) % 1000)
254
+
252 255
 #define PD_PDV_V(pdv) ((pdv) / 20)
253 256
 #define PD_PDV_CV(pdv) (5 * ((pdv) % 20))
254 257
 

+ 5
- 5
src/config.c View File

@@ -70,7 +70,7 @@ void pdbs_config_print(BaseSequentialStream *chp, const struct pdbs_config *cfg)
70 70
     chprintf(chp, "\r\n");
71 71
 
72 72
     /* Print voltage */
73
-    chprintf(chp, "v: %d.%02d V\r\n", PD_PDV_V(cfg->v), PD_PDV_CV(cfg->v));
73
+    chprintf(chp, "v: %d.%03d V\r\n", PD_MV_V(cfg->v), PD_MV_MV(cfg->v));
74 74
     /* Print current-deriving setting */
75 75
     switch (cfg->flags & PDBS_CONFIG_FLAGS_CURRENT_DEFN) {
76 76
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_I:
@@ -79,10 +79,10 @@ void pdbs_config_print(BaseSequentialStream *chp, const struct pdbs_config *cfg)
79 79
     }
80 80
     /* If either end of the range is non-zero, print the range */
81 81
     if (cfg->vmin != 0 || cfg->vmax != 0) {
82
-        chprintf(chp, "vmin: %d.%02d V\r\n", PD_PDV_V(cfg->vmin),
83
-                 PD_PDV_CV(cfg->vmin));
84
-        chprintf(chp, "vmax: %d.%02d V\r\n", PD_PDV_V(cfg->vmax),
85
-                 PD_PDV_CV(cfg->vmax));
82
+        chprintf(chp, "vmin: %d.%03d V\r\n", PD_MV_V(cfg->vmin),
83
+                 PD_MV_MV(cfg->vmin));
84
+        chprintf(chp, "vmax: %d.%03d V\r\n", PD_MV_V(cfg->vmax),
85
+                 PD_MV_MV(cfg->vmax));
86 86
     }
87 87
 }
88 88
 

+ 6
- 6
src/config.h View File

@@ -33,20 +33,20 @@ struct pdbs_config {
33 33
     uint16_t status;
34 34
     /* Flags halfword for miscellaneous small fields. */
35 35
     uint16_t flags;
36
-    /* Preferred voltage. */
36
+    /* Preferred voltage, in millivolts. */
37 37
     uint16_t v;
38 38
     /* Union for specifying how much current to request. */
39 39
     union {
40
-        /* Required current. */
40
+        /* Required current, in centiamperes. */
41 41
         uint16_t i;
42
-        /* Required power. */
42
+        /* Required power, in centiwatts. */
43 43
         uint16_t p;
44
-        /* Value of resistive load. */
44
+        /* Value of resistive load, in centiohms. */
45 45
         uint16_t r;
46 46
     };
47
-    /* Lower end of voltage range. */
47
+    /* Lower end of voltage range, in millivolts. */
48 48
     uint16_t vmin;
49
-    /* Upper end of voltage range. */
49
+    /* Upper end of voltage range, in millivolts. */
50 50
     uint16_t vmax;
51 51
     /* Extra bytes reserved for future use. */
52 52
     uint16_t _reserved[2];

+ 9
- 9
src/device_policy_manager.c View File

@@ -72,7 +72,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
72 72
             }
73 73
             /* If the V from the PDO equals our desired V and the I is at least
74 74
              * our desired I */
75
-            if (PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == scfg->v
75
+            if (PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == PD_MV2PDV(scfg->v)
76 76
                     && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i) {
77 77
                 /* We got what we wanted, so build a request for that */
78 78
                 request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
@@ -94,7 +94,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
94 94
                 }
95 95
 
96 96
                 /* Update requested voltage */
97
-                dpm_data->_requested_voltage = scfg->v;
97
+                dpm_data->_requested_voltage = PD_PDV2MV(PD_MV2PDV(scfg->v));
98 98
 
99 99
                 dpm_data->_capability_match = true;
100 100
                 return true;
@@ -118,7 +118,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
118 118
     }
119 119
 
120 120
     /* Update requested voltage */
121
-    dpm_data->_requested_voltage = PD_MV2PDV(5000);
121
+    dpm_data->_requested_voltage = 5000;
122 122
 
123 123
     /* At this point, we have a capability match iff the output is disabled */
124 124
     dpm_data->_capability_match = !dpm_data->output_enabled;
@@ -136,7 +136,7 @@ void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
136 136
 
137 137
     /* If we have no configuration or want something other than 5 V, add a PDO
138 138
      * for vSafe5V */
139
-    if (scfg == NULL || scfg->v != PD_MV2PDV(5000)) {
139
+    if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
140 140
         /* Minimum current, 5 V, and higher capability. */
141 141
         cap->obj[numobj++] = PD_PDO_TYPE_FIXED
142 142
             | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(5000))
@@ -146,10 +146,10 @@ void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
146 146
     /* Add a PDO for the desired power. */
147 147
     if (scfg != NULL) {
148 148
         cap->obj[numobj++] = PD_PDO_TYPE_FIXED
149
-            | PD_PDO_SNK_FIXED_VOLTAGE_SET(scfg->v)
149
+            | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(scfg->v))
150 150
             | PD_PDO_SNK_FIXED_CURRENT_SET(scfg->i);
151 151
         /* If we want more than 5 V, set the Higher Capability flag */
152
-        if (scfg->v != PD_MV2PDV(5000)) {
152
+        if (PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
153 153
             cap->obj[0] |= PD_PDO_SNK_FIXED_HIGHER_CAP;
154 154
         }
155 155
     }
@@ -184,7 +184,7 @@ bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
184 184
     struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
185 185
 
186 186
     /* We don't control the voltage anymore; it will always be 5 V. */
187
-    dpm_data->_requested_voltage = PD_MV2PDV(5000);
187
+    dpm_data->_requested_voltage = 5000;
188 188
 
189 189
     /* Make the present Type-C Current advertisement available to the rest of
190 190
      * the DPM */
@@ -192,7 +192,7 @@ bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
192 192
 
193 193
     /* If we have no configuration or don't want 5 V, Type-C Current can't
194 194
      * possibly satisfy our needs */
195
-    if (scfg == NULL || scfg->v != PD_MV2PDV(5000)) {
195
+    if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
196 196
         dpm_data->_capability_match = false;
197 197
         return false;
198 198
     }
@@ -256,7 +256,7 @@ void pdbs_dpm_transition_default(struct pdb_config *cfg)
256 256
     struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
257 257
 
258 258
     /* Pretend we requested 5 V */
259
-    dpm_data->_requested_voltage = PD_MV2PDV(5000);
259
+    dpm_data->_requested_voltage = 5000;
260 260
     /* Turn the output off */
261 261
     dpm_output_set(cfg->dpm_data, false);
262 262
 }

+ 2
- 2
src/device_policy_manager.h View File

@@ -40,9 +40,9 @@ struct pdbs_dpm_data {
40 40
     bool _unconstrained_power;
41 41
     /* Whether our capabilities matched or not */
42 42
     bool _capability_match;
43
-    /* The last explicitly or implicitly negotiated voltage in PDV */
43
+    /* The last explicitly or implicitly negotiated voltage, in millivolts */
44 44
     int _present_voltage;
45
-    /* The requested voltage */
45
+    /* The requested voltage, in millivolts */
46 46
     int _requested_voltage;
47 47
 };
48 48
 

+ 1
- 1
src/main.c View File

@@ -63,7 +63,7 @@ static struct pdbs_dpm_data dpm_data = {
63 63
     true,
64 64
     true,
65 65
     false,
66
-    ._present_voltage = PD_MV2PDV(5000)
66
+    ._present_voltage = 5000
67 67
 };
68 68
 
69 69
 /*

+ 1
- 2
src/shell.c View File

@@ -308,8 +308,7 @@ static void cmd_set_v(BaseSequentialStream *chp, int argc, char *argv[])
308 308
     char *endptr;
309 309
     long i = strtol(argv[0], &endptr, 0);
310 310
     if (i >= PD_MV_MIN && i <= PD_MV_MAX && endptr > argv[0]) {
311
-        /* Convert mV to the unit used by USB PD */
312
-        tmpcfg.v = PD_MV2PDV(i);
311
+        tmpcfg.v = i;
313 312
     } else {
314 313
         chprintf(chp, "Invalid voltage\r\n");
315 314
         return;

Loading…
Cancel
Save