1 void USART3_Configuration(void) //串口3配置---S 2 { 3 DMA_InitTypeDef DMA_InitStructure; 4 USART_InitTypeDef USART_InitStructure; 5 GPIO_InitTypeDef GPIO_InitStructure; 6 NVIC_InitTypeDef NVIC_InitStructure; 7 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); 8 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE); 9 //USART1 10 //TX 11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 12GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 13 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 14 GPIO_Init (GPIOB, & GPIO_InitStructure); 15 // RX 16 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; 17 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 18 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 19 GPIO_Init (GPIOB, & GPIO_InitStructure); 20 21 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART3-> RT); 22 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)_code_rece; 23 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 24 DMA_InitStructure.DMA_BufferSize = 4000; 25 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 26 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 27 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //HalfWord 28 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 29 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ; // DMA_Mode_Normal 30 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA_Priority_Low DMA_Priority_Medium DMA_Priority_High 31 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 32 DMA_Init(DMA1_Channel3, &DMA_InitStructure); 33 34 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&( USART3->DR); 35 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)_send_data; 36 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 37 DMA_InitStructure.DMA_BufferSize = 14; 38 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 39 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 40 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //HalfWord 41 DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; 42 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; // DMA_Mode_Normal 43 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA_Priority_Low DMA_Priority_Medium DMA_Priority_High 44 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 45 DMA_Init(DMA1_Channel2, &DMA_InitStructure); 46 47 USART_OverSampling8Cmd (USART3, ENABLE); 48 USART_InitStructure.USART_BaudRate = 460,800 ; 49 USART_InitStructure.USART_WordLength = USART_WordLength_8b; 50 USART_InitStructure.USART_StopBits = USART_StopBits_1; 51 USART_InitStructure.USART_Parity = USART_Parity_No; 52 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 53 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 54 USART_Init (USART3, & USART_InitStructure); 55 56 // NVIC设置,使能中断 57NVIC_PriorityGroupConfig (NVIC_PriorityGroup_1); // Select Interrupt packets. 1 58 59 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // Select Interrupt Serial 3 60 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0 ; // preemptive interrupt priority level setting. 1 61 is NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0 ; / / responsive to the interrupt priority. 1 62 is NVIC_InitStructure.NVIC_IRQChannelCmd the eNABLE =; // enable interrupt 63 is NVIC_Init (& NVIC_InitStructure); 64 USART_ITConfig (USART3, USART_IT_IDLE, the eNABLE); // enable interrupts 65 66 USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE); 67 USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE); 68 DMA_Cmd(DMA1_Channel3, ENABLE); 69 USART_Cmd(USART3, ENABLE); 70 USART3->SR; 71 USART3->DR; 72 } 73 74 DmaSendDataProc( DMA1_Channel2, 14 ); 75 //开启一次DMA传输 76 void DmaSendDataProc(DMA_Channel_TypeDef *DMA_Streamx,u16 ndtr) 77 { 78 79 DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输 80 81DMA_SetCurrDataCounter (DMA_Streamx, ndtr); // data traffic 82 83 DMA_Cmd (DMA_Streamx, the ENABLE); // open DMA transfer 84 85 } 86 void USART3_IRQHandler ( void ) // sent by the client data 87 { 88 IF (USART_GetFlagStatus (USART3 , USART_FLAG_IDLE) == the SET) // RxNE a received flag byte, 89 { 90 DMA_Cmd (DMA1_Channel3, the DISABLE); // Close DMA output 91 is 92 usart2_num = 2000 - DMA1_Channel3 -> CNDTR; // Get read bytes 93 // processing data 94 DMA1_Channel3-> CNDTR = 2000 ; // refill 95 DMA_Cmd (DMA1_Channel3, the ENABLE); // open DMA transfer 96 } 97 98 USART_ClearITPendingBit (USART3, USART_IT_IDLE); // Clear the receive interrupt flag 99 USART3- > the SR; 100 USART3-> the DR; 101 102 }