uart.c 3.9 KB

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