blob: d95a6e5dd891789e1661c5ae0eab28b1c86ddded [file] [log] [blame] [edit]
#include "include/power_rail/pmbus_lib.hpp"
namespace max34451
{
std::string parserStatusByte(const uint8_t statusByte)
{
if (statusByte == 0xff)
{
return "INVALID";
}
std::map<uint8_t, std::string> statusMap = {
{7, "BUSY "}, {6, "OFF "},
{5, "VOUT_OV_FAULT "}, {4, "IOUT_OC_FAULT "},
{3, "VIN_UV_FAULT "}, {2, "TEMPERATURE "},
{1, "CML "}, {0, "NONE_OF_THE_ABOVE "}};
std::string result;
for (const auto& [bit, status] : statusMap)
{
if ((statusByte & (1 << bit)) != 0)
{
result += status;
}
}
return result;
}
std::string parserStatusWord(const uint16_t statusWord)
{
if (statusWord == 0xffff)
{
return "INVALID";
}
std::map<uint8_t, std::string> statusMap = {
{15, "VOUT "}, {14, "IOUT/POUT "},
{13, "INPUT "}, {12, "MFR_SPECIFIC "},
{11, "POWER_GOOD# "}, {10, "FANS "},
{9, "OTHER "}, {8, "UNKNOWN "},
{7, "BUSY "}, {6, "OFF "},
{5, "VOUT_OV_FAULT "}, {4, "IOUT_OC_FAULT "},
{3, "VIN_UV_FAULT "}, {2, "TEMPERATURE "},
{1, "CML "}, {0, "NONE_OF_THE_ABOVE "}};
std::string result;
for (const auto& [bit, status] : statusMap)
{
if ((statusWord & (1 << bit)) != 0)
{
result += status;
}
}
return result;
}
std::string parserStatusVout(const uint8_t statusVout)
{
if (statusVout == 0xff)
{
return "INVALID";
}
std::map<uint8_t, std::string> statusMap = {
{7, "VOUT_OV_FAULT "}, {6, "VOUT_OV_WARNING "},
{5, "VOUT_UV_WARNING "}, {4, "VOUT_UV_FAULT "},
{3, "VOUT_MAX_WARNING "}, {2, "TON_MAX_FAULT "},
{1, "TOFF_MAX_WARNING "}, {0, "VOUT Tracking Error "}};
std::string result;
for (const auto& [bit, status] : statusMap)
{
if ((statusVout & (1 << bit)) != 0)
{
result += status;
}
}
return result;
}
std::string parserStatusIout(const uint8_t statusIout)
{
if (statusIout == 0xff)
{
return "INVALID";
}
std::map<uint8_t, std::string> statusMap = {
{7, "IOUT_OC_FAULT "}, {6, "IOUT_OC_LV_FAULT "},
{5, "IOUT_OC_WARNING "}, {4, "IOUT_UC_FAULT "},
{3, "Current Share Fault "}, {2, "In PowerLimiting Mode "},
{1, "POUT_OP_FAULT "}, {0, "POUT_OP_WARNING "}};
std::string result;
for (const auto& [bit, status] : statusMap)
{
if ((statusIout & (1 << bit)) != 0)
{
result += status;
}
}
return result;
}
std::string parserStatusInput(const uint8_t statusInput)
{
if (statusInput == 0xff)
{
return "INVALID";
}
std::map<uint8_t, std::string> statusMap = {
{7, "VIN_OV_FAULT "}, {6, "VIN_OV_WARN "}, {5, "VIN_UV_WARN "},
{4, "VIN_UV_FAULT "}, {3, "VIN_UV_OFF "}, {2, "IIN_OC_FAULT "},
{1, "IIN_OC_WARN "}, {0, "PIN_OP_WARN "}};
std::string result;
for (const auto& [bit, status] : statusMap)
{
if ((statusInput & (1 << bit)) != 0)
{
result += status;
}
}
return result;
}
std::string parserStatusTemperature(const uint8_t statusTemperature)
{
if (statusTemperature == 0xff)
{
return "INVALID";
}
std::map<uint8_t, std::string> statusMap = {
{7, "OT_FAULT "}, {6, "OT_WARN "}, {5, "UT_WARN "}, {4, "UT_FAULT "}};
std::string result;
for (const auto& [bit, status] : statusMap)
{
if ((statusTemperature & (1 << bit)) != 0)
{
result += status;
}
}
return result;
}
std::string parserStatusCml(const uint8_t statusCml)
{
if (statusCml == 0xff)
{
return "INVALID";
}
std::map<uint8_t, std::string> statusMap = {
{7, "CMD_FAULT "}, {6, "DATA_FAULT "}, {5, "PEC_FAULT "},
{4, "MEM_FAULT "}, {3, "PROC_FAULT "}, {1, "OTHER_COMM_FAULT "},
{0, "OTHER_CML_FAULT "}};
std::string result;
for (const auto& [bit, status] : statusMap)
{
if ((statusCml & (1 << bit)) != 0)
{
result += status;
}
}
return result;
}
void loadPage(const uint8_t bus, const uint8_t devAddr, const uint8_t page)
{
util::i2cWriteByte(bus, devAddr, 0x00, page);
}
uint8_t getStatusByte(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x78);
}
uint16_t getStatusWord(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x79);
}
uint8_t getStatusVout(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x7a);
}
uint8_t getStatusIout(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x7b);
}
uint8_t getStatusInput(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x7c);
}
uint8_t getStatusTemperature(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x7d);
}
uint8_t getStatusCml(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x7e);
}
uint8_t getOnOffConfig(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x02);
}
void setWriteProtect(const uint8_t bus, const uint8_t devAddr,
const uint8_t value)
{
util::i2cWriteByte(bus, devAddr, 0x10, value);
}
uint16_t readVin(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x88);
}
uint16_t readIin(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x89);
}
uint16_t readVout(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x8b);
}
uint16_t readIout(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x8c);
}
uint16_t readTemperature(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x8d);
}
uint16_t getVoutMax(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x24);
}
uint16_t getVoutOvFaultLimit(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x40);
}
uint16_t getIoutOcFaultLimit(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x46);
}
void setIoutOcFaultLimit(const uint8_t bus, const uint8_t devAddr,
const uint16_t value)
{
util::i2cWriteByte(bus, devAddr, 0x46, value);
}
uint16_t getOtFaultLimit(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x4f);
}
uint8_t getPmbusRevision(const uint8_t bus, const uint8_t devAddr)
{
return util::i2cReadByte(bus, devAddr, 0x98);
}
std::string getMfrId(const uint8_t bus, const uint8_t devAddr,
const uint8_t returnByte)
{
auto data = util::i2cReadBytes(bus, devAddr, 0x99, returnByte);
return util::byteArrayToHexString(data.data(), returnByte);
}
std::string getMfrModel(const uint8_t bus, const uint8_t devAddr,
const uint8_t returnByte)
{
auto data = util::i2cReadBytes(bus, devAddr, 0x9a, returnByte);
return util::byteArrayToHexString(data.data(), returnByte);
}
std::string getMfrRevision(const uint8_t bus, const uint8_t devAddr,
const uint8_t returnByte)
{
auto data = util::i2cReadBytes(bus, devAddr, 0x9b, returnByte);
return util::byteArrayToHexString(data.data(), returnByte);
}
std::string getIcDeviceId(const uint8_t bus, const uint8_t devAddr,
const uint8_t returnByte)
{
auto data = util::i2cReadBytes(bus, devAddr, 0xad, returnByte);
return util::byteArrayToHexString(data.data(), returnByte);
}
std::string getIcDeviceRev(const uint8_t bus, const uint8_t devAddr,
const uint8_t returnByte)
{
auto data = util::i2cReadBytes(bus, devAddr, 0xae, returnByte);
return util::byteArrayToHexString(data.data(), returnByte);
}
std::string getMfrDate(const uint8_t bus, const uint8_t devAddr,
const uint8_t returnByte)
{
auto data = util::i2cReadBytes(bus, devAddr, 0x9d, returnByte);
return util::byteArrayToHexString(data.data(), returnByte);
}
double transferLinear11(const uint16_t word)
{
int16_t y = static_cast<int16_t>(word & 0x07FF);
int8_t ns = static_cast<int8_t>(word >> 11);
if ((ns & (1 << 4)) != 0)
{
ns = static_cast<int8_t>(32 - ns);
}
return ns > 0 ? y / pow(2, ns) : y * pow(2, ns);
}
double transferLinear16(const uint16_t y)
{
return y / pow(2, 13);
}
double transferDirectDataFormat(const int m, const int y, const int r,
const int b)
{
return (y / pow(10, r) - b) / m;
}
double transferDirectValue(const int y, const double factor)
{
return y * factor;
}
std::string transferAscii(const std::vector<uint8_t>& asciiArray)
{
std::string asciiString;
for (auto hex : asciiArray)
{
asciiString += static_cast<char>(hex);
}
return asciiString;
}
void bindI2cDevice(const std::string& name, const int i2cbus,
const int chipaddress)
{
std::ofstream deviceFile("/sys/bus/i2c/devices/i2c-" +
std::to_string(i2cbus) + "/new_device");
deviceFile << name << " " << std::hex << chipaddress;
deviceFile.close();
}
void unbindI2cDevice(const int i2cbus, const int chipaddress)
{
std::ofstream deviceFile("/sys/bus/i2c/devices/i2c-" +
std::to_string(i2cbus) + "/delete_device");
deviceFile << std::hex << chipaddress;
deviceFile.close();
}
} // namespace max34451