如题,相同的配置,在使用usart3/usart6,uart4/5时,无法接收串口数据,usart1/2可以正常接收,配置如下:UART4为例
DMA配置如下:
[mw_shl_code=applescript,true]void MYDMA_Config_u6(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)
{
DMA_InitTypeDef DMA_InitStructure;
// if((u32)DMA_Streamx>(u32)DMA2)//得到当前stream是属于DMA2还是DMA1
// {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2时钟使能
// }else
// {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1时钟使能
// }
DMA_DeInit(DMA_Streamx);
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//等待DMA可配置
//printf("configsuccess
");
/* 配置 DMA Stream */
DMA_InitStructure.DMA_Channel = chx; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA 存储器0地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//存储器到外设模式
DMA_InitStructure.DMA_BufferSize = ndtr;//数据传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据长度:8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//存储器数据长度:8位
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// 使用普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//中等优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
DMA_Init(DMA_Streamx, &DMA_InitStructure);//初始化DMA Stream
//USART_Cmd(UART4, ENABLE); //使能串口6
//DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5);
DMA_Cmd(DMA_Streamx, ENABLE); //使能USART1 TX DMA1 所指示的通道
DMA_ClearFlag(DMA1_Stream2,DMA_FLAG_TCIF5);
MYDMA_Enable_u6(DMA_Streamx,ndtr);//开始一次DMA传输!
}
//开启一次DMA传输
//DMA_Streamx
MA数据流,DMA1_Stream0~7/DMA2_Stream0~7
//ndtr:数据传输量
void MYDMA_Enable_u6(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
{
DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){} //确保DMA可以被设置
DMA_SetCurrDataCounter(DMA_Streamx,ndtr); //数据传输量
DMA_ClearFlag(DMA1_Stream2,DMA_FLAG_TCIF5);
DMA_Cmd(DMA_Streamx, ENABLE); //开启DMA传输
} [/mw_shl_code]
串口配置如下:
[mw_shl_code=applescript,true] RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //使能GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);//使能USART6时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART6时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4); //GPIOC6复用为USART3
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4); //GPIOC7复用为USART3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 |GPIO_Pin_10; //GPIOB10与GPIOB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA9,PA10
//USART3 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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; //收发模式
USART_Init(UART4, &USART_InitStructure); //初始化串口6
USART_Cmd(UART4, ENABLE); //使能串口6
USART_ClearFlag(UART4, USART_FLAG_TC);
USART_DMACmd(UART4,USART_DMAReq_Rx,ENABLE); //使能串口6的DMA接收
#if EN_UART4_RX
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//开启相关中断
//USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);//开启相关中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
//USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//开启相关中断
USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);//开启相关中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
#endif
if(USART_GetITStatus(UART4, USART_IT_IDLE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
printf("recevie
");
DMA_Cmd(DMA1_Stream2, DISABLE);//关闭dma
USART_ReceiveData( UART4 ); // Clear IDLE interrupt flag bit
USART_ClearITPendingBit( UART4, USART_IT_IDLE ); //清除空闲中断
iTemp= Rx6_Buf_Size-DMA_GetCurrDataCounter(DMA1_Stream2)-2;//数据长度
// iTemp=4;
USART_RX_STA=iTemp;
printf("%d
",iTemp);
// printf("%c
",Rx6Buff[0]);
for (i=0; i<=iTemp; i++)
{
USART6_RX_BUF
=Rx6Buff;
printf("%c
",Rx6Buff);
// USART_SendData(USART2, USART2_RX_BUF);
// while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//等待发送结束
}
USART6_RX_BUF[iTemp+1]='e';
USART_RX_STA|=0x8000;
// printf("-
"); //打印
MYDMA_Enable_u6(DMA1_Stream2, Rx6_Buf_Size);
}
[/mw_shl_code]
另外:
在测试的时候,我的串口1,DMA流只能用DMA2_Stream5,不能用DMA2_Stream2,也会出现读不到数据
MYDMA_Config_u6(DMA1_Stream2,DMA_Channel_4,(u32)&UART4->DR,(u32)Rx6Buff,Rx6_Buf_Size);//DMA2,STEAM5,CH4,外设为串口1,存储器为SendBuff,长度为:SEND_BUF_SIZE.
全部代码见附件,我是输出长度recevie之后,始终是-2
一周热门 更多>