Skip to content

Commit

Permalink
correct pin validation and optimize pinmapping
Browse files Browse the repository at this point in the history
  • Loading branch information
skippermeister committed Aug 18, 2024
1 parent 5a665a7 commit 6fe81cb
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 174 deletions.
58 changes: 6 additions & 52 deletions docs/DeviceProfiles/my_very_special_board.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
"can0_rx": 26,
"can0_tx": 27
},
"mcp2515": {
"miso": 12,
"mosi": 13,
"clk": 14,
"irq": 35,
"cs": 15
},
"batteryConnectedInverter": {
"pre_charge": 32,
"full_power": 33
Expand Down Expand Up @@ -64,13 +57,6 @@
"can0_rx": 26,
"can0_tx": 27
},
"mcp2515": {
"miso": -1,
"mosi": -1,
"clk": -1,
"irq": -1,
"cs": -1
},
"batteryConnectedInverter": {
"pre_charge": 32,
"full_power": 33
Expand All @@ -81,7 +67,7 @@
}
},
{
"name": "my_test_board DalyBMS via RS232",
"name": "my_test_board MCP2515+DalyBMS via RS232",
"nrf24": {
"clk": 18,
"cs": 5,
Expand All @@ -103,15 +89,11 @@
"bms_wakeup": 4
},
"charger": {
"can0_rx": 26,
"can0_tx": 27
},
"mcp2515": {
"miso": -1,
"mosi": -1,
"clk": -1,
"irq": -1,
"cs": -1
"mcp2515_miso": 12,
"mcp2515_mosi": 13,
"mcp2515_clk": 14,
"mcp2515_irq": 35,
"mcp2515_cs": 15
},
"batteryConnectedInverter": {
"pre_charge": 32,
Expand Down Expand Up @@ -155,13 +137,6 @@
"can0_rx": 26,
"can0_tx": 27
},
"mcp2515": {
"miso": -1,
"mosi": -1,
"clk": -1,
"irq": -1,
"cs": -1
},
"batteryConnectedInverter": {
"pre_charge": 32,
"full_power": 33
Expand Down Expand Up @@ -202,13 +177,6 @@
"can0_rx": 26,
"can0_tx": 27
},
"mcp2515": {
"miso": 12,
"mosi": 13,
"clk": 14,
"irq": 35,
"cs": 15
},
"batteryConnectedInverter": {
"pre_charge": 32,
"full_power": 33
Expand Down Expand Up @@ -248,13 +216,6 @@
"can0_rx": 26,
"can0_tx": 27
},
"mcp2515": {
"miso": 12,
"mosi": 13,
"clk": 14,
"irq": 35,
"cs": 15
},
"batteryConnectedInverter": {
"pre_charge": 32,
"full_power": 33
Expand Down Expand Up @@ -294,13 +255,6 @@
"can0_rx": 26,
"can0_tx": 27
},
"mcp2515": {
"miso": 12,
"mosi": 13,
"clk": 14,
"irq": 35,
"cs": 15
},
"batteryConnectedInverter": {
"pre_charge": 32,
"full_power": 33
Expand Down
44 changes: 19 additions & 25 deletions include/PinMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

#define MAPPING_NAME_STRLEN 31

struct RS485_t {
int8_t rx;
int8_t tx;
int8_t rts;
};

struct RS232_t {
int8_t rx;
int8_t tx;
};

struct MCP2515_t {
int8_t miso;
int8_t mosi;
Expand Down Expand Up @@ -55,15 +66,8 @@ struct Battery_t {
} i2c;
#endif
#if defined(USE_PYLONTECH_RS485_RECEIVER) || defined(USE_DALYBMS_CONTROLLER) || defined(USE_JKBMS_CONTROLLER)
struct {
int8_t rx;
int8_t tx;
} rs232;
struct {
int8_t rx;
int8_t tx;
int8_t rts;
} rs485;
RS232_t rs232;
RS485_t rs485;
#endif
};
#if defined(USE_DALYBMS_CONTROLLER)
Expand Down Expand Up @@ -121,20 +125,10 @@ struct PinMapping_t {
int8_t led_rgb;
#endif

int8_t victron_tx;
int8_t victron_rx;

int8_t victron_tx2;
int8_t victron_rx2;

int8_t victron_tx3;
int8_t victron_rx3;
RS232_t victron[3];

#if defined(USE_REFUsol_INVERTER)
int8_t REFUsol_rx;
int8_t REFUsol_tx;
// int8_t REFUsol_cts;
int8_t REFUsol_rts;
RS485_t refusol;
#endif

Battery_t battery;
Expand Down Expand Up @@ -165,18 +159,18 @@ class PinMappingClass {
bool isValidEthConfig() const;
#endif
bool isValidBatteryConfig() const;
#if defined(USE_CHARGER_MEANWELL) || defined(USE_CHARGER_HUAWEI)
bool isValidChargerConfig() const;
#endif

bool isValidPreChargeConfig() const;
#if defined(USE_REFUsol_INVERTER)
bool isValidREFUsolConfig() const;
#endif

private:
void createPinMappingJson() const;

PinMapping_t _pinMapping;

const char* help[7] = {"unknown", "CAN0 Bus", "MCP2515", "I2C0/CAN", "I2C1/CAN", "RS232", "RS485"};
const char* help[7] = {"unknown", "CAN0 Bus", "MCP2515 CAN Bus", "I2C0/CAN Bus", "I2C1/CAN Bus", "RS232", "RS485"};
};

extern PinMappingClass PinMapping;
2 changes: 1 addition & 1 deletion src/BatteryCanReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bool BatteryCanReceiver::init(char const* providerName)

MessageOutput.printf("%s Initialize interface...", _providerName);

if (PinMapping.isValidBatteryConfig()) {
if (!PinMapping.isValidBatteryConfig()) {
MessageOutput.println(" Invalid pin config");
return false;
}
Expand Down
87 changes: 38 additions & 49 deletions src/PinMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,6 @@
#define VICTRON_PIN_TX 21
#endif

#ifndef VICTRON_PIN_RX2
#define VICTRON_PIN_RX2 -1
#endif

#ifndef VICTRON_PIN_TX2
#define VICTRON_PIN_TX2 -1
#endif

#ifndef VICTRON_PIN_RX3
#define VICTRON_PIN_RX3 -1
#endif

#ifndef VICTRON_PIN_TX3
#define VICTRON_PIN_TX3 -1
#endif

#ifndef REFUSOL_PIN_RX
#define REFUSOL_PIN_RX -1
#endif
Expand Down Expand Up @@ -262,19 +246,19 @@ PinMappingClass::PinMappingClass()
_pinMapping.led_rgb = LED_RGB;
#endif

_pinMapping.victron_tx = VICTRON_PIN_TX;
_pinMapping.victron_rx = VICTRON_PIN_RX;
_pinMapping.victron[0].tx = VICTRON_PIN_TX;
_pinMapping.victron[0].rx = VICTRON_PIN_RX;

_pinMapping.victron_tx2 = -1;
_pinMapping.victron_rx2 = -1;
_pinMapping.victron[1].tx = -1;
_pinMapping.victron[1].rx = -1;

_pinMapping.victron_tx3 = -1;
_pinMapping.victron_rx3 = -1;
_pinMapping.victron[2].tx = -1;
_pinMapping.victron[2].rx = -1;

#if defined(USE_REFUsol_INVERTER)
_pinMapping.REFUsol_rx = REFUSOL_PIN_RX;
_pinMapping.REFUsol_tx = REFUSOL_PIN_TX;
_pinMapping.REFUsol_rts = REFUSOL_PIN_RTS;
_pinMapping.REFUsol.rx = REFUSOL_PIN_RX;
_pinMapping.REFUsol.tx = REFUSOL_PIN_TX;
_pinMapping.REFUsol.rts = REFUSOL_PIN_RTS;
#endif

#if defined(USE_PYLONTECH_RS485_RECEIVER) || defined(USE_DALYBMS_CONTROLLER) || defined(USE_JKBMS_CONTROLLER)
Expand Down Expand Up @@ -384,22 +368,19 @@ void PinMappingClass::init(const String& deviceMapping)
#if defined(USE_LED_STRIP)
_pinMapping.led_rgb = doc[i]["led"]["rgb"] | LED_RGB;
#endif
_pinMapping.victron_rx = doc[i]["victron"]["rs232_rx"] | VICTRON_PIN_RX;
_pinMapping.victron_tx = doc[i]["victron"]["rs232_tx"] | VICTRON_PIN_TX;

_pinMapping.victron_rx2 = doc[i]["victron"]["rs232_rx2"] | VICTRON_PIN_RX2;
_pinMapping.victron_tx2 = doc[i]["victron"]["rs232_tx2"] | VICTRON_PIN_TX2;

_pinMapping.victron_rx3 = doc[i]["victron"]["rs232_rx3"] | VICTRON_PIN_RX3;
_pinMapping.victron_tx3 = doc[i]["victron"]["rs232_tx3"] | VICTRON_PIN_TX3;
for (int i=0; i<sizeof(_pinMapping.victron)/sizeof(RS232_t); i++) {
String offset = (i>0)?String(i+1):String("");
_pinMapping.victron[i].rx = doc[i]["victron"][String("rs232_rx")+offset] | -1;
_pinMapping.victron[i].tx = doc[i]["victron"][String("rs232_tx")+offset] | -1;
}

#if defined(USE_REFUsol_INVERTER)
_pinMapping.REFUsol_rx = doc[i]["refusol"]["rs485_rx"] | REFUSOL_PIN_RX;
_pinMapping.REFUsol_tx = doc[i]["refusol"]["rs485_tx"] | REFUSOL_PIN_TX;
_pinMapping.REFUsol.rx = doc[i]["refusol"]["rs485_rx"] | REFUSOL_PIN_RX;
_pinMapping.REFUsol.tx = doc[i]["refusol"]["rs485_tx"] | REFUSOL_PIN_TX;
if (doc[i]["refusol"].containsKey("rs485_rts")) {
_pinMapping.REFUsol_rts = doc[i]["refusol"]["rs485_rts"] | REFUSOL_PIN_RTS;
_pinMapping.REFUsol.rts = doc[i]["refusol"]["rs485_rts"] | REFUSOL_PIN_RTS;
} else {
_pinMapping.REFUsol_rts = -1;
_pinMapping.REFUsol.rts = -1;
}
#endif

Expand Down Expand Up @@ -611,9 +592,9 @@ bool PinMappingClass::isValidBatteryConfig() const
return false;
}

#if defined(USE_CHARGER_MEANWELL) || defined(USE_CHARGER_HUAWEI)
bool PinMappingClass::isValidChargerConfig() const
{
#if defined(USE_CHARGER_MEANWELL) || defined(USE_CHARGER_HUAWEI)
if (_pinMapping.charger.provider == Charger_Provider_t::CAN0)
return _pinMapping.charger.can0.rx >= 0 &&
_pinMapping.charger.can0.tx >= 0;
Expand All @@ -627,17 +608,26 @@ bool PinMappingClass::isValidChargerConfig() const
_pinMapping.charger.mcp2515.clk >= 0 &&
_pinMapping.charger.mcp2515.irq >= 0 &&
_pinMapping.charger.mcp2515.cs >= 0;

#endif
return false;
}
#endif

bool PinMappingClass::isValidPreChargeConfig() const
{
return _pinMapping.pre_charge >= 0
&& _pinMapping.full_power >= 0;
}

#if defined(USE_REFUsol_INVERTER)
bool PinMappingClass::isValidREFUsolConfig() const
{
return _pinMapping.REFUsol.rx > 0
&& _pinMapping.REFUsol.tx >= 0
&& _pinMapping.REFUsol.rx != _pinMapping.REFUsol.tx
&& _pinMapping.REFUsol.rts != _pinMapping.REFUsol.rx
&& _pinMapping.REFUsol.rts != _pinMapping.REFUsol.tx;
#endif

void PinMappingClass::createPinMappingJson() const
{
JsonDocument obj;
Expand Down Expand Up @@ -703,18 +693,17 @@ void PinMappingClass::createPinMappingJson() const
#endif

JsonObject victron = doc["victron"].to<JsonObject>();
victron["rs232_rx"] = _pinMapping.victron_rx;
victron["rs232_tx"] = _pinMapping.victron_tx;
victron["rs232_rx2"] = _pinMapping.victron_rx2;
victron["rs232_tx2"] = _pinMapping.victron_tx2;
victron["rs232_rx3"] = _pinMapping.victron_rx3;
victron["rs232_tx3"] = _pinMapping.victron_tx3;
for (int i=0; i<sizeof(_pinMapping.victron)/sizeof(RS232_t); i++) {
String offset = (i>0)?String(i+1):String("");
victron[String("rs232_rx")+offset] = _pinMapping.victron[i].rx;
victron[String("rs232_tx")+offset] = _pinMapping.victron[i].tx;
}

#if defined(USE_REFUsol_INVERTER)
JsonObject refusol = doc["refusol"].to<JsonObject>();
refusol["rs485_rx"] = _pinMapping.REFUsol_rx;
refusol["rs485_tx"] = _pinMapping.REFUsol_tx;
if (_pinMapping.REFUsol_rts >= 0) refusol["rs485_rts"] = _pinMapping.REFUsol_rts;
refusol["rs485_rx"] = _pinMapping.REFUsol.rx;
refusol["rs485_tx"] = _pinMapping.REFUsol.tx;
if (_pinMapping.REFUsol.rts >= 0) refusol["rs485_rts"] = _pinMapping.REFUsol.rts;
#endif

JsonObject battery = doc["battery"].to<JsonObject>();
Expand Down
6 changes: 3 additions & 3 deletions src/PylontechRS485Receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ bool PylontechRS485Receiver::init()
}
*/

const Battery_t& pin = PinMapping.get().battery;

if (!PinMapping.isValidChargerConfig()) {
if (!PinMapping.isValidBatteryConfig()) {
MessageOutput.println("Invalid pin config");
return false;
}
Expand All @@ -42,6 +40,8 @@ bool PylontechRS485Receiver::init()

_upSerial = std::make_unique<HardwareSerial>(*oHwSerialPort);

auto const& pin = PinMapping.get().battery;

_upSerial->begin(115200, SERIAL_8N1, pin.rs485.rx, pin.rs485.tx);
MessageOutput.printf("Port= %d, RS485 (Type %d) port rx = %d, tx = %d", *oHwSerialPort, pin.rs485.rts >= 0 ? 1 : 2, pin.rs485.rx, pin.rs485.tx);
if (pin.rs485.rts >= 0) {
Expand Down
Loading

0 comments on commit 6fe81cb

Please sign in to comment.