drv_uart.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-01-23 wangyq the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #include "drv_uart.h"
  15. #include <ald_gpio.h>
  16. #include <ald_uart.h>
  17. #ifdef RT_USING_SERIAL
  18. /* es32 uart driver */
  19. struct es32_uart
  20. {
  21. uart_handle_t huart;
  22. IRQn_Type irq;
  23. };
  24. static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  25. {
  26. gpio_init_t gpio_initstructure;
  27. struct es32_uart *uart;
  28. RT_ASSERT(serial != RT_NULL);
  29. RT_ASSERT(cfg != RT_NULL);
  30. uart = (struct es32_uart *)serial->parent.user_data;
  31. /* Initialize tx pin */
  32. gpio_initstructure.mode = GPIO_MODE_OUTPUT;
  33. gpio_initstructure.odos = GPIO_PUSH_PULL;
  34. gpio_initstructure.pupd = GPIO_PUSH_UP;
  35. gpio_initstructure.odrv = GPIO_OUT_DRIVE_NORMAL;
  36. gpio_initstructure.flt = GPIO_FILTER_DISABLE;
  37. gpio_initstructure.type = GPIO_TYPE_TTL;
  38. #ifdef BSP_USING_UART0
  39. gpio_initstructure.func = GPIO_FUNC_3;
  40. gpio_init(GPIOB, GPIO_PIN_10, &gpio_initstructure);
  41. /* Initialize rx pin ,the same as txpin except mode */
  42. gpio_initstructure.mode = GPIO_MODE_INPUT;
  43. gpio_init(GPIOB, GPIO_PIN_11, &gpio_initstructure);
  44. #endif /* uart0 gpio init */
  45. #ifdef BSP_USING_UART1
  46. gpio_initstructure.func = GPIO_FUNC_3;
  47. gpio_init(GPIOC, GPIO_PIN_10, &gpio_initstructure);
  48. /* Initialize rx pin ,the same as txpin except mode */
  49. gpio_initstructure.mode = GPIO_MODE_INPUT;
  50. gpio_init(GPIOC, GPIO_PIN_11, &gpio_initstructure);
  51. #endif /* uart1 gpio init */
  52. #ifdef BSP_USING_UART2
  53. gpio_initstructure.func = GPIO_FUNC_5;
  54. gpio_init(GPIOC, GPIO_PIN_12, &gpio_initstructure);
  55. /* Initialize rx pin ,the same as txpin except mode */
  56. gpio_initstructure.mode = GPIO_MODE_INPUT;
  57. gpio_init(GPIOD, GPIO_PIN_2, &gpio_initstructure);
  58. #endif /* uart2 gpio init */
  59. #ifdef BSP_USING_UART3
  60. gpio_initstructure.func = GPIO_FUNC_4;
  61. gpio_init(GPIOC, GPIO_PIN_4, &gpio_initstructure);
  62. /* Initialize rx pin ,the same as txpin except mode */
  63. gpio_initstructure.mode = GPIO_MODE_INPUT;
  64. gpio_init(GPIOC, GPIO_PIN_5, &gpio_initstructure);
  65. #endif /* uart3 gpio init */
  66. uart->huart.init.mode = UART_MODE_UART;
  67. uart->huart.init.baud = cfg->baud_rate;
  68. uart->huart.init.word_length = (uart_word_length_t)(cfg->data_bits - 5);
  69. uart->huart.init.parity = (uart_parity_t)(cfg->parity == PARITY_EVEN ? UART_PARITY_EVEN : cfg->parity);
  70. uart->huart.init.fctl = UART_HW_FLOW_CTL_DISABLE;
  71. uart_init(&uart->huart);
  72. if (cfg->bit_order == BIT_ORDER_MSB)
  73. {
  74. UART_MSB_FIRST_ENABLE(&uart->huart);
  75. }
  76. else
  77. {
  78. UART_MSB_FIRST_DISABLE(&uart->huart);
  79. }
  80. if (cfg->invert == NRZ_INVERTED)
  81. {
  82. UART_DATA_INV_ENABLE(&uart->huart);
  83. }
  84. else
  85. {
  86. UART_DATA_INV_DISABLE(&uart->huart);
  87. }
  88. /* enable rx int */
  89. uart_interrupt_config(&uart->huart, UART_IT_RXRD, ENABLE);
  90. return RT_EOK;
  91. }
  92. static rt_err_t es32f0x_control(struct rt_serial_device *serial, int cmd, void *arg)
  93. {
  94. struct es32_uart *uart;
  95. RT_ASSERT(serial != RT_NULL);
  96. uart = (struct es32_uart *)serial->parent.user_data;
  97. switch (cmd)
  98. {
  99. case RT_DEVICE_CTRL_CLR_INT:
  100. /* disable rx irq */
  101. NVIC_DisableIRQ(uart->irq);
  102. /* disable interrupt */
  103. uart_interrupt_config(&uart->huart, UART_IT_RXRD, DISABLE);
  104. break;
  105. case RT_DEVICE_CTRL_SET_INT:
  106. /* enable rx irq */
  107. NVIC_EnableIRQ(uart->irq);
  108. /* enable interrupt */
  109. uart_interrupt_config(&uart->huart, UART_IT_RXRD, ENABLE);
  110. break;
  111. }
  112. return RT_EOK;
  113. }
  114. static int es32f0x_putc(struct rt_serial_device *serial, char c)
  115. {
  116. struct es32_uart *uart;
  117. RT_ASSERT(serial != RT_NULL);
  118. uart = (struct es32_uart *)serial->parent.user_data;
  119. while (!(uart->huart.perh->SR & 0x40)) ;
  120. WRITE_REG(uart->huart.perh->TBR, c);
  121. return 1;
  122. }
  123. static int es32f0x_getc(struct rt_serial_device *serial)
  124. {
  125. int ch = -1;
  126. struct es32_uart *uart;
  127. RT_ASSERT(serial != RT_NULL);
  128. uart = (struct es32_uart *)serial->parent.user_data;
  129. if (uart->huart.perh->SR & 0x01)
  130. {
  131. ch = (uint8_t)(uart->huart.perh->RBR & 0xFF);
  132. }
  133. return ch;
  134. }
  135. static const struct rt_uart_ops es32f0x_uart_ops =
  136. {
  137. es32f0x_configure,
  138. es32f0x_control,
  139. es32f0x_putc,
  140. es32f0x_getc,
  141. };
  142. #ifdef BSP_USING_UART0
  143. /* UART0 device driver structure */
  144. struct es32_uart uart0 =
  145. {
  146. {UART0},
  147. UART0_IRQn
  148. };
  149. struct rt_serial_device serial0;
  150. void UART0_Handler(void)
  151. {
  152. /* enter interrupt */
  153. rt_interrupt_enter();
  154. if (UART0->RIF & 0x01)
  155. {
  156. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  157. }
  158. /* leave interrupt */
  159. rt_interrupt_leave();
  160. }
  161. #endif /* BSP_USING_UART0 */
  162. #ifdef BSP_USING_UART1
  163. /* UART1 device driver structure */
  164. struct es32_uart uart1 =
  165. {
  166. {UART1},
  167. UART1_IRQn
  168. };
  169. struct rt_serial_device serial1;
  170. void UART1_Handler(void)
  171. {
  172. /* enter interrupt */
  173. rt_interrupt_enter();
  174. if (UART1->RIF & 0x01)
  175. {
  176. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  177. }
  178. /* leave interrupt */
  179. rt_interrupt_leave();
  180. }
  181. #endif /* BSP_USING_UART1 */
  182. #ifdef BSP_USING_UART2
  183. /* UART2 device driver structure */
  184. struct es32_uart uart2 =
  185. {
  186. {UART2},
  187. BS16T1_UART2_IRQn
  188. };
  189. struct rt_serial_device serial2;
  190. void BS16T1_UART2_Handler(void)
  191. {
  192. /* enter interrupt */
  193. rt_interrupt_enter();
  194. if (UART2->RIF & 0x01)
  195. {
  196. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  197. }
  198. /* leave interrupt */
  199. rt_interrupt_leave();
  200. }
  201. #endif /* BSP_USING_UART2 */
  202. #ifdef BSP_USING_UART3
  203. /* UART3 device driver structure */
  204. struct es32_uart uart3 =
  205. {
  206. {UART3},
  207. BS16T2_UART3_IRQn
  208. };
  209. struct rt_serial_device serial3;
  210. void BS16T2_UART3_Handler(void)
  211. {
  212. /* enter interrupt */
  213. rt_interrupt_enter();
  214. if (UART3->RIF & 0x01)
  215. {
  216. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  217. }
  218. /* leave interrupt */
  219. rt_interrupt_leave();
  220. }
  221. #endif /* BSP_USING_UART3 */
  222. int rt_hw_uart_init(void)
  223. {
  224. struct es32_uart *uart;
  225. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  226. #ifdef BSP_USING_UART0
  227. uart = &uart0;
  228. serial0.ops = &es32f0x_uart_ops;
  229. serial0.config = config;
  230. /* register UART0 device */
  231. rt_hw_serial_register(&serial0, "uart0",
  232. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  233. uart);
  234. #endif /* BSP_USING_UART0 */
  235. #ifdef BSP_USING_UART1
  236. uart = &uart1;
  237. serial1.ops = &es32f0x_uart_ops;
  238. serial1.config = config;
  239. /* register UART1 device */
  240. rt_hw_serial_register(&serial1, "uart1",
  241. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  242. uart);
  243. #endif /* BSP_USING_UART1 */
  244. #ifdef BSP_USING_UART2
  245. uart = &uart2;
  246. serial2.ops = &es32f0x_uart_ops;
  247. serial2.config = config;
  248. /* register UART2 device */
  249. rt_hw_serial_register(&serial2, "uart2",
  250. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  251. uart);
  252. #endif /* BSP_USING_UART2 */
  253. #ifdef BSP_USING_UART3
  254. uart = &uart3;
  255. serial3.ops = &es32f0x_uart_ops;
  256. serial3.config = config;
  257. /* register UART3 device */
  258. rt_hw_serial_register(&serial3, "uart3",
  259. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  260. uart);
  261. #endif /* BSP_USING_UART3 */
  262. return 0;
  263. }
  264. INIT_BOARD_EXPORT(rt_hw_uart_init);
  265. #endif