drv_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-16 Tuber first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "drv_uart.h"
  14. #ifdef BSP_USING_UART
  15. struct uart_device
  16. {
  17. struct rt_serial_device serial;
  18. char *name;
  19. };
  20. #ifdef BSP_USING_UART0
  21. static struct uart_device uart_device0 =
  22. {
  23. .name = "uart0",
  24. };
  25. #endif
  26. #ifdef BSP_USING_UART1
  27. static struct uart_device uart_device1 =
  28. {
  29. .name = "uart1",
  30. };
  31. #endif
  32. #ifdef BSP_USING_UART2
  33. static struct uart_device uart_device2 =
  34. {
  35. .name = "uart2",
  36. };
  37. #endif
  38. #ifdef BSP_USING_UART3
  39. static struct uart_device uart_device3 =
  40. {
  41. .name = "uart3",
  42. };
  43. #endif
  44. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  45. {
  46. UINT32 x;
  47. UINT8V R8_UARTx_FCR = 0, R8_UARTx_LCR = 0, R8_UARTx_IER = 0, R8_UARTx_DIV = 0;
  48. UINT16V R16_UARTx_DL = 0;
  49. struct uart_device *uart_device = serial->parent.user_data;
  50. //设置波特率
  51. x = 10 * GetSysClock() / 8 / cfg->baud_rate;
  52. x = (x + 5) / 10;
  53. R16_UARTx_DL = (UINT16)x;
  54. //设置数据长度
  55. switch (cfg->data_bits)
  56. {
  57. case DATA_BITS_5:
  58. //R8_UARTx_LCR |= 0x00;
  59. break;
  60. case DATA_BITS_6:
  61. R8_UARTx_LCR |= 0x01;
  62. break;
  63. case DATA_BITS_7:
  64. R8_UARTx_LCR |= 0x02;
  65. break;
  66. case DATA_BITS_8:
  67. default:
  68. R8_UARTx_LCR |= 0x03;
  69. break;
  70. }
  71. //设置停止位
  72. switch (cfg->stop_bits)
  73. {
  74. case STOP_BITS_2:
  75. R8_UARTx_LCR |= 0x04;
  76. break;
  77. case STOP_BITS_1:
  78. default:
  79. //R8_UARTx_LCR |= 0x00;
  80. break;
  81. }
  82. //设置校验位
  83. switch (cfg->parity)
  84. {
  85. case PARITY_ODD:
  86. R8_UART1_LCR |= R8_LCR_PAR_EN;
  87. //R8_UART1_LCR |= 0x00;
  88. break;
  89. case PARITY_EVEN:
  90. R8_UART1_LCR |= R8_LCR_PAR_EN;
  91. R8_UART1_LCR |= 0x10;
  92. break;
  93. case PARITY_NONE:
  94. default:
  95. //R8_UART1_LCR &= (~R8_UART1_LCR);
  96. break;
  97. }
  98. #ifdef BSP_USING_UART0
  99. if (uart_device == &uart_device0)
  100. {
  101. GPIOB_SetBits(GPIO_Pin_7);
  102. GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU); // RXD-配置上拉输入
  103. GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA); // TXD-配置推挽输出,注意先让IO口输出高电平
  104. R16_UART0_DL = R16_UARTx_DL;
  105. R8_UART0_FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; // FIFO打开,触发点4字节
  106. R8_UART0_LCR = R8_UARTx_LCR;
  107. R8_UART0_IER = RB_IER_TXD_EN;
  108. R8_UART0_DIV = 1;
  109. }
  110. #endif
  111. #ifdef BSP_USING_UART1
  112. if (uart_device == &uart_device1)
  113. {
  114. GPIOA_SetBits(GPIO_Pin_9);
  115. GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU); // RXD-配置上拉输入
  116. GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA); // TXD-配置推挽输出,注意先让IO口输出高电平
  117. R16_UART1_DL = R16_UARTx_DL;
  118. R8_UART1_FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; // FIFO打开,触发点4字节
  119. R8_UART1_LCR = R8_UARTx_LCR;
  120. R8_UART1_IER = RB_IER_TXD_EN;
  121. R8_UART1_DIV = 1;
  122. }
  123. #endif
  124. #ifdef BSP_USING_UART2
  125. if (uart_device == &uart_device2)
  126. {
  127. GPIOA_SetBits(GPIO_Pin_7);
  128. GPIOA_ModeCfg(GPIO_Pin_6, GPIO_ModeIN_PU); // RXD-配置上拉输入
  129. GPIOA_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA); // TXD-配置推挽输出,注意先让IO口输出高电平
  130. R16_UART2_DL = R16_UARTx_DL;
  131. R8_UART2_FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; // FIFO打开,触发点4字节
  132. R8_UART2_LCR = R8_UARTx_LCR;
  133. R8_UART2_IER = RB_IER_TXD_EN;
  134. R8_UART2_DIV = 1;
  135. }
  136. #endif
  137. #ifdef BSP_USING_UART3
  138. if (uart_device == &uart_device3)
  139. {
  140. GPIOA_SetBits(GPIO_Pin_5);
  141. GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU); // RXD-配置上拉输入
  142. GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeOut_PP_5mA); // TXD-配置推挽输出,注意先让IO口输出高电平
  143. R16_UART3_DL = R16_UARTx_DL;
  144. R8_UART3_FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; // FIFO打开,触发点4字节
  145. R8_UART3_LCR = R8_UARTx_LCR;
  146. R8_UART3_IER = RB_IER_TXD_EN;
  147. R8_UART3_DIV = 1;
  148. }
  149. #endif
  150. return RT_EOK;
  151. }
  152. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  153. {
  154. struct uart_device *uart_device = serial->parent.user_data;
  155. switch (cmd)
  156. {
  157. case RT_DEVICE_CTRL_CLR_INT:
  158. #ifdef BSP_USING_UART0
  159. if (uart_device == &uart_device0)
  160. {
  161. UART0_INTCfg(DISABLE, RB_IER_RECV_RDY);
  162. NVIC_EnableIRQ(UART0_IRQn);
  163. }
  164. #endif
  165. #ifdef BSP_USING_UART1
  166. if (uart_device == &uart_device1)
  167. {
  168. UART1_INTCfg(DISABLE, RB_IER_RECV_RDY);
  169. NVIC_EnableIRQ(UART1_IRQn);
  170. }
  171. #endif
  172. #ifdef BSP_USING_UART2
  173. if (uart_device == &uart_device2)
  174. {
  175. UART2_INTCfg(DISABLE, RB_IER_RECV_RDY);
  176. NVIC_EnableIRQ(UART2_IRQn);
  177. }
  178. #endif
  179. #ifdef BSP_USING_UART3
  180. if (uart_device == &uart_device3)
  181. {
  182. UART3_INTCfg(DISABLE, RB_IER_RECV_RDY);
  183. NVIC_EnableIRQ(UART3_IRQn);
  184. }
  185. #endif
  186. break;
  187. case RT_DEVICE_CTRL_SET_INT:
  188. #ifdef BSP_USING_UART0
  189. if (uart_device == &uart_device0)
  190. {
  191. UART0_ByteTrigCfg(UART_1BYTE_TRIG);
  192. UART0_INTCfg(ENABLE, RB_IER_RECV_RDY);
  193. NVIC_EnableIRQ(UART0_IRQn);
  194. }
  195. #endif
  196. #ifdef BSP_USING_UART1
  197. if (uart_device == &uart_device1)
  198. {
  199. UART1_ByteTrigCfg(UART_1BYTE_TRIG);
  200. UART1_INTCfg(ENABLE, RB_IER_RECV_RDY);
  201. NVIC_EnableIRQ(UART1_IRQn);
  202. }
  203. #endif
  204. #ifdef BSP_USING_UART2
  205. if (uart_device == &uart_device2)
  206. {
  207. UART2_ByteTrigCfg(UART_1BYTE_TRIG);
  208. UART2_INTCfg(ENABLE, RB_IER_RECV_RDY);
  209. NVIC_EnableIRQ(UART2_IRQn);
  210. }
  211. #endif
  212. #ifdef BSP_USING_UART3
  213. if (uart_device == &uart_device3)
  214. {
  215. UART3_ByteTrigCfg(UART_1BYTE_TRIG);
  216. UART3_INTCfg(ENABLE, RB_IER_RECV_RDY);
  217. NVIC_EnableIRQ(UART3_IRQn);
  218. }
  219. #endif
  220. break;
  221. default:
  222. break;
  223. }
  224. return RT_EOK;
  225. }
  226. static int uart_putc(struct rt_serial_device *serial, char ch)
  227. {
  228. struct uart_device *uart_device = serial->parent.user_data;
  229. #ifdef BSP_USING_UART0
  230. if (uart_device == &uart_device0)
  231. {
  232. while (R8_UART0_TFC >= UART_FIFO_SIZE);
  233. R8_UART0_THR = ch;
  234. }
  235. #endif
  236. #ifdef BSP_USING_UART1
  237. if (uart_device == &uart_device1)
  238. {
  239. while (R8_UART1_TFC >= UART_FIFO_SIZE);
  240. R8_UART1_THR = ch;
  241. }
  242. #endif
  243. #ifdef BSP_USING_UART2
  244. if (uart_device == &uart_device2)
  245. {
  246. while (R8_UART2_TFC >= UART_FIFO_SIZE);
  247. R8_UART2_THR = ch;
  248. }
  249. #endif
  250. #ifdef BSP_USING_UART3
  251. if (uart_device == &uart_device3)
  252. {
  253. while (R8_UART3_TFC >= UART_FIFO_SIZE);
  254. R8_UART3_THR = ch;
  255. }
  256. #endif
  257. return 1;
  258. }
  259. static int uart_getc(struct rt_serial_device *serial)
  260. {
  261. struct uart_device *uart_device = serial->parent.user_data;
  262. #ifdef BSP_USING_UART0
  263. if (uart_device == &uart_device0)
  264. {
  265. if (R8_UART0_RFC > 0)
  266. {
  267. return R8_UART0_RBR;
  268. }
  269. }
  270. #endif
  271. #ifdef BSP_USING_UART1
  272. if (uart_device == &uart_device1)
  273. {
  274. if (R8_UART1_RFC > 0)
  275. {
  276. return R8_UART1_RBR;
  277. }
  278. }
  279. #endif
  280. #ifdef BSP_USING_UART2
  281. if (uart_device == &uart_device2)
  282. {
  283. if (R8_UART2_RFC > 0)
  284. {
  285. return R8_UART2_RBR;
  286. }
  287. }
  288. #endif
  289. #ifdef BSP_USING_UART3
  290. if (uart_device == &uart_device3)
  291. {
  292. if (R8_UART3_RFC > 0)
  293. {
  294. return R8_UART3_RBR;
  295. }
  296. }
  297. #endif
  298. return -1;
  299. }
  300. static const struct rt_uart_ops uart_ops =
  301. {
  302. .configure = uart_configure,
  303. .control = uart_control,
  304. .putc = uart_putc,
  305. .getc = uart_getc,
  306. .dma_transmit = RT_NULL,
  307. };
  308. void uart_isr(struct rt_serial_device *serial, UINT8 flag)
  309. {
  310. switch (flag)
  311. {
  312. case UART_II_RECV_RDY: // 数据达到设置触发点
  313. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  314. break;
  315. case UART_II_RECV_TOUT: // 接收超时,暂时一帧数据接收完成
  316. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_TIMEOUT);
  317. break;
  318. case UART_II_THR_EMPTY: // 发送缓存区空,可继续发送
  319. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. #ifdef BSP_USING_UART0
  326. void UART0_IRQHandler(void)
  327. {
  328. rt_interrupt_enter();
  329. uart_isr(&uart_device0.serial, UART0_GetITFlag());
  330. rt_interrupt_leave();
  331. }
  332. #endif
  333. #ifdef BSP_USING_UART1
  334. void UART1_IRQHandler(void)
  335. {
  336. rt_interrupt_enter();
  337. uart_isr(&uart_device1.serial, UART1_GetITFlag());
  338. rt_interrupt_leave();
  339. }
  340. #endif
  341. #ifdef BSP_USING_UART2
  342. void UART2_IRQHandler(void)
  343. {
  344. rt_interrupt_enter();
  345. uart_isr(&uart_device2.serial, UART2_GetITFlag());
  346. rt_interrupt_leave();
  347. }
  348. #endif
  349. #ifdef BSP_USING_UART3
  350. void UART3_IRQHandler(void)
  351. {
  352. rt_interrupt_enter();
  353. uart_isr(&uart_device3.serial, UART3_GetITFlag());
  354. rt_interrupt_leave();
  355. }
  356. #endif
  357. int rt_hw_uart_init(void)
  358. {
  359. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  360. #ifdef BSP_USING_UART0
  361. uart_device0.serial.config = config;
  362. uart_device0.serial.ops = &uart_ops;
  363. rt_hw_serial_register(&uart_device0.serial, uart_device0.name,
  364. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  365. &uart_device0);
  366. #endif
  367. #ifdef BSP_USING_UART1
  368. uart_device1.serial.config = config;
  369. uart_device1.serial.ops = &uart_ops;
  370. rt_hw_serial_register(&uart_device1.serial, uart_device1.name,
  371. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  372. &uart_device1);
  373. #endif
  374. #ifdef BSP_USING_UART2
  375. uart_device2.serial.config = config;
  376. uart_device2.serial.ops = &uart_ops;
  377. rt_hw_serial_register(&uart_device2.serial, uart_device2.name,
  378. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  379. &uart_device2);
  380. #endif
  381. #ifdef BSP_USING_UART3
  382. uart_device3.serial.config = config;
  383. uart_device3.serial.ops = &uart_ops;
  384. rt_hw_serial_register(&uart_device3.serial, uart_device3.name,
  385. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  386. &uart_device3);
  387. #endif
  388. return RT_EOK;
  389. }
  390. #endif /* BSP_USING_UART */