From d97f1ee1f6d40c3336913c72658cc05668d7f9f2 Mon Sep 17 00:00:00 2001 From: Nathaniel Graff Date: Thu, 3 Oct 2019 14:35:00 -0700 Subject: [PATCH] Provide metal_uart_get_device Signed-off-by: Nathaniel Graff --- metal/uart.h | 5 +++++ src/uart.c | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/metal/uart.h b/metal/uart.h index e83aaed0..68dd93dc 100644 --- a/metal/uart.h +++ b/metal/uart.h @@ -32,6 +32,11 @@ struct metal_uart { const struct metal_uart_vtable *vtable; }; +/*! @brief Get a handle for a UART device + * @param device_num The index of the desired UART device + * @return A handle to the UART device, or NULL if the device does not exist*/ +struct metal_uart *metal_uart_get_device(unsigned int device_num); + /*! * @brief Initialize UART device diff --git a/src/uart.c b/src/uart.c index fad237bb..a869a312 100644 --- a/src/uart.c +++ b/src/uart.c @@ -1,6 +1,7 @@ /* Copyright 2018 SiFive, Inc */ /* SPDX-License-Identifier: Apache-2.0 */ +#include #include extern __inline__ void metal_uart_init(struct metal_uart *uart, int baud_rate); @@ -10,3 +11,13 @@ extern __inline__ int metal_uart_getc(struct metal_uart *uart, int *c); extern __inline__ int metal_uart_get_baud_rate(struct metal_uart *uart); extern __inline__ int metal_uart_set_baud_rate(struct metal_uart *uart, int baud_rate); + +struct metal_uart *metal_uart_get_device(unsigned int device_num) { +#if __METAL_DT_MAX_UARTS > 0 + if (device_num < __METAL_DT_MAX_UARTS) { + return (struct metal_uart *)__metal_uart_table[device_num]; + } +#endif + + return NULL; +}