usart.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_UART6
  45. struct stm32_serial_int_rx uart6_int_rx;
  46. struct stm32_serial_device uart6 =
  47. {
  48. USART6,
  49. &uart6_int_rx,
  50. RT_NULL
  51. };
  52. struct rt_device uart6_device;
  53. #endif
  54. #ifdef RT_USING_UART2
  55. struct stm32_serial_int_rx uart2_int_rx;
  56. struct stm32_serial_device uart2 =
  57. {
  58. USART2,
  59. &uart2_int_rx,
  60. RT_NULL
  61. };
  62. struct rt_device uart2_device;
  63. #endif
  64. #ifdef RT_USING_UART3
  65. struct stm32_serial_int_rx uart3_int_rx;
  66. struct stm32_serial_dma_tx uart3_dma_tx;
  67. struct stm32_serial_device uart3 =
  68. {
  69. USART3,
  70. &uart3_int_rx,
  71. &uart3_dma_tx
  72. };
  73. struct rt_device uart3_device;
  74. #endif
  75. #define USART1_DR_Base 0x40013804
  76. #define USART2_DR_Base 0x40004404
  77. #define USART3_DR_Base 0x40004804
  78. /* USART1_REMAP = 0 */
  79. #define UART1_GPIO_TX GPIO_Pin_9
  80. #define UART1_GPIO_RX GPIO_Pin_10
  81. #define UART1_GPIO GPIOA
  82. #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
  83. #define UART1_TX_DMA DMA1_Channel4
  84. #define UART1_RX_DMA DMA1_Channel5
  85. #if defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL)
  86. #define UART2_GPIO_TX GPIO_Pin_5
  87. #define UART2_GPIO_RX GPIO_Pin_6
  88. #define UART2_GPIO GPIOD
  89. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  90. #else /* for STM32F10X_HD */
  91. /* USART2_REMAP = 0 */
  92. #define UART2_GPIO_TX GPIO_Pin_2
  93. #define UART2_GPIO_RX GPIO_Pin_3
  94. #define UART2_GPIO GPIOA
  95. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  96. #define UART2_TX_DMA DMA1_Channel7
  97. #define UART2_RX_DMA DMA1_Channel6
  98. #endif
  99. /* USART3_REMAP[1:0] = 00 */
  100. #define UART3_GPIO_RX GPIO_Pin_11
  101. #define UART3_GPIO_TX GPIO_Pin_10
  102. #define UART3_GPIO GPIOB
  103. #define RCC_APBPeriph_UART3 RCC_APB1Periph_USART3
  104. #define UART3_TX_DMA DMA1_Channel2
  105. #define UART3_RX_DMA DMA1_Channel3
  106. /* USART6_REMAP = 0 */
  107. #define UART6_GPIO_TX GPIO_Pin_6
  108. #define UART6_GPIO_RX GPIO_Pin_7
  109. #define UART6_GPIO GPIOC
  110. #define RCC_APBPeriph_UART6 RCC_APB2Periph_USART6
  111. //#define UART1_TX_DMA DMA1_Channel?
  112. //#define UART1_RX_DMA DMA1_Channel?
  113. static void RCC_Configuration(void)
  114. {
  115. #ifdef RT_USING_UART1
  116. /* Enable USART1 and GPIOA clocks */
  117. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  118. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  119. #endif
  120. #ifdef RT_USING_UART6
  121. /* Enable USART6 and GPIOC clocks */
  122. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  123. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
  124. #endif
  125. }
  126. static void GPIO_Configuration(void)
  127. {
  128. GPIO_InitTypeDef GPIO_InitStruct;
  129. #ifdef RT_USING_UART1
  130. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
  131. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  132. GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  133. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  134. GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
  135. GPIO_Init(GPIOA,&GPIO_InitStruct);
  136. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  137. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  138. #endif
  139. #ifdef RT_USING_UART6
  140. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
  141. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  142. GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  143. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  144. GPIO_InitStruct.GPIO_Pin=UART6_GPIO_TX|UART6_GPIO_RX;
  145. GPIO_Init(UART6_GPIO,&GPIO_InitStruct);
  146. GPIO_PinAFConfig(UART6_GPIO, GPIO_PinSource6, GPIO_AF_USART6);
  147. GPIO_PinAFConfig(UART6_GPIO, GPIO_PinSource7, GPIO_AF_USART6);
  148. #endif
  149. }
  150. static void NVIC_Configuration(void)
  151. {
  152. NVIC_InitTypeDef NVIC_InitStructure;
  153. #ifdef RT_USING_UART1
  154. /* Enable the USART1 Interrupt */
  155. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  156. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  157. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  158. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  159. NVIC_Init(&NVIC_InitStructure);
  160. #endif
  161. #ifdef RT_USING_UART6
  162. /* Enable the USART1 Interrupt */
  163. NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
  164. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  165. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  166. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  167. NVIC_Init(&NVIC_InitStructure);
  168. #endif
  169. }
  170. /*
  171. * Init all related hardware in here
  172. * rt_hw_serial_init() will register all supported USART device
  173. */
  174. void rt_hw_usart_init()
  175. {
  176. USART_InitTypeDef USART_InitStructure;
  177. RCC_Configuration();
  178. GPIO_Configuration();
  179. NVIC_Configuration();
  180. /* uart init */
  181. #ifdef RT_USING_UART1
  182. USART_DeInit(USART1);
  183. USART_InitStructure.USART_BaudRate = 115200;
  184. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  185. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  186. USART_InitStructure.USART_Parity = USART_Parity_No ;
  187. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  188. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  189. USART_Init(USART1, &USART_InitStructure);
  190. /* register uart1 */
  191. rt_hw_serial_register(&uart1_device, "uart1",
  192. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  193. &uart1);
  194. /* enable interrupt */
  195. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  196. /* Enable USART1 */
  197. USART_Cmd(USART1, ENABLE);
  198. USART_ClearFlag(USART1,USART_FLAG_TXE);
  199. #endif
  200. /* uart init */
  201. #ifdef RT_USING_UART6
  202. USART_DeInit(USART6);
  203. USART_InitStructure.USART_BaudRate = 115200;
  204. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  205. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  206. USART_InitStructure.USART_Parity = USART_Parity_No ;
  207. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  208. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  209. USART_Init(USART6, &USART_InitStructure);
  210. /* register uart1 */
  211. rt_hw_serial_register(&uart6_device, "uart6",
  212. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  213. &uart6);
  214. /* enable interrupt */
  215. USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
  216. /* Enable USART6 */
  217. USART_Cmd(USART6, ENABLE);
  218. USART_ClearFlag(USART6,USART_FLAG_TXE);
  219. #endif
  220. }
  221. #ifdef RT_USING_UART1
  222. void USART1_IRQHandler()
  223. {
  224. /* enter interrupt */
  225. rt_interrupt_enter();
  226. rt_hw_serial_isr(&uart1_device);
  227. /* leave interrupt */
  228. rt_interrupt_leave();
  229. }
  230. #endif
  231. #ifdef RT_USING_UART6
  232. void USART6_IRQHandler()
  233. {
  234. /* enter interrupt */
  235. rt_interrupt_enter();
  236. rt_hw_serial_isr(&uart6_device);
  237. /* leave interrupt */
  238. rt_interrupt_leave();
  239. }
  240. #endif