本帖最后由 诡异之云 于 2014-10-31 09:32 编辑
最近在研究usart,之前研究了一下串口打印、串口中断,现在看了一下串口的DMA方式接受,发现主要是配置的问题,配置好了DMA就能工作,剩下的就是处理数据,我整理了一下使用DMA方式接收的参数配置信息。
1、usart说明 GPIO_InitTypeDefGPIO_InitStructure; USART_InitTypeDefUSART_InitStructure;
/*波特率为9600,数据长度为8位,停止位为1位,无奇偶校验,无流控制,收发模式*/ USART_InitStructure.USART_BaudRate= 9600; USART_InitStructure.USART_WordLength= USART_WordLength_8b; USART_InitStructure.USART_StopBits= USART_StopBits_1; USART_InitStructure.USART_Parity= USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl=\ USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode= USART_Mode_Rx | USART_Mode_Tx;
//使能时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//管脚配置 GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
GPIO_InitStructure.GPIO_OType= GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; GPIO_Init(GPIOA,&GPIO_InitStructure);
/* USART_GetFlagStatus解决usart第一个字符发送不出去问题 USART_DMACmd使能usart1的dma接收请求,关系到dma配置是否成功。
*/ USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); USART_DMACmd(USART1,USART_DMAReq_Rx, ENABLE); USART_ITConfig(USART1,USART_IT_RXNE, ENABLE); USART_GetFlagStatus(USART1,USART_FLAG_TC);
2、dma说明 1)结构体说明 typedef struct { uint32_t DMA_Channel; //通道选择 uint32_t DMA_PeripheralBaseAddr; //外设基址 uint32_t DMA_Memory0BaseAddr; //内存基址 uint32_t DMA_DIR; //设定外设为数据传输的目的地还是来源 uint32_t DMA_BufferSize; //缓存大小 uint32_t DMA_PeripheralInc; //设定外设地址寄存器递增与否 uint32_t DMA_MemoryInc; //设定内存地址寄存器递增与否 uint32_t DMA_PeripheralDataSize; //外设数据宽度 uint32_t DMA_MemoryDataSize; //内存数据宽度 uint32_t DMA_Mode; //DMA缓存模式 uint32_t DMA_Priority; //DMA通道优先级 uint32_tDMA_FIFOMode; uint32_tDMA_FIFOThreshold; uint32_tDMA_MemoryBurst; uint32_t DMA_PeripheralBurst; }DMA_InitTypeDef; 2)配置代码说明 //查阅芯片手册,USART1的DMA通道是DMA2_Stream5_channel4,故做如下配置
DMA_InitTypeDef DMA_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2| RCC_AHB1Periph_DMA1 , ENABLE);
DMA_InitStructure.DMA_Channel =DMA_Channel_4; DMA_InitStructure.DMA_PeripheralBaseAddr = \ (uint32_t)USART1_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)usart_rx_buffer; DMA_InitStructure.DMA_DIR =DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize =RXBUFFERSIZE; DMA_InitStructure.DMA_PeripheralInc =DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc =DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize =DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority =DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode =DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold =DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst =DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst =DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream5,&DMA_InitStructure); DMA_Cmd(DMA2_Stream5, ENABLE);
按照上面的配置,串口发来的数据就可以存储在usart_rx_buffer这个数组中了。
|