usart.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * 2015-01-31 armink make sure the serial transmit complete in putc()
  16. */
  17. #include "stm32f10x.h"
  18. #include "usart.h"
  19. #include "board.h"
  20. #include <rtdevice.h>
  21. /* USART1 */
  22. #define UART1_GPIO_TX GPIO_Pin_9
  23. #define UART1_GPIO_RX GPIO_Pin_10
  24. #define UART1_GPIO GPIOA
  25. /* USART2 */
  26. #if defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL)
  27. #define UART2_GPIO_TX GPIO_Pin_5
  28. #define UART2_GPIO_RX GPIO_Pin_6
  29. #define UART2_GPIO GPIOD
  30. #else /* for STM32F10X_HD */
  31. /* USART2_REMAP = 0 */
  32. #define UART2_GPIO_TX GPIO_Pin_2
  33. #define UART2_GPIO_RX GPIO_Pin_3
  34. #define UART2_GPIO GPIOA
  35. #endif
  36. /* STM32 uart driver */
  37. struct stm32_uart
  38. {
  39. USART_TypeDef* uart_device;
  40. IRQn_Type irq;
  41. };
  42. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  43. {
  44. struct stm32_uart* uart;
  45. USART_InitTypeDef USART_InitStructure;
  46. RT_ASSERT(serial != RT_NULL);
  47. RT_ASSERT(cfg != RT_NULL);
  48. uart = (struct stm32_uart *)serial->parent.user_data;
  49. USART_InitStructure.USART_BaudRate = cfg->baud_rate;
  50. if (cfg->data_bits == DATA_BITS_8){
  51. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  52. } else if (cfg->data_bits == DATA_BITS_9) {
  53. USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  54. }
  55. if (cfg->stop_bits == STOP_BITS_1){
  56. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  57. } else if (cfg->stop_bits == STOP_BITS_2){
  58. USART_InitStructure.USART_StopBits = USART_StopBits_2;
  59. }
  60. if (cfg->parity == PARITY_NONE){
  61. USART_InitStructure.USART_Parity = USART_Parity_No;
  62. } else if (cfg->parity == PARITY_ODD) {
  63. USART_InitStructure.USART_Parity = USART_Parity_Odd;
  64. } else if (cfg->parity == PARITY_EVEN) {
  65. USART_InitStructure.USART_Parity = USART_Parity_Even;
  66. }
  67. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  68. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  69. USART_Init(uart->uart_device, &USART_InitStructure);
  70. /* Enable USART */
  71. USART_Cmd(uart->uart_device, ENABLE);
  72. return RT_EOK;
  73. }
  74. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  75. {
  76. struct stm32_uart* uart;
  77. RT_ASSERT(serial != RT_NULL);
  78. uart = (struct stm32_uart *)serial->parent.user_data;
  79. switch (cmd)
  80. {
  81. /* disable interrupt */
  82. case RT_DEVICE_CTRL_CLR_INT:
  83. /* disable rx irq */
  84. UART_DISABLE_IRQ(uart->irq);
  85. /* disable interrupt */
  86. USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
  87. break;
  88. /* enable interrupt */
  89. case RT_DEVICE_CTRL_SET_INT:
  90. /* enable rx irq */
  91. UART_ENABLE_IRQ(uart->irq);
  92. /* enable interrupt */
  93. USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
  94. break;
  95. }
  96. return RT_EOK;
  97. }
  98. static int stm32_putc(struct rt_serial_device *serial, char c)
  99. {
  100. struct stm32_uart* uart;
  101. RT_ASSERT(serial != RT_NULL);
  102. uart = (struct stm32_uart *)serial->parent.user_data;
  103. uart->uart_device->DR = c;
  104. while (!(uart->uart_device->SR & USART_FLAG_TC));
  105. return 1;
  106. }
  107. static int stm32_getc(struct rt_serial_device *serial)
  108. {
  109. int ch;
  110. struct stm32_uart* uart;
  111. RT_ASSERT(serial != RT_NULL);
  112. uart = (struct stm32_uart *)serial->parent.user_data;
  113. ch = -1;
  114. if (uart->uart_device->SR & USART_FLAG_RXNE)
  115. {
  116. ch = uart->uart_device->DR & 0xff;
  117. }
  118. return ch;
  119. }
  120. static const struct rt_uart_ops stm32_uart_ops =
  121. {
  122. stm32_configure,
  123. stm32_control,
  124. stm32_putc,
  125. stm32_getc,
  126. };
  127. #if defined(RT_USING_UART1)
  128. /* UART1 device driver structure */
  129. struct stm32_uart uart1 =
  130. {
  131. USART1,
  132. USART1_IRQn,
  133. };
  134. struct rt_serial_device serial1;
  135. void USART1_IRQHandler(void)
  136. {
  137. struct stm32_uart* uart;
  138. uart = &uart1;
  139. /* enter interrupt */
  140. rt_interrupt_enter();
  141. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  142. {
  143. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  144. /* clear interrupt */
  145. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  146. }
  147. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  148. {
  149. /* clear interrupt */
  150. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  151. }
  152. if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
  153. {
  154. stm32_getc(&serial1);
  155. }
  156. /* leave interrupt */
  157. rt_interrupt_leave();
  158. }
  159. #endif /* RT_USING_UART1 */
  160. #if defined(RT_USING_UART2)
  161. /* UART1 device driver structure */
  162. struct stm32_uart uart2 =
  163. {
  164. USART2,
  165. USART2_IRQn,
  166. };
  167. struct rt_serial_device serial2;
  168. void USART2_IRQHandler(void)
  169. {
  170. struct stm32_uart* uart;
  171. uart = &uart2;
  172. /* enter interrupt */
  173. rt_interrupt_enter();
  174. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  175. {
  176. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  177. /* clear interrupt */
  178. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  179. }
  180. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  181. {
  182. /* clear interrupt */
  183. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  184. }
  185. if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
  186. {
  187. stm32_getc(&serial2);
  188. }
  189. /* leave interrupt */
  190. rt_interrupt_leave();
  191. }
  192. #endif /* RT_USING_UART2 */
  193. static void RCC_Configuration(void)
  194. {
  195. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  196. #if defined(RT_USING_UART1)
  197. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  198. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  199. #endif /* RT_USING_UART1 */
  200. #if defined(RT_USING_UART2)
  201. #if (defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL))
  202. /* Enable AFIO and GPIOD clock */
  203. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE);
  204. /* Enable the USART2 Pins Software Remapping */
  205. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  206. #else
  207. /* Enable AFIO and GPIOA clock */
  208. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
  209. #endif
  210. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  211. #endif /* RT_USING_UART2 */
  212. }
  213. static void GPIO_Configuration(void)
  214. {
  215. GPIO_InitTypeDef GPIO_InitStructure;
  216. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  217. #if defined(RT_USING_UART1)
  218. /* Configure USART1 Rx (PA.10) as input floating */
  219. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  220. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
  221. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  222. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  223. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  224. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
  225. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  226. #endif /* RT_USING_UART1 */
  227. #if defined(RT_USING_UART2)
  228. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  229. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
  230. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  231. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  232. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
  233. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  234. #endif /* RT_USING_UART2 */
  235. }
  236. static void NVIC_Configuration(struct stm32_uart* uart)
  237. {
  238. NVIC_InitTypeDef NVIC_InitStructure;
  239. /* Enable the USART1 Interrupt */
  240. NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
  241. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  242. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  243. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  244. NVIC_Init(&NVIC_InitStructure);
  245. }
  246. void rt_hw_usart_init(void)
  247. {
  248. struct stm32_uart* uart;
  249. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  250. RCC_Configuration();
  251. GPIO_Configuration();
  252. #if defined(RT_USING_UART1)
  253. uart = &uart1;
  254. config.baud_rate = BAUD_RATE_115200;
  255. serial1.ops = &stm32_uart_ops;
  256. serial1.config = config;
  257. NVIC_Configuration(&uart1);
  258. /* register UART1 device */
  259. rt_hw_serial_register(&serial1, "uart1",
  260. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX ,
  261. uart);
  262. #endif /* RT_USING_UART1 */
  263. #if defined(RT_USING_UART2)
  264. uart = &uart2;
  265. config.baud_rate = BAUD_RATE_115200;
  266. serial2.ops = &stm32_uart_ops;
  267. serial2.config = config;
  268. NVIC_Configuration(&uart2);
  269. /* register UART1 device */
  270. rt_hw_serial_register(&serial2, "uart2",
  271. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  272. uart);
  273. #endif /* RT_USING_UART2 */
  274. }