STM32 USART3外接RS485,出现回环现象,使用串口空闲中断与发送DMA中断
/*
*********************************************************************************************************
* 函 数 名: bsp_InitUart
* 功能说明: 初始化CPU的USART1串口硬件设备。未启用中断。
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_InitUart3_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 串口1 TX = PA9 RX = PA10 */
/* 第1步: 配置GPIO */
/* 打开 GPIO 时钟 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* 打开 UART 时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* 将 PB10 映射为 USART3_TX */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
/* 将 PB11 映射为 USART3_RX */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
/* 配置 USART Tx 为复用功能 */
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_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 配置 USART Rx 为复用功能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 第2步: 配置串口硬件参数 */
USART_InitStructure.USART_BaudRate = 4800; /* 波特率 */
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(USART3, &USART_InitStructure);
//中断配置
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
USART_ClearITPendingBit(USART3, USART_IT_IDLE);
//DMA功能开启
USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);
USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);//enable usart3 dma send request
USART_Cmd(USART3, ENABLE); /* 使能串口 */
/* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
如下语句解决第1个字节无法正确发送出去的问题 */
// USART_ClearFlag(USART3, USART_FLAG_TC); /* 清发送完成标志,Transmission Complete flag */
}
static void BSP_USART3_DMA_Init(void)
{
DMA_InitTypeDef dmaInit;
//复位DMA
DMA_DeInit(DMA1_Stream1);
DMA_DeInit(DMA1_Stream3);
//启动DMA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
//DMA发送通道Tx
dmaInit.DMA_Channel = DMA_Channel_4;
dmaInit.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);
dmaInit.DMA_Memory0BaseAddr = (uint32_t)g_Usart3SendBuf;
dmaInit.DMA_DIR = DMA_DIR_MemoryToPeripheral;
dmaInit.DMA_BufferSize = USART3_SEND_BUF_SIZE;
dmaInit.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dmaInit.DMA_MemoryInc = DMA_MemoryInc_Enable;
dmaInit.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dmaInit.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
dmaInit.DMA_Mode = DMA_Mode_Normal;
dmaInit.DMA_Priority = DMA_Priority_Medium;
dmaInit.DMA_FIFOMode = DMA_FIFOMode_Disable;
dmaInit.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
dmaInit.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dmaInit.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream3, &dmaInit);
//DMA发送通道Rx
dmaInit.DMA_Channel = DMA_Channel_4;
dmaInit.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);
dmaInit.DMA_Memory0BaseAddr = (uint32_t)g_Usart3RecvDatas;
dmaInit.DMA_DIR = DMA_DIR_PeripheralToMemory;
dmaInit.DMA_BufferSize = USART3_RECV_BUF_SIZE;
dmaInit.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dmaInit.DMA_MemoryInc = DMA_MemoryInc_Enable;
dmaInit.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dmaInit.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
dmaInit.DMA_Mode = DMA_Mode_Circular;
dmaInit.DMA_Priority = DMA_Priority_Medium;
dmaInit.DMA_FIFOMode = DMA_FIFOMode_Disable;
dmaInit.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
dmaInit.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dmaInit.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream1, &dmaInit);
//中断配置
DMA_ITConfig(DMA1_Stream3, DMA_IT_TC, ENABLE);
DMA_ClearITPendingBit(DMA1_Stream3, DMA_IT_TC);
DMA_Cmd(DMA1_Stream1, ENABLE);
DMA_Cmd(DMA1_Stream3, ENABLE);
}
void BSP_USART3_NVIC_Init(void)
{
NVIC_InitTypeDef nvic;
nvic.NVIC_IRQChannel = USART3_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0x02;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
#if 1
nvic.NVIC_IRQChannel = DMA1_Stream3_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0x01;
nvic.NVIC_IRQChannelSubPriority = 1;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
#endif
}
void bsp_InitUart3(void)
{
BSP_USART3_NVIC_Init();
bsp_InitUart3_GPIO();
BSP_USART3_DMA_Init();
}
void MYDMA3_Enable(u16 ndtr)
{
DMA_Cmd(DMA1_Stream3, DISABLE); //关闭DMA传输
while (DMA_GetCmdStatus(DMA1_Stream3) != DISABLE){} //确保DMA可以被设置
DMA_SetCurrDataCounter(DMA1_Stream3,ndtr); //数据传输量
DMA_Cmd(DMA1_Stream3, ENABLE); //开启DMA传输
}
/*<FUNC+>
******************************************************************************
* 函数名称: void Uart3_Send(unsigned char *src,unsigned char src_len)
* 功能描述: 串口3发送函数
* 输入参数: src:发送数据,src_len:长度
* 输出参数: 无
* 返 回 值: NULL
* 其它说明: 串口3发送驱动
* 修改日期 版本号 修改人 修改内容
* ------------------------------------------------------------------------------
* 2019-02-14 v1.0 dingyuan create
*
*******************************************************************************
<FUNC->*/
void Uart3_Send(unsigned char *src,unsigned char src_len)
{
unsigned char ii=0;
RS485_WRITE;
memcpy(g_Usart3SendBuf,src,src_len);
USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE); //使能串口3的DMA发送
MYDMA3_Enable(src_len+2);
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_IDLE))
{
uint32_t temp;
temp = temp;
temp = USART3->SR;
temp = USART3->DR;
DMA_Cmd(DMA1_Stream1, DISABLE);
//获取当前接收长度
g_Usart3RecvLen = USART3_RECV_BUF_SIZE - DMA_GetCurrDataCounter(DMA1_Stream1);
g_Usart3RecvDatas[g_Usart3RecvLen]=' ';
//memcpy(g_Usart3SendBuf)
//将DMA归零
DMA_SetCurrDataCounter(DMA1_Stream1, USART3_RECV_BUF_SIZE);
DMA_Cmd(DMA1_Stream1, ENABLE);
#if(1)
uint8_t i;
printf("uart8RecvDatas : ");
for(i = 0;i < 20;i++)
{
printf(" %02x ", g_Usart3RecvDatas[i]);
}
printf("%s
",g_Usart3RecvDatas);
memset(g_Usart3RecvDatas,0x00,20);
#endif
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>