check chip state when enabled

This commit is contained in:
leoujz 2020-06-28 15:19:09 +08:00
parent 4677044f46
commit c309ddf5ee
2 changed files with 27 additions and 0 deletions

View File

@ -119,6 +119,10 @@ void TpUartDataLinkLayer::loop()
loadNextTxFrame(); loadNextTxFrame();
_loopState = TX_FRAME; _loopState = TX_FRAME;
} }
else if (!_waitConfirm) {
// if idle, state chip to check it is live or not
stateChip();
}
} }
break; break;
case TX_FRAME: case TX_FRAME:
@ -383,6 +387,28 @@ bool TpUartDataLinkLayer::sendFrame(CemiFrame& frame)
return true; return true;
} }
void TpUartDataLinkLayer::stateChip() {
// check chip state if enabled and no data received more than 10 seconds
if (_enabled && millis() - _lastByteRxTime > 10000) {
uint8_t cmd = U_STATE_REQ;
_platform.writeUart(cmd);
_waitConfirmStartTime = millis();
while (true)
{
int resp = _platform.readUart();
// I'm not sure about the possible values of 'resp', so any data from chip is treated to be alive
// Sometimes hardare issue would make it seems to be alive, such as Rx Tx connected.
if (resp != -1) {
break;
}
else if (millis() - _waitConfirmStartTime > RESET_TIMEOUT) {
_enabled = false;
break;
}
}
}
}
bool TpUartDataLinkLayer::resetChip() bool TpUartDataLinkLayer::resetChip()
{ {
uint8_t cmd = U_RESET_REQ; uint8_t cmd = U_RESET_REQ;

View File

@ -62,5 +62,6 @@ class TpUartDataLinkLayer : public DataLinkLayer
void dataConBytesReceived(uint8_t* buffer, uint16_t length, bool success); void dataConBytesReceived(uint8_t* buffer, uint16_t length, bool success);
bool resetChip(); bool resetChip();
void stopChip(); void stopChip();
void stateChip();
}; };
#endif #endif