usart.c 10 KB

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