我STM32串口为不占cpu资源使用DMA方式进行接收,设定接收缓冲区,在while(1)中查询已接收到的数据并进行处理,可是使用串口调试工具发送数据到板子,板子再将数据通过窗口打印处理,打印的数据和发送的数据出现不一致情况,求大侠帮忙看看!
[mw_shl_code=c,true]void User_Uart1Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; /* 定义GPIO初始化结构体 */
USART_InitTypeDef USART_InitStructure; /* 定义USART初始化结构体 */
/*-----------使能相关模块时钟 --------------------------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* 使能GPIO外设时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* 使能USART1外设时钟 */
/*-----------是否重映射端口连接模块 --------------------------------------------------------------------*/
GPIO_PinRemapConfig(GPIO_Remap_USART1,DISABLE); /* 引脚连接模块不重映射 */
/*-----------GPIO引脚始化 ------------------------------------------------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; /* 设置RX引脚模式 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; /* 设置TX引脚模式 */
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/*-----------UART1设置始化 -----------------------------------------------------------------------------*/
USART_InitStructure.USART_BaudRate = 9600; /* 设置USART模式 */
USART_InitStructure.USART_WordLength= USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE); /* 启动USART1外设 */
USART_GetFlagStatus(USART1,USART_FLAG_TC); //不清除将导致发送的第一个字节丢失
}
void Uart1_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitS ;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //开启DMA时钟
DMA_DeInit(DMA1_Channel5);
DMA_InitS.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
DMA_InitS.DMA_MemoryBaseAddr = (u32)Uart_Buffer.Rxbuffer;
DMA_InitS.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitS.DMA_BufferSize = RECVBUFF_SIZE;
DMA_InitS.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitS.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitS.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitS.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitS.DMA_Mode = DMA_Mode_Circular;
DMA_InitS.DMA_Priority = DMA_Priority_Medium;
DMA_InitS.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitS);
DMA_Cmd (DMA1_Channel5,ENABLE); //使能DMA
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
}
在while(1)中进行查询,调用下面这个函数[/mw_shl_code]
[mw_shl_code=c,true]
[mw_shl_code=c,true]void ReadUartBuffer(void)
{
static u8 count = 0;
static u8 revbuf[5] = {0};
u8 curr_char = 0;
Uart_Buffer.indexnew = (RECVBUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5));
if(Uart_Buffer.indexnew != Uart_Buffer.indexold)
{
while(Uart_Buffer.indexold != Uart_Buffer.indexnew)
{
curr_char = Uart_Buffer.Rxbuffer[Uart_Buffer.indexold%RECVBUFF_SIZE];
Uart_Buffer.indexold = (Uart_Buffer.indexold + 1)%RECVBUFF_SIZE;
if(curr_char == 0xFF) // packet head
{
count = 0;
revbuf[count++] = curr_char;
continue;
}
if(revbuf[0] != 0xFF)
{
continue;
}
revbuf[count] = curr_char;
if(count++ < 4)
{
continue;
}
//收到一个包
count = 0;
Uart_Unpack(revbuf);
}
if(Uart_Buffer.indexold >= RECVBUFF_SIZE)
{
Uart_Buffer.indexold = 0;
}
}
}
[/mw_shl_code]
[mw_shl_code=c,true]Uart_Unpack(revbuf);函数就是将收到的数据用printf通过串口打印出来。[/mw_shl_code]
[mw_shl_code=c,true]实际情况是,有时候是完全正常的,运行久了一段时间又会出现发送和打印的不一样,求解释![/mw_shl_code]
[/mw_shl_code]
|