uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2020, Shenzhen Academy of Aerospace Technology
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-10-16 Dystopia the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. #include <interrupt.h>
  15. #include "uart.h"
  16. #include "uart_reg.h"
  17. struct bm3803_uart
  18. {
  19. void *uart_base;
  20. int irq;
  21. };
  22. static void bm3803_uart_isr(int tt, void *param)
  23. {
  24. struct bm3803_uart *uart;
  25. struct rt_serial_device *serial;
  26. struct uart_reg *uart_base;
  27. serial = (struct rt_serial_device *)param;
  28. uart = (struct bm3803_uart *)serial->parent.user_data;
  29. uart_base = uart->uart_base;
  30. if (uart_base->uartstatus & 0x1)
  31. {
  32. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  33. }
  34. }
  35. #define NOT_IMPLEMENTED() RT_ASSERT(0)
  36. static rt_err_t bm3803_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  37. {
  38. struct bm3803_uart *uart;
  39. struct uart_reg *uart_base;
  40. RT_ASSERT(serial != RT_NULL);
  41. uart = (struct bm3803_uart *)serial->parent.user_data;
  42. RT_ASSERT(uart);
  43. uart_base = uart->uart_base;
  44. if (cfg->baud_rate == BAUD_RATE_115200)
  45. {
  46. uart_base->uartscaler = ((((CPU_FREQ * 10) / (8 * 115200)) - 5) / 10);
  47. }
  48. else if (cfg->baud_rate == BAUD_RATE_9600)
  49. {
  50. uart_base->uartscaler = ((((CPU_FREQ * 10) / (8 * 9600)) - 5) / 10);
  51. }
  52. else
  53. {
  54. NOT_IMPLEMENTED();
  55. }
  56. uart_base->uartctrl |= 0x3;
  57. return RT_EOK;
  58. }
  59. static rt_err_t bm3803_control(struct rt_serial_device *serial, int cmd, void *arg)
  60. {
  61. struct bm3803_uart *uart;
  62. RT_ASSERT(serial != RT_NULL);
  63. uart = (struct bm3803_uart *)serial->parent.user_data;
  64. switch (cmd)
  65. {
  66. case RT_DEVICE_CTRL_CLR_INT:
  67. /* disable rx irq */
  68. rt_hw_interrupt_mask(uart->irq);
  69. break;
  70. case RT_DEVICE_CTRL_SET_INT:
  71. /* enable rx irq */
  72. rt_hw_interrupt_umask(uart->irq);
  73. break;
  74. }
  75. return RT_EOK;
  76. }
  77. static int bm3803_putc(struct rt_serial_device *serial, char c)
  78. {
  79. struct bm3803_uart *uart;
  80. struct uart_reg *uart_base;
  81. RT_ASSERT(serial != RT_NULL);
  82. uart = (struct bm3803_uart *)serial->parent.user_data;
  83. uart_base = uart->uart_base;
  84. while (!(uart_base->uartstatus & 0x4));
  85. uart_base->uartdata = c;
  86. return 1;
  87. }
  88. static int bm3803_getc(struct rt_serial_device *serial)
  89. {
  90. int ch;
  91. struct bm3803_uart *uart;
  92. struct uart_reg *uart_base;
  93. RT_ASSERT(serial != RT_NULL);
  94. uart = (struct bm3803_uart *)serial->parent.user_data;
  95. uart_base = uart->uart_base;
  96. ch = -1;
  97. if (uart_base->uartstatus & 0x1)
  98. {
  99. ch = uart_base->uartdata;
  100. }
  101. return ch;
  102. }
  103. static const struct rt_uart_ops bm3803_uart_ops =
  104. {
  105. bm3803_configure,
  106. bm3803_control,
  107. bm3803_putc,
  108. bm3803_getc,
  109. };
  110. /* UART device driver structure */
  111. #ifdef RT_USING_UART1
  112. struct bm3803_uart uart1 =
  113. {
  114. (void *)UART1_BASE,
  115. UART1_TT,
  116. };
  117. struct rt_serial_device serial1;
  118. #endif
  119. int rt_hw_serial_init(void)
  120. {
  121. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  122. #ifdef RT_USING_UART1
  123. volatile struct lregs *regs = (struct lregs *)PREGS;
  124. serial1.ops = &bm3803_uart_ops;
  125. serial1.config = config;
  126. /* configure gpio direction */
  127. regs->piodir |= (1 << 15);
  128. regs->piodir &= ~(1 << 14);
  129. /* enable RX interrupt */
  130. regs->uartctrl1 = 0;
  131. regs->uartctrl1 |= 0x4;
  132. /* install ISR */
  133. rt_hw_interrupt_install(uart1.irq, bm3803_uart_isr, &serial1, "uart1");
  134. rt_hw_interrupt_mask(uart1.irq);
  135. /* register UART1 device */
  136. rt_hw_serial_register(&serial1, "uart1",
  137. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  138. &uart1);
  139. #endif
  140. return 0;
  141. }
  142. INIT_BOARD_EXPORT(rt_hw_serial_init);