Browse Source

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 years ago
parent
commit
b06d95cd44
4 changed files with 47 additions and 0 deletions
  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 View File

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

+ 3
- 0
src/config.c View File

86
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_P:
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));
87
             chprintf(chp, "p: %d.%02d W\r\n", PD_CW_W(cfg->p), PD_CW_CW(cfg->p));
88
             break;
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 View File

43
             return scfg->i;
43
             return scfg->i;
44
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_P:
44
         case PDBS_CONFIG_FLAGS_CURRENT_DEFN_P:
45
             return (scfg->p * 1000 + mv - 1) / mv;
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 View File

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
 static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
427
 static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
407
 {
428
 {
408
     if (argc == 0) {
429
     if (argc == 0) {
485
     {"set_vrange", cmd_set_vrange, "Set the minimum and maximum voltage in millivolts"},
506
     {"set_vrange", cmd_set_vrange, "Set the minimum and maximum voltage in millivolts"},
486
     {"set_i", cmd_set_i, "Set the current in milliamps"},
507
     {"set_i", cmd_set_i, "Set the current in milliamps"},
487
     {"set_p", cmd_set_p, "Set the power in milliwatts"},
508
     {"set_p", cmd_set_p, "Set the power in milliwatts"},
509
+    {"set_r", cmd_set_r, "Set the resistance in milliohms"},
488
     {"output", cmd_output, "Get or set the output status"},
510
     {"output", cmd_output, "Get or set the output status"},
489
     {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
511
     {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
490
     {NULL, NULL, NULL}
512
     {NULL, NULL, NULL}

Loading…
Cancel
Save