drv_usart.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #include "api_huart.h"
  13. #ifdef RT_USING_SERIAL
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.usart"
  16. #include <drv_log.h>
  17. #undef RT_SERIAL_USING_DMA
  18. enum
  19. {
  20. #ifdef BSP_USING_UART0
  21. UART0_INDEX,
  22. #endif
  23. #ifdef BSP_USING_UART1
  24. UART1_INDEX,
  25. #endif
  26. #ifdef BSP_USING_UART2
  27. UART2_INDEX,
  28. #endif
  29. };
  30. static struct ab32_uart_config uart_config[] =
  31. {
  32. #ifdef BSP_USING_UART0
  33. {
  34. .name = "uart0",
  35. .instance = UART0_BASE,
  36. .mode = UART_MODE_TX_RX | UART_MODE_1LINE,
  37. .fifo_size = BSP_UART0_FIFO_SIZE,
  38. },
  39. #endif
  40. #ifdef BSP_USING_UART1
  41. {
  42. .name = "uart1",
  43. .instance = UART1_BASE,
  44. .mode = UART_MODE_TX_RX,
  45. .fifo_size = BSP_UART1_FIFO_SIZE,
  46. },
  47. #endif
  48. #ifdef BSP_USING_UART2
  49. {
  50. .name = "uart2",
  51. .instance = UART2_BASE,
  52. .mode = UART_MODE_TX_RX,
  53. .fifo_size = BSP_UART2_FIFO_SIZE,
  54. }
  55. #endif
  56. };
  57. static struct ab32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  58. #ifdef HUART_ENABLE
  59. static rt_uint8_t huart_dma[512];
  60. #endif
  61. static rt_err_t ab32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  62. {
  63. struct ab32_uart *uart;
  64. RT_ASSERT(serial != RT_NULL);
  65. RT_ASSERT(cfg != RT_NULL);
  66. uart = rt_container_of(serial, struct ab32_uart, serial);
  67. uart->handle.instance = uart->config->instance;
  68. uart->handle.init.baud = cfg->baud_rate;
  69. uart->handle.init.mode = uart->config->mode;
  70. switch (cfg->data_bits)
  71. {
  72. case DATA_BITS_8:
  73. uart->handle.init.word_len = UART_WORDLENGTH_8B;
  74. break;
  75. case DATA_BITS_9:
  76. uart->handle.init.word_len = UART_WORDLENGTH_9B;
  77. break;
  78. default:
  79. uart->handle.init.word_len = UART_WORDLENGTH_8B;
  80. break;
  81. }
  82. switch (cfg->stop_bits)
  83. {
  84. case STOP_BITS_1:
  85. uart->handle.init.stop_bits = UART_STOPBITS_1;
  86. break;
  87. case STOP_BITS_2:
  88. uart->handle.init.stop_bits = UART_STOPBITS_2;
  89. break;
  90. default:
  91. uart->handle.init.stop_bits = UART_STOPBITS_1;
  92. break;
  93. }
  94. #ifdef RT_SERIAL_USING_DMA
  95. uart->dma_rx.last_index = 0;
  96. #endif
  97. if (!uart->uart_dma_flag) {
  98. hal_uart_init(&uart->handle);
  99. }
  100. #ifdef HUART_ENABLE
  101. else {
  102. huart_init_do(HUART_TR_PB3, HUART_TR_PB4, uart->handle.init.baud, huart_dma, 512);
  103. }
  104. #endif
  105. return RT_EOK;
  106. }
  107. static rt_err_t ab32_control(struct rt_serial_device *serial, int cmd, void *arg)
  108. {
  109. struct ab32_uart *uart;
  110. #ifdef RT_SERIAL_USING_DMA
  111. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  112. #endif
  113. RT_ASSERT(serial != RT_NULL);
  114. uart = rt_container_of(serial, struct ab32_uart, serial);
  115. switch (cmd)
  116. {
  117. /* disable interrupt */
  118. case RT_DEVICE_CTRL_CLR_INT:
  119. hal_uart_control(uart->handle.instance, UART_RXIT_ENABLE, HAL_DISABLE);
  120. break;
  121. /* enable interrupt */
  122. case RT_DEVICE_CTRL_SET_INT:
  123. hal_uart_clrflag(uart->handle.instance, UART_FLAG_RXPND);
  124. hal_uart_control(uart->handle.instance, UART_RXIT_ENABLE, HAL_ENABLE);
  125. break;
  126. case RT_DEVICE_CTRL_CLOSE:
  127. hal_uart_deinit(uart->handle.instance);
  128. break;
  129. }
  130. return RT_EOK;
  131. }
  132. static int ab32_putc(struct rt_serial_device *serial, char ch)
  133. {
  134. struct ab32_uart *uart;
  135. RT_ASSERT(serial != RT_NULL);
  136. uart = rt_container_of(serial, struct ab32_uart, serial);
  137. if (!uart->uart_dma_flag) {
  138. hal_uart_clrflag(uart->handle.instance, UART_FLAG_TXPND);
  139. hal_uart_write(uart->handle.instance, ch);
  140. while(hal_uart_getflag(uart->handle.instance, UART_FLAG_TXPND) == 0);
  141. }
  142. #ifdef HUART_ENABLE
  143. else {
  144. huart_putchar(ch);
  145. }
  146. #endif
  147. return 1;
  148. }
  149. static int ab32_getc(struct rt_serial_device *serial)
  150. {
  151. int ch;
  152. struct ab32_uart *uart;
  153. RT_ASSERT(serial != RT_NULL);
  154. uart = rt_container_of(serial, struct ab32_uart, serial);
  155. ch = -1;
  156. switch ((rt_uint32_t)(uart->handle.instance)) {
  157. case (rt_uint32_t)UART0_BASE:
  158. if (uart->rx_idx != uart->rx_idx_prev) {
  159. ch = (int)(uart->rx_buf[uart->rx_idx_prev++ % 10]);
  160. }
  161. break;
  162. case (rt_uint32_t)UART1_BASE:
  163. #ifdef HUART_ENABLE
  164. if ((uart->uart_dma_flag) && (huart_get_rxcnt())) {
  165. ch = huart_getchar();
  166. } else
  167. #endif
  168. {
  169. if (uart->rx_idx != uart->rx_idx_prev) {
  170. ch = (int)(uart->rx_buf[uart->rx_idx_prev++ % 10]);
  171. }
  172. }
  173. break;
  174. case (rt_uint32_t)UART2_BASE:
  175. if (uart->rx_idx != uart->rx_idx_prev) {
  176. ch = (int)(uart->rx_buf[uart->rx_idx_prev++ % 10]);
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. return ch;
  183. }
  184. static rt_size_t ab32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  185. {
  186. return -1;
  187. }
  188. void uart0_irq_process(void)
  189. {
  190. rt_hw_serial_isr(&(uart_obj[UART0_INDEX].serial), RT_SERIAL_EVENT_RX_IND);
  191. }
  192. #ifdef BSP_USING_UART1
  193. void uart1_irq_process(void)
  194. {
  195. rt_hw_serial_isr(&(uart_obj[UART1_INDEX].serial), RT_SERIAL_EVENT_RX_IND);
  196. }
  197. #endif
  198. #ifdef BSP_USING_UART2
  199. void uart2_irq_process(void)
  200. {
  201. rt_hw_serial_isr(&(uart_obj[UART2_INDEX].serial), RT_SERIAL_EVENT_RX_IND);
  202. }
  203. #endif
  204. rt_section(".irq.usart")
  205. static void uart_isr(int vector, void *param)
  206. {
  207. rt_interrupt_enter();
  208. #ifdef BSP_USING_UART0
  209. if(hal_uart_getflag(UART0_BASE, UART_FLAG_RXPND)) //RX one byte finish
  210. {
  211. uart_obj[0].rx_buf[uart_obj[0].rx_idx++ % 10] = hal_uart_read(UART0_BASE);
  212. hal_uart_clrflag(UART0_BASE, UART_FLAG_RXPND);
  213. uart0_irq_post();
  214. }
  215. #endif
  216. #ifdef BSP_USING_UART1
  217. if(hal_uart_getflag(UART1_BASE, UART_FLAG_RXPND)) //RX one byte finish
  218. {
  219. uart_obj[1].rx_buf[uart_obj[1].rx_idx++ % 10] = hal_uart_read(UART1_BASE);
  220. hal_uart_clrflag(UART1_BASE, UART_FLAG_RXPND);
  221. uart1_irq_post();
  222. }
  223. #endif
  224. #ifdef BSP_USING_UART2
  225. if(hal_uart_getflag(UART2_BASE, UART_FLAG_RXPND)) //RX one byte finish
  226. {
  227. uart_obj[2].rx_buf[uart_obj[2].rx_idx++ % 10] = hal_uart_read(UART2_BASE);
  228. hal_uart_clrflag(UART2_BASE, UART_FLAG_RXPND);
  229. uart2_irq_post();
  230. }
  231. #endif
  232. rt_interrupt_leave();
  233. }
  234. #ifdef HUART_ENABLE
  235. rt_section(".irq.huart")
  236. void huart_timer_isr(void)
  237. {
  238. huart_if_rx_ovflow();
  239. if (0 == huart_get_rxcnt()) {
  240. return;
  241. }
  242. uart1_irq_post();
  243. }
  244. #else
  245. rt_section(".irq.huart")
  246. void huart_timer_isr(void)
  247. {
  248. }
  249. #endif
  250. static const struct rt_uart_ops ab32_uart_ops =
  251. {
  252. .configure = ab32_configure,
  253. .control = ab32_control,
  254. .putc = ab32_putc,
  255. .getc = ab32_getc,
  256. .dma_transmit = ab32_dma_transmit
  257. };
  258. int rt_hw_usart_init(void)
  259. {
  260. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ab32_uart);
  261. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  262. rt_err_t result = 0;
  263. rt_hw_interrupt_install(IRQ_UART0_2_VECTOR, uart_isr, RT_NULL, "ut_isr");
  264. for (int i = 0; i < obj_num; i++)
  265. {
  266. /* init UART object */
  267. uart_obj[i].config = &uart_config[i];
  268. uart_obj[i].rx_idx = 0;
  269. uart_obj[i].rx_idx_prev = 0;
  270. uart_obj[i].serial.ops = &ab32_uart_ops;
  271. uart_obj[i].serial.config = config;
  272. uart_obj[i].serial.config.baud_rate = 1500000;
  273. uart_obj[i].rx_buf = rt_malloc(uart_config[i].fifo_size);
  274. if (uart_obj[i].rx_buf == RT_NULL) {
  275. LOG_E("uart%d malloc failed!", i);
  276. continue;
  277. }
  278. /* register UART device */
  279. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
  280. RT_DEVICE_FLAG_RDWR
  281. | RT_DEVICE_FLAG_INT_RX
  282. | RT_DEVICE_FLAG_INT_TX
  283. | uart_obj[i].uart_dma_flag
  284. , RT_NULL);
  285. RT_ASSERT(result == RT_EOK);
  286. }
  287. return result;
  288. }
  289. #endif