usart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. * 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
  14. * 2012-02-08 aozima update for F4.
  15. */
  16. #include "stm32f4xx.h"
  17. #include "usart.h"
  18. #include "board.h"
  19. #include <serial.h>
  20. /*
  21. * Use UART1 as console output and finsh input
  22. * interrupt Rx and poll Tx (stream mode)
  23. *
  24. * Use UART2 with interrupt Rx and poll Tx
  25. * Use UART3 with DMA Tx and interrupt Rx -- DMA channel 2
  26. *
  27. * USART DMA setting on STM32
  28. * USART1 Tx --> DMA Channel 4
  29. * USART1 Rx --> DMA Channel 5
  30. * USART2 Tx --> DMA Channel 7
  31. * USART2 Rx --> DMA Channel 6
  32. * USART3 Tx --> DMA Channel 2
  33. * USART3 Rx --> DMA Channel 3
  34. */
  35. #ifdef RT_USING_UART1
  36. struct stm32_serial_int_rx uart1_int_rx;
  37. struct stm32_serial_device uart1 =
  38. {
  39. USART1,
  40. &uart1_int_rx,
  41. RT_NULL
  42. };
  43. struct rt_device uart1_device;
  44. #endif
  45. #ifdef RT_USING_UART2
  46. struct stm32_serial_int_rx uart2_int_rx;
  47. struct stm32_serial_device uart2 =
  48. {
  49. USART2,
  50. &uart2_int_rx,
  51. RT_NULL
  52. };
  53. struct rt_device uart2_device;
  54. #endif
  55. #ifdef RT_USING_UART3
  56. struct stm32_serial_int_rx uart3_int_rx;
  57. struct stm32_serial_dma_tx uart3_dma_tx;
  58. struct stm32_serial_device uart3 =
  59. {
  60. USART3,
  61. &uart3_int_rx,
  62. &uart3_dma_tx
  63. };
  64. struct rt_device uart3_device;
  65. #endif
  66. //#define USART1_DR_Base 0x40013804
  67. //#define USART2_DR_Base 0x40004404
  68. //#define USART3_DR_Base 0x40004804
  69. /* USART1_REMAP = 0 */
  70. #define UART1_GPIO_TX GPIO_Pin_9
  71. #define UART1_TX_PIN_SOURCE GPIO_PinSource9
  72. #define UART1_GPIO_RX GPIO_Pin_10
  73. #define UART1_RX_PIN_SOURCE GPIO_PinSource10
  74. #define UART1_GPIO GPIOA
  75. #define UART1_GPIO_RCC RCC_AHB1Periph_GPIOA
  76. #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
  77. #define UART1_TX_DMA DMA1_Channel4
  78. #define UART1_RX_DMA DMA1_Channel5
  79. #define UART2_GPIO_TX GPIO_Pin_2
  80. #define UART2_TX_PIN_SOURCE GPIO_PinSource2
  81. #define UART2_GPIO_RX GPIO_Pin_3
  82. #define UART2_RX_PIN_SOURCE GPIO_PinSource3
  83. #define UART2_GPIO GPIOA
  84. #define UART2_GPIO_RCC RCC_AHB1Periph_GPIOA
  85. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  86. /* USART3_REMAP[1:0] = 00 */
  87. #define UART3_GPIO_TX GPIO_Pin_10
  88. #define UART3_TX_PIN_SOURCE GPIO_PinSource10
  89. #define UART3_GPIO_RX GPIO_Pin_11
  90. #define UART3_RX_PIN_SOURCE GPIO_PinSource11
  91. #define UART3_GPIO GPIOB
  92. #define UART3_GPIO_RCC RCC_AHB1Periph_GPIOB
  93. #define RCC_APBPeriph_UART3 RCC_APB1Periph_USART3
  94. #define UART3_TX_DMA DMA1_Stream1
  95. #define UART3_RX_DMA DMA1_Stream3
  96. static void RCC_Configuration(void)
  97. {
  98. #ifdef RT_USING_UART1
  99. /* Enable USART2 GPIO clocks */
  100. RCC_AHB1PeriphClockCmd(UART1_GPIO_RCC, ENABLE);
  101. /* Enable USART2 clock */
  102. RCC_APB2PeriphClockCmd(RCC_APBPeriph_UART1, ENABLE);
  103. #endif
  104. #ifdef RT_USING_UART2
  105. /* Enable USART2 GPIO clocks */
  106. RCC_AHB1PeriphClockCmd(UART2_GPIO_RCC, ENABLE);
  107. /* Enable USART2 clock */
  108. RCC_APB1PeriphClockCmd(RCC_APBPeriph_UART2, ENABLE);
  109. #endif
  110. #ifdef RT_USING_UART3
  111. /* Enable USART3 GPIO clocks */
  112. RCC_AHB1PeriphClockCmd(UART3_GPIO_RCC, ENABLE);
  113. /* Enable USART3 clock */
  114. RCC_APB1PeriphClockCmd(RCC_APBPeriph_UART3, ENABLE);
  115. /* DMA clock enable */
  116. RCC_APB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
  117. #endif
  118. }
  119. static void GPIO_Configuration(void)
  120. {
  121. GPIO_InitTypeDef GPIO_InitStructure;
  122. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  123. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  124. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  125. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  126. #ifdef RT_USING_UART1
  127. /* Configure USART1 Rx/tx PIN */
  128. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX | UART1_GPIO_TX;
  129. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  130. /* Connect alternate function */
  131. GPIO_PinAFConfig(UART1_GPIO, UART1_TX_PIN_SOURCE, GPIO_AF_USART1);
  132. GPIO_PinAFConfig(UART1_GPIO, UART1_RX_PIN_SOURCE, GPIO_AF_USART1);
  133. #endif
  134. #ifdef RT_USING_UART2
  135. /* Configure USART2 Rx/tx PIN */
  136. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX | UART2_GPIO_RX;
  137. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  138. /* Connect alternate function */
  139. GPIO_PinAFConfig(UART2_GPIO, UART2_TX_PIN_SOURCE, GPIO_AF_USART2);
  140. GPIO_PinAFConfig(UART2_GPIO, UART2_RX_PIN_SOURCE, GPIO_AF_USART2);
  141. #endif
  142. #ifdef RT_USING_UART3
  143. /* Configure USART3 Rx/tx PIN */
  144. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX | UART3_GPIO_RX;
  145. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  146. /* Connect alternate function */
  147. GPIO_PinAFConfig(UART3_GPIO, UART3_TX_PIN_SOURCE, GPIO_AF_USART3);
  148. GPIO_PinAFConfig(UART3_GPIO, UART3_RX_PIN_SOURCE, GPIO_AF_USART3);
  149. #endif
  150. }
  151. static void NVIC_Configuration(void)
  152. {
  153. NVIC_InitTypeDef NVIC_InitStructure;
  154. #ifdef RT_USING_UART1
  155. /* Enable the USART1 Interrupt */
  156. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  157. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  158. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  159. NVIC_Init(&NVIC_InitStructure);
  160. #endif
  161. #ifdef RT_USING_UART2
  162. /* Enable the USART2 Interrupt */
  163. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  164. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  165. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  166. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  167. NVIC_Init(&NVIC_InitStructure);
  168. #endif
  169. #ifdef RT_USING_UART3
  170. /* Enable the USART3 Interrupt */
  171. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  172. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  173. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  174. NVIC_Init(&NVIC_InitStructure);
  175. /* Enable the DMA1 Channel2 Interrupt */
  176. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn;
  177. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  178. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  179. NVIC_Init(&NVIC_InitStructure);
  180. #endif
  181. }
  182. static void DMA_Configuration(void)
  183. {
  184. #if defined (RT_USING_UART3)
  185. DMA_InitTypeDef DMA_InitStructure;
  186. // /* Configure DMA Stream */
  187. // DMA_InitStructure.DMA_Channel = DMA_CHANNEL;
  188. // DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SRC_Const_Buffer;
  189. // DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DST_Buffer;
  190. // DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToMemory;
  191. // DMA_InitStructure.DMA_BufferSize = (uint32_t)BUFFER_SIZE;
  192. // DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
  193. // DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  194. // DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  195. // DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  196. // DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  197. // DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  198. // DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  199. // DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  200. // DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  201. // DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  202. // DMA_Init(DMA_STREAM, &DMA_InitStructure);
  203. /* Configure DMA Stream */
  204. DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  205. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);
  206. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0;
  207. DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  208. DMA_InitStructure.DMA_BufferSize = (uint32_t)0;
  209. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  210. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  211. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  212. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  213. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  214. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  215. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  216. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  217. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  218. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  219. DMA_DeInit(UART3_TX_DMA);
  220. DMA_Init(UART3_TX_DMA, &DMA_InitStructure);
  221. // /* fill init structure */
  222. // DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  223. // DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  224. // DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  225. // DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  226. // DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  227. // DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  228. // DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  229. //
  230. // /* DMA1 Channel5 (triggered by USART3 Tx event) Config */
  231. // DMA_DeInit(UART3_TX_DMA);
  232. // DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
  233. // DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  234. // DMA_InitStructure.DMA_MemoryBaseAddr = (u32)0;
  235. // DMA_InitStructure.DMA_BufferSize = 0;
  236. // DMA_Init(UART3_TX_DMA, &DMA_InitStructure);
  237. DMA_ITConfig(UART3_TX_DMA, DMA_IT_TC | DMA_IT_TE, ENABLE);
  238. // DMA_ClearFlag(DMA1_FLAG_TC5);
  239. #endif
  240. }
  241. volatile USART_TypeDef * uart2_debug = USART2;
  242. /*
  243. * Init all related hardware in here
  244. * rt_hw_serial_init() will register all supported USART device
  245. */
  246. void rt_hw_usart_init()
  247. {
  248. USART_InitTypeDef USART_InitStructure;
  249. RCC_Configuration();
  250. GPIO_Configuration();
  251. NVIC_Configuration();
  252. DMA_Configuration();
  253. /* uart init */
  254. #ifdef RT_USING_UART1
  255. USART_InitStructure.USART_BaudRate = 115200;
  256. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  257. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  258. USART_InitStructure.USART_Parity = USART_Parity_No;
  259. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  260. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  261. USART_Init(USART1, &USART_InitStructure);
  262. /* register uart1 */
  263. rt_hw_serial_register(&uart1_device, "uart1",
  264. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  265. &uart1);
  266. /* enable interrupt */
  267. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  268. #endif
  269. #ifdef RT_USING_UART2
  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_Init(USART2, &USART_InitStructure);
  277. /* register uart2 */
  278. rt_hw_serial_register(&uart2_device, "uart2",
  279. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  280. &uart2);
  281. /* Enable USART2 DMA Rx request */
  282. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  283. #endif
  284. #ifdef RT_USING_UART3
  285. USART_InitStructure.USART_BaudRate = 115200;
  286. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  287. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  288. USART_InitStructure.USART_Parity = USART_Parity_No;
  289. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  290. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  291. USART_Init(USART3, &USART_InitStructure);
  292. // uart3_dma_tx.dma_channel= UART3_TX_DMA;
  293. /* register uart3 */
  294. rt_hw_serial_register(&uart3_device, "uart3",
  295. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,
  296. &uart3);
  297. /* Enable USART3 DMA Tx request */
  298. USART_DMACmd(USART3, USART_DMAReq_Tx , ENABLE);
  299. /* enable interrupt */
  300. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  301. #endif
  302. }