drv_usart.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2020-2021, Bluetrum Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-11-20 greedyhao first version
  9. */
  10. #include "board.h"
  11. #include "drv_usart.h"
  12. #ifdef RT_USING_SERIAL
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.usart"
  15. #include <drv_log.h>
  16. #undef RT_SERIAL_USING_DMA
  17. enum
  18. {
  19. UART0_INDEX,
  20. UART1_INDEX,
  21. };
  22. static struct ab32_uart_config uart_config[] =
  23. {
  24. {
  25. .name = "uart0",
  26. .instance = UART0_BASE,
  27. },
  28. {
  29. .name = "uart1",
  30. .instance = UART1_BASE,
  31. }
  32. };
  33. static struct ab32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  34. static rt_err_t ab32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  35. {
  36. struct ab32_uart *uart;
  37. RT_ASSERT(serial != RT_NULL);
  38. RT_ASSERT(cfg != RT_NULL);
  39. uart = rt_container_of(serial, struct ab32_uart, serial);
  40. uart->handle.instance = uart->config->instance;
  41. uart->handle.init.baud = cfg->baud_rate;
  42. uart->handle.init.mode = UART_MODE_TX_RX;
  43. switch (cfg->data_bits)
  44. {
  45. case DATA_BITS_8:
  46. uart->handle.init.word_len = UART_WORDLENGTH_8B;
  47. break;
  48. case DATA_BITS_9:
  49. uart->handle.init.word_len = UART_WORDLENGTH_9B;
  50. break;
  51. default:
  52. uart->handle.init.word_len = UART_WORDLENGTH_8B;
  53. break;
  54. }
  55. switch (cfg->stop_bits)
  56. {
  57. case STOP_BITS_1:
  58. uart->handle.init.stop_bits = UART_STOPBITS_1;
  59. break;
  60. case STOP_BITS_2:
  61. uart->handle.init.stop_bits = UART_STOPBITS_2;
  62. break;
  63. default:
  64. uart->handle.init.stop_bits = UART_STOPBITS_1;
  65. break;
  66. }
  67. #ifdef RT_SERIAL_USING_DMA
  68. uart->dma_rx.last_index = 0;
  69. #endif
  70. hal_uart_init(&uart->handle);
  71. return RT_EOK;
  72. }
  73. static rt_err_t ab32_control(struct rt_serial_device *serial, int cmd, void *arg)
  74. {
  75. struct ab32_uart *uart;
  76. #ifdef RT_SERIAL_USING_DMA
  77. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  78. #endif
  79. RT_ASSERT(serial != RT_NULL);
  80. uart = rt_container_of(serial, struct ab32_uart, serial);
  81. switch (cmd)
  82. {
  83. /* disable interrupt */
  84. case RT_DEVICE_CTRL_CLR_INT:
  85. hal_uart_control(uart->handle.instance, UART_RXIT_ENABLE, HAL_DISABLE);
  86. break;
  87. /* enable interrupt */
  88. case RT_DEVICE_CTRL_SET_INT:
  89. hal_uart_clrflag(uart->handle.instance, UART_FLAG_RXPND);
  90. hal_uart_control(uart->handle.instance, UART_RXIT_ENABLE, HAL_ENABLE);
  91. break;
  92. case RT_DEVICE_CTRL_CLOSE:
  93. hal_uart_deinit(uart->handle.instance);
  94. break;
  95. }
  96. return RT_EOK;
  97. }
  98. static int ab32_putc(struct rt_serial_device *serial, char ch)
  99. {
  100. struct ab32_uart *uart;
  101. RT_ASSERT(serial != RT_NULL);
  102. uart = rt_container_of(serial, struct ab32_uart, serial);
  103. hal_uart_clrflag(uart->handle.instance, UART_FLAG_TXPND);
  104. hal_uart_write(uart->handle.instance, ch);
  105. while(hal_uart_getflag(uart->handle.instance, UART_FLAG_TXPND) == 0);
  106. return 1;
  107. }
  108. static int ab32_getc(struct rt_serial_device *serial)
  109. {
  110. int ch;
  111. struct ab32_uart *uart;
  112. RT_ASSERT(serial != RT_NULL);
  113. uart = rt_container_of(serial, struct ab32_uart, serial);
  114. ch = -1;
  115. if(hal_uart_getflag(uart->handle.instance, UART_FLAG_RXPND) != HAL_RESET) {
  116. ch = hal_uart_read(uart->handle.instance);
  117. hal_uart_clrflag(uart->handle.instance, UART_FLAG_RXPND);
  118. }
  119. return ch;
  120. }
  121. static rt_size_t ab32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  122. {
  123. return -1;
  124. }
  125. static void uart_isr(int vector, void *param)
  126. {
  127. rt_interrupt_enter();
  128. if(hal_uart_getflag(UART0_BASE, UART_FLAG_RXPND)) //RX one byte finish
  129. {
  130. rt_hw_serial_isr(&(uart_obj[UART0_INDEX].serial), RT_SERIAL_EVENT_RX_IND);
  131. }
  132. // if(hal_uart_getflag(UART1_BASE, UART_FLAG_RXPND)) //RX one byte finish
  133. // {
  134. // rt_hw_serial_isr(&(uart_obj[UART1_INDEX].serial), RT_SERIAL_EVENT_RX_IND);
  135. // }
  136. rt_interrupt_leave();
  137. }
  138. static const struct rt_uart_ops ab32_uart_ops =
  139. {
  140. .configure = ab32_configure,
  141. .control = ab32_control,
  142. .putc = ab32_putc,
  143. .getc = ab32_getc,
  144. .dma_transmit = ab32_dma_transmit
  145. };
  146. int rt_hw_usart_init(void)
  147. {
  148. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ab32_uart);
  149. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  150. rt_err_t result = 0;
  151. rt_hw_interrupt_install(IRQ_UART0_2_VECTOR, uart_isr, RT_NULL, "ut_isr");
  152. for (int i = 0; i < obj_num; i++)
  153. {
  154. /* init UART object */
  155. uart_obj[i].config = &uart_config[i];
  156. uart_obj[i].serial.ops = &ab32_uart_ops;
  157. uart_obj[i].serial.config = config;
  158. uart_obj[i].serial.config.baud_rate = 1500000;
  159. /* register UART device */
  160. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
  161. RT_DEVICE_FLAG_RDWR
  162. | RT_DEVICE_FLAG_INT_RX
  163. | RT_DEVICE_FLAG_INT_TX
  164. | uart_obj[i].uart_dma_flag
  165. , NULL);
  166. RT_ASSERT(result == RT_EOK);
  167. }
  168. return result;
  169. }
  170. #endif