Skip to content

Commit

Permalink
RS485: Fix Auto ID assignment (mysensors#1451)
Browse files Browse the repository at this point in the history
* S485: Fix auto id assignment

* RS485: assertDE(),deassertDE() cleanup

Co-authored-by: Klaudiusz <Klaudiusz>
  • Loading branch information
klaudiusz223 authored Apr 14, 2022
1 parent 263ea26 commit 4afa7a6
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions hal/transport/RS485/MyTransportRS485.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, LOW)
#else
#define assertDE() hwDigitalWrite(MY_RS485_DE_PIN, LOW); delayMicroseconds(5)
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, HIG)
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, HIGH)
#endif
#else
#define assertDE()
Expand Down Expand Up @@ -142,11 +142,14 @@ bool _serialProcess()
// Case 0 looks for the header. Bytes arrive in the serial interface and get
// shifted through a header buffer. When the start and end characters in
// the buffer match the SOH/STX pair, and the destination station ID matches
// our ID, save the header information and progress to the next state.
// our ID or BROADCAST_ADDRESS, save the header information and progress to
// the next state.
case 0:
memcpy(&_header[0],&_header[1],5);
_header[5] = inch;
if ((_header[0] == SOH) && (_header[5] == STX) && (_header[1] != _header[2])) {
if ((_header[0] == SOH) && (_header[5] == STX) &&
((_header[1] == (char)_nodeId) ||
(_header[1] == (char)BROADCAST_ADDRESS && _header[2] != (char)_nodeId))) {
_recCalcCS = 0;
_recStation = _header[1];
_recSender = _header[2];
Expand All @@ -165,16 +168,6 @@ bool _serialProcess()
break;
}

//Check if we should process this message
//We reject the message if we are the sender
//We reject if we are not the receiver and message is not a broadcast
if ((_recSender == _nodeId) ||
(_recStation != _nodeId &&
_recStation != BROADCAST_ADDRESS)) {
_serialReset();
break;
}

if (_recLen == 0) {
_recPhase = 2;
}
Expand Down Expand Up @@ -267,14 +260,7 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
}
}

#if defined(MY_RS485_DE_PIN)
#if !defined(MY_RS485_DE_INVERSE)
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
#else
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
#endif
delayMicroseconds(5);
#endif
assertDE();

// Start of header by writing multiple SOH
for(byte w=0; w<MY_RS485_SOH_COUNT; w++) {
Expand Down Expand Up @@ -318,11 +304,7 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
_dev.flush();
#endif
#endif
#if !defined(MY_RS485_DE_INVERSE)
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
#else
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
#endif
deassertDE();
#endif
return true;
}
Expand All @@ -334,13 +316,10 @@ bool transportInit(void)
// Reset the state machine
_dev.begin(MY_RS485_BAUD_RATE);
_serialReset();
_nodeId = AUTO;
#if defined(MY_RS485_DE_PIN)
hwPinMode(MY_RS485_DE_PIN, OUTPUT);
#if !defined(MY_RS485_DE_INVERSE)
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
#else
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
#endif
deassertDE();
#endif
return true;
}
Expand Down

0 comments on commit 4afa7a6

Please sign in to comment.