Use full duplex in tpuart_data_link_layer. (#161)

* TPUART full duplex handling

* Stay in loop when RX_L_DATA.
This commit is contained in:
mptei 2021-12-28 19:53:48 +01:00 committed by GitHub
parent fb74931bec
commit 12d0ea1ad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 316 additions and 257 deletions

View File

@ -11,6 +11,9 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
// Activate trace output
//#define DBG_TRACE
// NCN5120 // NCN5120
//#define NCN5120 //#define NCN5120
@ -75,25 +78,28 @@
#define U_STOP_MODE_IND 0x2B #define U_STOP_MODE_IND 0x2B
#define U_SYSTEM_STAT_IND 0x4B #define U_SYSTEM_STAT_IND 0x4B
//loop states //tx states
#define IDLE 0 enum {
#define RX_FIRST_BYTE 1 TX_IDLE,
#define RX_L_DATA 2 TX_FRAME,
#define RX_WAIT_DATA_CON 3 TX_WAIT_ECHO,
#define TX_FRAME 4 TX_WAIT_CONN
};
#define BYTE_TIMEOUT 10 //milli seconds //rx states
enum {
RX_WAIT_START,
RX_L_DATA,
RX_WAIT_EOP
};
#define EOP_TIMEOUT 2 //milli seconds; end of layer-2 packet gap
#define CONFIRM_TIMEOUT 500 //milli seconds #define CONFIRM_TIMEOUT 500 //milli seconds
#define RESET_TIMEOUT 100 //milli seconds #define RESET_TIMEOUT 100 //milli seconds
void TpUartDataLinkLayer::loop() void TpUartDataLinkLayer::loop()
{ {
_receiveBuffer[0] = 0x29;
_receiveBuffer[1] = 0;
uint8_t* buffer = _receiveBuffer + 2;
uint8_t rxByte;
if (!_enabled) if (!_enabled)
{ {
if (millis() - _lastResetChipTime > 1000) if (millis() - _lastResetChipTime > 1000)
@ -107,266 +113,317 @@ void TpUartDataLinkLayer::loop()
if (!_enabled) if (!_enabled)
return; return;
switch (_loopState) do {
{ _receiveBuffer[0] = 0x29;
case IDLE: _receiveBuffer[1] = 0;
if (_platform.uartAvailable()) uint8_t* buffer = _receiveBuffer + 2;
{ uint8_t rxByte;
_loopState = RX_FIRST_BYTE; bool isEchoComplete = false; // Flag that a complete echo is received
} bool dataConnMsg = 0; // The DATA_CONN message just seen or 0
else bool isEOP = (millis() - _lastByteRxTime > EOP_TIMEOUT); // Flag that an EOP gap is seen
{ switch (_rxState)
if (!_waitConfirm && !isTxQueueEmpty()) {
case RX_WAIT_START:
if (_platform.uartAvailable())
{ {
loadNextTxFrame(); rxByte = _platform.readUart();
_loopState = TX_FRAME; #ifdef DBG_TRACE
} print(rxByte, HEX);
} #endif
break; _lastByteRxTime = millis();
case TX_FRAME:
if (sendSingleFrameByte() == false)
{
_waitConfirm = true;
_waitConfirmStartTime = millis();
_loopState = IDLE;
}
break;
case RX_FIRST_BYTE:
rxByte = _platform.readUart();
_lastByteRxTime = millis();
_RxByteCnt = 0;
_xorSum = 0;
if ((rxByte & L_DATA_MASK) == L_DATA_STANDARD_IND)
{
buffer[_RxByteCnt++] = rxByte;
_xorSum ^= rxByte;
_RxByteCnt++; //convert to L_DATA_EXTENDED
_convert = true;
_loopState = RX_L_DATA;
break;
}
else if ((rxByte & L_DATA_MASK) == L_DATA_EXTENDED_IND)
{
buffer[_RxByteCnt++] = rxByte;
_xorSum ^= rxByte;
_convert = false;
_loopState = RX_L_DATA;
break;
}
else if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON)
{
println("got unexpected L_DATA_CON");
}
else if (rxByte == L_POLL_DATA_IND)
{
// not sure if this can happen
println("got L_POLL_DATA_IND");
}
else if ((rxByte & L_ACKN_MASK) == L_ACKN_IND)
{
// this can only happen in bus monitor mode
println("got L_ACKN_IND");
}
else if (rxByte == U_RESET_IND)
{
println("got U_RESET_IND");
}
else if ((rxByte & U_STATE_IND) == U_STATE_IND)
{
print("got U_STATE_IND: 0x");
print(rxByte, HEX);
println();
}
else if ((rxByte & U_FRAME_STATE_MASK) == U_FRAME_STATE_IND)
{
print("got U_FRAME_STATE_IND: 0x");
print(rxByte, HEX);
println();
}
else if ((rxByte & U_CONFIGURE_MASK) == U_CONFIGURE_IND)
{
print("got U_CONFIGURE_IND: 0x");
print(rxByte, HEX);
println();
}
else if (rxByte == U_FRAME_END_IND)
{
println("got U_FRAME_END_IND");
}
else if (rxByte == U_STOP_MODE_IND)
{
println("got U_STOP_MODE_IND");
}
else if (rxByte == U_SYSTEM_STAT_IND)
{
print("got U_SYSTEM_STAT_IND: 0x");
while (true)
{
int tmp = _platform.readUart();
if (tmp < 0)
continue;
print(tmp, HEX); // Check for layer-2 packets
break; _RxByteCnt = 0;
} _xorSum = 0;
println(); if ((rxByte & L_DATA_MASK) == L_DATA_STANDARD_IND)
}
else
{
print("got UNEXPECTED: 0x");
print(rxByte, HEX);
println();
}
_loopState = IDLE;
break;
case RX_L_DATA:
if (millis() - _lastByteRxTime > BYTE_TIMEOUT)
{
_RxByteCnt = 0;
_loopState = IDLE;
println("Timeout during RX_L_DATA");
break;
}
if (!_platform.uartAvailable())
break;
_lastByteRxTime = millis();
rxByte = _platform.readUart();
if (_RxByteCnt == MAX_KNX_TELEGRAM_SIZE)
{
_loopState = IDLE;
println("invalid telegram size");
}
else
{
buffer[_RxByteCnt++] = rxByte;
}
if (_RxByteCnt == 7)
{
//Destination Address + payload available
_xorSum ^= rxByte;
//check if echo
if (_sendBuffer != nullptr && (!((buffer[0] ^ _sendBuffer[0]) & ~0x20) && !memcmp(buffer + _convert + 1, _sendBuffer + 1, 5)))
{ //ignore repeated bit of control byte
_isEcho = true;
}
else
{
_isEcho = false;
}
//convert into Extended.ind
if (_convert)
{
uint8_t payloadLength = buffer[6] & 0x0F;
buffer[1] = buffer[6] & 0xF0;
buffer[6] = payloadLength;
}
if (!_isEcho)
{
uint8_t c = 0x10;
// The bau knows everything and could either check the address table object (normal device)
// or any filter tables (coupler) to see if we are addressed.
//check if individual or group address
bool isGroupAddress = (buffer[1] & 0x80) != 0;
uint16_t addr = getWord(buffer + 4);
if (_cb.isAckRequired(addr, isGroupAddress))
{ {
c |= 0x01; buffer[_RxByteCnt++] = rxByte;
_xorSum ^= rxByte;
_RxByteCnt++; //convert to L_DATA_EXTENDED
_convert = true;
_rxState = RX_L_DATA;
#ifdef DBG_TRACE
println("RLS");
#endif
break;
}
else if ((rxByte & L_DATA_MASK) == L_DATA_EXTENDED_IND)
{
buffer[_RxByteCnt++] = rxByte;
_xorSum ^= rxByte;
_convert = false;
_rxState = RX_L_DATA;
#ifdef DBG_TRACE
println("RLX");
#endif
break;
} }
_platform.writeUart(c); // Handle all single byte packets here
} else if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON)
}
else if (_RxByteCnt == buffer[6] + 7 + 2)
{
//complete Frame received, payloadLength+1 for TCPI +1 for CRC
if (rxByte == (uint8_t)(~_xorSum))
{
//check if crc is correct
if (_isEcho && _sendBuffer != NULL)
{ {
//check if it is realy an echo, rx_crc = tx_crc dataConnMsg = rxByte;
if (rxByte == _sendBuffer[_sendBufferLength - 1])
_isEcho = true;
else
_isEcho = false;
} }
if (_isEcho) else if (rxByte == L_POLL_DATA_IND)
{ {
_loopState = RX_WAIT_DATA_CON; // not sure if this can happen
println("got L_POLL_DATA_IND");
}
else if ((rxByte & L_ACKN_MASK) == L_ACKN_IND)
{
// this can only happen in bus monitor mode
println("got L_ACKN_IND");
}
else if (rxByte == U_RESET_IND)
{
println("got U_RESET_IND");
}
else if ((rxByte & U_STATE_IND) == U_STATE_IND)
{
print("got U_STATE_IND:");
if (rxByte & 0x80) print (" SC");
if (rxByte & 0x40) print (" RE");
if (rxByte & 0x20) print (" TE");
if (rxByte & 0x10) print (" PE");
if (rxByte & 0x08) print (" TW");
println();
}
else if ((rxByte & U_FRAME_STATE_MASK) == U_FRAME_STATE_IND)
{
print("got U_FRAME_STATE_IND: 0x");
print(rxByte, HEX);
println();
}
else if ((rxByte & U_CONFIGURE_MASK) == U_CONFIGURE_IND)
{
print("got U_CONFIGURE_IND: 0x");
print(rxByte, HEX);
println();
}
else if (rxByte == U_FRAME_END_IND)
{
println("got U_FRAME_END_IND");
}
else if (rxByte == U_STOP_MODE_IND)
{
println("got U_STOP_MODE_IND");
}
else if (rxByte == U_SYSTEM_STAT_IND)
{
print("got U_SYSTEM_STAT_IND: 0x");
while (true)
{
int tmp = _platform.readUart();
if (tmp < 0)
continue;
print(tmp, HEX);
break;
}
println();
} }
else else
{ {
frameBytesReceived(_receiveBuffer, _RxByteCnt + 2); print("got UNEXPECTED: 0x");
_loopState = IDLE; print(rxByte, HEX);
println();
}
}
break;
case RX_L_DATA:
if (isEOP)
{
_rxState = RX_WAIT_START;
print("EOP inside RX_L_DATA");
printHex(" => ", buffer, _RxByteCnt);
break;
}
if (!_platform.uartAvailable())
break;
_lastByteRxTime = millis();
rxByte = _platform.readUart();
#ifdef DBG_TRACE
print(rxByte, HEX);
#endif
if (_RxByteCnt == MAX_KNX_TELEGRAM_SIZE)
{
_rxState = RX_WAIT_EOP;
println("invalid telegram size");
}
else
{
buffer[_RxByteCnt++] = rxByte;
}
if (_RxByteCnt == 7)
{
//Destination Address + payload available
_xorSum ^= rxByte;
//check if echo
if (_sendBuffer != nullptr && (!((buffer[0] ^ _sendBuffer[0]) & ~0x20) && !memcmp(buffer + _convert + 1, _sendBuffer + 1, 5)))
{ //ignore repeated bit of control byte
_isEcho = true;
}
else
{
_isEcho = false;
}
//convert into Extended.ind
if (_convert)
{
uint8_t payloadLength = buffer[6] & 0x0F;
buffer[1] = buffer[6] & 0xF0;
buffer[6] = payloadLength;
}
if (!_isEcho)
{
uint8_t c = 0x10;
// The bau knows everything and could either check the address table object (normal device)
// or any filter tables (coupler) to see if we are addressed.
//check if individual or group address
bool isGroupAddress = (buffer[1] & 0x80) != 0;
uint16_t addr = getWord(buffer + 4);
if (_cb.isAckRequired(addr, isGroupAddress))
{
c |= 0x01;
}
// Hint: We can send directly here, this doesn't disturb other transmissions
_platform.writeUart(c);
}
}
else if (_RxByteCnt == buffer[6] + 7 + 2)
{
//complete Frame received, payloadLength+1 for TCPI +1 for CRC
if (rxByte == (uint8_t)(~_xorSum))
{
//check if crc is correct
if (_isEcho && _sendBuffer != NULL)
{
//check if it is realy an echo, rx_crc = tx_crc
if (rxByte == _sendBuffer[_sendBufferLength - 1])
_isEcho = true;
else
_isEcho = false;
}
if (_isEcho)
{
isEchoComplete = true;
}
else
{
frameBytesReceived(_receiveBuffer, _RxByteCnt + 2);
}
_rxState = RX_WAIT_START;
#ifdef DBG_TRACE
println("RX_WAIT_START");
#endif
}
else
{
println("frame with invalid crc ignored");
_rxState = RX_WAIT_EOP;
} }
} }
else else
{ {
println("frame with invalid crc ignored"); _xorSum ^= rxByte;
_loopState = IDLE;
} }
}
else
{
_xorSum ^= rxByte;
}
break;
case RX_WAIT_DATA_CON:
if (!_platform.uartAvailable())
break; break;
rxByte = _platform.readUart(); case RX_WAIT_EOP:
_lastByteRxTime = millis(); if (isEOP)
if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON) {
{ _RxByteCnt = 0;
//println("L_DATA_CON received"); _rxState = RX_WAIT_START;
dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, ((rxByte & SUCCESS) > 0)); #ifdef DBG_TRACE
_waitConfirm = false; println("RX_WAIT_START");
delete[] _sendBuffer; #endif
_sendBuffer = 0; break;
_sendBufferLength = 0; }
_loopState = IDLE; if (!_platform.uartAvailable())
} break;
else _lastByteRxTime = millis();
{ rxByte = _platform.readUart();
//should not happen #ifdef DBG_TRACE
println("expected L_DATA_CON not received"); print(rxByte, HEX);
dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, false); #endif
_waitConfirm = false; break;
delete[] _sendBuffer; default:
_sendBuffer = 0; break;
_sendBufferLength = 0;
_loopState = IDLE;
}
break;
default:
break;
}
if (_waitConfirm)
{
if (millis() - _waitConfirmStartTime > CONFIRM_TIMEOUT)
{
println("L_DATA_CON not received within expected time");
uint8_t cemiBuffer[MAX_KNX_TELEGRAM_SIZE];
cemiBuffer[0] = 0x29;
cemiBuffer[1] = 0;
memcpy((cemiBuffer + 2), _sendBuffer, _sendBufferLength);
dataConBytesReceived(cemiBuffer, _sendBufferLength + 2, false);
_waitConfirm = false;
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
if (_loopState == RX_WAIT_DATA_CON)
_loopState = IDLE;
} }
}
// Check for spurios DATA_CONN message
if (dataConnMsg && _txState != TX_WAIT_CONN && _txState != TX_WAIT_ECHO) {
println("got unexpected L_DATA_CON");
}
switch (_txState)
{
case TX_IDLE:
if (!isTxQueueEmpty())
{
loadNextTxFrame();
_txState = TX_FRAME;
#ifdef DBG_TRACE
println("TX_FRAME");
#endif
}
break;
case TX_FRAME:
if (sendSingleFrameByte() == false)
{
_waitConfirmStartTime = millis();
_txState = TX_WAIT_ECHO;
#ifdef DBG_TRACE
println("TX_WAIT_ECHO");
#endif
}
break;
case TX_WAIT_ECHO:
case TX_WAIT_CONN:
if (isEchoComplete)
{
_txState = TX_WAIT_CONN;
#ifdef DBG_TRACE
println("TX_WAIT_CONN");
#endif
}
else if (dataConnMsg)
{
bool waitEcho = (_txState == TX_WAIT_ECHO);
if (waitEcho) {
println("L_DATA_CON without echo");
}
dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, !waitEcho && ((dataConnMsg & SUCCESS) > 0));
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
_txState = TX_IDLE;
}
else if (millis() - _waitConfirmStartTime > CONFIRM_TIMEOUT)
{
println("L_DATA_CON not received within expected time");
uint8_t cemiBuffer[MAX_KNX_TELEGRAM_SIZE];
cemiBuffer[0] = 0x29;
cemiBuffer[1] = 0;
memcpy((cemiBuffer + 2), _sendBuffer, _sendBufferLength);
dataConBytesReceived(cemiBuffer, _sendBufferLength + 2, false);
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
_txState = TX_IDLE;
#ifdef DBG_TRACE
println("TX_IDLE");
#endif
}
break;
}
} while (_rxState == RX_L_DATA);
} }
bool TpUartDataLinkLayer::sendFrame(CemiFrame& frame) bool TpUartDataLinkLayer::sendFrame(CemiFrame& frame)
@ -496,6 +553,9 @@ bool TpUartDataLinkLayer::sendSingleFrameByte()
cmd[0] = U_L_DATA_END_REQ | _TxByteCnt; cmd[0] = U_L_DATA_END_REQ | _TxByteCnt;
cmd[1] = _sendBuffer[_TxByteCnt]; cmd[1] = _sendBuffer[_TxByteCnt];
#ifdef DBG_TRACE
print(cmd[1], HEX);
#endif
_platform.writeUart(cmd, 2); _platform.writeUart(cmd, 2);
_TxByteCnt++; _TxByteCnt++;

View File

@ -31,16 +31,15 @@ class TpUartDataLinkLayer : public DataLinkLayer
private: private:
bool _enabled = false; bool _enabled = false;
bool _waitConfirm = false;
uint8_t* _sendBuffer = 0; uint8_t* _sendBuffer = 0;
uint16_t _sendBufferLength = 0; uint16_t _sendBufferLength = 0;
uint8_t _receiveBuffer[MAX_KNX_TELEGRAM_SIZE]; uint8_t _receiveBuffer[MAX_KNX_TELEGRAM_SIZE];
uint8_t _loopState = 0; uint8_t _txState = 0;
uint8_t _rxState = 0;
uint16_t _RxByteCnt = 0; uint16_t _RxByteCnt = 0;
uint16_t _TxByteCnt = 0; uint16_t _TxByteCnt = 0;
uint8_t _oldIdx = 0; uint8_t _oldIdx = 0;
bool _isEcho = false; bool _isEcho = false;
bool _isAddressed = false;
bool _convert = false; bool _convert = false;
uint8_t _xorSum = 0; uint8_t _xorSum = 0;
uint32_t _lastByteRxTime; uint32_t _lastByteRxTime;