Pārlūkot izejas kodu

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 gadus atpakaļ
vecāks
revīzija
26af506e06
3 mainītis faili ar 41 papildinājumiem un 0 dzēšanām
  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 Parādīt failu

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

+ 3
- 0
src/config.c Parādīt failu

@@ -83,6 +83,9 @@ void pdbs_config_print(BaseSequentialStream *chp, const struct pdbs_config *cfg)
83 83
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_I:
84 84
             chprintf(chp, "i: %d.%02d A\r\n", PD_PDI_A(cfg->i), PD_PDI_CA(cfg->i));
85 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 Parādīt failu

@@ -382,6 +382,27 @@ static void cmd_set_i(BaseSequentialStream *chp, int argc, char *argv[])
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 406
 static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
386 407
 {
387 408
     if (argc == 0) {
@@ -463,6 +484,7 @@ static const struct pdbs_shell_cmd commands[] = {
463 484
     {"set_v", cmd_set_v, "Set the voltage in millivolts"},
464 485
     {"set_vrange", cmd_set_vrange, "Set the minimum and maximum voltage in millivolts"},
465 486
     {"set_i", cmd_set_i, "Set the current in milliamps"},
487
+    {"set_p", cmd_set_p, "Set the power in milliwatts"},
466 488
     {"output", cmd_output, "Get or set the output status"},
467 489
     {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
468 490
     {NULL, NULL, NULL}

Notiek ielāde…
Atcelt
Saglabāt