diff options
author | Rob Barnes <robbarnes@google.com> | 2021-08-06 13:18:02 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-08-21 18:48:30 +0000 |
commit | dd46fdb485a9cdb82ae733e4cec2320931e78dbc (patch) | |
tree | 9b6e38ce3058b0575dc8d9eb80ad1b416d8c2cda | |
parent | 1418bf3629b9fb767fca0a72455659bf46f06498 (diff) | |
download | chrome-ec-dd46fdb485a9cdb82ae733e4cec2320931e78dbc.tar.gz |
common: Add Milli Kelvin conversion macros
Add macros to convert to and from milli kelvin and various temperature
units. Utilize round_divide for more accurate conversions.
BUG=b:176994331
TEST=Unit test
BRANCH=None
Change-Id: Ie6750b9d2d2b8093fdf9c14f904382e91d8d95bb
Signed-off-by: Rob Barnes <robbarnes@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3078051
Reviewed-by: Keith Short <keithshort@chromium.org>
-rw-r--r-- | common/battery.c | 1 | ||||
-rw-r--r-- | common/build.mk | 2 | ||||
-rw-r--r-- | include/common.h | 18 | ||||
-rw-r--r-- | test/math_util.c | 46 | ||||
-rw-r--r-- | test/sbs_charging_v2.c | 1 |
5 files changed, 65 insertions, 3 deletions
diff --git a/common/battery.c b/common/battery.c index c24bf59da9..6791e4d3a2 100644 --- a/common/battery.c +++ b/common/battery.c @@ -15,6 +15,7 @@ #include "gpio.h" #include "hooks.h" #include "host_command.h" +#include "math_util.h" #include "timer.h" #include "usb_pd.h" #include "util.h" diff --git a/common/build.mk b/common/build.mk index caed246a74..7c505c066f 100644 --- a/common/build.mk +++ b/common/build.mk @@ -43,7 +43,7 @@ common-$(CONFIG_BACKLIGHT_LID)+=backlight_lid.o common-$(CONFIG_BASE32)+=base32.o common-$(CONFIG_BLINK)+=blink.o common-$(CONFIG_DETACHABLE_BASE)+=base_state.o -common-$(CONFIG_BATTERY)+=battery.o +common-$(CONFIG_BATTERY)+=battery.o math_util.o common-$(CONFIG_BATTERY_FUEL_GAUGE)+=battery_fuel_gauge.o common-$(CONFIG_BLUETOOTH_LE)+=bluetooth_le.o common-$(CONFIG_BLUETOOTH_LE_STACK)+=btle_hci_controller.o btle_ll.o diff --git a/include/common.h b/include/common.h index 03e1b19d55..373663ea88 100644 --- a/include/common.h +++ b/include/common.h @@ -218,8 +218,22 @@ /* 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) +/* + * round_divide is part of math_utils, so you may need to import math_utils.h + * and link math_utils.o if you use the following macros. + */ +#define CELSIUS_TO_DECI_KELVIN(temp_c) \ + (round_divide(CELSIUS_TO_MILLI_KELVIN(temp_c), 100)) +#define DECI_KELVIN_TO_CELSIUS(temp_dk) \ + (MILLI_KELVIN_TO_CELSIUS((temp_dk) * 100)) +#define MILLI_KELVIN_TO_MILLI_CELSIUS(temp_mk) ((temp_mk) - 273150) +#define MILLI_CELSIUS_TO_MILLI_KELVIN(temp_mc) ((temp_mc) + 273150) +#define MILLI_KELVIN_TO_KELVIN(temp_mk) (round_divide((temp_mk), 1000)) +#define KELVIN_TO_MILLI_KELVIN(temp_k) ((temp_k) * 1000) +#define CELSIUS_TO_MILLI_KELVIN(temp_c) \ + (MILLI_CELSIUS_TO_MILLI_KELVIN((temp_c) * 1000)) +#define MILLI_KELVIN_TO_CELSIUS(temp_mk) \ + (round_divide(MILLI_KELVIN_TO_MILLI_CELSIUS(temp_mk), 1000)) /* Calculate a value with error margin considered. For example, * TARGET_WITH_MARGIN(X, 5) returns X' where X' * 100.5% is almost equal to diff --git a/test/math_util.c b/test/math_util.c index b45de1a29d..6482888e55 100644 --- a/test/math_util.c +++ b/test/math_util.c @@ -78,12 +78,58 @@ static int test_rotate(void) return EC_SUCCESS; } +test_static int test_round_divide(void) +{ + /* Check function version */ + TEST_EQ(round_divide(10, 1), 10, "%d"); + TEST_EQ(round_divide(10, 2), 5, "%d"); + TEST_EQ(round_divide(10, 3), 3, "%d"); + TEST_EQ(round_divide(10, 4), 3, "%d"); + TEST_EQ(round_divide(10, 5), 2, "%d"); + TEST_EQ(round_divide(10, 6), 2, "%d"); + TEST_EQ(round_divide(10, 7), 1, "%d"); + TEST_EQ(round_divide(10, 9), 1, "%d"); + TEST_EQ(round_divide(10, 10), 1, "%d"); + TEST_EQ(round_divide(10, 11), 1, "%d"); + TEST_EQ(round_divide(10, 20), 1, "%d"); + TEST_EQ(round_divide(10, 21), 0, "%d"); + + /* Check negative conditions */ + TEST_EQ(round_divide(-10, 6), -2, "%d"); + TEST_EQ(round_divide(10, -6), -2, "%d"); + TEST_EQ(round_divide(-10, -6), 2, "%d"); + + return EC_SUCCESS; +} + +test_static int test_temp_conversion(void) +{ + TEST_EQ(C_TO_K(100), 373, "%d"); + TEST_EQ(K_TO_C(100), -173, "%d"); + + TEST_EQ((int)CELSIUS_TO_DECI_KELVIN(100), 3732, "%d"); + TEST_EQ(DECI_KELVIN_TO_CELSIUS(100), -263, "%d"); + + TEST_EQ(MILLI_KELVIN_TO_MILLI_CELSIUS(100), -273050, "%d"); + TEST_EQ(MILLI_CELSIUS_TO_MILLI_KELVIN(100), 273250, "%d"); + + TEST_EQ(MILLI_KELVIN_TO_KELVIN(5000), 5, "%d"); + TEST_EQ(KELVIN_TO_MILLI_KELVIN(100), 100000, "%d"); + + TEST_EQ(CELSIUS_TO_MILLI_KELVIN(100), 373150, "%d"); + TEST_EQ(MILLI_KELVIN_TO_CELSIUS(100), -273, "%d"); + + return EC_SUCCESS; +} + void run_test(int argc, char **argv) { test_reset(); RUN_TEST(test_acos); RUN_TEST(test_rotate); + RUN_TEST(test_round_divide); + RUN_TEST(test_temp_conversion); test_print_result(); } diff --git a/test/sbs_charging_v2.c b/test/sbs_charging_v2.c index 520db7bb20..bdca592d6c 100644 --- a/test/sbs_charging_v2.c +++ b/test/sbs_charging_v2.c @@ -12,6 +12,7 @@ #include "gpio.h" #include "hooks.h" #include "host_command.h" +#include "math_util.h" #include "power.h" #include "task.h" #include "test_util.h" |