]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/metal/uart.h
Update RISCC-V-RV32-SiFive_HiFive1_FreedomStudio project to latest tools and metal...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_FreedomStudio / freedom-metal / metal / uart.h
1 /* Copyright 2018 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #ifndef METAL__UART_H
5 #define METAL__UART_H
6
7 /*!
8  * @file uart.h
9  * @brief API for UART serial ports
10  */
11
12 #include <metal/interrupt.h>
13
14 struct metal_uart;
15 #undef getc
16 #undef putc
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);
26 };
27
28 /*!
29  * @brief Handle for a UART serial device
30  */
31 struct metal_uart {
32     const struct metal_uart_vtable *vtable;
33 };
34
35 /*!
36  * @brief Initialize UART device
37  
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.
40  *
41  * @param uart The UART device handle
42  * @param baud_rate the baud rate to set the UART to
43  */
44 __inline__ void metal_uart_init(struct metal_uart *uart, int baud_rate) { uart->vtable->init(uart, baud_rate); }
45
46 /*!
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
51  */
52 __inline__ int metal_uart_putc(struct metal_uart *uart, int c) { return uart->vtable->putc(uart, c); }
53
54 /*!
55  * @brief Test, determine if tx output is blocked(full/busy)
56  * @param uart The UART device handle
57  * @return 0 not blocked
58  */
59 __inline__ int metal_uart_txready(struct metal_uart *uart) { return uart->vtable->txready(uart); }
60
61 /*!
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
66  *
67  * If "c == -1" no char was ready.
68  * If "c != -1" then C == byte value (0x00 to 0xff)
69  */
70 __inline__ int metal_uart_getc(struct metal_uart *uart, int *c) { return uart->vtable->getc(uart, c); }
71
72 /*!
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
76  */
77 __inline__ int metal_uart_get_baud_rate(struct metal_uart *uart) { return uart->vtable->get_baud_rate(uart); }
78
79 /*!
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
84  */
85 __inline__ int metal_uart_set_baud_rate(struct metal_uart *uart, int baud_rate) { return uart->vtable->set_baud_rate(uart, baud_rate); }
86
87 /*!
88  * @brief Get the interrupt controller of the UART peripheral
89  *
90  * Get the interrupt controller for the UART peripheral. The interrupt
91  * controller must be initialized before any interrupts can be registered
92  * or enabled with it.
93  *
94  * @param uart The UART device handle
95  * @return The handle for the UART interrupt controller
96  */
97 __inline__ struct metal_interrupt* metal_uart_interrupt_controller(struct metal_uart *uart) { return uart->vtable->controller_interrupt(uart); }
98
99 /*!
100  * @brief Get the interrupt ID of the UART controller
101  * @param uart The UART device handle
102  * @return The UART interrupt id
103  */
104 __inline__ int metal_uart_get_interrupt_id(struct metal_uart *uart) { return uart->vtable->get_interrupt_id(uart); }
105
106 #endif