1 /* Copyright 2018 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
9 * @brief API for UART serial ports
12 #include <metal/interrupt.h>
17 struct metal_uart_vtable {
18 void (*init)(struct metal_uart *uart, int baud_rate);
19 int (*putc)(struct metal_uart *uart, int c);
20 int (*txready)(struct metal_uart *uart);
21 int (*getc)(struct metal_uart *uart, int *c);
22 int (*get_baud_rate)(struct metal_uart *uart);
23 int (*set_baud_rate)(struct metal_uart *uart, int baud_rate);
24 struct metal_interrupt* (*controller_interrupt)(struct metal_uart *uart);
25 int (*get_interrupt_id)(struct metal_uart *uart);
29 * @brief Handle for a UART serial device
32 const struct metal_uart_vtable *vtable;
36 * @brief Initialize UART device
38 * Initialize the UART device described by the UART handle. This function must be called before any
39 * other method on the UART can be invoked. It is invalid to initialize a UART more than once.
41 * @param uart The UART device handle
42 * @param baud_rate the baud rate to set the UART to
44 __inline__ void metal_uart_init(struct metal_uart *uart, int baud_rate) { uart->vtable->init(uart, baud_rate); }
47 * @brief Output a character over the UART
48 * @param uart The UART device handle
49 * @param c The character to send over the UART
50 * @return 0 upon success
52 __inline__ int metal_uart_putc(struct metal_uart *uart, int c) { return uart->vtable->putc(uart, c); }
55 * @brief Test, determine if tx output is blocked(full/busy)
56 * @param uart The UART device handle
57 * @return 0 not blocked
59 __inline__ int metal_uart_txready(struct metal_uart *uart) { return uart->vtable->txready(uart); }
62 * @brief Read a character sent over the UART
63 * @param uart The UART device handle
64 * @param c The varible to hold the read character
65 * @return 0 upon success
67 * If "c == -1" no char was ready.
68 * If "c != -1" then C == byte value (0x00 to 0xff)
70 __inline__ int metal_uart_getc(struct metal_uart *uart, int *c) { return uart->vtable->getc(uart, c); }
73 * @brief Get the baud rate of the UART peripheral
74 * @param uart The UART device handle
75 * @return The current baud rate of the UART
77 __inline__ int metal_uart_get_baud_rate(struct metal_uart *uart) { return uart->vtable->get_baud_rate(uart); }
80 * @brief Set the baud rate of the UART peripheral
81 * @param uart The UART device handle
82 * @param baud_rate The baud rate to configure
83 * @return the new baud rate of the UART
85 __inline__ int metal_uart_set_baud_rate(struct metal_uart *uart, int baud_rate) { return uart->vtable->set_baud_rate(uart, baud_rate); }
88 * @brief Get the interrupt controller of the UART peripheral
90 * Get the interrupt controller for the UART peripheral. The interrupt
91 * controller must be initialized before any interrupts can be registered
94 * @param uart The UART device handle
95 * @return The handle for the UART interrupt controller
97 __inline__ struct metal_interrupt* metal_uart_interrupt_controller(struct metal_uart *uart) { return uart->vtable->controller_interrupt(uart); }
100 * @brief Get the interrupt ID of the UART controller
101 * @param uart The UART device handle
102 * @return The UART interrupt id
104 __inline__ int metal_uart_get_interrupt_id(struct metal_uart *uart) { return uart->vtable->get_interrupt_id(uart); }