usart.c 11 KB

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