From f577c2c080667c333172f6aa570611f8a668805d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnim=20L=C3=A4uger?= Date: Wed, 1 Mar 2017 12:41:56 +0100 Subject: [PATCH] Sync uart configuration to Tx FIFO level (#1806) * sync uart configuration to tx fifo level * poll tx fifo empty before disabling interrupts * echo parameter is optional for uart.setup() --- app/driver/uart.c | 22 ++++++++++++++++++++++ docs/en/modules/uart.md | 10 +++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/driver/uart.c b/app/driver/uart.c index 8687712b..379d3da0 100755 --- a/app/driver/uart.c +++ b/app/driver/uart.c @@ -43,6 +43,22 @@ static void (*alt_uart0_tx)(char txchar); LOCAL void ICACHE_RAM_ATTR uart0_rx_intr_handler(void *para); + +/****************************************************************************** + * FunctionName : uart_wait_tx_empty + * Description : Internal used function + * Wait for TX FIFO to become empty. + * Parameters : uart_no, use UART0 or UART1 defined ahead + * Returns : NONE +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +uart_wait_tx_empty(uint8 uart_no) +{ + while ((READ_PERI_REG(UART_STATUS(uart_no)) & (UART_TXFIFO_CNT< 0) + ; +} + + /****************************************************************************** * FunctionName : uart_config * Description : Internal used function @@ -54,6 +70,8 @@ uart0_rx_intr_handler(void *para); LOCAL void ICACHE_FLASH_ATTR uart_config(uint8 uart_no) { + uart_wait_tx_empty(uart_no); + if (uart_no == UART1) { PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); } else { @@ -98,6 +116,8 @@ uart_config(uint8 uart_no) void ICACHE_FLASH_ATTR uart0_alt(uint8 on) { + uart_wait_tx_empty(UART0); + if (on) { PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); @@ -348,6 +368,8 @@ uart_setup(uint8 uart_no) #ifdef BIT_RATE_AUTOBAUD uart_stop_autobaud(); #endif + // poll Tx FIFO empty outside before disabling interrupts + uart_wait_tx_empty(uart_no); ETS_UART_INTR_DISABLE(); uart_config(uart_no); ETS_UART_INTR_ENABLE(); diff --git a/docs/en/modules/uart.md b/docs/en/modules/uart.md index 49c376c9..1b08cddc 100644 --- a/docs/en/modules/uart.md +++ b/docs/en/modules/uart.md @@ -67,10 +67,14 @@ end, 0) ## uart.setup() -(Re-)configures the communication parameters of the UART. +(Re-)configures the communication parameters of the UART. + +!!! note + + Bytes sent to the UART can get lost if this function re-configures the UART while reception is in progress. #### Syntax -`uart.setup(id, baud, databits, parity, stopbits, echo)` +`uart.setup(id, baud, databits, parity, stopbits[, echo])` #### Parameters - `id` always zero, only one uart supported @@ -78,7 +82,7 @@ end, 0) - `databits` one of 5, 6, 7, 8 - `parity` `uart.PARITY_NONE`, `uart.PARITY_ODD`, or `uart.PARITY_EVEN` - `stopbits` `uart.STOPBITS_1`, `uart.STOPBITS_1_5`, or `uart.STOPBITS_2` -- `echo` if 0, disable echo, otherwise enable echo +- `echo` if 0, disable echo, otherwise enable echo (default if omitted) #### Returns configured baud rate (number)