Преглед изворни кода

Add support for resistance-derived current

This commit adds support for configuring the current requested from the
power supply in the form of a resistance.  This is useful for powering
resistive loads when a voltage range is configured.
Clara Hobbs пре 6 година
родитељ
комит
b06d95cd44
4 измењених фајлова са 47 додато и 0 уклоњено
  1. 16
    0
      lib/include/pd.h
  2. 3
    0
      src/config.c
  3. 6
    0
      src/device_policy_manager.c
  4. 22
    0
      src/shell.c

+ 16
- 0
lib/include/pd.h Прегледај датотеку

@@ -322,6 +322,10 @@
322 322
  * W: watt
323 323
  * CW: centiwatt
324 324
  * MW: milliwatt
325
+ *
326
+ * O: ohm
327
+ * CO: centiohm
328
+ * MO: milliohm
325 329
  */
326 330
 #define PD_MV2PRV(mv) ((mv) / 20)
327 331
 #define PD_MV2PDV(mv) ((mv) / 50)
@@ -330,6 +334,7 @@
330 334
 #define PD_PDV2MV(pdv) ((pdv) * 50)
331 335
 #define PD_PAV2MV(pav) ((pav) * 100)
332 336
 
337
+#define PD_MA2CA(ma) (((ma) + 10 - 1) / 10)
333 338
 #define PD_MA2PDI(ma) (((ma) + 10 - 1) / 10)
334 339
 #define PD_MA2PAI(ma) (((ma) + 50 - 1) / 50)
335 340
 #define PD_CA2PAI(ca) (((ca) + 5 - 1) / 5)
@@ -339,6 +344,8 @@
339 344
 
340 345
 #define PD_MW2CW(mw) ((mw) / 10)
341 346
 
347
+#define PD_MO2CO(mo) ((mo) / 10)
348
+
342 349
 /* Get portions of a voltage in more normal units */
343 350
 #define PD_MV_V(mv) ((mv) / 1000)
344 351
 #define PD_MV_MV(mv) ((mv) % 1000)
@@ -360,6 +367,10 @@
360 367
 #define PD_CW_W(cw) ((cw) / 100)
361 368
 #define PD_CW_CW(cw) ((cw) % 100)
362 369
 
370
+/* Get portions of a resistance in more normal units */
371
+#define PD_CO_O(co) ((co) / 100)
372
+#define PD_CO_CO(co) ((co) % 100)
373
+
363 374
 /*
364 375
  * Unit constants
365 376
  */
@@ -370,11 +381,16 @@
370 381
 
371 382
 #define PD_MA_MIN 0
372 383
 #define PD_MA_MAX 5000
384
+#define PD_CA_MIN PD_MA2CA(PD_MA_MIN)
385
+#define PD_CA_MAX PD_MA2CA(PD_MA_MAX)
373 386
 #define PD_PDI_MIN PD_MA2PDI(PD_MA_MIN)
374 387
 #define PD_PDI_MAX PD_MA2PDI(PD_MA_MAX)
375 388
 
376 389
 #define PD_MW_MIN 0
377 390
 #define PD_MW_MAX 100000
378 391
 
392
+#define PD_MO_MIN 500
393
+#define PD_MO_MAX 655350
394
+
379 395
 
380 396
 #endif /* PDB_PD_H */

+ 3
- 0
src/config.c Прегледај датотеку

@@ -86,6 +86,9 @@ void pdbs_config_print(BaseSequentialStream *chp, const struct pdbs_config *cfg)
86 86
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_P:
87 87
             chprintf(chp, "p: %d.%02d W\r\n", PD_CW_W(cfg->p), PD_CW_CW(cfg->p));
88 88
             break;
89
+        case PDBS_CONFIG_FLAGS_CURRENT_DEFN_R:
90
+            chprintf(chp, "r: %d.%02d \316\251\r\n", PD_CO_O(cfg->r), PD_CO_CO(cfg->r));
91
+            break;
89 92
     }
90 93
 }
91 94
 

+ 6
- 0
src/device_policy_manager.c Прегледај датотеку

@@ -43,6 +43,12 @@ static uint16_t dpm_get_current(struct pdbs_config *scfg, uint16_t mv)
43 43
             return scfg->i;
44 44
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_P:
45 45
             return (scfg->p * 1000 + mv - 1) / mv;
46
+        case PDBS_CONFIG_FLAGS_CURRENT_DEFN_R:
47
+            return (mv * 10 + scfg->r - 1) / scfg->r;
48
+        default:
49
+            /* In the case of an error, return a current that can't be
50
+             * requested. */
51
+            return PD_CA_MAX + 1;
46 52
     }
47 53
 }
48 54
 

+ 22
- 0
src/shell.c Прегледај датотеку

@@ -403,6 +403,27 @@ static void cmd_set_p(BaseSequentialStream *chp, int argc, char *argv[])
403 403
     }
404 404
 }
405 405
 
406
+static void cmd_set_r(BaseSequentialStream *chp, int argc, char *argv[])
407
+{
408
+    if (argc != 1) {
409
+        chprintf(chp, "Usage: set_r power_in_m\316\251\r\n");
410
+        return;
411
+    }
412
+
413
+    char *endptr;
414
+    long i = strtol(argv[0], &endptr, 0);
415
+    if (i >= PD_MO_MIN && i <= PD_MO_MAX && endptr > argv[0]) {
416
+        /* Convert mohms to the unit used in the configuration object */
417
+        tmpcfg.r = PD_MO2CO(i);
418
+        /* Set the flags to say we're storing a resistance */
419
+        tmpcfg.flags &= ~PDBS_CONFIG_FLAGS_CURRENT_DEFN;
420
+        tmpcfg.flags |= PDBS_CONFIG_FLAGS_CURRENT_DEFN_R;
421
+    } else {
422
+        chprintf(chp, "Invalid resistance\r\n");
423
+        return;
424
+    }
425
+}
426
+
406 427
 static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
407 428
 {
408 429
     if (argc == 0) {
@@ -485,6 +506,7 @@ static const struct pdbs_shell_cmd commands[] = {
485 506
     {"set_vrange", cmd_set_vrange, "Set the minimum and maximum voltage in millivolts"},
486 507
     {"set_i", cmd_set_i, "Set the current in milliamps"},
487 508
     {"set_p", cmd_set_p, "Set the power in milliwatts"},
509
+    {"set_r", cmd_set_r, "Set the resistance in milliohms"},
488 510
     {"output", cmd_output, "Get or set the output status"},
489 511
     {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
490 512
     {NULL, NULL, NULL}

Loading…
Откажи
Сачувај