mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 00:39:50 +03:00
@@ -16,6 +16,7 @@
|
|||||||
_EMSRxBuf * pEMSRxBuf;
|
_EMSRxBuf * pEMSRxBuf;
|
||||||
_EMSRxBuf * paEMSRxBuf[EMS_MAXBUFFERS];
|
_EMSRxBuf * paEMSRxBuf[EMS_MAXBUFFERS];
|
||||||
uint8_t emsRxBufIdx = 0;
|
uint8_t emsRxBufIdx = 0;
|
||||||
|
uint8_t phantomBrk = 0; // tells Rx about upcoming phantom break from Tx
|
||||||
|
|
||||||
os_event_t recvTaskQueue[EMSUART_recvTaskQueueLen]; // our Rx queue
|
os_event_t recvTaskQueue[EMSUART_recvTaskQueueLen]; // our Rx queue
|
||||||
|
|
||||||
@@ -49,6 +50,12 @@ static void emsuart_rx_intr_handler(void * para) {
|
|||||||
|
|
||||||
U0IC = (1 << UIBD); // INT clear the BREAK detect interrupt
|
U0IC = (1 << UIBD); // INT clear the BREAK detect interrupt
|
||||||
|
|
||||||
|
// we've send a BRK with loopback enabled, so we must compensate the phantom break
|
||||||
|
if (phantomBrk) {
|
||||||
|
length--; // compensate buffer length
|
||||||
|
phantomBrk = 0; // clear flag
|
||||||
|
}
|
||||||
|
|
||||||
// copy data into transfer buffer
|
// copy data into transfer buffer
|
||||||
pEMSRxBuf->writePtr = length;
|
pEMSRxBuf->writePtr = length;
|
||||||
os_memcpy((void *)pEMSRxBuf->buffer, (void *)&uart_buffer, length);
|
os_memcpy((void *)pEMSRxBuf->buffer, (void *)&uart_buffer, length);
|
||||||
@@ -237,13 +244,13 @@ void ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len) {
|
|||||||
emsuart_flush_fifos();
|
emsuart_flush_fifos();
|
||||||
|
|
||||||
// throw out the telegram...
|
// throw out the telegram...
|
||||||
for (uint8_t i = 0; i < len; i++) {
|
for (uint8_t i = 0; i < len;) {
|
||||||
USF(EMSUART_UART) = buf[i]; // send each Tx byte
|
USF(EMSUART_UART) = buf[i++]; // send each Tx byte
|
||||||
}
|
// wait for echo from busmaster
|
||||||
|
while ((((USS(EMSUART_UART) >> USRXC) & 0xFF) < i || (USIS(EMSUART_UART) & (1 << UIBD)))) {
|
||||||
|
delayMicroseconds(EMSUART_BIT_TIME); // burn CPU cycles...
|
||||||
|
|
||||||
// wait until we received sizeof(telegram) or RxBRK (== collision detect)
|
}
|
||||||
while ((((USS(EMSUART_UART) >> USRXC) & 0xFF) < len) || (USIS(EMSUART_UART) & (1 << UIBD))) {
|
|
||||||
delayMicroseconds(11 * EMSUART_BIT_TIME); // burn CPU cycles...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we got the whole telegram in Rx buffer
|
// we got the whole telegram in Rx buffer
|
||||||
@@ -251,13 +258,15 @@ void ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len) {
|
|||||||
// otherwise, we send the final Tx-BRK in loopback and enable Rx-INT.
|
// otherwise, we send the final Tx-BRK in loopback and enable Rx-INT.
|
||||||
// worst case, we'll see an additional Rx-BRK...
|
// worst case, we'll see an additional Rx-BRK...
|
||||||
if (!(USIS(EMSUART_UART) & (1 << UIBD))) {
|
if (!(USIS(EMSUART_UART) & (1 << UIBD))) {
|
||||||
|
phantomBrk = 1; // tell Rx to expect a phantom BRK
|
||||||
|
|
||||||
// no bus collision - send terminating BRK signal
|
// no bus collision - send terminating BRK signal
|
||||||
emsuart_loopback(true);
|
emsuart_loopback(true);
|
||||||
USC0(EMSUART_UART) |= (1 << UCBRK); // set <BRK>
|
USC0(EMSUART_UART) |= (1 << UCBRK); // set <BRK>
|
||||||
|
|
||||||
// wait until BRK detected...
|
// wait until BRK detected...
|
||||||
while (!(USIS(EMSUART_UART) & (1 << UIBD))) {
|
while (!(USIS(EMSUART_UART) & (1 << UIBD))) {
|
||||||
delayMicroseconds(EMSUART_BIT_TIME / 8); // ~13µs
|
delayMicroseconds(EMSUART_BIT_TIME);
|
||||||
}
|
}
|
||||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear <BRK>
|
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear <BRK>
|
||||||
|
|
||||||
@@ -265,7 +274,8 @@ void ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len) {
|
|||||||
emsuart_loopback(false); // disable loopback mode
|
emsuart_loopback(false); // disable loopback mode
|
||||||
}
|
}
|
||||||
|
|
||||||
emsuart_flush_fifos(); // flush Rx buffer to be sure
|
// w/o flushing the Rx FIFO we will get a trailing \0 from our loopback BRK
|
||||||
|
// emsuart_flush_fifos(); // flush Rx buffer to be sure
|
||||||
ETS_UART_INTR_ENABLE(); // receive anything from FIFO...
|
ETS_UART_INTR_ENABLE(); // receive anything from FIFO...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user