drv_usartX.c 8.6 KB

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