usart.c 11 KB

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