usart.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. #ifdef RT_USING_UART6
  67. struct stm32_serial_int_rx uart6_int_rx;
  68. struct stm32_serial_device uart6 =
  69. {
  70. USART6,
  71. &uart6_int_rx,
  72. RT_NULL
  73. };
  74. struct rt_device uart6_device;
  75. #endif
  76. //#define USART1_DR_Base 0x40013804
  77. //#define USART2_DR_Base 0x40004404
  78. //#define USART3_DR_Base 0x40004804
  79. /* USART1_REMAP = 0 */
  80. #define UART1_GPIO_TX GPIO_Pin_9
  81. #define UART1_TX_PIN_SOURCE GPIO_PinSource9
  82. #define UART1_GPIO_RX GPIO_Pin_10
  83. #define UART1_RX_PIN_SOURCE GPIO_PinSource10
  84. #define UART1_GPIO GPIOA
  85. #define UART1_GPIO_RCC RCC_AHB1Periph_GPIOA
  86. #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
  87. #define UART1_TX_DMA DMA1_Channel4
  88. #define UART1_RX_DMA DMA1_Channel5
  89. #define UART2_GPIO_TX GPIO_Pin_2
  90. #define UART2_TX_PIN_SOURCE GPIO_PinSource2
  91. #define UART2_GPIO_RX GPIO_Pin_3
  92. #define UART2_RX_PIN_SOURCE GPIO_PinSource3
  93. #define UART2_GPIO GPIOA
  94. #define UART2_GPIO_RCC RCC_AHB1Periph_GPIOA
  95. #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
  96. /* USART3_REMAP[1:0] = 00 */
  97. #define UART3_GPIO_TX GPIO_Pin_10
  98. #define UART3_TX_PIN_SOURCE GPIO_PinSource10
  99. #define UART3_GPIO_RX GPIO_Pin_11
  100. #define UART3_RX_PIN_SOURCE GPIO_PinSource11
  101. #define UART3_GPIO GPIOB
  102. #define UART3_GPIO_RCC RCC_AHB1Periph_GPIOB
  103. #define RCC_APBPeriph_UART3 RCC_APB1Periph_USART3
  104. #define UART3_TX_DMA DMA1_Stream1
  105. #define UART3_RX_DMA DMA1_Stream3
  106. #define UART6_GPIO_TX GPIO_Pin_6
  107. #define UART6_TX_PIN_SOURCE GPIO_PinSource6
  108. #define UART6_GPIO_RX GPIO_Pin_7
  109. #define UART6_RX_PIN_SOURCE GPIO_PinSource7
  110. #define UART6_GPIO GPIOC
  111. #define UART6_GPIO_RCC RCC_AHB1Periph_GPIOC
  112. #define RCC_APBPeriph_UART6 RCC_APB2Periph_USART6
  113. static void RCC_Configuration(void)
  114. {
  115. #ifdef RT_USING_UART1
  116. /* Enable USART2 GPIO clocks */
  117. RCC_AHB1PeriphClockCmd(UART1_GPIO_RCC, ENABLE);
  118. /* Enable USART2 clock */
  119. RCC_APB2PeriphClockCmd(RCC_APBPeriph_UART1, ENABLE);
  120. #endif
  121. #ifdef RT_USING_UART2
  122. /* Enable USART2 GPIO clocks */
  123. RCC_AHB1PeriphClockCmd(UART2_GPIO_RCC, ENABLE);
  124. /* Enable USART2 clock */
  125. RCC_APB1PeriphClockCmd(RCC_APBPeriph_UART2, ENABLE);
  126. #endif
  127. #ifdef RT_USING_UART3
  128. /* Enable USART3 GPIO clocks */
  129. RCC_AHB1PeriphClockCmd(UART3_GPIO_RCC, ENABLE);
  130. /* Enable USART3 clock */
  131. RCC_APB1PeriphClockCmd(RCC_APBPeriph_UART3, ENABLE);
  132. /* DMA clock enable */
  133. RCC_APB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
  134. #endif
  135. #ifdef RT_USING_UART6
  136. /* Enable USART6 GPIO clocks */
  137. RCC_AHB1PeriphClockCmd(UART6_GPIO_RCC, ENABLE);
  138. /* Enable USART6 clock */
  139. RCC_APB2PeriphClockCmd(RCC_APBPeriph_UART6, ENABLE);
  140. #endif
  141. }
  142. static void GPIO_Configuration(void)
  143. {
  144. GPIO_InitTypeDef GPIO_InitStructure;
  145. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  146. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  147. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  148. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  149. #ifdef RT_USING_UART1
  150. /* Configure USART1 Rx/tx PIN */
  151. GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX | UART1_GPIO_TX;
  152. GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
  153. /* Connect alternate function */
  154. GPIO_PinAFConfig(UART1_GPIO, UART1_TX_PIN_SOURCE, GPIO_AF_USART1);
  155. GPIO_PinAFConfig(UART1_GPIO, UART1_RX_PIN_SOURCE, GPIO_AF_USART1);
  156. #endif
  157. #ifdef RT_USING_UART2
  158. /* Configure USART2 Rx/tx PIN */
  159. GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX | UART2_GPIO_RX;
  160. GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
  161. /* Connect alternate function */
  162. GPIO_PinAFConfig(UART2_GPIO, UART2_TX_PIN_SOURCE, GPIO_AF_USART2);
  163. GPIO_PinAFConfig(UART2_GPIO, UART2_RX_PIN_SOURCE, GPIO_AF_USART2);
  164. #endif
  165. #ifdef RT_USING_UART3
  166. /* Configure USART3 Rx/tx PIN */
  167. GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX | UART3_GPIO_RX;
  168. GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
  169. /* Connect alternate function */
  170. GPIO_PinAFConfig(UART3_GPIO, UART3_TX_PIN_SOURCE, GPIO_AF_USART3);
  171. GPIO_PinAFConfig(UART3_GPIO, UART3_RX_PIN_SOURCE, GPIO_AF_USART3);
  172. #endif
  173. #ifdef RT_USING_UART6
  174. /* Configure USART6 Rx/tx PIN */
  175. GPIO_InitStructure.GPIO_Pin = UART6_GPIO_TX | UART6_GPIO_RX;
  176. GPIO_Init(UART6_GPIO, &GPIO_InitStructure);
  177. /* Connect alternate function */
  178. GPIO_PinAFConfig(UART6_GPIO, UART6_TX_PIN_SOURCE, GPIO_AF_USART6);
  179. GPIO_PinAFConfig(UART6_GPIO, UART6_RX_PIN_SOURCE, GPIO_AF_USART6);
  180. #endif
  181. }
  182. static void NVIC_Configuration(void)
  183. {
  184. NVIC_InitTypeDef NVIC_InitStructure;
  185. #ifdef RT_USING_UART1
  186. /* Enable the USART1 Interrupt */
  187. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  188. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  189. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  190. NVIC_Init(&NVIC_InitStructure);
  191. #endif
  192. #ifdef RT_USING_UART2
  193. /* Enable the USART2 Interrupt */
  194. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  195. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  196. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  197. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  198. NVIC_Init(&NVIC_InitStructure);
  199. #endif
  200. #ifdef RT_USING_UART3
  201. /* Enable the USART3 Interrupt */
  202. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  203. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  204. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  205. NVIC_Init(&NVIC_InitStructure);
  206. /* Enable the DMA1 Channel2 Interrupt */
  207. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn;
  208. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  209. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  210. NVIC_Init(&NVIC_InitStructure);
  211. #endif
  212. #ifdef RT_USING_UART6
  213. /* Enable the USART6 Interrupt */
  214. NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
  215. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  216. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  217. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  218. NVIC_Init(&NVIC_InitStructure);
  219. #endif
  220. }
  221. static void DMA_Configuration(void)
  222. {
  223. #if defined (RT_USING_UART3)
  224. DMA_InitTypeDef DMA_InitStructure;
  225. // /* Configure DMA Stream */
  226. // DMA_InitStructure.DMA_Channel = DMA_CHANNEL;
  227. // DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SRC_Const_Buffer;
  228. // DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DST_Buffer;
  229. // DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToMemory;
  230. // DMA_InitStructure.DMA_BufferSize = (uint32_t)BUFFER_SIZE;
  231. // DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
  232. // DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  233. // DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  234. // DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  235. // DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  236. // DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  237. // DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  238. // DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  239. // DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  240. // DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  241. // DMA_Init(DMA_STREAM, &DMA_InitStructure);
  242. /* Configure DMA Stream */
  243. DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  244. DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);
  245. DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0;
  246. DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  247. DMA_InitStructure.DMA_BufferSize = (uint32_t)0;
  248. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  249. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  250. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  251. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  252. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  253. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  254. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  255. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  256. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  257. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  258. DMA_DeInit(UART3_TX_DMA);
  259. DMA_Init(UART3_TX_DMA, &DMA_InitStructure);
  260. // /* fill init structure */
  261. // DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  262. // DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  263. // DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  264. // DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  265. // DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  266. // DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  267. // DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  268. //
  269. // /* DMA1 Channel5 (triggered by USART3 Tx event) Config */
  270. // DMA_DeInit(UART3_TX_DMA);
  271. // DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
  272. // DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  273. // DMA_InitStructure.DMA_MemoryBaseAddr = (u32)0;
  274. // DMA_InitStructure.DMA_BufferSize = 0;
  275. // DMA_Init(UART3_TX_DMA, &DMA_InitStructure);
  276. DMA_ITConfig(UART3_TX_DMA, DMA_IT_TC | DMA_IT_TE, ENABLE);
  277. // DMA_ClearFlag(DMA1_FLAG_TC5);
  278. #endif
  279. }
  280. volatile USART_TypeDef * uart2_debug = USART2;
  281. /*
  282. * Init all related hardware in here
  283. * rt_hw_serial_init() will register all supported USART device
  284. */
  285. void rt_hw_usart_init()
  286. {
  287. USART_InitTypeDef USART_InitStructure;
  288. RCC_Configuration();
  289. GPIO_Configuration();
  290. NVIC_Configuration();
  291. DMA_Configuration();
  292. /* uart init */
  293. #ifdef RT_USING_UART1
  294. USART_InitStructure.USART_BaudRate = 115200;
  295. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  296. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  297. USART_InitStructure.USART_Parity = USART_Parity_No;
  298. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  299. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  300. USART_Init(USART1, &USART_InitStructure);
  301. /* register uart1 */
  302. rt_hw_serial_register(&uart1_device, "uart1",
  303. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  304. &uart1);
  305. /* enable interrupt */
  306. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  307. #endif
  308. #ifdef RT_USING_UART2
  309. USART_InitStructure.USART_BaudRate = 115200;
  310. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  311. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  312. USART_InitStructure.USART_Parity = USART_Parity_No;
  313. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  314. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  315. USART_Init(USART2, &USART_InitStructure);
  316. /* register uart2 */
  317. rt_hw_serial_register(&uart2_device, "uart2",
  318. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  319. &uart2);
  320. /* Enable USART2 DMA Rx request */
  321. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  322. #endif
  323. #ifdef RT_USING_UART3
  324. USART_InitStructure.USART_BaudRate = 115200;
  325. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  326. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  327. USART_InitStructure.USART_Parity = USART_Parity_No;
  328. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  329. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  330. USART_Init(USART3, &USART_InitStructure);
  331. // uart3_dma_tx.dma_channel= UART3_TX_DMA;
  332. /* register uart3 */
  333. rt_hw_serial_register(&uart3_device, "uart3",
  334. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,
  335. &uart3);
  336. /* Enable USART3 DMA Tx request */
  337. USART_DMACmd(USART3, USART_DMAReq_Tx , ENABLE);
  338. /* enable interrupt */
  339. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  340. #endif
  341. #ifdef RT_USING_UART6
  342. USART_InitStructure.USART_BaudRate = 9600;
  343. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  344. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  345. USART_InitStructure.USART_Parity = USART_Parity_No;
  346. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  347. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  348. USART_Init(USART6, &USART_InitStructure);
  349. /* register uart6 */
  350. rt_hw_serial_register(&uart6_device, "uart6",
  351. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  352. &uart6);
  353. #endif
  354. }