[Eastron] Add support for RS485 collision detection on ESP32

This commit is contained in:
TD-er
2023-08-06 17:52:00 +02:00
parent 2650b29659
commit 5a8609c288
15 changed files with 35 additions and 17 deletions
+6 -2
View File
@@ -258,9 +258,13 @@ String ESPeasySerial::getLogString() const {
return getSerialConfig().getLogString();
}
bool ESPeasySerial::setRS485Mode(int8_t rtsPin) {
bool ESPeasySerial::setRS485Mode(int8_t rtsPin, bool enableCollisionDetection) {
if (_serialPort != nullptr) {
return _serialPort->setRS485Mode(rtsPin);
#ifdef ESP32
return _serialPort->setRS485Mode(rtsPin, enableCollisionDetection);
#else
return _serialPort->setRS485Mode(rtsPin, false);
#endif
}
return false;
}
+1 -1
View File
@@ -166,7 +166,7 @@ public:
// Try enabling RTS pin and set to UART_RS485_HALF_DUPLEX
// RTS pin can then be connected to ~RE/DE pin of MAX485
// @retval True when supported and successful.
bool setRS485Mode(int8_t rtsPin);
bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection = false);
private:
@@ -273,14 +273,14 @@ size_t Port_ESPEasySerial_HardwareSerial_t::setTxBufferSize(size_t new_size)
}
bool Port_ESPEasySerial_HardwareSerial_t::setRS485Mode(int8_t rtsPin)
bool Port_ESPEasySerial_HardwareSerial_t::setRS485Mode(int8_t rtsPin, bool enableCollisionDetection)
{
#ifdef ESP32
if (_serial != nullptr) {
if (rtsPin >= 0) {
return _serial->setPins(-1, -1, -1, rtsPin) &&
_serial->setHwFlowCtrlMode(UART_HW_FLOWCTRL_RTS) &&
_serial->setMode(UART_MODE_RS485_HALF_DUPLEX);
_serial->setMode(enableCollisionDetection ? UART_MODE_RS485_COLLISION_DETECT : UART_MODE_RS485_HALF_DUPLEX);
}
_serial->setMode(UART_MODE_UART);
}
@@ -50,7 +50,7 @@ public:
// Try enabling RTS pin and set to UART_RS485_HALF_DUPLEX
// RTS pin can then be connected to ~RE/DE pin of MAX485
// @retval True when supported and successful.
bool setRS485Mode(int8_t rtsPin);
bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection = false);
private:
@@ -135,7 +135,7 @@ size_t Port_ESPEasySerial_I2C_SC16IS752_t::setTxBufferSize(size_t new_size)
return 0;
}
bool Port_ESPEasySerial_I2C_SC16IS752_t::setRS485Mode(int8_t rtsPin)
bool Port_ESPEasySerial_I2C_SC16IS752_t::setRS485Mode(int8_t rtsPin, bool enableCollisionDetection)
{
// TODO TD-er: Check if we can enable RTS on this chip
return false;
@@ -44,7 +44,7 @@ public:
size_t setRxBufferSize(size_t new_size);
size_t setTxBufferSize(size_t new_size);
bool setRS485Mode(int8_t rtsPin);
bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection = false);
private:
@@ -169,7 +169,7 @@ size_t Port_ESPEasySerial_SW_Serial_t::setTxBufferSize(size_t new_size)
return 0;
}
bool Port_ESPEasySerial_SW_Serial_t::setRS485Mode(int8_t rtsPin)
bool Port_ESPEasySerial_SW_Serial_t::setRS485Mode(int8_t rtsPin, bool enableCollisionDetection)
{
// TODO TD-er: Check if we can include toggling this pin in the SW serial lib
return false;
@@ -46,7 +46,7 @@ public:
size_t setRxBufferSize(size_t new_size);
size_t setTxBufferSize(size_t new_size);
bool setRS485Mode(int8_t rtsPin);
bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection = false);
private:
#if USES_LATEST_SOFTWARE_SERIAL_LIBRARY
@@ -261,7 +261,7 @@ size_t Port_ESPEasySerial_USBCDC_t::setTxBufferSize(size_t new_size)
return 0;
}
bool Port_ESPEasySerial_USBCDC_t::setRS485Mode(int8_t rtsPin)
bool Port_ESPEasySerial_USBCDC_t::setRS485Mode(int8_t rtsPin, bool enableCollisionDetection)
{
return false;
}
@@ -41,7 +41,7 @@ public:
size_t setRxBufferSize(size_t new_size);
size_t setTxBufferSize(size_t new_size);
bool setRS485Mode(int8_t rtsPin);
bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection = false);
virtual int getBaudRate() const override;
@@ -195,7 +195,7 @@ size_t Port_ESPEasySerial_USB_HWCDC_t::setTxBufferSize(size_t new_size)
return 0;
}
bool Port_ESPEasySerial_USB_HWCDC_t::setRS485Mode(int8_t rtsPin)
bool Port_ESPEasySerial_USB_HWCDC_t::setRS485Mode(int8_t rtsPin, bool enableCollisionDetection)
{
return false;
}
@@ -40,7 +40,7 @@ public:
size_t setRxBufferSize(size_t new_size);
size_t setTxBufferSize(size_t new_size);
bool setRS485Mode(int8_t rtsPin);
bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection = false);
private:
+1 -1
View File
@@ -76,7 +76,7 @@ public:
virtual size_t setRxBufferSize(size_t new_size) = 0;
virtual size_t setTxBufferSize(size_t new_size) = 0;
virtual bool setRS485Mode(int8_t rtsPin) = 0;
virtual bool setRS485Mode(int8_t rtsPin, bool enableCollisionDetection) = 0;
+12 -2
View File
@@ -139,6 +139,13 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string)
addFormNumericBox(F("Modbus Address"), P078_DEV_ID_LABEL, P078_DEV_ID, 1, 247);
#ifdef ESP32
addFormCheckBox(F("Enable Collision Detection"), F(P078_FLAG_COLL_DETECT_LABEL), P078_GET_FLAG_COLL_DETECT);
addFormNote(F("/RE connected to GND, only supported on hardware serial"));
#endif
if (Plugin_078_SDM != nullptr) {
addRowLabel(F("Checksum (pass/fail)"));
String chksumStats;
@@ -201,6 +208,9 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string)
P078_DEV_ID = getFormItemInt(P078_DEV_ID_LABEL);
P078_MODEL = getFormItemInt(P078_MODEL_LABEL);
P078_BAUDRATE = getFormItemInt(P078_BAUDRATE_LABEL);
#ifdef ESP32
P078_SET_FLAG_COLL_DETECT(isFormItemChecked(F(P078_FLAG_COLL_DETECT_LABEL)));
#endif
Plugin_078_init = false; // Force device setup next time
success = true;
@@ -228,7 +238,7 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string)
Plugin_078_SDM = nullptr;
}
if (Plugin_078_ESPEasySerial->setRS485Mode(P078_DEPIN)) {
if (Plugin_078_ESPEasySerial->setRS485Mode(P078_DEPIN, P078_GET_FLAG_COLL_DETECT)) {
Plugin_078_SDM = new SDM(*Plugin_078_ESPEasySerial, baudrate);
} else {
Plugin_078_SDM = new SDM(*Plugin_078_ESPEasySerial, baudrate, P078_DEPIN);
@@ -371,7 +381,7 @@ boolean Plugin_078(uint8_t function, struct EventStruct *event, String& string)
for (int i = 0; i < nrBaudRates && new_baud > 5; ++i) {
if (new_baud == baudrates[i]) {
new_baud = baudrates[i];
new_baud = i;
}
}
}
+4
View File
@@ -17,6 +17,10 @@
# define P078_BAUDRATE PCONFIG(2)
# define P078_BAUDRATE_LABEL PCONFIG_LABEL(2)
# define P078_GET_FLAG_COLL_DETECT bitRead(PCONFIG(7), 0)
# define P078_SET_FLAG_COLL_DETECT(x) bitWrite(PCONFIG(7), 0, x)
# define P078_FLAG_COLL_DETECT_LABEL "colldet"
# define P078_QUERY1_CONFIG_POS 3
# define P078_QUERY1 PCONFIG(P078_QUERY1_CONFIG_POS)