]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_GCC/freedom-metal/metal/uart.h
Base project to replace existing Freedom Studio project using latest Freedom Studio...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_GCC / 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
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);
24 };
25
26 /*!
27  * @brief Handle for a UART serial device
28  */
29 struct metal_uart {
30     const struct metal_uart_vtable *vtable;
31 };
32
33 /*!
34  * @brief Initialize UART device
35  
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.
38  *
39  * @param uart The UART device handle
40  * @param baud_rate the baud rate to set the UART to
41  */
42 inline void metal_uart_init(struct metal_uart *uart, int baud_rate) { return uart->vtable->init(uart, baud_rate); }
43
44 /*!
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
49  */
50 inline int metal_uart_putc(struct metal_uart *uart, unsigned char c) { return uart->vtable->putc(uart, c); }
51
52 /*!
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
57  */
58 inline int metal_uart_getc(struct metal_uart *uart, unsigned char *c) { return uart->vtable->getc(uart, c); }
59
60 /*!
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
64  */
65 inline int metal_uart_get_baud_rate(struct metal_uart *uart) { return uart->vtable->get_baud_rate(uart); }
66
67 /*!
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
72  */
73 inline int metal_uart_set_baud_rate(struct metal_uart *uart, int baud_rate) { return uart->vtable->set_baud_rate(uart, baud_rate); }
74
75 /*!
76  * @brief Get the interrupt controller of the UART peripheral
77  *
78  * Get the interrupt controller for the UART peripheral. The interrupt
79  * controller must be initialized before any interrupts can be registered
80  * or enabled with it.
81  *
82  * @param uart The UART device handle
83  * @return The handle for the UART interrupt controller
84  */
85 inline struct metal_interrupt* metal_uart_interrupt_controller(struct metal_uart *uart) { return uart->vtable->controller_interrupt(uart); }
86
87 /*!
88  * @brief Get the interrupt ID of the UART controller
89  * @param uart The UART device handle
90  * @return The UART interrupt id
91  */
92 inline int metal_uart_get_interrupt_id(struct metal_uart *uart) { return uart->vtable->get_interrupt_id(uart); }
93
94 #endif