drv_uart.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021/04/22 Juice Add UART0 support for JuiceVm.
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #include "drv_uart.h"
  15. #include "rv_mtvec_map.h"
  16. struct rt_serial_device *serial;
  17. char *device_name = "uart0";
  18. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  19. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  20. static int uart_putc(struct rt_serial_device *serial, char c);
  21. static int uart_getc(struct rt_serial_device *serial);
  22. static rt_ssize_t uart_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  23. void UART0_DriverIRQHandler(void);
  24. const struct rt_uart_ops _uart_ops =
  25. {
  26. uart_configure,
  27. uart_control,
  28. uart_putc,
  29. uart_getc,
  30. uart_dma_transmit
  31. };
  32. static void uart_isr(struct rt_serial_device *serial);
  33. #if defined(BSP_USING_UART0)
  34. struct rt_serial_device serial0;
  35. void UART0_DriverIRQHandler(void)
  36. {
  37. uart_isr(&serial0);
  38. }
  39. #endif
  40. /*
  41. * UART Initiation
  42. */
  43. int rt_hw_uart_init(void)
  44. {
  45. int i;
  46. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  47. serial = &serial0;
  48. serial->ops = &_uart_ops;
  49. serial->config = config;
  50. /* register UART device */
  51. rt_hw_serial_register(serial,
  52. device_name,
  53. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  54. (void *)0);
  55. return 0;
  56. }
  57. /*
  58. * UART interface
  59. */
  60. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  61. {
  62. RT_ASSERT(serial != RT_NULL);
  63. RT_ASSERT(cfg != RT_NULL);
  64. return RT_EOK;
  65. }
  66. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  67. {
  68. RT_ASSERT(serial != RT_NULL);
  69. return RT_EOK;
  70. }
  71. static int uart_putc(struct rt_serial_device *serial, char c)
  72. {
  73. RT_ASSERT(serial != RT_NULL);
  74. *((char *)(pdev_uart0_write_addr)) = c;
  75. return (1);
  76. }
  77. static int uart_getc(struct rt_serial_device *serial)
  78. {
  79. int ch;
  80. RT_ASSERT(serial != RT_NULL);
  81. ch = -1;
  82. if (*(char *)(pdev_uart0_state_addr) == pdev_uart0_readbusy_state)
  83. {
  84. ch = *(char *)(pdev_uart0_read_addr);
  85. *(char *)(pdev_uart0_state_addr) = pdev_uart0_free_state;
  86. }
  87. return ch;
  88. }
  89. static rt_ssize_t uart_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  90. {
  91. return (0);
  92. }
  93. /* UART ISR */
  94. /**
  95. * Uart common interrupt process. This need add to uart ISR.
  96. *
  97. * @param serial serial device
  98. */
  99. static void uart_isr(struct rt_serial_device *serial)
  100. {
  101. RT_ASSERT(serial != RT_NULL);
  102. if (*(char *)(pdev_uart0_state_addr) == pdev_uart0_readbusy_state)
  103. {
  104. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  105. }
  106. }