summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorRob Barnes <robbarnes@google.com>2021-08-06 13:18:02 -0600
committerCommit Bot <commit-bot@chromium.org>2021-08-21 18:48:30 +0000
commitdd46fdb485a9cdb82ae733e4cec2320931e78dbc (patch)
tree9b6e38ce3058b0575dc8d9eb80ad1b416d8c2cda /include
parent1418bf3629b9fb767fca0a72455659bf46f06498 (diff)
downloadchrome-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>
Diffstat (limited to 'include')
-rw-r--r--include/common.h18
1 files changed, 16 insertions, 2 deletions
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