summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Sanders <nsanders@chromium.org>2017-07-18 18:10:08 -0700
committerchrome-bot <chrome-bot@chromium.org>2017-07-24 22:54:04 -0700
commit9e1e58b62ab70edb54065412584e8b36fba1e71b (patch)
tree559d0df7814b3c26279f11e63b86d715920e599d
parent15c3bec8a52fae390cb759c6eba7f52216319b54 (diff)
downloadchrome-ec-9e1e58b62ab70edb54065412584e8b36fba1e71b.tar.gz
sweetberry: allow larger sense resistors
Currently sweetberry hits an integer truncation issue at 2.4 ohm when uA per div goes below 1. We can use 100ths of a uA as the current per div scale. BRANCH=None BUG=chromium:608039 TEST=log from sweetberry with 10 ohm config. Signed-off-by: Nick Sanders <nsanders@chromium.org> Change-Id: I9e9216230329483fd0bfcb44ce23cd15bae864b3 Reviewed-on: https://chromium-review.googlesource.com/577051 Commit-Ready: Nick Sanders <nsanders@chromium.org> Tested-by: Nick Sanders <nsanders@chromium.org> Reviewed-by: Nick Sanders <nsanders@chromium.org>
-rw-r--r--chip/stm32/usb_power.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/chip/stm32/usb_power.c b/chip/stm32/usb_power.c
index 60e104a953..f2517dd24c 100644
--- a/chip/stm32/usb_power.c
+++ b/chip/stm32/usb_power.c
@@ -458,15 +458,20 @@ static int usb_power_init_inas(struct usb_power_config const *config)
/*
* Calculate INA231 Calibration register
* CurrentLSB = uA per div = 80mV / (Rsh * 2^15)
- * CurrentLSB uA = 80000000nV / (Rsh mOhm * 0x8000)
+ * CurrentLSB 100x uA = 100x 80000000nV / (Rsh mOhm * 0x8000)
*/
- ina->scale = 80000000 / (ina->rs * 0x8000);
+ if (ina->rs == 0)
+ return -1;
+
+ ina->scale = (100 * (80000000 / 0x8000)) / ina->rs;
/*
* CAL = .00512 / (CurrentLSB * Rsh)
* CAL = 5120000 / (uA * mOhm)
*/
- value = 5120000 / (ina->scale * ina->rs);
+ if (ina->scale == 0)
+ return -1;
+ value = (5120000 * 100) / (ina->scale * ina->rs);
ret = ina2xx_write(ina->port, ina->addr, INA231_REG_CAL, value);
if (ret != EC_SUCCESS) {
CPRINTS("[CAP] usb_power_init_inas CAL FAIL: %d", ret);
@@ -478,7 +483,7 @@ static int usb_power_init_inas(struct usb_power_config const *config)
actual = ina2xx_read(ina->port, ina->addr, INA231_REG_CAL);
CPRINTS("[CAP] scale: %d uA/div, %d uW/div, cal:%x act:%x",
- ina->scale, ina->scale*25, value, actual);
+ ina->scale / 100, ina->scale*25/100, value, actual);
}
#endif
/* Conversion time, shunt + bus, set average. */
@@ -586,8 +591,8 @@ static int usb_power_get_samples(struct usb_power_config const *config)
int uV = ((int)voltage * 25) / 10;
int mV = ((int)bvoltage * 125) / 100;
int uA = (uV * 1000) / ina->rs;
- int CuA = ((int)current * ina->scale);
- int uW = ((int)power * ina->scale*25);
+ int CuA = (((int)current * ina->scale) / 100);
+ int uW = (((int)power * ina->scale*25)/100);
CPRINTS("[CAP] %d (%d,0x%02x): %dmV / %dmO = %dmA",
i, ina->port, ina->addr, uV/1000, ina->rs, uA/1000);