Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 PMBus 校验逻辑, 增加 ESPHome 样例 #10

Merged
merged 2 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Software/Arduino_CSPS_Lib/KCORES_CSPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ CSPS::CSPS(byte CSPS_addr)
{
_CSPS_addr = CSPS_addr;
_ROM_addr = CSPS_addr - 8;
_CSPS_READ_CHECKSUM = false;
Wire.begin();
}

CSPS::CSPS(byte CSPS_addr, byte ROM_addr)
{
_CSPS_addr = CSPS_addr;
_ROM_addr = ROM_addr;
_CSPS_READ_CHECKSUM = false;
Wire.begin();
}


CSPS::CSPS(byte CSPS_addr, byte ROM_addr, bool ENABLE_CHECKSUM)
{
_CSPS_addr = CSPS_addr;
_ROM_addr = ROM_addr;
_CSPS_READ_CHECKSUM = ENABLE_CHECKSUM;
Wire.begin();
}

Expand All @@ -44,6 +55,17 @@ byte CSPS::readROM(byte dataAddr)
return rec;
}

// Calclate checksum read from PM BUS
uint8_t checksum(uint32_t data) {
uint8_t cs = 0;
cs += ((data & 0xFF0000) >> 16);
cs += ((data & 0xFF00) >> 8);
cs += (data & 0xFF);

cs = ((0xFF - cs) + 1) & 0xFF;
return cs;
}

uint32_t CSPS::readCSPSword(byte dataAddr)
{
byte regCS = ((0xFF - (dataAddr + (_CSPS_addr << 1))) + 1) & 0xFF;
Expand All @@ -58,6 +80,20 @@ uint32_t CSPS::readCSPSword(byte dataAddr)
rec = Wire.read();
rec |= Wire.read() << 8;
rec |= Wire.read() << 16;

if (_CSPS_READ_CHECKSUM) {
uint8_t cs = checksum(rec);

if (cs != 0) {
// Output error to serial
if (Serial) {
Serial.printf("CSPS::readCSPSword() Missmatch checksum. addr: 0x%02x, chk: 0x%02x\n", dataAddr, cs);
}
}

// Don't return checksum
return rec & 0x00FFFF;
}
return rec;
}
else
Expand Down
2 changes: 2 additions & 0 deletions Software/Arduino_CSPS_Lib/KCORES_CSPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CSPS
public:
CSPS(byte CSPS_addr);
CSPS(byte CSPS_addr, byte ROM_addr);
CSPS(byte CSPS_addr, byte ROM_addr, bool ENABLE_CHECKSUM);

String getROM(byte addr, byte len);

Expand Down Expand Up @@ -114,6 +115,7 @@ class CSPS
};

private:
bool _CSPS_READ_CHECKSUM;
byte _CSPS_addr;
byte _ROM_addr;
byte readROM(byte dataAddr);
Expand Down
56 changes: 56 additions & 0 deletions Software/Arduino_CSPS_Lib/examples/ESPHome/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# ESPHome 集成

在 ESPHome 的 yaml 中增加

```yaml
esphome:
# 原先内容
includes:
- CSPS/main.hpp
- CSPS/KCORES_CSPS.h
- CSPS/KCORES_CSPS.cpp
```

创建文件夹 `CSPS` 将 `main.hpp`, `KCORES_CSPS.h` 以及 `KCORES_CSPS.cpp` 丢进去

## 增加传感器

使用自定义传感器获取数据

```yaml
sensor:
- platform: custom
lambda: |-
auto power = new CSPSPower();
App.register_component(power);
return {power->fan_speed, power->temp1, power->temp2, power->power_out, power->power_in, power->current_out, power->current_in, power->voltage_out, power->voltage_in};

sensors:
- name: "Fan Speed"
accuracy_decimals: 0
unit_of_measurement: RPM
- name: "Temp 1"
accuracy_decimals: 1
unit_of_measurement: °C
- name: "Temp 2"
accuracy_decimals: 1
unit_of_measurement: °C
- name: "Power Out"
accuracy_decimals: 0
unit_of_measurement: W
- name: "Power In"
accuracy_decimals: 0
unit_of_measurement: W
- name: "Current Out"
accuracy_decimals: 1
unit_of_measurement: A
- name: "Current In"
accuracy_decimals: 1
unit_of_measurement: A
- name: "Voltage Out"
accuracy_decimals: 1
unit_of_measurement: V
- name: "Voltage In"
accuracy_decimals: 1
unit_of_measurement: V
```
44 changes: 44 additions & 0 deletions Software/Arduino_CSPS_Lib/examples/ESPHome/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "KCORES_CSPS.h"
#include "esphome.h"

CSPS PowerSupply(0x5F, 0x57, true);

class CSPSPower : public PollingComponent {
public:
Sensor *fan_speed = new Sensor();
Sensor *temp1 = new Sensor();
Sensor *temp2 = new Sensor();
Sensor *power_in = new Sensor();
Sensor *power_out = new Sensor();
Sensor *current_out = new Sensor();
Sensor *current_in = new Sensor();
Sensor *voltage_out = new Sensor();
Sensor *voltage_in = new Sensor();

CSPSPower(): PollingComponent(15000) { }

void setup() override {
Wire.setClock(10000);

ESP_LOGD("Power Supply", "Spare Part No: %s", PowerSupply.getSPN().c_str());
ESP_LOGD("Power Supply", "Manufacture Date: %s", PowerSupply.getMFG().c_str());
ESP_LOGD("Power Supply", "Manufacturer: %s", PowerSupply.getMFR().c_str());
ESP_LOGD("Power Supply", "Power Name: %s", PowerSupply.getName().c_str());
ESP_LOGD("Power Supply", "Option Kit No: %s", PowerSupply.getOKN().c_str());
ESP_LOGD("Power Supply", "CT Date Codes: %s", PowerSupply.getCT().c_str());
}

void update() override {
// float current_out = PowerSupply.getOutputCurrent() / 256 / 256 / 256;

fan_speed->publish_state(PowerSupply.getFanRPM());
temp1->publish_state(PowerSupply.getTemp1());
temp2->publish_state(PowerSupply.getTemp2());
power_out->publish_state(PowerSupply.getOutputPower());
power_in->publish_state(PowerSupply.getInputPower());
current_out->publish_state(PowerSupply.getOutputCurrent());
current_in->publish_state(PowerSupply.getInputCurrent());
voltage_out->publish_state(PowerSupply.getOutputVoltage());
voltage_in->publish_state(PowerSupply.getInputVoltage());
}
};