From 04b51e381e2ff3f4c7aba662e1c817ce5daa931d Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 14 Nov 2021 05:23:14 +1100 Subject: Update UART driver API (#14839) * Add uart_puts() and uart_gets() * Add some docs * Rework API * Formatting * Update docs/uart_driver.md Co-authored-by: Sergey Vlasov * Simplify a uart_write() loop * Update platforms/avr/drivers/uart.c Co-authored-by: Joel Challis Co-authored-by: Sergey Vlasov Co-authored-by: Joel Challis --- platforms/avr/drivers/uart.c | 26 +++++++++++++++++++------- platforms/avr/drivers/uart.h | 8 ++++++-- platforms/chibios/drivers/uart.c | 8 ++++++-- platforms/chibios/drivers/uart.h | 8 ++++++-- 4 files changed, 37 insertions(+), 13 deletions(-) (limited to 'platforms') diff --git a/platforms/avr/drivers/uart.c b/platforms/avr/drivers/uart.c index c6abcb6fe0..01cf6b1fb8 100644 --- a/platforms/avr/drivers/uart.c +++ b/platforms/avr/drivers/uart.c @@ -100,7 +100,7 @@ void uart_init(uint32_t baud) { } // Transmit a byte -void uart_putchar(uint8_t c) { +void uart_write(uint8_t data) { uint8_t i; i = tx_buffer_head + 1; @@ -110,27 +110,39 @@ void uart_putchar(uint8_t c) { while (tx_buffer_tail == i) ; // wait until space in buffer // cli(); - tx_buffer[i] = c; + tx_buffer[i] = data; tx_buffer_head = i; UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn) | (1 << UDRIEn); // sei(); } // Receive a byte -uint8_t uart_getchar(void) { - uint8_t c, i; +uint8_t uart_read(void) { + uint8_t data, i; while (rx_buffer_head == rx_buffer_tail) ; // wait for character i = rx_buffer_tail + 1; if (i >= RX_BUFFER_SIZE) i = 0; - c = rx_buffer[i]; + data = rx_buffer[i]; rx_buffer_tail = i; - return c; + return data; +} + +void uart_transmit(const uint8_t *data, uint16_t length) { + for (uint16_t i = 0; i < length; i++) { + uart_write(data[i]); + } +} + +void uart_receive(uint8_t *data, uint16_t length) { + for (uint16_t i = 0; i < length; i++) { + data[i] = uart_read(); + } } // Return whether the number of bytes waiting in the receive buffer is nonzero. -// Call this before uart_getchar() to check if it will need +// Call this before uart_read() to check if it will need // to wait for a byte to arrive. bool uart_available(void) { uint8_t head, tail; diff --git a/platforms/avr/drivers/uart.h b/platforms/avr/drivers/uart.h index 602eb3d8b0..9cb7652b08 100644 --- a/platforms/avr/drivers/uart.h +++ b/platforms/avr/drivers/uart.h @@ -28,8 +28,12 @@ void uart_init(uint32_t baud); -void uart_putchar(uint8_t c); +void uart_write(uint8_t data); -uint8_t uart_getchar(void); +uint8_t uart_read(void); + +void uart_transmit(const char *data, uint16_t length); + +void uart_receive(char *data, uint16_t length); bool uart_available(void); diff --git a/platforms/chibios/drivers/uart.c b/platforms/chibios/drivers/uart.c index 0e8e0515af..297c1892c3 100644 --- a/platforms/chibios/drivers/uart.c +++ b/platforms/chibios/drivers/uart.c @@ -39,12 +39,16 @@ void uart_init(uint32_t baud) { } } -void uart_putchar(uint8_t c) { sdPut(&SERIAL_DRIVER, c); } +void uart_write(uint8_t data) { sdPut(&SERIAL_DRIVER, c); } -uint8_t uart_getchar(void) { +uint8_t uart_read(void) { msg_t res = sdGet(&SERIAL_DRIVER); return (uint8_t)res; } +void uart_transmit(const uint8_t *data, uint16_t length) { sdWrite(&SERIAL_DRIVER, data, length); } + +void uart_receive(uint8_t *data, uint16_t length) { sdRead(&SERIAL_DRIVER, data, length); } + bool uart_available(void) { return !sdGetWouldBlock(&SERIAL_DRIVER); } diff --git a/platforms/chibios/drivers/uart.h b/platforms/chibios/drivers/uart.h index b4e20e9fd3..5bc4875901 100644 --- a/platforms/chibios/drivers/uart.h +++ b/platforms/chibios/drivers/uart.h @@ -70,8 +70,12 @@ void uart_init(uint32_t baud); -void uart_putchar(uint8_t c); +void uart_write(uint8_t data); -uint8_t uart_getchar(void); +uint8_t uart_read(void); + +void uart_transmit(const uint8_t *data, uint16_t length); + +void uart_receive(uint8_t *data, uint16_t length); bool uart_available(void); -- cgit v1.2.3