regulators: Add convertFromVoutLinear for sensor monitoring
Add convertFromVoutLinear function.
The function is for converting from linear_16 format to a normal
decimal number.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: If6e2a0227502a5674f72f93623108dc155e0c99c
diff --git a/phosphor-regulators/src/pmbus_utils.hpp b/phosphor-regulators/src/pmbus_utils.hpp
index 93d2dce..438ee46 100644
--- a/phosphor-regulators/src/pmbus_utils.hpp
+++ b/phosphor-regulators/src/pmbus_utils.hpp
@@ -224,6 +224,26 @@
}
/**
+ * Converts a linear data format output voltage value to a volts value.
+ *
+ * This data format consists of the following:
+ * - Two byte value
+ * - 16-bit unsigned mantissa value stored in the two bytes
+ * - 5-bit signed exponent value that is not stored in the two bytes
+ *
+ * @param value linear data format output voltage value
+ * @param exponent exponent value obtained from VOUT_MODE or device
+ * documentation
+ * @return normal decimal number
+ */
+inline double convertFromVoutLinear(uint16_t value, int8_t exponent)
+{
+ // compute value as mantissa * 2^(exponent)
+ double decimal = value * std::pow(2.0, exponent);
+ return decimal;
+}
+
+/**
* Converts a volts value to the linear data format for output voltage.
*
* This data format consists of the following:
diff --git a/phosphor-regulators/test/pmbus_utils_tests.cpp b/phosphor-regulators/test/pmbus_utils_tests.cpp
index 96e10bd..8e92151 100644
--- a/phosphor-regulators/test/pmbus_utils_tests.cpp
+++ b/phosphor-regulators/test/pmbus_utils_tests.cpp
@@ -254,6 +254,33 @@
EXPECT_DOUBLE_EQ(pmbus_utils::convertFromLinear(value), -16);
}
+TEST(PMBusUtilsTests, ConvertFromVoutLinear)
+{
+ uint16_t value;
+ int8_t exponent;
+
+ // mantissa : 1, exponent : 2, decimal = 1 * 2^2 = 4
+ value = 0x0001;
+ exponent = 2;
+ EXPECT_DOUBLE_EQ(pmbus_utils::convertFromVoutLinear(value, exponent), 4);
+
+ // mantissa : 15, exponent : 0, decimal = 15 * 2^0 = 15
+ value = 0x000f;
+ exponent = 0;
+ EXPECT_DOUBLE_EQ(pmbus_utils::convertFromVoutLinear(value, exponent), 15);
+
+ // mantissa : 255, exponent : -3, decimal = 255 * 2^-3 = 31.875
+ value = 0x00ff;
+ exponent = -3;
+ EXPECT_DOUBLE_EQ(pmbus_utils::convertFromVoutLinear(value, exponent),
+ 31.875);
+
+ // mantissa : 0, exponent : 10, decimal = 0 * 2^10 = 0
+ value = 0x0000;
+ exponent = 10;
+ EXPECT_DOUBLE_EQ(pmbus_utils::convertFromVoutLinear(value, exponent), 0);
+}
+
TEST(PMBusUtilsTests, ConvertToVoutLinear)
{
double volts;