1 /** 2 * Copyright © 2020 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include "pmbus_utils.hpp" 17 18 #include <cstdint> 19 20 #include <gtest/gtest.h> 21 22 using namespace phosphor::power::regulators; 23 24 TEST(PMBusUtilsTests, ParseVoutMode) 25 { 26 uint8_t voutModeValue; 27 pmbus_utils::VoutDataFormat format; 28 int8_t parameter; 29 30 // Linear format: Exponent is negative: 0b1'1111 31 voutModeValue = 0b0001'1111u; 32 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 33 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::linear); 34 EXPECT_EQ(parameter, -1); 35 36 // Linear format: Exponent is negative: 0b1'0000 37 voutModeValue = 0b1001'0000u; 38 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 39 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::linear); 40 EXPECT_EQ(parameter, -16); 41 42 // Linear format: Exponent is positive: 0b0'1111 43 voutModeValue = 0b1000'1111u; 44 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 45 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::linear); 46 EXPECT_EQ(parameter, 15); 47 48 // Linear format: Exponent is positive: 0b0'0001 49 voutModeValue = 0b0000'0001u; 50 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 51 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::linear); 52 EXPECT_EQ(parameter, 1); 53 54 // Linear format: Exponent is zero: 0b0'0000 55 voutModeValue = 0b0000'0000u; 56 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 57 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::linear); 58 EXPECT_EQ(parameter, 0); 59 60 // VID format: VID code is 0b1'1111 61 voutModeValue = 0b0011'1111u; 62 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 63 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::vid); 64 EXPECT_EQ(parameter, 31); 65 66 // VID format: VID code is 0b1'0000 67 voutModeValue = 0b1011'0000u; 68 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 69 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::vid); 70 EXPECT_EQ(parameter, 16); 71 72 // VID format: VID code is 0b0'1111 73 voutModeValue = 0b1010'1111u; 74 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 75 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::vid); 76 EXPECT_EQ(parameter, 15); 77 78 // VID format: VID code is 0b0'0001 79 voutModeValue = 0b0010'0001u; 80 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 81 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::vid); 82 EXPECT_EQ(parameter, 1); 83 84 // VID format: VID code is 0b0'0000 85 voutModeValue = 0b1010'0000u; 86 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 87 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::vid); 88 EXPECT_EQ(parameter, 0); 89 90 // Direct format 91 voutModeValue = 0b1100'0000u; 92 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 93 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::direct); 94 EXPECT_EQ(parameter, 0); 95 96 // IEEE format 97 voutModeValue = 0b0110'0000u; 98 pmbus_utils::parseVoutMode(voutModeValue, format, parameter); 99 EXPECT_EQ(format, pmbus_utils::VoutDataFormat::ieee); 100 EXPECT_EQ(parameter, 0); 101 } 102 103 TEST(PMBusUtilsTests, ConvertToVoutLinear) 104 { 105 double volts; 106 int8_t exponent; 107 108 // Exponent > 0: Value is not rounded up 109 volts = 13.9; 110 exponent = 2; 111 // 13.9 / 2^2 == 3.475 = 3 112 EXPECT_EQ(pmbus_utils::convertToVoutLinear(volts, exponent), 3); 113 114 // Exponent > 0: Value is rounded up 115 volts = 14.0; 116 exponent = 2; 117 // 14.0 / 2^2 == 3.5 = 4 118 EXPECT_EQ(pmbus_utils::convertToVoutLinear(volts, exponent), 4); 119 120 // Exponent = 0: Value is not rounded up 121 volts = 2.49; 122 exponent = 0; 123 // 2.49 / 2^0 == 2.49 = 2 124 EXPECT_EQ(pmbus_utils::convertToVoutLinear(volts, exponent), 2); 125 126 // Exponent = 0: Value is rounded up 127 volts = 2.51; 128 exponent = 0; 129 // 2.51 / 2^0 == 2.51 = 3 130 EXPECT_EQ(pmbus_utils::convertToVoutLinear(volts, exponent), 3); 131 132 // Exponent < 0: Value is not rounded up 133 volts = 1.32613; 134 exponent = -8; 135 // 1.32613 / 2^-8 == 339.48928 == 339 136 EXPECT_EQ(pmbus_utils::convertToVoutLinear(volts, exponent), 339); 137 138 // Exponent < 0: Value is rounded up 139 volts = 1.32618; 140 exponent = -8; 141 // 1.32618 / 2^-8 == 339.50208 == 340 142 EXPECT_EQ(pmbus_utils::convertToVoutLinear(volts, exponent), 340); 143 } 144