Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 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 | |
| 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 | |
| 27 | namespace phosphor::power::regulators |
| 28 | { |
| 29 | |
| 30 | bool 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 | |
| 74 | std::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 | |
| 84 | // Only linear format is currently supported |
| 85 | ss << "format: linear"; |
| 86 | |
| 87 | if (exponent.has_value()) |
| 88 | { |
| 89 | ss << ", exponent: " << static_cast<int16_t>(exponent.value()); |
| 90 | } |
| 91 | |
| 92 | ss << ", is_verified: " << std::boolalpha << isWriteVerified << " }"; |
| 93 | return ss.str(); |
| 94 | } |
| 95 | |
| 96 | int8_t |
| 97 | PMBusWriteVoutCommandAction::getExponentValue(i2c::I2CInterface& interface) |
| 98 | { |
| 99 | // Check if an exponent value is defined for this action |
| 100 | if (exponent.has_value()) |
| 101 | { |
| 102 | return exponent.value(); |
| 103 | } |
| 104 | |
| 105 | // Read value of the VOUT_MODE command |
| 106 | uint8_t voutModeValue; |
| 107 | interface.read(pmbus_utils::VOUT_MODE, voutModeValue); |
| 108 | |
| 109 | // Parse VOUT_MODE value to get data format and parameter value |
| 110 | pmbus_utils::VoutDataFormat format; |
| 111 | int8_t parameter; |
| 112 | pmbus_utils::parseVoutMode(voutModeValue, format, parameter); |
| 113 | |
| 114 | // Verify format is linear; other formats not currently supported |
| 115 | if (format != pmbus_utils::VoutDataFormat::linear) |
| 116 | { |
| 117 | throw PMBusError("VOUT_MODE contains unsupported data format"); |
| 118 | } |
| 119 | |
| 120 | // Return parameter value; it contains the exponent when format is linear |
| 121 | return parameter; |
| 122 | } |
| 123 | |
| 124 | double |
| 125 | PMBusWriteVoutCommandAction::getVoltsValue(ActionEnvironment& environment) |
| 126 | { |
| 127 | double voltsValue; |
| 128 | |
| 129 | if (volts.has_value()) |
| 130 | { |
| 131 | // A volts value is defined for this action |
| 132 | voltsValue = volts.value(); |
| 133 | } |
Shawn McCarney | 0fd07d7 | 2020-03-06 17:16:24 -0600 | [diff] [blame] | 134 | else if (environment.getVolts().has_value()) |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 135 | { |
| 136 | // A volts value is defined in the ActionEnvironment |
Shawn McCarney | 0fd07d7 | 2020-03-06 17:16:24 -0600 | [diff] [blame] | 137 | voltsValue = environment.getVolts().value(); |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 138 | } |
| 139 | else |
| 140 | { |
| 141 | throw ActionError(*this, "No volts value defined"); |
| 142 | } |
| 143 | |
| 144 | return voltsValue; |
| 145 | } |
| 146 | |
| 147 | void PMBusWriteVoutCommandAction::verifyWrite(ActionEnvironment& environment, |
| 148 | i2c::I2CInterface& interface, |
| 149 | uint16_t valueWritten) |
| 150 | { |
| 151 | // Read current value of VOUT_COMMAND. I2CInterface method reads low byte |
| 152 | // first as required by PMBus. |
| 153 | uint16_t valueRead{0x00}; |
| 154 | interface.read(pmbus_utils::VOUT_COMMAND, valueRead); |
| 155 | |
| 156 | // Verify value read equals value written |
| 157 | if (valueRead != valueWritten) |
| 158 | { |
| 159 | std::ostringstream ss; |
| 160 | ss << "device: " << environment.getDeviceID() |
| 161 | << ", register: VOUT_COMMAND, value_written: 0x" << std::hex |
| 162 | << std::uppercase << valueWritten << ", value_read: 0x" << valueRead; |
| 163 | throw WriteVerificationError(ss.str()); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | } // namespace phosphor::power::regulators |