smart tx - try to fix Tx problems

- fix typo - replace emsaurt_tx_poll by emsuart_tx_poll
- smart tx - enabled by EMS_Sys_Status.emsTxDelay == 2
  use loopback to send in burst mode
- change emsuart_tx_poll to use emsuart_tx_buffer
- encapsulate uart_swap in #ifndef NO_UART_SWAP (private use)
This commit is contained in:
Susis Strolch
2019-05-21 22:50:26 +02:00
parent f35fdfab7c
commit 93b6b1ea4c
3 changed files with 86 additions and 23 deletions

View File

@@ -697,7 +697,7 @@ void _ems_readTelegram(uint8_t * telegram, uint8_t length) {
} else {
// nothing to send so just send a poll acknowledgement back
if (EMS_Sys_Status.emsPollEnabled) {
emsaurt_tx_poll();
emsuart_tx_poll();
}
}
} else if (EMS_Sys_Status.emsTxStatus == EMS_TX_STATUS_WAIT) {
@@ -705,14 +705,14 @@ void _ems_readTelegram(uint8_t * telegram, uint8_t length) {
if (value == EMS_TX_SUCCESS) {
EMS_Sys_Status.emsTxPkgs++;
// got a success 01. Send a validate to check the value of the last write
emsaurt_tx_poll(); // send a poll to free the EMS bus
emsuart_tx_poll(); // send a poll to free the EMS bus
_createValidate(); // create a validate Tx request (if needed)
} else if (value == EMS_TX_ERROR) {
// last write failed (04), delete it from queue and dont bother to retry
if (EMS_Sys_Status.emsLogging == EMS_SYS_LOGGING_VERBOSE) {
myDebug_P(PSTR("** Write command failed from host"));
}
emsaurt_tx_poll(); // send a poll to free the EMS bus
emsuart_tx_poll(); // send a poll to free the EMS bus
_removeTxQueue(); // remove from queue
}
}
@@ -1052,7 +1052,7 @@ void _processType(_EMS_RxTelegram * EMS_RxTelegram) {
}
}
emsaurt_tx_poll(); // send Acknowledgement back to free the EMS bus since we have the telegram
emsuart_tx_poll(); // send Acknowledgement back to free the EMS bus since we have the telegram
}

View File

@@ -40,8 +40,7 @@ static void emsuart_rx_intr_handler(void * para) {
}
// clear Rx FIFO full and Rx FIFO timeout interrupts
U0IC = (1 << UIFF);
U0IC = (1 << UITO);
U0IC = (1 << UIFF) | (1 << UITO);
}
// BREAK detection = End of EMS data block
@@ -76,6 +75,13 @@ static void ICACHE_FLASH_ATTR emsuart_recvTask(os_event_t * events) {
pEMSRxBuf = paEMSRxBuf[++emsRxBufIdx % EMS_MAXBUFFERS]; // next free EMS Receive buffer
}
static inline void ICACHE_FLASH_ATTR emsuart_flush_fifos() {
// flush everything left over in buffer, this clears both rx and tx FIFOs
uint32_t tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask
USC0(EMSUART_UART) |= (tmp); // set bits
USC0(EMSUART_UART) &= ~(tmp); // clear bits
}
/*
* init UART0 driver
*/
@@ -100,11 +106,8 @@ void ICACHE_FLASH_ATTR emsuart_init() {
USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD);
USC0(EMSUART_UART) = EMSUART_CONFIG; // 8N1
// flush everything left over in buffer, this clears both rx and tx FIFOs
uint32_t tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask
USC0(EMSUART_UART) |= (tmp); // set bits
USC0(EMSUART_UART) &= ~(tmp); // clear bits
emsuart_flush_fifos();
// conf1 params
// UCTOE = RX TimeOut enable (default is 1)
// UCTOT = RX TimeOut Threshold (7bit) = want this when no more data after 2 characters. (default is 2)
@@ -128,7 +131,9 @@ void ICACHE_FLASH_ATTR emsuart_init() {
system_set_os_print(0); // https://github.com/espruino/Espruino/issues/655
// swap Rx and Tx pins to use GPIO13 (D7) and GPIO15 (D8) respectively
#ifndef NO_UART_SWAP
system_uart_swap();
#endif
ETS_UART_INTR_ATTACH(emsuart_rx_intr_handler, NULL);
ETS_UART_INTR_ENABLE();
@@ -172,26 +177,84 @@ void ICACHE_FLASH_ATTR emsuart_tx_brk() {
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear bit
}
/*
* set loopback mode and clear Tx/Rx FIFO
*/
static inline void ICACHE_FLASH_ATTR emsuart_loopback(boolean enable) {
uint32_t tmp = (1 << UCLBE); // Loopback mask
if (enable)
USC0(EMSUART_UART) |= (tmp); // enable loopback
else
USC0(EMSUART_UART) &= ~(tmp); // disable loopback
}
/*
* Send to Tx, ending with a <BRK>
*/
void ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len) {
for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i];
uint32_t tmp;
// backward compatibility
if (EMS_Sys_Status.emsTxDelay<2) {
for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i];
// check if we need to force a delay to slow down Tx
// https://github.com/proddy/EMS-ESP/issues/23#
if (EMS_Sys_Status.emsTxDelay) {
delayMicroseconds(EMS_TX_BRK_WAIT);
// check if we need to force a delay to slow down Tx
// https://github.com/proddy/EMS-ESP/issues/23#
if (EMS_Sys_Status.emsTxDelay==1) {
delayMicroseconds(EMS_TX_BRK_WAIT);
}
}
emsuart_tx_brk(); // send <BRK>
} else {
// smart Tx
#define UART_BIT_TIME 104 // bit time @9600 baud
ETS_UART_INTR_DISABLE(); // disable rx interrupt
emsuart_flush_fifos();
emsuart_loopback(true); // reset FIFOs & enable loopback
for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i]; // send byte
delayMicroseconds(10*UART_BIT_TIME);
/* wait until
* ° loopback char is received
* ° Rx-FIFO full (unlikely)
* ° Rx-TimeOut (unlikely)
* ° Break detected (bus collision) - not handled now...
*/
for (uint8_t l = 0; l < 13; l++) {
if (((USS(EMSUART_UART) >> USRXC) & 0xFF)
|| (U0IS & ((1 << UIFF) | (1 << UITO) | (1 << UIBD))))
break;
delayMicroseconds(UART_BIT_TIME / 8); // ~13µs
}
uint32_t break_detect = (U0IS & (1 << UIBD)); // keep break detect interrupt
(void) (USF(EMSUART_UART)); // read out fifo, also clears FIFO counter
U0IC = (1 << UIFF) | (1 << UITO) | (1 << UIBD); // clear pending interrupts
if (break_detect)
break; // collision / abort from master
}
// send <BRK> - wait until <BRK> detect
USC0(EMSUART_UART) |= (1 << UCBRK); // send <BRK>
while (!(U0IS & (1 << UIBD)))
delayMicroseconds(UART_BIT_TIME / 8); // ~13µs
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear <BRK>
U0IC = (1 << UIFF) | (1 << UITO) | (1 << UIBD); // clear pending interrupts
emsuart_flush_fifos();
emsuart_loopback(false); // disable loopback mode
ETS_UART_INTR_ENABLE(); // enable rx interrupt
}
emsuart_tx_brk(); // send <BRK>
}
/*
* Send the Poll (our own ID) to Tx as a single byte and end with a <BRK>
*/
void ICACHE_FLASH_ATTR emsaurt_tx_poll() {
USF(EMSUART_UART) = EMS_ID_ME;
emsuart_tx_brk(); // send <BRK>
}
void ICACHE_FLASH_ATTR emsuart_tx_poll() {
static uint8_t buf[] = {EMS_ID_ME};
emsuart_tx_buffer(buf,1);
}

View File

@@ -33,5 +33,5 @@ void ICACHE_FLASH_ATTR emsuart_init();
void ICACHE_FLASH_ATTR emsuart_stop();
void ICACHE_FLASH_ATTR emsuart_start();
void ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len);
void ICACHE_FLASH_ATTR emsaurt_tx_poll();
void ICACHE_FLASH_ATTR emsuart_tx_poll();
void ICACHE_FLASH_ATTR emsuart_tx_brk();