1 /* Copyright 2018 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
9 * @brief API for UART serial ports
12 #include <metal/interrupt.h>
16 struct metal_uart_vtable {
17 void (*init)(struct metal_uart *uart, int baud_rate);
18 int (*putc)(struct metal_uart *uart, unsigned char c);
19 int (*getc)(struct metal_uart *uart, unsigned char *c);
20 int (*get_baud_rate)(struct metal_uart *uart);
21 int (*set_baud_rate)(struct metal_uart *uart, int baud_rate);
22 struct metal_interrupt* (*controller_interrupt)(struct metal_uart *uart);
23 int (*get_interrupt_id)(struct metal_uart *uart);
27 * @brief Handle for a UART serial device
30 const struct metal_uart_vtable *vtable;
34 * @brief Initialize UART device
36 * Initialize the UART device described by the UART handle. This function must be called before any
37 * other method on the UART can be invoked. It is invalid to initialize a UART more than once.
39 * @param uart The UART device handle
40 * @param baud_rate the baud rate to set the UART to
42 inline void metal_uart_init(struct metal_uart *uart, int baud_rate) { return uart->vtable->init(uart, baud_rate); }
45 * @brief Output a character over the UART
46 * @param uart The UART device handle
47 * @param c The character to send over the UART
48 * @return 0 upon success
50 inline int metal_uart_putc(struct metal_uart *uart, unsigned char c) { return uart->vtable->putc(uart, c); }
53 * @brief Read a character sent over the UART
54 * @param uart The UART device handle
55 * @param c The varible to hold the read character
56 * @return 0 upon success
58 inline int metal_uart_getc(struct metal_uart *uart, unsigned char *c) { return uart->vtable->getc(uart, c); }
61 * @brief Get the baud rate of the UART peripheral
62 * @param uart The UART device handle
63 * @return The current baud rate of the UART
65 inline int metal_uart_get_baud_rate(struct metal_uart *uart) { return uart->vtable->get_baud_rate(uart); }
68 * @brief Set the baud rate of the UART peripheral
69 * @param uart The UART device handle
70 * @param baud_rate The baud rate to configure
71 * @return the new baud rate of the UART
73 inline int metal_uart_set_baud_rate(struct metal_uart *uart, int baud_rate) { return uart->vtable->set_baud_rate(uart, baud_rate); }
76 * @brief Get the interrupt controller of the UART peripheral
78 * Get the interrupt controller for the UART peripheral. The interrupt
79 * controller must be initialized before any interrupts can be registered
82 * @param uart The UART device handle
83 * @return The handle for the UART interrupt controller
85 inline struct metal_interrupt* metal_uart_interrupt_controller(struct metal_uart *uart) { return uart->vtable->controller_interrupt(uart); }
88 * @brief Get the interrupt ID of the UART controller
89 * @param uart The UART device handle
90 * @return The UART interrupt id
92 inline int metal_uart_get_interrupt_id(struct metal_uart *uart) { return uart->vtable->get_interrupt_id(uart); }