uart.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright 2018 SiFive, Inc */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. #ifndef METAL__UART_H
  4. #define METAL__UART_H
  5. /*!
  6. * @file uart.h
  7. * @brief API for UART serial ports
  8. */
  9. #include <metal/interrupt.h>
  10. struct metal_uart;
  11. #undef getc
  12. #undef putc
  13. struct metal_uart_vtable {
  14. void (*init)(struct metal_uart *uart, int baud_rate);
  15. int (*putc)(struct metal_uart *uart, int c);
  16. int (*txready)(struct metal_uart *uart);
  17. int (*getc)(struct metal_uart *uart, int *c);
  18. int (*get_baud_rate)(struct metal_uart *uart);
  19. int (*set_baud_rate)(struct metal_uart *uart, int baud_rate);
  20. struct metal_interrupt* (*controller_interrupt)(struct metal_uart *uart);
  21. int (*get_interrupt_id)(struct metal_uart *uart);
  22. };
  23. /*!
  24. * @brief Handle for a UART serial device
  25. */
  26. struct metal_uart {
  27. const struct metal_uart_vtable *vtable;
  28. };
  29. /*!
  30. * @brief Initialize UART device
  31. * Initialize the UART device described by the UART handle. This function must be called before any
  32. * other method on the UART can be invoked. It is invalid to initialize a UART more than once.
  33. *
  34. * @param uart The UART device handle
  35. * @param baud_rate the baud rate to set the UART to
  36. */
  37. __inline__ void metal_uart_init(struct metal_uart *uart, int baud_rate) { uart->vtable->init(uart, baud_rate); }
  38. /*!
  39. * @brief Output a character over the UART
  40. * @param uart The UART device handle
  41. * @param c The character to send over the UART
  42. * @return 0 upon success
  43. */
  44. __inline__ int metal_uart_putc(struct metal_uart *uart, int c) { return uart->vtable->putc(uart, c); }
  45. /*!
  46. * @brief Test, determine if tx output is blocked(full/busy)
  47. * @param uart The UART device handle
  48. * @return 0 not blocked
  49. */
  50. __inline__ int metal_uart_txready(struct metal_uart *uart) { return uart->vtable->txready(uart); }
  51. /*!
  52. * @brief Read a character sent over the UART
  53. * @param uart The UART device handle
  54. * @param c The varible to hold the read character
  55. * @return 0 upon success
  56. *
  57. * If "c == -1" no char was ready.
  58. * If "c != -1" then C == byte value (0x00 to 0xff)
  59. */
  60. __inline__ int metal_uart_getc(struct metal_uart *uart, int *c) { return uart->vtable->getc(uart, c); }
  61. /*!
  62. * @brief Get the baud rate of the UART peripheral
  63. * @param uart The UART device handle
  64. * @return The current baud rate of the UART
  65. */
  66. __inline__ int metal_uart_get_baud_rate(struct metal_uart *uart) { return uart->vtable->get_baud_rate(uart); }
  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. * @brief Get the interrupt controller of the UART peripheral
  76. *
  77. * Get the interrupt controller for the UART peripheral. The interrupt
  78. * controller must be initialized before any interrupts can be registered
  79. * or enabled with it.
  80. *
  81. * @param uart The UART device handle
  82. * @return The handle for the UART interrupt controller
  83. */
  84. __inline__ struct metal_interrupt* metal_uart_interrupt_controller(struct metal_uart *uart) { return uart->vtable->controller_interrupt(uart); }
  85. /*!
  86. * @brief Get the interrupt ID of the UART controller
  87. * @param uart The UART device handle
  88. * @return The UART interrupt id
  89. */
  90. __inline__ int metal_uart_get_interrupt_id(struct metal_uart *uart) { return uart->vtable->get_interrupt_id(uart); }
  91. #endif