usart.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_GPIO_TX GPIO_Pin_9
  22. #define UART1_GPIO_RX GPIO_Pin_10
  23. #define UART1_GPIO GPIOA
  24. /* USART2 */
  25. #define UART2_GPIO_TX GPIO_Pin_2
  26. #define UART2_GPIO_RX GPIO_Pin_3
  27. #define UART2_GPIO GPIOA
  28. /* USART3_REMAP[1:0] = 00 */
  29. #define UART3_GPIO_TX GPIO_Pin_10
  30. #define UART3_GPIO_RX GPIO_Pin_11
  31. #define UART3_GPIO GPIOB
  32. /* STM32 uart driver */
  33. struct stm32_uart
  34. {
  35. USART_TypeDef* uart_device;
  36. IRQn_Type irq;
  37. };
  38. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  39. {
  40. struct stm32_uart* uart;
  41. USART_InitTypeDef USART_InitStructure;
  42. RT_ASSERT(serial != RT_NULL);
  43. RT_ASSERT(cfg != RT_NULL);
  44. uart = (struct stm32_uart *)serial->parent.user_data;
  45. USART_InitStructure.USART_BaudRate = cfg->baud_rate;
  46. if (cfg->data_bits == DATA_BITS_8)
  47. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  48. if (cfg->stop_bits == STOP_BITS_1)
  49. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  50. else if (cfg->stop_bits == STOP_BITS_2)
  51. USART_InitStructure.USART_StopBits = USART_StopBits_2;
  52. USART_InitStructure.USART_Parity = USART_Parity_No;
  53. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  54. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  55. USART_Init(uart->uart_device, &USART_InitStructure);
  56. /* Enable USART */
  57. USART_Cmd(uart->uart_device, ENABLE);
  58. /* enable interrupt */
  59. USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
  60. return RT_EOK;
  61. }
  62. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  63. {
  64. struct stm32_uart* uart;
  65. RT_ASSERT(serial != RT_NULL);
  66. uart = (struct stm32_uart *)serial->parent.user_data;
  67. switch (cmd)
  68. {
  69. case RT_DEVICE_CTRL_CLR_INT:
  70. /* disable rx irq */
  71. UART_DISABLE_IRQ(uart->irq);
  72. break;
  73. case RT_DEVICE_CTRL_SET_INT:
  74. /* enable rx irq */
  75. UART_ENABLE_IRQ(uart->irq);
  76. break;
  77. }
  78. return RT_EOK;
  79. }
  80. static int stm32_putc(struct rt_serial_device *serial, char c)
  81. {
  82. struct stm32_uart* uart;
  83. RT_ASSERT(serial != RT_NULL);
  84. uart = (struct stm32_uart *)serial->parent.user_data;
  85. while (!(uart->uart_device->SR & USART_FLAG_TXE));
  86. uart->uart_device->DR = c;
  87. return 1;
  88. }
  89. static int stm32_getc(struct rt_serial_device *serial)
  90. {
  91. int ch;
  92. struct stm32_uart* uart;
  93. RT_ASSERT(serial != RT_NULL);
  94. uart = (struct stm32_uart *)serial->parent.user_data;
  95. ch = -1;
  96. if (uart->uart_device->SR & USART_FLAG_RXNE)
  97. {
  98. ch = uart->uart_device->DR & 0xff;
  99. }
  100. return ch;
  101. }
  102. static const struct rt_uart_ops stm32_uart_ops =
  103. {
  104. stm32_configure,
  105. stm32_control,
  106. stm32_putc,
  107. stm32_getc,
  108. };
  109. #if defined(RT_USING_UART1)
  110. /* UART1 device driver structure */
  111. struct serial_ringbuffer uart1_int_rx;
  112. struct stm32_uart uart1 =
  113. {
  114. USART1,
  115. USART1_IRQn,
  116. };
  117. struct rt_serial_device serial1;
  118. void USART1_IRQHandler(void)
  119. {
  120. struct stm32_uart* uart;
  121. uart = &uart1;
  122. /* enter interrupt */
  123. rt_interrupt_enter();
  124. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  125. {
  126. rt_hw_serial_isr(&serial1);
  127. /* clear interrupt */
  128. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  129. }
  130. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  131. {
  132. /* clear interrupt */
  133. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  134. }
  135. /* leave interrupt */
  136. rt_interrupt_leave();
  137. }
  138. #endif /* RT_USING_UART1 */
  139. #if defined(RT_USING_UART2)
  140. /* UART1 device driver structure */
  141. struct serial_ringbuffer uart2_int_rx;
  142. struct stm32_uart uart2 =
  143. {
  144. USART2,
  145. USART2_IRQn,
  146. };
  147. struct rt_serial_device serial2;
  148. void USART2_IRQHandler(void)
  149. {
  150. struct stm32_uart* uart;
  151. uart = &uart2;
  152. /* enter interrupt */
  153. rt_interrupt_enter();
  154. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  155. {
  156. rt_hw_serial_isr(&serial2);
  157. /* clear interrupt */
  158. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  159. }
  160. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  161. {
  162. /* clear interrupt */
  163. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  164. }
  165. /* leave interrupt */
  166. rt_interrupt_leave();
  167. }
  168. #endif /* RT_USING_UART2 */
  169. #if defined(RT_USING_UART3)
  170. /* UART1 device driver structure */
  171. struct serial_ringbuffer uart3_int_rx;
  172. struct stm32_uart uart3 =
  173. {
  174. USART3,
  175. USART3_IRQn,
  176. };
  177. struct rt_serial_device serial3;
  178. void USART3_IRQHandler(void)
  179. {
  180. struct stm32_uart* uart;
  181. uart = &uart3;
  182. /* enter interrupt */
  183. rt_interrupt_enter();
  184. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  185. {
  186. rt_hw_serial_isr(&serial3);
  187. /* clear interrupt */
  188. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  189. }
  190. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  191. {
  192. /* clear interrupt */
  193. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  194. }
  195. /* leave interrupt */
  196. rt_interrupt_leave();
  197. }
  198. #endif /* RT_USING_UART3 */
  199. static void RCC_Configuration(void)
  200. {
  201. #ifdef RT_USING_UART1
  202. /* Enable UART GPIO clocks */
  203. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  204. /* Enable UART clock */
  205. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  206. #endif /* RT_USING_UART1 */
  207. #ifdef RT_USING_UART2
  208. /* Enable UART GPIO clocks */
  209. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  210. /* Enable UART clock */
  211. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  212. #endif /* RT_USING_UART2 */
  213. #ifdef RT_USING_UART3
  214. /* Enable UART GPIO clocks */
  215. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  216. /* Enable UART clock */
  217. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  218. #endif /* RT_USING_UART3 */
  219. }
  220. static void GPIO_Configuration(void)
  221. {
  222. GPIO_InitTypeDef GPIO_InitStructure;
  223. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  224. #ifdef RT_USING_UART1
  225. /* Configure USART Rx/tx PIN */
  226. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  227. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
  228. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  229. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  230. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
  231. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  232. #endif /* RT_USING_UART1 */
  233. #ifdef RT_USING_UART2
  234. /* Configure USART Rx/tx PIN */
  235. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  236. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
  237. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  238. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  239. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
  240. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  241. #endif /* RT_USING_UART2 */
  242. #ifdef RT_USING_UART3
  243. /* Configure USART Rx/tx PIN */
  244. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  245. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX;
  246. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  247. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  248. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_TX;
  249. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  250. #endif /* RT_USING_UART3 */
  251. }
  252. static void NVIC_Configuration(struct stm32_uart* uart)
  253. {
  254. NVIC_InitTypeDef NVIC_InitStructure;
  255. /* Enable the USART1 Interrupt */
  256. NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
  257. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  258. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  259. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  260. NVIC_Init(&NVIC_InitStructure);
  261. }
  262. void rt_hw_usart_init(void)
  263. {
  264. struct stm32_uart* uart;
  265. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  266. RCC_Configuration();
  267. GPIO_Configuration();
  268. #ifdef RT_USING_UART1
  269. uart = &uart1;
  270. config.baud_rate = BAUD_RATE_115200;
  271. serial1.ops = &stm32_uart_ops;
  272. serial1.int_rx = &uart1_int_rx;
  273. serial1.config = config;
  274. NVIC_Configuration(&uart1);
  275. /* register UART1 device */
  276. rt_hw_serial_register(&serial1, "uart1",
  277. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  278. uart);
  279. #endif /* RT_USING_UART1 */
  280. #ifdef RT_USING_UART2
  281. uart = &uart2;
  282. config.baud_rate = BAUD_RATE_115200;
  283. serial2.ops = &stm32_uart_ops;
  284. serial2.int_rx = &uart2_int_rx;
  285. serial2.config = config;
  286. NVIC_Configuration(&uart2);
  287. /* register UART1 device */
  288. rt_hw_serial_register(&serial2, "uart2",
  289. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  290. uart);
  291. #endif /* RT_USING_UART2 */
  292. #ifdef RT_USING_UART3
  293. uart = &uart3;
  294. config.baud_rate = BAUD_RATE_115200;
  295. serial3.ops = &stm32_uart_ops;
  296. serial3.int_rx = &uart3_int_rx;
  297. serial3.config = config;
  298. NVIC_Configuration(&uart3);
  299. /* register UART1 device */
  300. rt_hw_serial_register(&serial3, "uart3",
  301. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  302. uart);
  303. #endif /* RT_USING_UART3 */
  304. }