usart.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 stm32_uart uart1 =
  112. {
  113. USART1,
  114. USART1_IRQn,
  115. };
  116. struct rt_serial_device serial1;
  117. void USART1_IRQHandler(void)
  118. {
  119. struct stm32_uart* uart;
  120. uart = &uart1;
  121. /* enter interrupt */
  122. rt_interrupt_enter();
  123. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  124. {
  125. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  126. /* clear interrupt */
  127. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  128. }
  129. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  130. {
  131. /* clear interrupt */
  132. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  133. }
  134. /* leave interrupt */
  135. rt_interrupt_leave();
  136. }
  137. #endif /* RT_USING_UART1 */
  138. #if defined(RT_USING_UART2)
  139. /* UART1 device driver structure */
  140. struct stm32_uart uart2 =
  141. {
  142. USART2,
  143. USART2_IRQn,
  144. };
  145. struct rt_serial_device serial2;
  146. void USART2_IRQHandler(void)
  147. {
  148. struct stm32_uart* uart;
  149. uart = &uart2;
  150. /* enter interrupt */
  151. rt_interrupt_enter();
  152. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  153. {
  154. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  155. /* clear interrupt */
  156. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  157. }
  158. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  159. {
  160. /* clear interrupt */
  161. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  162. }
  163. /* leave interrupt */
  164. rt_interrupt_leave();
  165. }
  166. #endif /* RT_USING_UART2 */
  167. #if defined(RT_USING_UART3)
  168. /* UART3 device driver structure */
  169. struct stm32_uart uart3 =
  170. {
  171. USART3,
  172. USART3_IRQn,
  173. };
  174. struct rt_serial_device serial3;
  175. void USART3_IRQHandler(void)
  176. {
  177. struct stm32_uart* uart;
  178. uart = &uart3;
  179. /* enter interrupt */
  180. rt_interrupt_enter();
  181. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  182. {
  183. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  184. /* clear interrupt */
  185. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  186. }
  187. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  188. {
  189. /* clear interrupt */
  190. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  191. }
  192. /* leave interrupt */
  193. rt_interrupt_leave();
  194. }
  195. #endif /* RT_USING_UART3 */
  196. static void RCC_Configuration(void)
  197. {
  198. #ifdef RT_USING_UART1
  199. /* Enable UART GPIO clocks */
  200. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  201. /* Enable UART clock */
  202. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  203. #endif /* RT_USING_UART1 */
  204. #ifdef RT_USING_UART2
  205. /* Enable UART GPIO clocks */
  206. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  207. /* Enable UART clock */
  208. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  209. #endif /* RT_USING_UART2 */
  210. #ifdef RT_USING_UART3
  211. /* Enable UART GPIO clocks */
  212. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  213. /* Enable UART clock */
  214. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  215. #endif /* RT_USING_UART3 */
  216. }
  217. static void GPIO_Configuration(void)
  218. {
  219. GPIO_InitTypeDef GPIO_InitStructure;
  220. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  221. #ifdef RT_USING_UART1
  222. /* Configure USART Rx/tx PIN */
  223. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  224. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
  225. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  226. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  227. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
  228. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  229. #endif /* RT_USING_UART1 */
  230. #ifdef RT_USING_UART2
  231. /* Configure USART Rx/tx PIN */
  232. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  233. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
  234. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  235. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  236. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
  237. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  238. #endif /* RT_USING_UART2 */
  239. #ifdef RT_USING_UART3
  240. /* Configure USART Rx/tx PIN */
  241. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  242. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX;
  243. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  244. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  245. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_TX;
  246. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  247. #endif /* RT_USING_UART3 */
  248. }
  249. static void NVIC_Configuration(struct stm32_uart* uart)
  250. {
  251. NVIC_InitTypeDef NVIC_InitStructure;
  252. /* Enable the USART1 Interrupt */
  253. NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
  254. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  255. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  256. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  257. NVIC_Init(&NVIC_InitStructure);
  258. }
  259. void rt_hw_usart_init(void)
  260. {
  261. struct stm32_uart* uart;
  262. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  263. RCC_Configuration();
  264. GPIO_Configuration();
  265. #ifdef RT_USING_UART1
  266. uart = &uart1;
  267. config.baud_rate = BAUD_RATE_115200;
  268. serial1.ops = &stm32_uart_ops;
  269. serial1.config = config;
  270. NVIC_Configuration(&uart1);
  271. /* register UART1 device */
  272. rt_hw_serial_register(&serial1, "uart1",
  273. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  274. uart);
  275. #endif /* RT_USING_UART1 */
  276. #ifdef RT_USING_UART2
  277. uart = &uart2;
  278. config.baud_rate = BAUD_RATE_115200;
  279. serial2.ops = &stm32_uart_ops;
  280. serial2.config = config;
  281. NVIC_Configuration(&uart2);
  282. /* register UART1 device */
  283. rt_hw_serial_register(&serial2, "uart2",
  284. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  285. uart);
  286. #endif /* RT_USING_UART2 */
  287. #ifdef RT_USING_UART3
  288. uart = &uart3;
  289. config.baud_rate = BAUD_RATE_115200;
  290. serial3.ops = &stm32_uart_ops;
  291. serial3.config = config;
  292. NVIC_Configuration(&uart3);
  293. /* register UART1 device */
  294. rt_hw_serial_register(&serial3, "uart3",
  295. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  296. uart);
  297. #endif /* RT_USING_UART3 */
  298. }