From: Nathael Pajani Date: Tue, 10 Nov 2015 20:28:36 +0000 (+0100) Subject: Add some helper functions for serial (UART) driver. X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=1e27274b2d252a3a3f8fdb9928f1ae02767a0c72;p=soft%2Flpc122x%2Fcore Add some helper functions for serial (UART) driver. --- diff --git a/drivers/serial.c b/drivers/serial.c index c7522f6..42212f6 100644 --- a/drivers/serial.c +++ b/drivers/serial.c @@ -149,6 +149,22 @@ static void uart_start_sending(uint32_t uart_num) } +/****************************************************************************** */ +/* Serial send byte - quick function with almost no tests. + * If the uart is not sending, the byte is placed directly in the data buffer and + * the call returns 0. + * Else, the call returns -EBUSY and nothing is sent. + */ +int serial_send_quickbyte(uint32_t uart_num, uint8_t data) +{ + struct uart_device* uart = &uarts[uart_num]; + if (!uart->sending) { + uart->regs->func.buffer = data; + return 0; + } else { + return -EBUSY; + } +} /***************************************************************************** */ /* Serial Write @@ -239,7 +255,8 @@ struct uart_clk_cfg { uint8_t mul_val; }; static struct uart_clk_cfg uart_clk_table[] = { - { 1152000, 2, 4, 13}, + { 250000, 12, 0, 1}, + { 1152000, 2, 3, 10}, { 0, 0, 0, 0, }, }; @@ -260,7 +277,7 @@ static void uart_clk_on(uint32_t uart_num, uint32_t baudrate) pclk = get_main_clock(); /* See above note */ div = (pclk / (baudrate * 16)); sys_ctrl->uart_clk_div[uart_num] = 0x01; - /* The easy one : divider is an integer, or baudrate is low enought for the aproximation */ + /* The easy one : divider is an integer, or baudrate is low enough for the aproximation */ if ((baudrate <= 115200) || ((div * baudrate * 16) == pclk)) { uart->line_ctrl |= LPC_UART_ENABLE_DLAB; uart->ctrl.divisor_latch_lsb = (div & 0xff); @@ -300,7 +317,7 @@ static uint32_t uart_setup(uint32_t uart_num) struct lpc_uart* uart = uarts[uart_num].regs; /* Get the right registers */ uint32_t status = 0; /* Set up UART mode */ - uart->line_ctrl = uarts[uart_num].config;; + uart->line_ctrl = uarts[uart_num].config; /* Clear all fifo, reset and enable them */ /* Note : fifo trigger level is one bit */ uart->ctrl.fifo_ctrl = (LPC_UART_FIFO_EN | LPC_UART_TX_CLR | LPC_UART_RX_CLR); @@ -312,6 +329,21 @@ static uint32_t uart_setup(uint32_t uart_num) return status; } +/* Change UART configuration (number of data, parity and stop bits). + * config is a mask of LPC_UART_xBIT (x = 5..8), LPC_UART_xSTOP (x = 1..2) + * and one of LPC_UART_NO_PAR, LPC_UART_ODD_PAR or LPC_UART_EVEN_PAR. + */ +int uart_set_config(uint8_t uart_num, uint8_t config) +{ + struct uart_device* uart = NULL; + + if (uart_num >= NUM_UARTS) + return -EINVAL; + uart = &uarts[uart_num]; + uart->config = config; + return 0; +} + struct uart_def { uint32_t irq; diff --git a/include/drivers/serial.h b/include/drivers/serial.h index 7f0bce8..8a6fcd8 100644 --- a/include/drivers/serial.h +++ b/include/drivers/serial.h @@ -77,11 +77,25 @@ int serial_write(uint32_t uart_num, const char *buf, uint32_t length); int serial_flush(uint32_t uart_num); +/****************************************************************************** */ +/* Serial send byte - quick function with almost no tests. + * If the uart is not sending, the byte is placed directly in the data buffer and + * the call returns 0. + * Else, the call returns -EBUSY and nothing is sent. + */ +int serial_send_quickbyte(uint32_t uart_num, uint8_t data); /***************************************************************************** */ /* Public access to UART setup */ + +/* Change UART configuration (number of data, parity and stop bits). + * config is a mask of LPC_UART_xBIT (x = 5..8), LPC_UART_xSTOP (x = 1..2) + * and one of LPC_UART_NO_PAR, LPC_UART_ODD_PAR or LPC_UART_EVEN_PAR. + */ +int uart_set_config(uint8_t uart_num, uint8_t config); + /* Change uart mode to RS485 * return -ENODEV when the device does not support RS485 mode. */