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()
This commit is contained in:
Arnim Läuger 2017-03-01 12:41:56 +01:00 committed by Marcel Stör
parent 2168e5185e
commit f577c2c080
2 changed files with 29 additions and 3 deletions

View File

@ -43,6 +43,22 @@ static void (*alt_uart0_tx)(char txchar);
LOCAL void ICACHE_RAM_ATTR LOCAL void ICACHE_RAM_ATTR
uart0_rx_intr_handler(void *para); 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<<UART_TXFIFO_CNT_S)) > 0)
;
}
/****************************************************************************** /******************************************************************************
* FunctionName : uart_config * FunctionName : uart_config
* Description : Internal used function * Description : Internal used function
@ -54,6 +70,8 @@ uart0_rx_intr_handler(void *para);
LOCAL void ICACHE_FLASH_ATTR LOCAL void ICACHE_FLASH_ATTR
uart_config(uint8 uart_no) uart_config(uint8 uart_no)
{ {
uart_wait_tx_empty(uart_no);
if (uart_no == UART1) { if (uart_no == UART1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
} else { } else {
@ -98,6 +116,8 @@ uart_config(uint8 uart_no)
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
uart0_alt(uint8 on) uart0_alt(uint8 on)
{ {
uart_wait_tx_empty(UART0);
if (on) if (on)
{ {
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U);
@ -348,6 +368,8 @@ uart_setup(uint8 uart_no)
#ifdef BIT_RATE_AUTOBAUD #ifdef BIT_RATE_AUTOBAUD
uart_stop_autobaud(); uart_stop_autobaud();
#endif #endif
// poll Tx FIFO empty outside before disabling interrupts
uart_wait_tx_empty(uart_no);
ETS_UART_INTR_DISABLE(); ETS_UART_INTR_DISABLE();
uart_config(uart_no); uart_config(uart_no);
ETS_UART_INTR_ENABLE(); ETS_UART_INTR_ENABLE();

View File

@ -67,10 +67,14 @@ end, 0)
## uart.setup() ## 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 #### Syntax
`uart.setup(id, baud, databits, parity, stopbits, echo)` `uart.setup(id, baud, databits, parity, stopbits[, echo])`
#### Parameters #### Parameters
- `id` always zero, only one uart supported - `id` always zero, only one uart supported
@ -78,7 +82,7 @@ end, 0)
- `databits` one of 5, 6, 7, 8 - `databits` one of 5, 6, 7, 8
- `parity` `uart.PARITY_NONE`, `uart.PARITY_ODD`, or `uart.PARITY_EVEN` - `parity` `uart.PARITY_NONE`, `uart.PARITY_ODD`, or `uart.PARITY_EVEN`
- `stopbits` `uart.STOPBITS_1`, `uart.STOPBITS_1_5`, or `uart.STOPBITS_2` - `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 #### Returns
configured baud rate (number) configured baud rate (number)