金牌会员
- 积分
- 1108
- 金钱
- 1108
- 注册时间
- 2018-11-6
- 在线时间
- 240 小时
|
发表于 2019-12-9 10:31:10
|
显示全部楼层
stm32f407 串口USART2接收和发送全部使用DMA方式,同时使用串口空闲中断可以解决:DMA接收不定长报文
void USART2_DMA_Tx_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE); //DMA1时钟使能
DMA_DeInit(DMA1_Stream6);
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE); //等待DMA可配置
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //DMA通道配置
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; //DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART2_DMA_TX_Buffer; //发送缓存指针
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //DMA传输方向:内存--->外设
DMA_InitStructure.DMA_BufferSize = USART2_DMA_TX_BUFFER_MAX_LENGTH; //数据传输字节数量
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_Medium; //中等优先级 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(DMA1_Stream6, &DMA_InitStructure); //初始化DMA Stream
DMA_Cmd(DMA1_Stream6, DISABLE); //开启DMA传输
}
void USART2_DMA_Rx_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE); //DMA1时钟使能
DMA_DeInit(DMA1_Stream5);
while (DMA_GetCmdStatus(DMA1_Stream5) != DISABLE); //等待DMA可配置
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; //DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART2_DMA_RX_Buffer; //接收缓存指针
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ; //DMA传输方向:外设到存储器模式:外设--->内存
DMA_InitStructure.DMA_BufferSize = USART2_DMA_RX_BUFFER_MAX_LENGTH; //缓冲大小
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_Medium; //中等优先级 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_Stream
DMA_Cmd(DMA1_Stream5, ENABLE); //开启DMA传输
}
void USART2_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendCount)
{
if (nSendCount < USART2_DMA_TX_BUFFER_MAX_LENGTH)
{
memcpy(USART2_DMA_TX_Buffer , send_buffer , nSendCount);
DMA_Cmd(DMA1_Stream6 , DISABLE); //关闭DMA传输
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE); //确保DMA可以被设置
DMA_SetCurrDataCounter(DMA1_Stream6 , nSendCount); //数据传输量
DMA_Cmd(DMA1_Stream6 , ENABLE); //开启DMA传输
}
}
void USART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE); //for USART2, USART3, UART4 or UART5.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
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_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2, USART_FLAG_TC); //清除发送完成标志
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); //等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
USART_ClearFlag(USART2, USART_FLAG_TC); //清除发送完成标志
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //禁止USART1接收不为空中断
USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //禁止USART1发送空中断
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //开启USART1空闲中断
USART_ITConfig(USART2, USART_IT_TC, ENABLE); //开启USART1传输完成中断
USART_DMACmd(USART2 , USART_DMAReq_Tx,ENABLE); //使能串口的DMA发送
USART_DMACmd(USART2 , USART_DMAReq_Rx,ENABLE); //使能串口的DMA接收
}
void USART2_Begin_Send(void)
{
GPIO_USART2_RS485_SEND_enable();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
USART2_DMA_Begin_Send(MB_USART2.send_buffer , MB_USART2.sendCount);
}
void USART2_IRQHandler(void)
{
int16_t ch;
if (USART_GetITStatus(USART2,USART_IT_IDLE) != RESET)
{
USART_ClearITPendingBit(USART2 , USART_IT_IDLE); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
ch = USART_ReceiveData(USART2); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
DMA_Cmd(DMA1_Stream5 , DISABLE); //关闭DMA,防止处理其间有数据
DMA_ClearFlag(DMA1_Stream5 , DMA_FLAG_TCIF5 | DMA_FLAG_FEIF5 | DMA_FLAG_DMEIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_HTIF5);//清零标志位
ch = USART2_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA1_Stream5);
if (ch > 0)
{
{
MB_USART2.Outtime_mark = TRUE; //置标识,表示DMA接收通信数据完毕
MB_USART2.receCount = ch; //DMA接收通信数据长度
memcpy(MB_USART2.mscomm_buffer , USART2_DMA_RX_Buffer , MB_USART2.receCount);//从DMA接收缓冲区拷贝到解析报文缓冲区
}
}
DMA_SetCurrDataCounter(DMA1_Stream5 , USART2_DMA_RX_BUFFER_MAX_LENGTH);
DMA_Cmd(DMA1_Stream5, ENABLE);
}
else if (USART_GetITStatus(USART2,USART_IT_TC)!= RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_TC);
//DMA_Cmd(DMA1_Stream6 , DISABLE);//这条语句必须屏蔽,否则485通信时会出现异常情况,2018.10.18
DMA_ClearFlag(DMA1_Stream6 , DMA_FLAG_TCIF6 | DMA_FLAG_FEIF6 | DMA_FLAG_DMEIF6 | DMA_FLAG_TEIF6 | DMA_FLAG_HTIF6);
DMA_SetCurrDataCounter(DMA1_Stream6 , 0); //清除数据长度
GPIO_USART2_RS485_RECIVE_enable();
}
}
|
|