usart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. */
  14. #include "usart.h"
  15. #include <serial.h>
  16. #include <stm32f10x_dma.h>
  17. /*
  18. * Use UART1 as console output and finsh input
  19. * interrupt Rx and poll Tx (stream mode)
  20. *
  21. * Use UART2 with DMA Rx and poll Tx -- DMA channel 6
  22. * Use UART3 with DMA Tx and interrupt Rx -- DMA channel 2
  23. *
  24. * USART DMA setting on STM32
  25. * USART1 Tx --> DMA Channel 4
  26. * USART1 Rx --> DMA Channel 5
  27. * USART2 Tx --> DMA Channel 7
  28. * USART2 Rx --> DMA Channel 6
  29. * USART3 Tx --> DMA Channel 2
  30. * USART3 Rx --> DMA Channel 3
  31. */
  32. #ifdef RT_USING_UART1
  33. struct stm32_serial_int_rx uart1_int_rx;
  34. struct stm32_serial_device uart1 =
  35. {
  36. USART1,
  37. &uart1_int_rx,
  38. RT_NULL,
  39. RT_NULL,
  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_dma_rx uart2_dma_rx;
  47. struct stm32_serial_device uart2 =
  48. {
  49. USART2,
  50. &uart2_int_rx,
  51. RT_NULL,
  52. RT_NULL,
  53. RT_NULL
  54. };
  55. struct rt_device uart2_device;
  56. #endif
  57. #ifdef RT_USING_UART3
  58. struct stm32_serial_int_rx uart3_int_rx;
  59. struct stm32_serial_dma_tx uart3_dma_tx;
  60. struct stm32_serial_device uart3 =
  61. {
  62. USART3,
  63. &uart3_int_rx,
  64. RT_NULL,
  65. RT_NULL,
  66. &uart3_dma_tx
  67. };
  68. struct rt_device uart3_device;
  69. #endif
  70. #define USART1_DR_Base 0x40013804
  71. #define USART2_DR_Base 0x40004404
  72. #define USART3_DR_Base 0x40004804
  73. /* USART1_REMAP = 0 */
  74. #define UART1_GPIO_TX GPIO_Pin_9
  75. #define UART1_GPIO_RX GPIO_Pin_10
  76. #define UART1_GPIO GPIOA
  77. #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
  78. #define UART1_TX_DMA DMA1_Channel4
  79. #define UART1_RX_DMA DMA1_Channel5
  80. /* USART2_REMAP = 0 */
  81. #define UART2_GPIO_TX GPIO_Pin_2
  82. #define UART2_GPIO_RX GPIO_Pin_3
  83. #define UART2_GPIO GPIOA
  84. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  85. #define UART2_TX_DMA DMA1_Channel7
  86. #define UART2_RX_DMA DMA1_Channel6
  87. /* USART3_REMAP[1:0] = 00 */
  88. #define UART3_GPIO_RX GPIO_Pin_11
  89. #define UART3_GPIO_TX GPIO_Pin_10
  90. #define UART3_GPIO GPIOB
  91. #define RCC_APBPeriph_UART3 RCC_APB1Periph_USART3
  92. #define UART3_TX_DMA DMA1_Channel2
  93. #define UART3_RX_DMA DMA1_Channel3
  94. static void RCC_Configuration(void)
  95. {
  96. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  97. #ifdef RT_USING_UART1
  98. /* Enable USART1 and GPIOA clocks */
  99. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  100. #endif
  101. #ifdef RT_USING_UART2
  102. /* Enable GPIOD clocks */
  103. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  104. /* Enable USART2 clock */
  105. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  106. #endif
  107. #ifdef RT_USING_UART3
  108. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  109. /* Enable USART3 clock */
  110. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  111. #endif
  112. #if defined (RT_USING_UART2) || defined (RT_USING_UART3)
  113. /* DMA clock enable */
  114. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  115. #endif
  116. }
  117. static void GPIO_Configuration(void)
  118. {
  119. GPIO_InitTypeDef GPIO_InitStructure;
  120. #ifdef RT_USING_UART1
  121. /* Configure USART1 Rx (PA.10) as input floating */
  122. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
  123. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  124. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  125. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  126. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
  127. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  128. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  129. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  130. #endif
  131. #ifdef RT_USING_UART2
  132. /* Configure USART2 Rx as input floating */
  133. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
  134. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  135. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  136. /* Configure USART2 Tx as alternate function push-pull */
  137. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
  138. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  139. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  140. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  141. #endif
  142. #ifdef RT_USING_UART3
  143. /* Configure USART3 Rx as input floating */
  144. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX;
  145. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  146. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  147. /* Configure USART3 Tx as alternate function push-pull */
  148. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_TX;
  149. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  150. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  151. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  152. #endif
  153. }
  154. static void NVIC_Configuration(void)
  155. {
  156. NVIC_InitTypeDef NVIC_InitStructure;
  157. /* Configure the NVIC Preemption Priority Bits */
  158. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  159. #ifdef RT_USING_UART1
  160. /* Enable the USART1 Interrupt */
  161. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  162. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  163. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  164. NVIC_Init(&NVIC_InitStructure);
  165. #endif
  166. #ifdef RT_USING_UART2
  167. /* Enable the USART2 Interrupt */
  168. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  169. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  170. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  171. NVIC_Init(&NVIC_InitStructure);
  172. #endif
  173. #ifdef RT_USING_UART3
  174. /* Enable the USART3 Interrupt */
  175. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  176. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  177. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  178. NVIC_Init(&NVIC_InitStructure);
  179. /* Enable the DMA1 Channel2 Interrupt */
  180. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;
  181. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  182. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  183. NVIC_Init(&NVIC_InitStructure);
  184. #endif
  185. }
  186. static void DMA_Configuration(void)
  187. {
  188. #if defined (RT_USING_UART3)
  189. DMA_InitTypeDef DMA_InitStructure;
  190. /* fill init structure */
  191. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  192. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  193. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  194. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  195. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  196. DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  197. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  198. /* DMA1 Channel5 (triggered by USART3 Tx event) Config */
  199. DMA_DeInit(UART3_TX_DMA);
  200. DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
  201. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  202. DMA_InitStructure.DMA_MemoryBaseAddr = (u32)0;
  203. DMA_InitStructure.DMA_BufferSize = 0;
  204. DMA_Init(UART3_TX_DMA, &DMA_InitStructure);
  205. DMA_ITConfig(UART3_TX_DMA, DMA_IT_TC | DMA_IT_TE, ENABLE);
  206. DMA_ClearFlag(DMA1_FLAG_TC5);
  207. #endif
  208. }
  209. /*
  210. * Init all related hardware in here
  211. * rt_hw_serial_init() will register all supported USART device
  212. */
  213. void rt_hw_usart_init()
  214. {
  215. USART_InitTypeDef USART_InitStructure;
  216. USART_ClockInitTypeDef USART_ClockInitStructure;
  217. RCC_Configuration();
  218. GPIO_Configuration();
  219. NVIC_Configuration();
  220. DMA_Configuration();
  221. /* uart init */
  222. #ifdef RT_USING_UART1
  223. USART_InitStructure.USART_BaudRate = 115200;
  224. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  225. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  226. USART_InitStructure.USART_Parity = USART_Parity_No;
  227. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  228. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  229. USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  230. USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  231. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  232. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  233. USART_Init(USART1, &USART_InitStructure);
  234. USART_ClockInit(USART1, &USART_ClockInitStructure);
  235. /* register uart1 */
  236. rt_hw_serial_register(&uart1_device, "uart1",
  237. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  238. &uart1);
  239. /* enable interrupt */
  240. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  241. #endif
  242. #ifdef RT_USING_UART2
  243. USART_InitStructure.USART_BaudRate = 115200;
  244. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  245. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  246. USART_InitStructure.USART_Parity = USART_Parity_No;
  247. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  248. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  249. USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  250. USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  251. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  252. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  253. USART_Init(USART2, &USART_InitStructure);
  254. USART_ClockInit(USART2, &USART_ClockInitStructure);
  255. /* register uart2 */
  256. rt_hw_serial_register(&uart2_device, "uart2",
  257. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  258. &uart2);
  259. /* Enable USART2 DMA Rx request */
  260. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  261. #endif
  262. #ifdef RT_USING_UART3
  263. USART_InitStructure.USART_BaudRate = 115200;
  264. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  265. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  266. USART_InitStructure.USART_Parity = USART_Parity_No;
  267. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  268. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  269. USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  270. USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  271. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  272. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  273. USART_Init(USART3, &USART_InitStructure);
  274. USART_ClockInit(USART3, &USART_ClockInitStructure);
  275. uart3_dma_tx.dma_channel= UART3_TX_DMA;
  276. /* register uart3 */
  277. rt_hw_serial_register(&uart3_device, "uart3",
  278. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,
  279. &uart3);
  280. /* Enable USART3 DMA Tx request */
  281. USART_DMACmd(USART3, USART_DMAReq_Tx , ENABLE);
  282. /* enable interrupt */
  283. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  284. #endif
  285. }