From 982c4429c1789be328b0c021c7b661689ef9ee20 Mon Sep 17 00:00:00 2001 From: Adam Green Date: Sat, 7 Sep 2013 01:20:52 -0700 Subject: [PATCH] serial_putc() to make better use of Tx FIFO If don't know if this is an issue that anyone cares about. I am also not sure what the best way to solve it is either. I just thought I would issue a pull request with this commit to bring the issue to light and show a possible solution that I have tested on my mbed-1768 device. Previously the serial_putc() API didn't make any use of the Tx FIFO since the serial_writable() API it utilizes only returns true when the FIFO is completely empty. This is due to the fact that the THRE bit of the UART's LSR (Line Status Register) only goes high when the whole FIFO is empty. I noticed this when doing some performance testing with the network stack. I went from calling printf() to output 3 bytes every 10 seconds (with packet drop stats) to instead output 4 bytes every 10 seconds. I thought these should easily fit in the 16 byte FIFO but outputting one extra byte caused an additional three 550 byte UDP packets to be dropped. This should only happen if the additional character being sent to the UART was taking away extra CPU cycles from the network stack. My solution is to keep track of the number of bytes that have been placed in the Tx FIFO since it was last detected as being completely empty (via the THRE bit). Only once this count hits 16 does the code then block, waiting for the THRE bit to go high. Each time the THRE bit does go high, the count is reset to 0 again and it is incremented for each byte that is loaded into the THR. --- .../targets/hal/TARGET_NXP/TARGET_LPC176X/objects.h | 1 + .../targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/objects.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/objects.h index 86736c1ecd1..ace5371b469 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/objects.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/objects.h @@ -47,6 +47,7 @@ struct pwmout_s { struct serial_s { LPC_UART_TypeDef *uart; int index; + uint8_t count; }; struct analogin_s { diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c index f62fcaab63d..207a9729938 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c @@ -106,6 +106,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) { case UART_2: obj->index = 2; break; case UART_3: obj->index = 3; break; } + obj->count = 0; is_stdio_uart = (uart == STDIO_UART) ? (1) : (0); @@ -283,6 +284,7 @@ int serial_getc(serial_t *obj) { void serial_putc(serial_t *obj, int c) { while (!serial_writable(obj)); obj->uart->THR = c; + obj->count++; uint32_t lsr = obj->uart->LSR; lsr = lsr; @@ -295,7 +297,13 @@ int serial_readable(serial_t *obj) { } int serial_writable(serial_t *obj) { - return obj->uart->LSR & 0x20; + int isWritable = 1; + if (obj->uart->LSR & 0x20) + obj->count = 0; + else if (obj->count >= 16) + isWritable = 0; + + return isWritable; } void serial_clear(serial_t *obj) {