上次简介了串口通过DMA的方式进行数据的先发送后接收的模式,本文是对发送的数据进行先接收然后再发送的详细过程的介绍。
1.USART2的初始化
static void USART2_Init( void )
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
// 使能USART2和DMA1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
// 配置USART2引脚
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// 模式控制: 因为使用的是RS485
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStruct);
// 2023-05-18 缺少端口复用
//串口2对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
// 配置USART2
USART_InitStruct.USART_BaudRate = 115200;// 波特率可以适当大些
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStruct);
// 配置USART2的中断
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 使能USART2的中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
// 初始化DMA
#if 1
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
// 配置DMA1通道4,用于USART2接收
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) rx_buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = RX_BUFFER_SIZE;
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_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // DMA_Mode_Normal DMA_Mode_Circular
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
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(DMA1_Stream5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
#if 1
DMA_InitTypeDef DMA_InitStruct;
// 配置DMA1通道7,用于USART2发送
DMA_InitStruct.DMA_Channel = DMA_Channel_4;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t) & (USART2->DR);
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t) tx_buffer;
DMA_InitStruct.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStruct.DMA_BufferSize = TX_BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream6, &DMA_InitStruct);
// 设置中断
DMA_ITConfig(DMA1_Stream6, DMA_IT_TC, ENABLE);
NVIC_EnableIRQ(DMA1_Stream6_IRQn);
#endif
// 使能USART2接收DMA请求
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
// 使能USART2
USART_Cmd(USART2, ENABLE);
// 使能DMA1通道4
DMA_Cmd(DMA1_Stream5, ENABLE);
#if 1
// 使能USART2发送DMA请求
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
// 使能USART2
USART_Cmd(USART2, ENABLE);
// 使能DMA1通道7 先关闭
DMA_Cmd(DMA1_Stream6, DISABLE);
#endif
TX_ENABLE = 0; //开启接收模式
}
2.初始化完USART2后开始等待数据的到来,通过USART2的中断的方式来等到数据的到来
// 然后在USART2中断处理函数中,启动DMA传输:
void USART2_IRQHandler(void)
{
OSIntEnter();
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
DMA_Cmd(DMA1_Stream5, DISABLE);
DMA_SetCurrDataCounter(DMA1_Stream5, BUFFER_SIZE);
DMA_Cmd(DMA1_Stream5, ENABLE);
}
LED1_TOG;// 每接收一个字节的数据就反转一下
OSIntExit();
}
3.USART2接收完一个完整数据帧的标志是DMA的数据流中断
// 最后在DMA传输完成中断处理函数中,处理接收到的数据:
// 接收完成设定的字节数,才发生中断,这里是10个字节
void DMA1_Stream5_IRQHandler(void)
{
OSIntEnter();
if (DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5) != RESET)
{
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5);
#if 0 // 测试用
for (int i = 0; i < BUFFER_SIZE; i++)
{
printf("%c", buffer[i]);
}
#endif
TX_ENABLE = 1; //开启发送模式
Frame_Recv_Flag = 1;// 接收一帧数据的标志位
#if 0 // 测试用
// 接收完一帧数据后,开启发送模式,并把一帧数据发送出去
TX_ENABLE = 1; //开启发送模式
// 发送数据
tx_buffer[0] = 0x11;
tx_buffer[1] = 0x22;
tx_buffer[2] = 0x33;
tx_buffer[3] = 0x44;
tx_buffer[4] = 0x55;
tx_buffer[5] = 0x66;
tx_buffer[6] = 0x77;
tx_buffer[7] = 0x99;
// 为啥最后2个字节的数据发送不出去呢
// 最终不在这里发送数据,
tx_buffer[8] = 0x99;
tx_buffer[9] = 0x99;
DMA_Cmd(DMA1_Stream6, DISABLE);
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE);
DMA_SetCurrDataCounter(DMA1_Stream6, 10);
DMA_Cmd(DMA1_Stream6, ENABLE);
//DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6);
//DMA_Cmd(DMA1_Stream6, ENABLE);
#endif
// 接收完数据
LED2_TOG;// 串口助手每发一帧数据,灯就会一闪一下,即接收完一帧数据
}
OSIntExit();
}
4.在任务函数中进行接收的数据帧进行解析,然后发送一帧数据出去
void CommPoll_bk_01( void )
{
if(1 == Frame_Recv_Flag) // 如果有一帧数据过来的话,
{
// 如果有一帧数据过来,先解析这一帧数据
// 然后再发送一帧数据回去
// 先封装一帧数据
// 然后启动发送
#if 1
// 接收完一帧数据后,开启发送模式,并把一帧数据发送出去
TX_ENABLE = 1; //开启发送模式
// 发送数据帧的组装
tx_buffer[0] = 0x11;
tx_buffer[1] = 0x22;
tx_buffer[2] = 0x33;
tx_buffer[3] = 0x44;
tx_buffer[4] = 0x55;
tx_buffer[5] = 0x66;
tx_buffer[6] = 0x77;
tx_buffer[7] = 0x99;
tx_buffer[8] = 0x99; // 为啥最后2个字节的数据发送不出去呢
tx_buffer[9] = 0x99;
tx_buffer[10] = 0x11;
tx_buffer[11] = 0x22;
tx_buffer[12] = 0x33;
tx_buffer[13] = 0x44;
tx_buffer[14] = 0x55;
tx_buffer[15] = 0x66;
tx_buffer[16] = 0x77;
tx_buffer[17] = 0x99;
tx_buffer[18] = 0x99; // 为啥最后2个字节的数据发送不出去呢
tx_buffer[19] = 0x99;
tx_buffer[20] = 0x11;
tx_buffer[21] = 0x22;
tx_buffer[22] = 0x33;
tx_buffer[23] = 0x44;
tx_buffer[24] = 0x55;
tx_buffer[25] = 0x66;
tx_buffer[26] = 0x77;
tx_buffer[27] = 0x99;
tx_buffer[28] = 0x99; // 为啥最后2个字节的数据发送不出去呢
tx_buffer[29] = 0x99;
tx_buffer[30] = 0xAA;
tx_buffer[31] = 0xBB;
tx_buffer[32] = 0xCC;
tx_buffer[33] = 0xDD;
tx_buffer[34] = 0xEE;
tx_buffer[35] = 0xFF;
tx_buffer[36] = 0x77;
tx_buffer[37] = 0x99;
tx_buffer[38] = 0x99; // 为啥最后2个字节的数据发送不出去呢
// 2023-07-15 发不出去的原因是:需要在DMA中断函数中,等待USART的传输完成标志的到来才
// 进行RS485的切换,因为DMA传输比USART快的缘故
DMA_Cmd(DMA1_Stream6, DISABLE);
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE);
DMA_SetCurrDataCounter(DMA1_Stream6, TX_BUFFER_SIZE);
DMA_Cmd(DMA1_Stream6, ENABLE);
#endif
}
}
5.DMA发送完成一帧数据的中断函数
void DMA1_Stream6_IRQHandler(void)
{
OSIntEnter();
if (DMA_GetITStatus(DMA1_Stream6, DMA_IT_TCIF6) != RESET)
{
DMA_Cmd(DMA1_Stream6, DISABLE);
DMA_ClearITPendingBit(DMA1_Stream6, DMA_IT_TCIF6);
}
// 这里需不需要等待串口发送完成,才切换到接收模式,因为DMA发送很快,不一定串口发送完
// 验证了问题就出现再这里,DMA发送先完成,要等到串口发送完成才切换为接收模式
while(!USART_GetFlagStatus(USART2, USART_FLAG_TC));
TX_ENABLE = 0; //开启接收模式
Frame_Recv_Flag = 0;// 为了接收下一帧数据,先清零
LED3_TOG;// 每发送一帧数据完成就切换一下
OSIntExit();
}