usart.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-01-05 Bernard the first version
  9. * 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
  10. */
  11. #include "usart.h"
  12. #include <serial.h>
  13. #include <stm32f2xx.h>
  14. #include <stm32f2xx_dma.h>
  15. /*
  16. * Use UART1 as console output and finsh input
  17. * interrupt Rx and poll Tx (stream mode)
  18. *
  19. * Use UART2 with interrupt Rx and poll Tx
  20. * Use UART3 with DMA Tx and interrupt Rx -- DMA channel 2
  21. *
  22. * USART DMA setting on STM32
  23. * USART1 Tx --> DMA Channel 4
  24. * USART1 Rx --> DMA Channel 5
  25. * USART2 Tx --> DMA Channel 7
  26. * USART2 Rx --> DMA Channel 6
  27. * USART3 Tx --> DMA Channel 2
  28. * USART3 Rx --> DMA Channel 3
  29. */
  30. #ifdef RT_USING_UART1
  31. struct stm32_serial_int_rx uart1_int_rx;
  32. struct stm32_serial_device uart1 =
  33. {
  34. USART1,
  35. &uart1_int_rx,
  36. RT_NULL
  37. };
  38. struct rt_device uart1_device;
  39. #endif
  40. #ifdef RT_USING_UART6
  41. struct stm32_serial_int_rx uart6_int_rx;
  42. struct stm32_serial_device uart6 =
  43. {
  44. USART6,
  45. &uart6_int_rx,
  46. RT_NULL
  47. };
  48. struct rt_device uart6_device;
  49. #endif
  50. #ifdef RT_USING_UART2
  51. struct stm32_serial_int_rx uart2_int_rx;
  52. struct stm32_serial_device uart2 =
  53. {
  54. USART2,
  55. &uart2_int_rx,
  56. RT_NULL
  57. };
  58. struct rt_device uart2_device;
  59. #endif
  60. #ifdef RT_USING_UART3
  61. struct stm32_serial_int_rx uart3_int_rx;
  62. struct stm32_serial_dma_tx uart3_dma_tx;
  63. struct stm32_serial_device uart3 =
  64. {
  65. USART3,
  66. &uart3_int_rx,
  67. &uart3_dma_tx
  68. };
  69. struct rt_device uart3_device;
  70. #endif
  71. #define USART1_DR_Base 0x40013804
  72. #define USART2_DR_Base 0x40004404
  73. #define USART3_DR_Base 0x40004804
  74. /* USART1_REMAP = 0 */
  75. #define UART1_GPIO_TX GPIO_Pin_9
  76. #define UART1_GPIO_RX GPIO_Pin_10
  77. #define UART1_GPIO GPIOA
  78. #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
  79. #define UART1_TX_DMA DMA1_Channel4
  80. #define UART1_RX_DMA DMA1_Channel5
  81. #if defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL)
  82. #define UART2_GPIO_TX GPIO_Pin_5
  83. #define UART2_GPIO_RX GPIO_Pin_6
  84. #define UART2_GPIO GPIOD
  85. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  86. #else /* for STM32F10X_HD */
  87. /* USART2_REMAP = 0 */
  88. #define UART2_GPIO_TX GPIO_Pin_2
  89. #define UART2_GPIO_RX GPIO_Pin_3
  90. #define UART2_GPIO GPIOA
  91. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  92. #define UART2_TX_DMA DMA1_Channel7
  93. #define UART2_RX_DMA DMA1_Channel6
  94. #endif
  95. /* USART3_REMAP[1:0] = 00 */
  96. #define UART3_GPIO_RX GPIO_Pin_11
  97. #define UART3_GPIO_TX GPIO_Pin_10
  98. #define UART3_GPIO GPIOB
  99. #define RCC_APBPeriph_UART3 RCC_APB1Periph_USART3
  100. #define UART3_TX_DMA DMA1_Channel2
  101. #define UART3_RX_DMA DMA1_Channel3
  102. /* USART6_REMAP = 0 */
  103. #define UART6_GPIO_TX GPIO_Pin_6
  104. #define UART6_GPIO_RX GPIO_Pin_7
  105. #define UART6_GPIO GPIOC
  106. #define RCC_APBPeriph_UART6 RCC_APB2Periph_USART6
  107. //#define UART1_TX_DMA DMA1_Channel?
  108. //#define UART1_RX_DMA DMA1_Channel?
  109. static void RCC_Configuration(void)
  110. {
  111. #ifdef RT_USING_UART1
  112. /* Enable USART1 and GPIOA clocks */
  113. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  114. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  115. #endif
  116. #ifdef RT_USING_UART6
  117. /* Enable USART6 and GPIOC clocks */
  118. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  119. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
  120. #endif
  121. }
  122. static void GPIO_Configuration(void)
  123. {
  124. GPIO_InitTypeDef GPIO_InitStruct;
  125. #ifdef RT_USING_UART1
  126. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
  127. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  128. GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  129. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  130. GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
  131. GPIO_Init(GPIOA,&GPIO_InitStruct);
  132. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  133. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  134. #endif
  135. #ifdef RT_USING_UART6
  136. GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
  137. GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  138. GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
  139. GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
  140. GPIO_InitStruct.GPIO_Pin=UART6_GPIO_TX|UART6_GPIO_RX;
  141. GPIO_Init(UART6_GPIO,&GPIO_InitStruct);
  142. GPIO_PinAFConfig(UART6_GPIO, GPIO_PinSource6, GPIO_AF_USART6);
  143. GPIO_PinAFConfig(UART6_GPIO, GPIO_PinSource7, GPIO_AF_USART6);
  144. #endif
  145. }
  146. static void NVIC_Configuration(void)
  147. {
  148. NVIC_InitTypeDef NVIC_InitStructure;
  149. #ifdef RT_USING_UART1
  150. /* Enable the USART1 Interrupt */
  151. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  152. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  153. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  154. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  155. NVIC_Init(&NVIC_InitStructure);
  156. #endif
  157. #ifdef RT_USING_UART6
  158. /* Enable the USART1 Interrupt */
  159. NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
  160. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  161. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  162. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  163. NVIC_Init(&NVIC_InitStructure);
  164. #endif
  165. }
  166. /*
  167. * Init all related hardware in here
  168. * rt_hw_serial_init() will register all supported USART device
  169. */
  170. void rt_hw_usart_init()
  171. {
  172. USART_InitTypeDef USART_InitStructure;
  173. RCC_Configuration();
  174. GPIO_Configuration();
  175. NVIC_Configuration();
  176. /* uart init */
  177. #ifdef RT_USING_UART1
  178. USART_DeInit(USART1);
  179. USART_InitStructure.USART_BaudRate = 115200;
  180. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  181. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  182. USART_InitStructure.USART_Parity = USART_Parity_No ;
  183. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  184. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  185. USART_Init(USART1, &USART_InitStructure);
  186. /* register uart1 */
  187. rt_hw_serial_register(&uart1_device, "uart1",
  188. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  189. &uart1);
  190. /* enable interrupt */
  191. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  192. /* Enable USART1 */
  193. USART_Cmd(USART1, ENABLE);
  194. USART_ClearFlag(USART1,USART_FLAG_TXE);
  195. #endif
  196. /* uart init */
  197. #ifdef RT_USING_UART6
  198. USART_DeInit(USART6);
  199. USART_InitStructure.USART_BaudRate = 115200;
  200. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  201. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  202. USART_InitStructure.USART_Parity = USART_Parity_No ;
  203. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  204. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  205. USART_Init(USART6, &USART_InitStructure);
  206. /* register uart1 */
  207. rt_hw_serial_register(&uart6_device, "uart6",
  208. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  209. &uart6);
  210. /* enable interrupt */
  211. USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
  212. /* Enable USART6 */
  213. USART_Cmd(USART6, ENABLE);
  214. USART_ClearFlag(USART6,USART_FLAG_TXE);
  215. #endif
  216. }
  217. #ifdef RT_USING_UART1
  218. void USART1_IRQHandler()
  219. {
  220. /* enter interrupt */
  221. rt_interrupt_enter();
  222. rt_hw_serial_isr(&uart1_device);
  223. /* leave interrupt */
  224. rt_interrupt_leave();
  225. }
  226. #endif
  227. #ifdef RT_USING_UART6
  228. void USART6_IRQHandler()
  229. {
  230. /* enter interrupt */
  231. rt_interrupt_enter();
  232. rt_hw_serial_isr(&uart6_device);
  233. /* leave interrupt */
  234. rt_interrupt_leave();
  235. }
  236. #endif