Browse Source

Added unit conversion macros

PD uses odd units for voltages and currents.  Power too, but we don't
work with power (yet).  In the shell, we have to convert to and from
these odd units.  We had been doing this in-place every time, which was
a bit crumby.  Now it's done with macros in pd.h, making prettier and
more maintainable code.
Clara Hobbs 7 years ago
parent
commit
b5d499bf1f
3 changed files with 38 additions and 10 deletions
  1. 27
    0
      src/pd.h
  2. 3
    6
      src/shell.c
  3. 8
    4
      src/storage.c

+ 27
- 0
src/pd.h View File

216
 #define PD_T_TYPEC_SINK_WAIT_CAP MS2ST(465)
216
 #define PD_T_TYPEC_SINK_WAIT_CAP MS2ST(465)
217
 
217
 
218
 
218
 
219
+/*
220
+ * Unit conversions
221
+ *
222
+ * V: volt
223
+ * CV: centivolt
224
+ * MV: millivolt
225
+ * PDV: Power Delivery voltage unit (50 mV)
226
+ * A: ampere
227
+ * CA: centiampere
228
+ * MA: milliampere
229
+ * PDI: Power Delivery current unit (10 mA)
230
+ */
231
+#define PD_MV2PDV(mv) ((mv) / 50)
232
+#define PD_PDV2MV(pdv) ((pdv) * 50)
233
+#define PD_MA2PDI(ma) ((ma) / 10)
234
+#define PD_PDI2MA(pdi) ((pdi) * 10)
235
+
236
+/* Get portions of a PD voltage in more normal units */
237
+#define PD_PDV_V(pdv) ((pdv) / 20)
238
+#define PD_PDV_CV(pdv) (5 * ((pdv) % 20))
239
+
240
+/* Get portions of a PD current in more normal units */
241
+#define PD_PDI_A(pdv) ((pdv) / 100)
242
+#define PD_PDI_CA(pdv) ((pdv) % 100)
243
+
244
+
245
+
219
 #endif /* PDB_PD_H */
246
 #endif /* PDB_PD_H */

+ 3
- 6
src/shell.c View File

37
 #include <stdlib.h>
37
 #include <stdlib.h>
38
 #include <string.h>
38
 #include <string.h>
39
 
39
 
40
-#include <ch.h>
41
-
42
 #include "chprintf.h"
40
 #include "chprintf.h"
43
 
41
 
44
 #include "usbcfg.h"
42
 #include "usbcfg.h"
45
 #include "storage.h"
43
 #include "storage.h"
46
 #include "led.h"
44
 #include "led.h"
45
+#include "pd.h"
47
 
46
 
48
 
47
 
49
 /* Buffer for unwritten configuration */
48
 /* Buffer for unwritten configuration */
178
     if (i >= 0 && i <= UINT16_MAX && endptr > argv[0]) {
177
     if (i >= 0 && i <= UINT16_MAX && endptr > argv[0]) {
179
         tmpcfg.status = PDB_CONFIG_STATUS_VALID;
178
         tmpcfg.status = PDB_CONFIG_STATUS_VALID;
180
         /* Convert mV to the unit used by USB PD */
179
         /* Convert mV to the unit used by USB PD */
181
-        /* XXX this could use a macro */
182
-        tmpcfg.v = i / 50;
180
+        tmpcfg.v = PD_MV2PDV(i);
183
     } else {
181
     } else {
184
         chprintf(chp, "Invalid voltage\r\n");
182
         chprintf(chp, "Invalid voltage\r\n");
185
         return;
183
         return;
198
     if (i >= 0 && i <= UINT16_MAX && endptr > argv[0]) {
196
     if (i >= 0 && i <= UINT16_MAX && endptr > argv[0]) {
199
         tmpcfg.status = PDB_CONFIG_STATUS_VALID;
197
         tmpcfg.status = PDB_CONFIG_STATUS_VALID;
200
         /* Convert mA to the unit used by USB PD */
198
         /* Convert mA to the unit used by USB PD */
201
-        /* XXX this could use a macro */
202
-        tmpcfg.i = i / 10;
199
+        tmpcfg.i = PD_MA2PDI(i);
203
     } else {
200
     } else {
204
         chprintf(chp, "Invalid current\r\n");
201
         chprintf(chp, "Invalid current\r\n");
205
         return;
202
         return;

+ 8
- 4
src/storage.c View File

20
 
20
 
21
 #include "chprintf.h"
21
 #include "chprintf.h"
22
 
22
 
23
+#include "pd.h"
24
+
23
 
25
 
24
 struct pdb_config *pdb_config_array = (struct pdb_config *) PDB_CONFIG_BASE;
26
 struct pdb_config *pdb_config_array = (struct pdb_config *) PDB_CONFIG_BASE;
25
 
27
 
59
     chprintf(chp, "\r\n");
61
     chprintf(chp, "\r\n");
60
 
62
 
61
     /* Print voltages and current */
63
     /* Print voltages and current */
62
-    chprintf(chp, "v: %d.%02d V\r\n", cfg->v/20, 5*(cfg->v%20));
63
-    chprintf(chp, "i: %d.%02d A\r\n", cfg->i/100, cfg->i%100);
64
+    chprintf(chp, "v: %d.%02d V\r\n", PD_PDV_V(cfg->v), PD_PDV_CV(cfg->v));
65
+    chprintf(chp, "i: %d.%02d A\r\n", PD_PDI_A(cfg->i), PD_PDI_CA(cfg->i));
64
     if (cfg->flags & PDB_CONFIG_FLAGS_VAR_BAT) {
66
     if (cfg->flags & PDB_CONFIG_FLAGS_VAR_BAT) {
65
-        chprintf(chp, "v_min: %d.%02d V\r\n", cfg->v_min/20, 5*(cfg->v_min%20));
66
-        chprintf(chp, "v_max: %d.%02d V\r\n", cfg->v_max/20, 5*(cfg->v_max%20));
67
+        chprintf(chp, "v_min: %d.%02d V\r\n", PD_PDV_V(cfg->v_min),
68
+                 PD_PDV_CV(cfg->v_min));
69
+        chprintf(chp, "v_max: %d.%02d V\r\n", PD_PDV_V(cfg->v_max),
70
+                 PD_PDV_CV(cfg->v_max));
67
     }
71
     }
68
 }
72
 }
69
 
73
 

Loading…
Cancel
Save