usart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * File : usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, 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. */
  15. #include "usart.h"
  16. #include <serial.h>
  17. #include <stm32f2xx.h>
  18. #include <stm32f2xx_dma.h>
  19. /*
  20. * Use UART1 as console output and finsh input
  21. * interrupt Rx and poll Tx (stream mode)
  22. *
  23. * Use UART2 with interrupt Rx and poll Tx
  24. * Use UART3 with DMA Tx and interrupt Rx -- DMA channel 2
  25. *
  26. * USART DMA setting on STM32
  27. * USART1 Tx --> DMA Channel 4
  28. * USART1 Rx --> DMA Channel 5
  29. * USART2 Tx --> DMA Channel 7
  30. * USART2 Rx --> DMA Channel 6
  31. * USART3 Tx --> DMA Channel 2
  32. * USART3 Rx --> DMA Channel 3
  33. */
  34. #ifdef RT_USING_UART1
  35. struct stm32_serial_int_rx uart1_int_rx;
  36. struct stm32_serial_device uart1 =
  37. {
  38. USART1,
  39. &uart1_int_rx,
  40. RT_NULL
  41. };
  42. struct rt_device uart1_device;
  43. #endif
  44. #ifdef RT_USING_UART2
  45. struct stm32_serial_int_rx uart2_int_rx;
  46. struct stm32_serial_device uart2 =
  47. {
  48. USART2,
  49. &uart2_int_rx,
  50. RT_NULL
  51. };
  52. struct rt_device uart2_device;
  53. #endif
  54. #ifdef RT_USING_UART3
  55. struct stm32_serial_int_rx uart3_int_rx;
  56. struct stm32_serial_dma_tx uart3_dma_tx;
  57. struct stm32_serial_device uart3 =
  58. {
  59. USART3,
  60. &uart3_int_rx,
  61. &uart3_dma_tx
  62. };
  63. struct rt_device uart3_device;
  64. #endif
  65. #define USART1_DR_Base 0x40013804
  66. #define USART2_DR_Base 0x40004404
  67. #define USART3_DR_Base 0x40004804
  68. /* USART1_REMAP = 0 */
  69. #define UART1_GPIO_TX GPIO_Pin_9
  70. #define UART1_GPIO_RX GPIO_Pin_10
  71. #define UART1_GPIO GPIOA
  72. #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
  73. #define UART1_TX_DMA DMA1_Channel4
  74. #define UART1_RX_DMA DMA1_Channel5
  75. #if defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL)
  76. #define UART2_GPIO_TX GPIO_Pin_5
  77. #define UART2_GPIO_RX GPIO_Pin_6
  78. #define UART2_GPIO GPIOD
  79. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  80. #else /* for STM32F10X_HD */
  81. /* USART2_REMAP = 0 */
  82. #define UART2_GPIO_TX GPIO_Pin_2
  83. #define UART2_GPIO_RX GPIO_Pin_3
  84. #define UART2_GPIO GPIOA
  85. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  86. #define UART2_TX_DMA DMA1_Channel7
  87. #define UART2_RX_DMA DMA1_Channel6
  88. #endif
  89. /* USART3_REMAP[1:0] = 00 */
  90. #define UART3_GPIO_RX GPIO_Pin_11
  91. #define UART3_GPIO_TX GPIO_Pin_10
  92. #define UART3_GPIO GPIOB
  93. #define RCC_APBPeriph_UART3 RCC_APB1Periph_USART3
  94. #define UART3_TX_DMA DMA1_Channel2
  95. #define UART3_RX_DMA DMA1_Channel3
  96. static void RCC_Configuration(void)
  97. {
  98. #ifdef RT_USING_UART1
  99. /* Enable USART1 and GPIOA clocks */
  100. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  101. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  102. #endif
  103. }
  104. static void GPIO_Configuration(void)
  105. {
  106. GPIO_InitTypeDef GPIO_InitStruct;
  107. #ifdef RT_USING_UART1
  108. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
  109. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  110. GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  111. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  112. GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
  113. GPIO_Init(GPIOA,&GPIO_InitStruct);
  114. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  115. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  116. #endif
  117. }
  118. static void NVIC_Configuration(void)
  119. {
  120. NVIC_InitTypeDef NVIC_InitStructure;
  121. #ifdef RT_USING_UART1
  122. /* Enable the USART1 Interrupt */
  123. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  124. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  125. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  126. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  127. NVIC_Init(&NVIC_InitStructure);
  128. #endif
  129. }
  130. /*
  131. * Init all related hardware in here
  132. * rt_hw_serial_init() will register all supported USART device
  133. */
  134. void rt_hw_usart_init()
  135. {
  136. USART_InitTypeDef USART_InitStructure;
  137. RCC_Configuration();
  138. GPIO_Configuration();
  139. NVIC_Configuration();
  140. /* uart init */
  141. #ifdef RT_USING_UART1
  142. USART_DeInit(USART1);
  143. USART_InitStructure.USART_BaudRate = 115200;
  144. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  145. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  146. USART_InitStructure.USART_Parity = USART_Parity_No ;
  147. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  148. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  149. USART_Init(USART1, &USART_InitStructure);
  150. /* register uart1 */
  151. rt_hw_serial_register(&uart1_device, "uart1",
  152. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  153. &uart1);
  154. /* enable interrupt */
  155. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  156. /* Enable USART1 */
  157. USART_Cmd(USART1, ENABLE);
  158. USART_ClearFlag(USART1,USART_FLAG_TXE);
  159. #endif
  160. }
  161. void USART1_IRQHandler()
  162. {
  163. /* enter interrupt */
  164. rt_interrupt_enter();
  165. rt_hw_serial_isr(&uart1_device);
  166. /* leave interrupt */
  167. rt_interrupt_leave();
  168. }