usart.c 9.9 KB

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