usart.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * 2013-11-15 bright the first version
  13. */
  14. #include <stm32f0xx.h>
  15. #include <rtdevice.h>
  16. #include "usart.h"
  17. /* USART1 */
  18. #define UART1_GPIO_TX GPIO_Pin_9
  19. #define UART1_GPIO_TX_SOURCE GPIO_PinSource9
  20. #define UART1_GPIO_RX GPIO_Pin_10
  21. #define UART1_GPIO_RX_SOURCE GPIO_PinSource10
  22. #define UART1_GPIO_AF GPIO_AF_1
  23. #define UART1_GPIO GPIOA
  24. /* USART2 */
  25. #define UART2_GPIO_TX GPIO_Pin_2
  26. #define UART2_GPIO_TX_SOURCE GPIO_PinSource2
  27. #define UART2_GPIO_RX GPIO_Pin_3
  28. #define UART2_GPIO_RX_SOURCE GPIO_PinSource3
  29. #define UART2_GPIO_AF GPIO_AF_1
  30. #define UART2_GPIO GPIOA
  31. /* STM32 uart driver */
  32. struct stm32_uart
  33. {
  34. USART_TypeDef* uart_device;
  35. IRQn_Type irq;
  36. };
  37. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  38. {
  39. struct stm32_uart* uart;
  40. USART_InitTypeDef USART_InitStructure;
  41. RT_ASSERT(serial != RT_NULL);
  42. RT_ASSERT(cfg != RT_NULL);
  43. uart = (struct stm32_uart *)serial->parent.user_data;
  44. USART_InitStructure.USART_BaudRate = cfg->baud_rate;
  45. if (cfg->data_bits == DATA_BITS_8)
  46. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  47. if (cfg->stop_bits == STOP_BITS_1)
  48. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  49. else if (cfg->stop_bits == STOP_BITS_2)
  50. USART_InitStructure.USART_StopBits = USART_StopBits_2;
  51. USART_InitStructure.USART_Parity = USART_Parity_No;
  52. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  53. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  54. USART_Init(uart->uart_device, &USART_InitStructure);
  55. /* Enable USART */
  56. USART_Cmd(uart->uart_device, ENABLE);
  57. /* enable interrupt */
  58. USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
  59. return RT_EOK;
  60. }
  61. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  62. {
  63. struct stm32_uart* uart;
  64. RT_ASSERT(serial != RT_NULL);
  65. uart = (struct stm32_uart *)serial->parent.user_data;
  66. switch (cmd)
  67. {
  68. case RT_DEVICE_CTRL_CLR_INT:
  69. /* disable rx irq */
  70. UART_DISABLE_IRQ(uart->irq);
  71. break;
  72. case RT_DEVICE_CTRL_SET_INT:
  73. /* enable rx irq */
  74. UART_ENABLE_IRQ(uart->irq);
  75. break;
  76. }
  77. return RT_EOK;
  78. }
  79. static int stm32_putc(struct rt_serial_device *serial, char c)
  80. {
  81. struct stm32_uart* uart;
  82. RT_ASSERT(serial != RT_NULL);
  83. uart = (struct stm32_uart *)serial->parent.user_data;
  84. while (!(uart->uart_device->ISR & USART_FLAG_TXE));
  85. uart->uart_device->TDR = c;
  86. return 1;
  87. }
  88. static int stm32_getc(struct rt_serial_device *serial)
  89. {
  90. int ch;
  91. struct stm32_uart* uart;
  92. RT_ASSERT(serial != RT_NULL);
  93. uart = (struct stm32_uart *)serial->parent.user_data;
  94. ch = -1;
  95. if (uart->uart_device->ISR & USART_FLAG_RXNE)
  96. {
  97. ch = uart->uart_device->RDR & 0xff;
  98. }
  99. return ch;
  100. }
  101. static const struct rt_uart_ops stm32_uart_ops =
  102. {
  103. stm32_configure,
  104. stm32_control,
  105. stm32_putc,
  106. stm32_getc,
  107. };
  108. #if defined(RT_USING_UART1)
  109. /* UART1 device driver structure */
  110. struct stm32_uart uart1 =
  111. {
  112. USART1,
  113. USART1_IRQn,
  114. };
  115. struct rt_serial_device serial1;
  116. void USART1_IRQHandler(void)
  117. {
  118. struct stm32_uart* uart;
  119. uart = &uart1;
  120. /* enter interrupt */
  121. rt_interrupt_enter();
  122. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  123. {
  124. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  125. /* clear interrupt */
  126. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  127. }
  128. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  129. {
  130. /* clear interrupt */
  131. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  132. }
  133. /* leave interrupt */
  134. rt_interrupt_leave();
  135. }
  136. #endif /* RT_USING_UART1 */
  137. #if defined(RT_USING_UART2)
  138. /* UART2 device driver structure */
  139. struct stm32_uart uart2 =
  140. {
  141. USART2,
  142. USART2_IRQn,
  143. };
  144. struct rt_serial_device serial2;
  145. void USART2_IRQHandler(void)
  146. {
  147. struct stm32_uart* uart;
  148. uart = &uart2;
  149. /* enter interrupt */
  150. rt_interrupt_enter();
  151. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  152. {
  153. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  154. /* clear interrupt */
  155. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  156. }
  157. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  158. {
  159. /* clear interrupt */
  160. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  161. }
  162. /* leave interrupt */
  163. rt_interrupt_leave();
  164. }
  165. #endif /* RT_USING_UART2 */
  166. static void RCC_Configuration(void)
  167. {
  168. #ifdef RT_USING_UART1
  169. /* Enable GPIO clock */
  170. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  171. /* Enable USART clock */
  172. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  173. #endif /* RT_USING_UART1 */
  174. #ifdef RT_USING_UART2
  175. /* Enable GPIO clock */
  176. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  177. /* Enable USART clock */
  178. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  179. #endif /* RT_USING_UART2 */
  180. }
  181. static void GPIO_Configuration(void)
  182. {
  183. GPIO_InitTypeDef GPIO_InitStructure;
  184. #ifdef RT_USING_UART1
  185. /* Connect PXx to USARTx_Tx */
  186. GPIO_PinAFConfig(UART1_GPIO, UART1_GPIO_TX_SOURCE, UART1_GPIO_AF);
  187. /* Connect PXx to USARTx_Rx */
  188. GPIO_PinAFConfig(UART1_GPIO, UART1_GPIO_RX_SOURCE, UART1_GPIO_AF);
  189. /* Configure USART Tx, Rx as alternate function push-pull */
  190. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX | UART1_GPIO_RX;
  191. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  192. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  193. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  194. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  195. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  196. #endif /* RT_USING_UART1 */
  197. #ifdef RT_USING_UART2
  198. /* Connect PXx to USARTx_Tx */
  199. GPIO_PinAFConfig(UART2_GPIO, UART2_GPIO_TX_SOURCE, UART2_GPIO_AF);
  200. /* Connect PXx to USARTx_Rx */
  201. GPIO_PinAFConfig(UART2_GPIO, UART2_GPIO_RX_SOURCE, UART2_GPIO_AF);
  202. /* Configure USART Tx, Rx as alternate function push-pull */
  203. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX | UART2_GPIO_RX;
  204. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  205. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  206. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  207. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  208. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  209. #endif /* RT_USING_UART2 */
  210. }
  211. static void NVIC_Configuration(struct stm32_uart* uart)
  212. {
  213. NVIC_InitTypeDef NVIC_InitStructure;
  214. /* Enable the USART Interrupt */
  215. NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
  216. NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  217. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  218. NVIC_Init(&NVIC_InitStructure);
  219. }
  220. void rt_hw_usart_init(void)
  221. {
  222. struct stm32_uart* uart;
  223. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  224. RCC_Configuration();
  225. GPIO_Configuration();
  226. #ifdef RT_USING_UART1
  227. uart = &uart1;
  228. config.baud_rate = BAUD_RATE_115200;
  229. serial1.ops = &stm32_uart_ops;
  230. serial1.config = config;
  231. NVIC_Configuration(&uart1);
  232. /* register UART1 device */
  233. rt_hw_serial_register(&serial1, "uart1",
  234. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  235. uart);
  236. #endif /* RT_USING_UART1 */
  237. #ifdef RT_USING_UART2
  238. uart = &uart2;
  239. config.baud_rate = BAUD_RATE_115200;
  240. serial2.ops = &stm32_uart_ops;
  241. serial2.config = config;
  242. NVIC_Configuration(&uart2);
  243. /* register UART1 device */
  244. rt_hw_serial_register(&serial2, "uart2",
  245. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  246. uart);
  247. #endif /* RT_USING_UART2 */
  248. }