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 6 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
 #define PD_MA2PDI(ma) ((ma) / 10)
248
 #define PD_MA2PDI(ma) ((ma) / 10)
249
 #define PD_PDI2MA(pdi) ((pdi) * 10)
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
 #define PD_PDV_V(pdv) ((pdv) / 20)
255
 #define PD_PDV_V(pdv) ((pdv) / 20)
253
 #define PD_PDV_CV(pdv) (5 * ((pdv) % 20))
256
 #define PD_PDV_CV(pdv) (5 * ((pdv) % 20))
254
 
257
 

+ 5
- 5
src/config.c View File

70
     chprintf(chp, "\r\n");
70
     chprintf(chp, "\r\n");
71
 
71
 
72
     /* Print voltage */
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
     /* Print current-deriving setting */
74
     /* Print current-deriving setting */
75
     switch (cfg->flags & PDBS_CONFIG_FLAGS_CURRENT_DEFN) {
75
     switch (cfg->flags & PDBS_CONFIG_FLAGS_CURRENT_DEFN) {
76
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_I:
76
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_I:
79
     }
79
     }
80
     /* If either end of the range is non-zero, print the range */
80
     /* If either end of the range is non-zero, print the range */
81
     if (cfg->vmin != 0 || cfg->vmax != 0) {
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
     uint16_t status;
33
     uint16_t status;
34
     /* Flags halfword for miscellaneous small fields. */
34
     /* Flags halfword for miscellaneous small fields. */
35
     uint16_t flags;
35
     uint16_t flags;
36
-    /* Preferred voltage. */
36
+    /* Preferred voltage, in millivolts. */
37
     uint16_t v;
37
     uint16_t v;
38
     /* Union for specifying how much current to request. */
38
     /* Union for specifying how much current to request. */
39
     union {
39
     union {
40
-        /* Required current. */
40
+        /* Required current, in centiamperes. */
41
         uint16_t i;
41
         uint16_t i;
42
-        /* Required power. */
42
+        /* Required power, in centiwatts. */
43
         uint16_t p;
43
         uint16_t p;
44
-        /* Value of resistive load. */
44
+        /* Value of resistive load, in centiohms. */
45
         uint16_t r;
45
         uint16_t r;
46
     };
46
     };
47
-    /* Lower end of voltage range. */
47
+    /* Lower end of voltage range, in millivolts. */
48
     uint16_t vmin;
48
     uint16_t vmin;
49
-    /* Upper end of voltage range. */
49
+    /* Upper end of voltage range, in millivolts. */
50
     uint16_t vmax;
50
     uint16_t vmax;
51
     /* Extra bytes reserved for future use. */
51
     /* Extra bytes reserved for future use. */
52
     uint16_t _reserved[2];
52
     uint16_t _reserved[2];

+ 9
- 9
src/device_policy_manager.c View File

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

+ 2
- 2
src/device_policy_manager.h View File

40
     bool _unconstrained_power;
40
     bool _unconstrained_power;
41
     /* Whether our capabilities matched or not */
41
     /* Whether our capabilities matched or not */
42
     bool _capability_match;
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
     int _present_voltage;
44
     int _present_voltage;
45
-    /* The requested voltage */
45
+    /* The requested voltage, in millivolts */
46
     int _requested_voltage;
46
     int _requested_voltage;
47
 };
47
 };
48
 
48
 

+ 1
- 1
src/main.c View File

63
     true,
63
     true,
64
     true,
64
     true,
65
     false,
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
     char *endptr;
308
     char *endptr;
309
     long i = strtol(argv[0], &endptr, 0);
309
     long i = strtol(argv[0], &endptr, 0);
310
     if (i >= PD_MV_MIN && i <= PD_MV_MAX && endptr > argv[0]) {
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
     } else {
312
     } else {
314
         chprintf(chp, "Invalid voltage\r\n");
313
         chprintf(chp, "Invalid voltage\r\n");
315
         return;
314
         return;

Loading…
Cancel
Save