Browse Source

Implemented power support in the command shell

This commit adds the `set_p` command for setting power, and the `p`
configuration field for reading it back.  There is no support for making
requests based on power yet (the field is still interpreted as a current
unconditionally).
Clara Hobbs 6 years ago
parent
commit
26af506e06
3 changed files with 41 additions and 0 deletions
  1. 16
    0
      lib/include/pd.h
  2. 3
    0
      src/config.c
  3. 22
    0
      src/shell.c

+ 16
- 0
lib/include/pd.h View File

310
  * PRV: Programmable RDO voltage unit (20 mV)
310
  * PRV: Programmable RDO voltage unit (20 mV)
311
  * PDV: Power Delivery voltage unit (50 mV)
311
  * PDV: Power Delivery voltage unit (50 mV)
312
  * PAV: PPS APDO voltage unit (100 mV)
312
  * PAV: PPS APDO voltage unit (100 mV)
313
+ *
313
  * A: ampere
314
  * A: ampere
314
  * CA: centiampere
315
  * CA: centiampere
315
  * MA: milliampere
316
  * MA: milliampere
316
  * PDI: Power Delivery current unit (10 mA)
317
  * PDI: Power Delivery current unit (10 mA)
317
  * PAI: PPS APDO current unit (50 mA)
318
  * PAI: PPS APDO current unit (50 mA)
319
+ *
320
+ * W: watt
321
+ * CW: centiwatt
322
+ * MW: milliwatt
318
  */
323
  */
319
 #define PD_MV2PRV(mv) ((mv) / 20)
324
 #define PD_MV2PRV(mv) ((mv) / 20)
320
 #define PD_MV2PDV(mv) ((mv) / 50)
325
 #define PD_MV2PDV(mv) ((mv) / 50)
322
 #define PD_PRV2MV(prv) ((prv) * 20)
327
 #define PD_PRV2MV(prv) ((prv) * 20)
323
 #define PD_PDV2MV(pdv) ((pdv) * 50)
328
 #define PD_PDV2MV(pdv) ((pdv) * 50)
324
 #define PD_PAV2MV(pav) ((pav) * 100)
329
 #define PD_PAV2MV(pav) ((pav) * 100)
330
+
325
 #define PD_MA2PDI(ma) ((ma) / 10)
331
 #define PD_MA2PDI(ma) ((ma) / 10)
326
 #define PD_MA2PAI(ma) ((ma) / 50)
332
 #define PD_MA2PAI(ma) ((ma) / 50)
327
 #define PD_CA2PAI(ca) ((ca) / 5)
333
 #define PD_CA2PAI(ca) ((ca) / 5)
329
 #define PD_PAI2MA(pai) ((pai) * 50)
335
 #define PD_PAI2MA(pai) ((pai) * 50)
330
 #define PD_PAI2CA(pai) ((pai) * 5)
336
 #define PD_PAI2CA(pai) ((pai) * 5)
331
 
337
 
338
+#define PD_MW2CW(mw) ((mw) / 10)
339
+
332
 /* Get portions of a voltage in more normal units */
340
 /* Get portions of a voltage in more normal units */
333
 #define PD_MV_V(mv) ((mv) / 1000)
341
 #define PD_MV_V(mv) ((mv) / 1000)
334
 #define PD_MV_MV(mv) ((mv) % 1000)
342
 #define PD_MV_MV(mv) ((mv) % 1000)
346
 #define PD_PAI_A(pai) ((pai) / 20)
354
 #define PD_PAI_A(pai) ((pai) / 20)
347
 #define PD_PAI_CA(pai) (5 * ((pai) % 20))
355
 #define PD_PAI_CA(pai) (5 * ((pai) % 20))
348
 
356
 
357
+/* Get portions of a power in more normal units */
358
+#define PD_CW_W(cw) ((cw) / 100)
359
+#define PD_CW_CW(cw) ((cw) % 100)
360
+
349
 /*
361
 /*
350
  * Unit constants
362
  * Unit constants
351
  */
363
  */
353
 #define PD_MV_MAX 21000
365
 #define PD_MV_MAX 21000
354
 #define PD_PDV_MIN PD_MV2PDV(PD_MV_MIN)
366
 #define PD_PDV_MIN PD_MV2PDV(PD_MV_MIN)
355
 #define PD_PDV_MAX PD_MV2PDV(PD_MV_MAX)
367
 #define PD_PDV_MAX PD_MV2PDV(PD_MV_MAX)
368
+
356
 #define PD_MA_MIN 0
369
 #define PD_MA_MIN 0
357
 #define PD_MA_MAX 5000
370
 #define PD_MA_MAX 5000
358
 #define PD_PDI_MIN PD_MA2PDI(PD_MA_MIN)
371
 #define PD_PDI_MIN PD_MA2PDI(PD_MA_MIN)
359
 #define PD_PDI_MAX PD_MA2PDI(PD_MA_MAX)
372
 #define PD_PDI_MAX PD_MA2PDI(PD_MA_MAX)
360
 
373
 
374
+#define PD_MW_MIN 0
375
+#define PD_MW_MAX 100000
376
+
361
 
377
 
362
 #endif /* PDB_PD_H */
378
 #endif /* PDB_PD_H */

+ 3
- 0
src/config.c View File

83
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_I:
83
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_I:
84
             chprintf(chp, "i: %d.%02d A\r\n", PD_PDI_A(cfg->i), PD_PDI_CA(cfg->i));
84
             chprintf(chp, "i: %d.%02d A\r\n", PD_PDI_A(cfg->i), PD_PDI_CA(cfg->i));
85
             break;
85
             break;
86
+        case PDBS_CONFIG_FLAGS_CURRENT_DEFN_P:
87
+            chprintf(chp, "p: %d.%02d W\r\n", PD_CW_W(cfg->p), PD_CW_CW(cfg->p));
88
+            break;
86
     }
89
     }
87
 }
90
 }
88
 
91
 

+ 22
- 0
src/shell.c View File

382
     }
382
     }
383
 }
383
 }
384
 
384
 
385
+static void cmd_set_p(BaseSequentialStream *chp, int argc, char *argv[])
386
+{
387
+    if (argc != 1) {
388
+        chprintf(chp, "Usage: set_p power_in_mW\r\n");
389
+        return;
390
+    }
391
+
392
+    char *endptr;
393
+    long i = strtol(argv[0], &endptr, 0);
394
+    if (i >= PD_MW_MIN && i <= PD_MW_MAX && endptr > argv[0]) {
395
+        /* Convert mW to the unit used in the configuration object */
396
+        tmpcfg.p = PD_MW2CW(i);
397
+        /* Set the flags to say we're storing a power */
398
+        tmpcfg.flags &= ~PDBS_CONFIG_FLAGS_CURRENT_DEFN;
399
+        tmpcfg.flags |= PDBS_CONFIG_FLAGS_CURRENT_DEFN_P;
400
+    } else {
401
+        chprintf(chp, "Invalid power\r\n");
402
+        return;
403
+    }
404
+}
405
+
385
 static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
406
 static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
386
 {
407
 {
387
     if (argc == 0) {
408
     if (argc == 0) {
463
     {"set_v", cmd_set_v, "Set the voltage in millivolts"},
484
     {"set_v", cmd_set_v, "Set the voltage in millivolts"},
464
     {"set_vrange", cmd_set_vrange, "Set the minimum and maximum voltage in millivolts"},
485
     {"set_vrange", cmd_set_vrange, "Set the minimum and maximum voltage in millivolts"},
465
     {"set_i", cmd_set_i, "Set the current in milliamps"},
486
     {"set_i", cmd_set_i, "Set the current in milliamps"},
487
+    {"set_p", cmd_set_p, "Set the power in milliwatts"},
466
     {"output", cmd_output, "Get or set the output status"},
488
     {"output", cmd_output, "Get or set the output status"},
467
     {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
489
     {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
468
     {NULL, NULL, NULL}
490
     {NULL, NULL, NULL}

Loading…
Cancel
Save