usart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * File : usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-01-05 Bernard the first version
  13. * 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
  14. * 2013-05-13 aozima update for kehong-lingtai.
  15. */
  16. #include "stm32f10x.h"
  17. #include "usart.h"
  18. #include "board.h"
  19. #include <rtdevice.h>
  20. /* USART1 */
  21. #define UART1_TX_PIN GPIO_Pin_9
  22. #define UART1_TX_GPIO GPIOA
  23. #define UART1_RX_PIN GPIO_Pin_10
  24. #define UART1_RX_GPIO GPIOA
  25. #define UART1_CTS_PIN ((uint16_t)0x0000)// cts is disable
  26. #define UART1_CTS_GPIO ((GPIO_TypeDef *)0x00000000)
  27. #define UART1_RTS_PIN ((uint16_t)0x0000)// rts is disable
  28. #define UART1_RTS_GPIO ((GPIO_TypeDef *)0x00000000)
  29. #define UART1_REMAP (((uint32_t)0x00000000))// remap is disable
  30. /* USART2 */
  31. #define UART2_TX_PIN GPIO_Pin_2
  32. #define UART2_TX_GPIO GPIOA
  33. #define UART2_RX_PIN GPIO_Pin_3
  34. #define UART2_RX_GPIO GPIOA
  35. #define UART2_CTS_PIN ((uint16_t)0x0000)// cts is disable
  36. #define UART2_CTS_GPIO ((GPIO_TypeDef *)0x00000000)
  37. #define UART2_RTS_PIN ((uint16_t)0x0000)// rts is disable
  38. #define UART2_RTS_GPIO ((GPIO_TypeDef *)0x00000000)
  39. #define UART2_REMAP (((uint32_t)0x00000000))// remap is disable
  40. /* USART3_REMAP[1:0] = 11 */
  41. #define UART3_TX_PIN GPIO_Pin_8
  42. #define UART3_TX_GPIO GPIOD
  43. #define UART3_RX_PIN GPIO_Pin_9
  44. #define UART3_RX_GPIO GPIOD
  45. #define UART3_CTS_PIN ((uint16_t)0x0000)// cts is disable
  46. #define UART3_CTS_GPIO ((GPIO_TypeDef *)0x00000000)
  47. #define UART3_RTS_PIN ((uint16_t)0x0000)// rts is disable
  48. #define UART3_RTS_GPIO ((GPIO_TypeDef *)0x00000000)
  49. #define UART3_REMAP GPIO_FullRemap_USART3
  50. /* USART4 */
  51. #define UART4_TX_PIN GPIO_Pin_10
  52. #define UART4_TX_GPIO GPIOC
  53. #define UART4_RX_PIN GPIO_Pin_11
  54. #define UART4_RX_GPIO GPIOC
  55. #define UART4_CTS_PIN ((uint16_t)0x0000)// cts is disable
  56. #define UART4_CTS_GPIO ((GPIO_TypeDef *)0x00000000)
  57. #define UART4_RTS_PIN ((uint16_t)0x0000)// rts is disable
  58. #define UART4_RTS_GPIO ((GPIO_TypeDef *)0x00000000)
  59. #define UART4_REMAP (((uint32_t)0x00000000))// remap is disable
  60. /* USART5 */
  61. #define UART5_TX_PIN GPIO_Pin_12
  62. #define UART5_TX_GPIO GPIOC
  63. #define UART5_RX_PIN GPIO_Pin_2
  64. #define UART5_RX_GPIO GPIOD
  65. #define UART5_CTS_PIN ((uint16_t)0x0000)// cts is disable
  66. #define UART5_CTS_GPIO ((GPIO_TypeDef *)0x00000000)
  67. #define UART5_RTS_PIN ((uint16_t)0x0000)// rts is disable
  68. #define UART5_RTS_GPIO ((GPIO_TypeDef *)0x00000000)
  69. #define UART4_REMAP (((uint32_t)0x00000000))// remap is disable
  70. /* STM32 uart driver */
  71. struct stm32_uart
  72. {
  73. USART_TypeDef* uart_device;
  74. IRQn_Type irq;
  75. uint16_t uart_tx_pin;
  76. GPIO_TypeDef *uart_tx_gpio;
  77. uint16_t uart_rx_pin;
  78. GPIO_TypeDef *uart_rx_gpio;
  79. uint16_t uart_cts_pin;
  80. GPIO_TypeDef *uart_cts_gpio;
  81. uint16_t uart_rts_pin;
  82. GPIO_TypeDef *uart_rts_gpio;
  83. uint32_t uart_remap;
  84. };
  85. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  86. {
  87. struct stm32_uart* uart;
  88. USART_InitTypeDef USART_InitStructure;
  89. RT_ASSERT(serial != RT_NULL);
  90. RT_ASSERT(cfg != RT_NULL);
  91. uart = (struct stm32_uart *)serial->parent.user_data;
  92. USART_StructInit(&USART_InitStructure);
  93. USART_InitStructure.USART_BaudRate = cfg->baud_rate;
  94. if (cfg->data_bits == DATA_BITS_8)
  95. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  96. if (cfg->stop_bits == STOP_BITS_1)
  97. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  98. else if (cfg->stop_bits == STOP_BITS_2)
  99. USART_InitStructure.USART_StopBits = USART_StopBits_2;
  100. if (cfg->parity == PARITY_NONE)
  101. USART_InitStructure.USART_Parity = USART_Parity_No;
  102. else if (cfg->parity == PARITY_EVEN)
  103. USART_InitStructure.USART_Parity = USART_Parity_Even;
  104. else if (cfg->parity == PARITY_ODD)
  105. USART_InitStructure.USART_Parity = USART_Parity_Odd;
  106. if (cfg->hw_control == HW_CONTROL_NONE)
  107. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  108. else if (cfg->hw_control == HW_CONTROL_RTS_CTS)
  109. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
  110. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  111. USART_Init(uart->uart_device, &USART_InitStructure);
  112. /* Enable USART */
  113. USART_Cmd(uart->uart_device, ENABLE);
  114. /* enable interrupt */
  115. USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
  116. return RT_EOK;
  117. }
  118. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  119. {
  120. struct stm32_uart* uart;
  121. RT_ASSERT(serial != RT_NULL);
  122. uart = (struct stm32_uart *)serial->parent.user_data;
  123. switch (cmd)
  124. {
  125. case RT_DEVICE_CTRL_CLR_INT:
  126. /* disable rx irq */
  127. UART_DISABLE_IRQ(uart->irq);
  128. break;
  129. case RT_DEVICE_CTRL_SET_INT:
  130. /* enable rx irq */
  131. UART_ENABLE_IRQ(uart->irq);
  132. break;
  133. }
  134. return RT_EOK;
  135. }
  136. static int stm32_putc(struct rt_serial_device *serial, char c)
  137. {
  138. struct stm32_uart* uart;
  139. RT_ASSERT(serial != RT_NULL);
  140. uart = (struct stm32_uart *)serial->parent.user_data;
  141. while (!(uart->uart_device->SR & USART_FLAG_TXE));
  142. uart->uart_device->DR = c;
  143. return 1;
  144. }
  145. static int stm32_getc(struct rt_serial_device *serial)
  146. {
  147. int ch;
  148. struct stm32_uart* uart;
  149. RT_ASSERT(serial != RT_NULL);
  150. uart = (struct stm32_uart *)serial->parent.user_data;
  151. ch = -1;
  152. if (uart->uart_device->SR & USART_FLAG_RXNE)
  153. {
  154. ch = uart->uart_device->DR & 0xff;
  155. }
  156. return ch;
  157. }
  158. static const struct rt_uart_ops stm32_uart_ops =
  159. {
  160. stm32_configure,
  161. stm32_control,
  162. stm32_putc,
  163. stm32_getc,
  164. };
  165. __STATIC_INLINE void serial_device_isr(struct rt_serial_device *serial)
  166. {
  167. volatile int ch = 0;
  168. struct stm32_uart *uart = serial->parent.user_data;
  169. /* process uart error */
  170. if(uart->uart_device->SR & (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE))
  171. {
  172. ch = uart->uart_device->DR; // invalid read
  173. while (uart->uart_device->SR & USART_FLAG_RXNE)
  174. {
  175. ch = uart->uart_device->DR; // invalid read
  176. }
  177. }
  178. /* process usart receive */
  179. if (uart->uart_device->SR & USART_FLAG_RXNE)
  180. {
  181. rt_hw_serial_isr(serial);//auto clear interrupt flag
  182. }
  183. /* process usart send */
  184. if (uart->uart_device->SR & USART_IT_TC)
  185. {
  186. /* clear interrupt */
  187. uart->uart_device->SR = ~((uint16_t)0x01 << (uint16_t)(USART_IT_TC >> 0x08));
  188. }
  189. }
  190. #if defined(RT_USING_UART1)
  191. /* UART1 device driver structure */
  192. struct serial_ringbuffer uart1_int_rx;
  193. #define UART1_POOL_SIZE 64
  194. rt_uint8_t uart1_pool[UART1_POOL_SIZE];
  195. struct stm32_uart uart1 =
  196. {
  197. USART1,
  198. USART1_IRQn,
  199. UART1_TX_PIN,
  200. UART1_TX_GPIO,
  201. UART1_RX_PIN,
  202. UART1_RX_GPIO,
  203. UART1_CTS_PIN,
  204. UART1_CTS_GPIO,
  205. UART1_RTS_PIN,
  206. UART1_RTS_GPIO,
  207. UART1_REMAP,
  208. };
  209. struct rt_serial_device serial1;
  210. void USART1_IRQHandler(void)
  211. {
  212. /* enter interrupt */
  213. rt_interrupt_enter();
  214. serial_device_isr(&serial1);
  215. /* leave interrupt */
  216. rt_interrupt_leave();
  217. }
  218. #endif /* RT_USING_UART1 */
  219. #if defined(RT_USING_UART2)
  220. /* UART2 device driver structure */
  221. struct serial_ringbuffer uart2_int_rx;
  222. #define UART2_POOL_SIZE 64
  223. rt_uint8_t uart2_pool[UART2_POOL_SIZE];
  224. struct stm32_uart uart2 =
  225. {
  226. USART2,
  227. USART2_IRQn,
  228. UART2_TX_PIN,
  229. UART2_TX_GPIO,
  230. UART2_RX_PIN,
  231. UART2_RX_GPIO,
  232. UART2_CTS_PIN,
  233. UART2_CTS_GPIO,
  234. UART2_RTS_PIN,
  235. UART2_RTS_GPIO,
  236. UART2_REMAP,
  237. };
  238. struct rt_serial_device serial2;
  239. void USART2_IRQHandler(void)
  240. {
  241. /* enter interrupt */
  242. rt_interrupt_enter();
  243. serial_device_isr(&serial2);
  244. /* leave interrupt */
  245. rt_interrupt_leave();
  246. }
  247. #endif /* RT_USING_UART2 */
  248. #if defined(RT_USING_UART3)
  249. /* UART3 device driver structure */
  250. struct serial_ringbuffer uart3_int_rx;
  251. #define UART3_POOL_SIZE 64
  252. rt_uint8_t uart3_pool[UART3_POOL_SIZE];
  253. struct stm32_uart uart3 =
  254. {
  255. USART3,
  256. USART3_IRQn,
  257. UART3_TX_PIN,
  258. UART3_TX_GPIO,
  259. UART3_RX_PIN,
  260. UART3_RX_GPIO,
  261. UART3_CTS_PIN,
  262. UART3_CTS_GPIO,
  263. UART3_RTS_PIN,
  264. UART3_RTS_GPIO,
  265. UART3_REMAP,
  266. };
  267. struct rt_serial_device serial3;
  268. void USART3_IRQHandler(void)
  269. {
  270. /* enter interrupt */
  271. rt_interrupt_enter();
  272. serial_device_isr(&serial3);
  273. /* leave interrupt */
  274. rt_interrupt_leave();
  275. }
  276. #endif /* RT_USING_UART3 */
  277. #if defined(RT_USING_UART4)
  278. /* UART4 device driver structure */
  279. struct serial_ringbuffer uart4_int_rx;
  280. #define UART4_POOL_SIZE 64
  281. rt_uint8_t uart4_pool[UART4_POOL_SIZE];
  282. struct stm32_uart uart4 =
  283. {
  284. UART4,
  285. UART4_IRQn,
  286. UART4_TX_PIN,
  287. UART4_TX_GPIO,
  288. UART4_RX_PIN,
  289. UART4_RX_GPIO,
  290. UART4_CTS_PIN,
  291. UART4_CTS_GPIO,
  292. UART4_RTS_PIN,
  293. UART4_RTS_GPIO,
  294. UART4_REMAP,
  295. };
  296. struct rt_serial_device serial4;
  297. void UART4_IRQHandler(void)
  298. {
  299. /* enter interrupt */
  300. rt_interrupt_enter();
  301. serial_device_isr(&serial4);
  302. /* leave interrupt */
  303. rt_interrupt_leave();
  304. }
  305. #endif /* RT_USING_UART4 */
  306. #if defined(RT_USING_UART5)
  307. /* UART5 device driver structure */
  308. struct serial_ringbuffer uart5_int_rx;
  309. #define UART5_POOL_SIZE 64
  310. rt_uint8_t uart5_pool[UART5_POOL_SIZE];
  311. struct stm32_uart uart5 =
  312. {
  313. UART5,
  314. UART5_IRQn,
  315. UART5_TX_PIN,
  316. UART5_TX_GPIO,
  317. UART5_RX_PIN,
  318. UART5_RX_GPIO,
  319. UART5_CTS_PIN,
  320. UART5_CTS_GPIO,
  321. UART5_RTS_PIN,
  322. UART5_RTS_GPIO,
  323. UART5_REMAP,
  324. };
  325. struct rt_serial_device serial5;
  326. void UART5_IRQHandler(void)
  327. {
  328. /* enter interrupt */
  329. rt_interrupt_enter();
  330. serial_device_isr(&serial5);
  331. /* leave interrupt */
  332. rt_interrupt_leave();
  333. }
  334. #endif /* RT_USING_UART5 */
  335. #define GPIO_RCC_ENABLE(x) \
  336. do{ \
  337. if (IS_GPIO_ALL_PERIPH(x)) \
  338. { \
  339. if ((x) == GPIOA) \
  340. { \
  341. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); \
  342. } \
  343. if ((x) == GPIOB) \
  344. { \
  345. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); \
  346. } \
  347. if ((x) == GPIOC) \
  348. { \
  349. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); \
  350. } \
  351. if ((x) == GPIOD) \
  352. { \
  353. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); \
  354. } \
  355. if ((x) == GPIOE) \
  356. { \
  357. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); \
  358. } \
  359. if ((x) == GPIOF) \
  360. { \
  361. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); \
  362. } \
  363. if ((x) == GPIOG) \
  364. { \
  365. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); \
  366. } \
  367. } \
  368. }while(0)
  369. #define UART_RCC_ENABLE(x) \
  370. do{ \
  371. if (IS_USART_ALL_PERIPH(x)) \
  372. { \
  373. if ((x) == USART1) \
  374. { \
  375. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); \
  376. } \
  377. if ((x) == USART2) \
  378. { \
  379. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); \
  380. } \
  381. if ((x) == USART3) \
  382. { \
  383. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); \
  384. } \
  385. if ((x) == UART4) \
  386. { \
  387. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); \
  388. } \
  389. if ((x) == UART5) \
  390. { \
  391. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); \
  392. } \
  393. } \
  394. }while(0)
  395. static void RCC_Configuration(struct stm32_uart* uart)
  396. {
  397. /* Enable UART GPIO clocks */
  398. GPIO_RCC_ENABLE(uart->uart_tx_gpio);
  399. GPIO_RCC_ENABLE(uart->uart_rx_gpio);
  400. GPIO_RCC_ENABLE(uart->uart_cts_gpio);
  401. GPIO_RCC_ENABLE(uart->uart_rts_gpio);
  402. /* Enable UART GPIO AF clocks */
  403. if (IS_GPIO_REMAP(uart->uart_remap))
  404. {
  405. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  406. GPIO_PinRemapConfig(uart->uart_remap, ENABLE);
  407. }
  408. /* Enable UART clock */
  409. UART_RCC_ENABLE(uart->uart_device);
  410. }
  411. static void GPIO_Configuration(struct stm32_uart* uart)
  412. {
  413. GPIO_InitTypeDef GPIO_InitStructure;
  414. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  415. /* Configure USART Rx/tx PIN */
  416. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  417. GPIO_InitStructure.GPIO_Pin = uart->uart_rx_pin;
  418. GPIO_Init(uart->uart_rx_gpio, &GPIO_InitStructure);
  419. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  420. GPIO_InitStructure.GPIO_Pin = uart->uart_tx_pin;
  421. GPIO_Init(uart->uart_tx_gpio, &GPIO_InitStructure);
  422. /* Configure USART RTS/CTS PIN */
  423. if (IS_GPIO_ALL_PERIPH(uart->uart_cts_gpio) &&
  424. IS_GPIO_ALL_PERIPH(uart->uart_rts_gpio) &&
  425. IS_GPIO_PIN(uart->uart_cts_pin) &&
  426. IS_GPIO_PIN(uart->uart_rts_pin))
  427. {
  428. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  429. GPIO_InitStructure.GPIO_Pin = uart->uart_cts_pin;
  430. GPIO_Init(uart->uart_cts_gpio, &GPIO_InitStructure);
  431. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  432. GPIO_InitStructure.GPIO_Pin = uart->uart_rts_pin;
  433. GPIO_Init(uart->uart_rts_gpio, &GPIO_InitStructure);
  434. }
  435. }
  436. static void NVIC_Configuration(struct stm32_uart* uart)
  437. {
  438. NVIC_InitTypeDef NVIC_InitStructure;
  439. /* Enable the USART Interrupt */
  440. NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
  441. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  442. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  443. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  444. NVIC_Init(&NVIC_InitStructure);
  445. }
  446. void rt_hw_usart_init(void)
  447. {
  448. struct stm32_uart* uart;
  449. struct rt_serial_device *serial;
  450. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  451. #ifdef RT_USING_UART1
  452. uart = &uart1;
  453. serial = &serial1;
  454. uart1_int_rx.pool = uart1_pool;
  455. uart1_int_rx.size = UART1_POOL_SIZE;
  456. config.baud_rate = BAUD_RATE_115200;
  457. serial->ops = &stm32_uart_ops;
  458. serial->int_rx = &uart1_int_rx;
  459. serial->config = config;
  460. RCC_Configuration(uart);
  461. GPIO_Configuration(uart);
  462. NVIC_Configuration(uart);
  463. /* register UART1 device */
  464. rt_hw_serial_register(serial, "uart1",
  465. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  466. uart);
  467. #endif /* RT_USING_UART1 */
  468. #ifdef RT_USING_UART2
  469. uart = &uart2;
  470. serial = &serial2;
  471. uart2_int_rx.pool = uart2_pool;
  472. uart2_int_rx.size = UART2_POOL_SIZE;
  473. config.baud_rate = BAUD_RATE_115200;
  474. serial->ops = &stm32_uart_ops;
  475. serial->int_rx = &uart2_int_rx;
  476. serial->config = config;
  477. RCC_Configuration(uart);
  478. GPIO_Configuration(uart);
  479. NVIC_Configuration(uart);
  480. /* register UART2 device */
  481. rt_hw_serial_register(serial, "uart2",
  482. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  483. uart);
  484. #endif /* RT_USING_UART2 */
  485. #ifdef RT_USING_UART3
  486. uart = &uart3;
  487. serial = &serial3;
  488. uart3_int_rx.pool = uart3_pool;
  489. uart3_int_rx.size = UART3_POOL_SIZE;
  490. config.baud_rate = BAUD_RATE_115200;
  491. serial->ops = &stm32_uart_ops;
  492. serial->int_rx = &uart3_int_rx;
  493. serial->config = config;
  494. RCC_Configuration(uart);
  495. GPIO_Configuration(uart);
  496. NVIC_Configuration(uart);
  497. /* register UART3 device */
  498. rt_hw_serial_register(serial, "uart3",
  499. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  500. uart);
  501. #endif /* RT_USING_UART3 */
  502. #ifdef RT_USING_UART4
  503. uart = &uart4;
  504. serial = &serial4;
  505. uart4_int_rx.pool = uart4_pool;
  506. uart4_int_rx.size = UART4_POOL_SIZE;
  507. config.baud_rate = BAUD_RATE_115200;
  508. serial->ops = &stm32_uart_ops;
  509. serial->int_rx = &uart4_int_rx;
  510. serial->config = config;
  511. RCC_Configuration(uart);
  512. GPIO_Configuration(uart);
  513. NVIC_Configuration(uart);
  514. /* register UART4 device */
  515. rt_hw_serial_register(serial, "uart4",
  516. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  517. uart);
  518. #endif /* RT_USING_UART4 */
  519. #ifdef RT_USING_UART5
  520. uart = &uart5;
  521. serial = &serial5;
  522. uart5_int_rx.pool = uart5_pool;
  523. uart5_int_rx.size = UART5_POOL_SIZE;
  524. config.baud_rate = BAUD_RATE_115200;
  525. serial->ops = &stm32_uart_ops;
  526. serial->int_rx = &uart5_int_rx;
  527. serial->config = config;
  528. RCC_Configuration(uart);
  529. GPIO_Configuration(uart);
  530. NVIC_Configuration(uart);
  531. /* register UART5 device */
  532. rt_hw_serial_register(serial, "uart5",
  533. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  534. uart);
  535. #endif /* RT_USING_UART5 */
  536. }
  537. /* uart device test */
  538. #ifdef RT_USING_FINSH
  539. #include <finsh.h>
  540. #if (defined(RT_USING_UART1) || defined(RT_USING_UART2) || defined(RT_USING_UART3) || defined(RT_USING_UART4) || defined(RT_USING_UART5))
  541. void uart_rw(const char *name, rt_int8_t cmd, const char *str)
  542. {
  543. rt_device_t uart;
  544. uart = rt_device_find(name);
  545. if (uart != RT_NULL)
  546. {
  547. if (uart->open_flag == RT_DEVICE_OFLAG_CLOSE)
  548. {
  549. rt_device_open(uart, RT_DEVICE_OFLAG_RDWR);
  550. }
  551. if (cmd == 0)
  552. {
  553. rt_int8_t temp[20];
  554. memset(temp, '\0', 20);
  555. rt_device_read(uart,0,(void *)temp,20);
  556. rt_kprintf("%s", temp);
  557. }
  558. else
  559. {
  560. rt_kprintf("%s, %d", str, strlen(str));
  561. rt_device_write(uart,0,str,strlen(str));
  562. }
  563. //rt_device_close(uart);
  564. }
  565. else
  566. {
  567. rt_kprintf("device %s is not exist!\n", name);
  568. }
  569. }
  570. FINSH_FUNCTION_EXPORT(uart_rw, set uart[name 0 xxx] for read.)
  571. #endif
  572. #endif