summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Richardson <wfrichar@chromium.org>2013-07-31 13:08:11 -0700
committerChromeBot <chrome-bot@google.com>2013-08-07 17:24:09 -0700
commit6f8e276cc842d0804292d473fe3305295d2e7052 (patch)
tree3b66c44e86db4b8f2ebbc157666e6ed62f6e3ff1
parente98bde3fec5e53314a15fd031c83ee630973483f (diff)
downloadchrome-ec-6f8e276cc842d0804292d473fe3305295d2e7052.tar.gz
Use macros for C <-> K conversions
This just replaces all the "X - 273", "Y + 273" stuff with a macro. BUG=none BRANCH=falco,peppy TEST=manual Run the EC console command "temps". It should print human-readable things. Change-Id: Icc4284c89fdbc0cd3b206a0faacf121973652a63 Signed-off-by: Bill Richardson <wfrichar@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/65005 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--chip/lm4/adc.c3
-rw-r--r--chip/lm4/chip_temp_sensor.c2
-rw-r--r--chip/lm4/peci.c2
-rw-r--r--common/pmu_tps65090_charger.c15
-rw-r--r--common/temp_sensor.c2
-rw-r--r--common/temp_sensor_g781.c2
-rw-r--r--include/battery_pack.h3
-rw-r--r--include/common.h6
8 files changed, 17 insertions, 18 deletions
diff --git a/chip/lm4/adc.c b/chip/lm4/adc.c
index 8e17854104..17f3d26191 100644
--- a/chip/lm4/adc.c
+++ b/chip/lm4/adc.c
@@ -8,6 +8,7 @@
#include "adc.h"
#include "clock.h"
#include "console.h"
+#include "common.h"
#include "gpio.h"
#include "hooks.h"
#include "lm4_adc.h"
@@ -161,7 +162,7 @@ DECLARE_IRQ(LM4_IRQ_ADC0_SS3, ss3_interrupt, 2);
static int command_ectemp(int argc, char **argv)
{
int t = adc_read_channel(ADC_CH_EC_TEMP);
- ccprintf("EC temperature is %d K = %d C\n", t, t-273);
+ ccprintf("EC temperature is %d K = %d C\n", t, K_TO_C(t));
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(ectemp, command_ectemp,
diff --git a/chip/lm4/chip_temp_sensor.c b/chip/lm4/chip_temp_sensor.c
index 2377806023..d470da407f 100644
--- a/chip/lm4/chip_temp_sensor.c
+++ b/chip/lm4/chip_temp_sensor.c
@@ -11,7 +11,7 @@
#include "lm4_adc.h"
/* Initialize temperature reading to a sane value (27 C) */
-static int last_val = 300;
+static int last_val = C_TO_K(27);
static void chip_temp_sensor_poll(void)
{
diff --git a/chip/lm4/peci.c b/chip/lm4/peci.c
index b791c97a89..88e13d18c1 100644
--- a/chip/lm4/peci.c
+++ b/chip/lm4/peci.c
@@ -136,7 +136,7 @@ static int command_peci_temp(int argc, char **argv)
ccprintf("PECI error 0x%04x\n", LM4_PECI_M0D0 & 0xffff);
return EC_ERROR_UNKNOWN;
}
- ccprintf("CPU temp = %d K = %d C\n", t, t - 273);
+ ccprintf("CPU temp = %d K = %d C\n", t, K_TO_C(t));
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(pecitemp, command_peci_temp,
diff --git a/common/pmu_tps65090_charger.c b/common/pmu_tps65090_charger.c
index d91a52b83b..39be704a4d 100644
--- a/common/pmu_tps65090_charger.c
+++ b/common/pmu_tps65090_charger.c
@@ -59,28 +59,23 @@ static void enable_charging(int enable)
gpio_set_level(GPIO_CHARGER_EN, enable);
}
-static int battery_temperature_celsius(int deci_k)
-{
- return (deci_k - 2731) / 10;
-}
-
static int battery_start_charging_range(int deci_k)
{
- int8_t temp_c = battery_temperature_celsius(deci_k);
+ int8_t temp_c = DECI_KELVIN_TO_CELSIUS(deci_k);
return (temp_c >= bat_temp_ranges.start_charging_min_c &&
temp_c < bat_temp_ranges.start_charging_max_c);
}
static int battery_charging_range(int deci_k)
{
- int8_t temp_c = battery_temperature_celsius(deci_k);
+ int8_t temp_c = DECI_KELVIN_TO_CELSIUS(deci_k);
return (temp_c >= bat_temp_ranges.charging_min_c &&
temp_c < bat_temp_ranges.charging_max_c);
}
static int battery_discharging_range(int deci_k)
{
- int8_t temp_c = battery_temperature_celsius(deci_k);
+ int8_t temp_c = DECI_KELVIN_TO_CELSIUS(deci_k);
return (temp_c >= bat_temp_ranges.discharging_min_c &&
temp_c < bat_temp_ranges.discharging_max_c);
}
@@ -260,7 +255,7 @@ static int calc_next_state(int state)
} else if (!battery_charging_range(batt_temp)) {
CPRINTF("[pmu] charging: temperature out of range "
"%dC\n",
- battery_temperature_celsius(batt_temp));
+ DECI_KELVIN_TO_CELSIUS(batt_temp));
return ST_CHARGING_ERROR;
}
@@ -342,7 +337,7 @@ static int calc_next_state(int state)
if (!battery_discharging_range(batt_temp)) {
CPRINTF("[pmu] discharging: temperature out of"
"range %dC\n",
- battery_temperature_celsius(batt_temp));
+ DECI_KELVIN_TO_CELSIUS(batt_temp));
return system_off();
}
}
diff --git a/common/temp_sensor.c b/common/temp_sensor.c
index e33d787b9c..9b8fbe32fb 100644
--- a/common/temp_sensor.c
+++ b/common/temp_sensor.c
@@ -119,7 +119,7 @@ static int command_temps(int argc, char **argv)
switch (rv) {
case EC_SUCCESS:
- ccprintf("%d K = %d C\n", t, t - 273);
+ ccprintf("%d K = %d C\n", t, K_TO_C(t));
break;
case EC_ERROR_NOT_POWERED:
ccprintf("Not powered\n");
diff --git a/common/temp_sensor_g781.c b/common/temp_sensor_g781.c
index d11af27a99..4a2698ea2e 100644
--- a/common/temp_sensor_g781.c
+++ b/common/temp_sensor_g781.c
@@ -40,6 +40,6 @@ int g781_get_val(int idx, int *temp_ptr)
temp_raw = ~(~temp_raw & 0xff) + 1;
/* Temperature from sensor is in degrees Celsius */
- *temp_ptr = temp_raw + 273;
+ *temp_ptr = C_TO_K(temp_raw);
return EC_SUCCESS;
}
diff --git a/include/battery_pack.h b/include/battery_pack.h
index 5e023db471..41e881afbc 100644
--- a/include/battery_pack.h
+++ b/include/battery_pack.h
@@ -9,9 +9,6 @@
#include "common.h"
-#define CELSIUS_TO_DECI_KELVIN(temp_c) ((temp_c) * 10 + 2731)
-#define DECI_KELVIN_TO_CELSIUS(temp_dk) ((temp_dk - 2731) / 10)
-
/* Battery parameters */
struct batt_params {
int temperature; /* Temperature in 0.1 K */
diff --git a/include/common.h b/include/common.h
index 235540d220..03fd7affee 100644
--- a/include/common.h
+++ b/include/common.h
@@ -44,6 +44,12 @@
#define __packed __attribute__((packed))
#endif
+/* There isn't really a better place for this */
+#define C_TO_K(temp_c) ((temp_c) + 273)
+#define K_TO_C(temp_c) ((temp_c) - 273)
+#define CELSIUS_TO_DECI_KELVIN(temp_c) ((temp_c) * 10 + 2731)
+#define DECI_KELVIN_TO_CELSIUS(temp_dk) ((temp_dk - 2731) / 10)
+
/* Include top-level configuration file */
#include "config.h"