blob: 2a245a9e0577631700a6104b75f664ca70a9b66d [file] [log] [blame]
Shawn McCarneya8119f22020-03-02 16:20:18 -06001/**
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
17#include "pmbus_write_vout_command_action.hpp"
18
19#include "action_error.hpp"
20#include "pmbus_error.hpp"
21#include "write_verification_error.hpp"
22
23#include <exception>
24#include <ios>
25#include <sstream>
26
27namespace phosphor::power::regulators
28{
29
30bool PMBusWriteVoutCommandAction::execute(ActionEnvironment& environment)
31{
32 try
33 {
34 // Get volts value
35 double voltsValue = getVoltsValue(environment);
36
37 // Get I2C interface to current device
38 i2c::I2CInterface& interface = getI2CInterface(environment);
39
40 // Get exponent value for converting volts value to linear format
41 int8_t exponentValue = getExponentValue(interface);
42
43 // Convert volts value to linear data format
44 uint16_t linearValue =
45 pmbus_utils::convertToVoutLinear(voltsValue, exponentValue);
46
47 // Write linear format value to VOUT_COMMAND. I2CInterface method
48 // writes low-order byte first as required by PMBus.
49 interface.write(pmbus_utils::VOUT_COMMAND, linearValue);
50
51 // Verify write if necessary
52 if (isWriteVerified)
53 {
54 verifyWrite(environment, interface, linearValue);
55 }
56 }
57 // Nest the following exception types within an ActionError so the caller
58 // will have both the low level error information and the action information
59 catch (const i2c::I2CException& i2cError)
60 {
61 std::throw_with_nested(ActionError(*this));
62 }
63 catch (const PMBusError& pmbusError)
64 {
65 std::throw_with_nested(ActionError(*this));
66 }
67 catch (const WriteVerificationError& verifyError)
68 {
69 std::throw_with_nested(ActionError(*this));
70 }
71 return true;
72}
73
74std::string PMBusWriteVoutCommandAction::toString() const
75{
76 std::ostringstream ss;
77 ss << "pmbus_write_vout_command: { ";
78
79 if (volts.has_value())
80 {
81 ss << "volts: " << volts.value() << ", ";
82 }
83
Bob Kingd6820bb2020-04-28 15:37:02 +080084 ss << "format: " << pmbus_utils::toString(format);
Shawn McCarneya8119f22020-03-02 16:20:18 -060085
86 if (exponent.has_value())
87 {
88 ss << ", exponent: " << static_cast<int16_t>(exponent.value());
89 }
90
91 ss << ", is_verified: " << std::boolalpha << isWriteVerified << " }";
92 return ss.str();
93}
94
95int8_t
96 PMBusWriteVoutCommandAction::getExponentValue(i2c::I2CInterface& interface)
97{
98 // Check if an exponent value is defined for this action
99 if (exponent.has_value())
100 {
101 return exponent.value();
102 }
103
104 // Read value of the VOUT_MODE command
105 uint8_t voutModeValue;
106 interface.read(pmbus_utils::VOUT_MODE, voutModeValue);
107
108 // Parse VOUT_MODE value to get data format and parameter value
109 pmbus_utils::VoutDataFormat format;
110 int8_t parameter;
111 pmbus_utils::parseVoutMode(voutModeValue, format, parameter);
112
113 // Verify format is linear; other formats not currently supported
114 if (format != pmbus_utils::VoutDataFormat::linear)
115 {
116 throw PMBusError("VOUT_MODE contains unsupported data format");
117 }
118
119 // Return parameter value; it contains the exponent when format is linear
120 return parameter;
121}
122
123double
124 PMBusWriteVoutCommandAction::getVoltsValue(ActionEnvironment& environment)
125{
126 double voltsValue;
127
128 if (volts.has_value())
129 {
130 // A volts value is defined for this action
131 voltsValue = volts.value();
132 }
Shawn McCarney0fd07d72020-03-06 17:16:24 -0600133 else if (environment.getVolts().has_value())
Shawn McCarneya8119f22020-03-02 16:20:18 -0600134 {
135 // A volts value is defined in the ActionEnvironment
Shawn McCarney0fd07d72020-03-06 17:16:24 -0600136 voltsValue = environment.getVolts().value();
Shawn McCarneya8119f22020-03-02 16:20:18 -0600137 }
138 else
139 {
140 throw ActionError(*this, "No volts value defined");
141 }
142
143 return voltsValue;
144}
145
146void PMBusWriteVoutCommandAction::verifyWrite(ActionEnvironment& environment,
147 i2c::I2CInterface& interface,
148 uint16_t valueWritten)
149{
150 // Read current value of VOUT_COMMAND. I2CInterface method reads low byte
151 // first as required by PMBus.
152 uint16_t valueRead{0x00};
153 interface.read(pmbus_utils::VOUT_COMMAND, valueRead);
154
155 // Verify value read equals value written
156 if (valueRead != valueWritten)
157 {
158 std::ostringstream ss;
159 ss << "device: " << environment.getDeviceID()
160 << ", register: VOUT_COMMAND, value_written: 0x" << std::hex
161 << std::uppercase << valueWritten << ", value_read: 0x" << valueRead;
162 throw WriteVerificationError(ss.str());
163 }
164}
165
166} // namespace phosphor::power::regulators