drv_usart.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <shell.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. },
  38. #endif
  39. #ifdef BSP_USING_UART1
  40. {
  41. .name = "uart1",
  42. .instance = UART1_BASE,
  43. .mode = UART_MODE_TX_RX,
  44. },
  45. #endif
  46. #ifdef BSP_USING_UART2
  47. {
  48. .name = "uart2",
  49. .instance = UART2_BASE,
  50. .mode = UART_MODE_TX_RX,
  51. }
  52. #endif
  53. };
  54. static struct ab32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  55. static rt_err_t ab32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  56. {
  57. struct ab32_uart *uart;
  58. RT_ASSERT(serial != RT_NULL);
  59. RT_ASSERT(cfg != RT_NULL);
  60. uart = rt_container_of(serial, struct ab32_uart, serial);
  61. uart->handle.instance = uart->config->instance;
  62. uart->handle.init.baud = cfg->baud_rate;
  63. uart->handle.init.mode = uart->config->mode;
  64. switch (cfg->data_bits)
  65. {
  66. case DATA_BITS_8:
  67. uart->handle.init.word_len = UART_WORDLENGTH_8B;
  68. break;
  69. case DATA_BITS_9:
  70. uart->handle.init.word_len = UART_WORDLENGTH_9B;
  71. break;
  72. default:
  73. uart->handle.init.word_len = UART_WORDLENGTH_8B;
  74. break;
  75. }
  76. switch (cfg->stop_bits)
  77. {
  78. case STOP_BITS_1:
  79. uart->handle.init.stop_bits = UART_STOPBITS_1;
  80. break;
  81. case STOP_BITS_2:
  82. uart->handle.init.stop_bits = UART_STOPBITS_2;
  83. break;
  84. default:
  85. uart->handle.init.stop_bits = UART_STOPBITS_1;
  86. break;
  87. }
  88. #ifdef RT_SERIAL_USING_DMA
  89. uart->dma_rx.last_index = 0;
  90. #endif
  91. hal_uart_init(&uart->handle);
  92. return RT_EOK;
  93. }
  94. static rt_err_t ab32_control(struct rt_serial_device *serial, int cmd, void *arg)
  95. {
  96. struct ab32_uart *uart;
  97. #ifdef RT_SERIAL_USING_DMA
  98. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  99. #endif
  100. RT_ASSERT(serial != RT_NULL);
  101. uart = rt_container_of(serial, struct ab32_uart, serial);
  102. switch (cmd)
  103. {
  104. /* disable interrupt */
  105. case RT_DEVICE_CTRL_CLR_INT:
  106. hal_uart_control(uart->handle.instance, UART_RXIT_ENABLE, HAL_DISABLE);
  107. break;
  108. /* enable interrupt */
  109. case RT_DEVICE_CTRL_SET_INT:
  110. hal_uart_clrflag(uart->handle.instance, UART_FLAG_RXPND);
  111. hal_uart_control(uart->handle.instance, UART_RXIT_ENABLE, HAL_ENABLE);
  112. break;
  113. case RT_DEVICE_CTRL_CLOSE:
  114. hal_uart_deinit(uart->handle.instance);
  115. break;
  116. }
  117. return RT_EOK;
  118. }
  119. static int ab32_putc(struct rt_serial_device *serial, char ch)
  120. {
  121. struct ab32_uart *uart;
  122. RT_ASSERT(serial != RT_NULL);
  123. uart = rt_container_of(serial, struct ab32_uart, serial);
  124. hal_uart_clrflag(uart->handle.instance, UART_FLAG_TXPND);
  125. hal_uart_write(uart->handle.instance, ch);
  126. while(hal_uart_getflag(uart->handle.instance, UART_FLAG_TXPND) == 0);
  127. return 1;
  128. }
  129. RT_SECTION(".irq.usart")
  130. static int ab32_getc(struct rt_serial_device *serial)
  131. {
  132. int ch;
  133. struct ab32_uart *uart;
  134. RT_ASSERT(serial != RT_NULL);
  135. uart = rt_container_of(serial, struct ab32_uart, serial);
  136. ch = -1;
  137. if(hal_uart_getflag(uart->handle.instance, UART_FLAG_RXPND) != HAL_RESET) {
  138. ch = hal_uart_read(uart->handle.instance);
  139. hal_uart_clrflag(uart->handle.instance, UART_FLAG_RXPND);
  140. }
  141. return ch;
  142. }
  143. static rt_size_t ab32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  144. {
  145. return -1;
  146. }
  147. extern struct finsh_shell *shell;
  148. RT_SECTION(".irq.usart")
  149. static rt_err_t shell_rx_ind(void)
  150. {
  151. RT_ASSERT(shell != RT_NULL);
  152. /* release semaphore to let finsh thread rx data */
  153. rt_sem_release(&shell->rx_sem);
  154. return RT_EOK;
  155. }
  156. RT_SECTION(".irq.usart")
  157. void uart_irq_process(struct rt_serial_device *serial)
  158. {
  159. int ch = -1;
  160. rt_base_t level;
  161. struct rt_serial_rx_fifo* rx_fifo;
  162. /* interrupt mode receive */
  163. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  164. RT_ASSERT(rx_fifo != RT_NULL);
  165. while (1)
  166. {
  167. ch = serial->ops->getc(serial);
  168. if (ch == -1) break;
  169. /* disable interrupt */
  170. level = rt_hw_interrupt_disable();
  171. rx_fifo->buffer[rx_fifo->put_index] = ch;
  172. rx_fifo->put_index += 1;
  173. if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
  174. /* if the next position is read index, discard this 'read char' */
  175. if (rx_fifo->put_index == rx_fifo->get_index)
  176. {
  177. rx_fifo->get_index += 1;
  178. rx_fifo->is_full = RT_TRUE;
  179. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  180. // _serial_check_buffer_size();
  181. }
  182. /* enable interrupt */
  183. rt_hw_interrupt_enable(level);
  184. }
  185. rt_size_t rx_length;
  186. /* get rx length */
  187. level = rt_hw_interrupt_disable();
  188. rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
  189. (serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
  190. rt_hw_interrupt_enable(level);
  191. if ((serial->parent.rx_indicate != RT_NULL) && (rx_length != 0)) {
  192. #ifdef RT_CONSOLE_DEVICE_NAME
  193. if (serial == &uart_obj[*(RT_CONSOLE_DEVICE_NAME + 4) - '0'].serial) {
  194. shell_rx_ind();
  195. } else
  196. #endif
  197. {
  198. rt_kprintf("rx_indicate must loacted in the .comm section!\n");
  199. //serial->parent.rx_indicate(&serial->parent, rx_length);
  200. }
  201. }
  202. }
  203. RT_SECTION(".irq.usart")
  204. static void uart_isr(int vector, void *param)
  205. {
  206. rt_interrupt_enter();
  207. #ifdef BSP_USING_UART0
  208. if(hal_uart_getflag(UART0_BASE, UART_FLAG_RXPND)) //RX one byte finish
  209. {
  210. uart_irq_process(&(uart_obj[UART0_INDEX].serial));
  211. }
  212. #endif
  213. #ifdef BSP_USING_UART1
  214. if(hal_uart_getflag(UART1_BASE, UART_FLAG_RXPND)) //RX one byte finish
  215. {
  216. uart_irq_process(&(uart_obj[UART1_INDEX].serial));
  217. }
  218. #endif
  219. #ifdef BSP_USING_UART2
  220. if(hal_uart_getflag(UART2_BASE, UART_FLAG_RXPND)) //RX one byte finish
  221. {
  222. uart_irq_process(&(uart_obj[UART2_INDEX].serial));
  223. }
  224. #endif
  225. rt_interrupt_leave();
  226. }
  227. static const struct rt_uart_ops ab32_uart_ops =
  228. {
  229. .configure = ab32_configure,
  230. .control = ab32_control,
  231. .putc = ab32_putc,
  232. .getc = ab32_getc,
  233. .dma_transmit = ab32_dma_transmit
  234. };
  235. int rt_hw_usart_init(void)
  236. {
  237. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ab32_uart);
  238. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  239. rt_err_t result = 0;
  240. rt_hw_interrupt_install(IRQ_UART0_2_VECTOR, uart_isr, RT_NULL, "ut_isr");
  241. for (int i = 0; i < obj_num; i++)
  242. {
  243. /* init UART object */
  244. uart_obj[i].config = &uart_config[i];
  245. uart_obj[i].serial.ops = &ab32_uart_ops;
  246. uart_obj[i].serial.config = config;
  247. uart_obj[i].serial.config.baud_rate = 1500000;
  248. /* register UART device */
  249. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
  250. RT_DEVICE_FLAG_RDWR
  251. | RT_DEVICE_FLAG_INT_RX
  252. | RT_DEVICE_FLAG_INT_TX
  253. | uart_obj[i].uart_dma_flag
  254. , NULL);
  255. RT_ASSERT(result == RT_EOK);
  256. }
  257. return result;
  258. }
  259. #endif