BSP|gd407替换st207采用st库-串口空闲中断+dma收发

【BSP|gd407替换st207采用st库-串口空闲中断+dma收发】st采用串口3-dma收发,串口空闲中断,用来读写计量芯片RN8209D
GPIO配置:

void GPIO_CFG(COM_TypeDef Com) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd( COM_TX_PORT_CLK[Com] | COM_RX_PORT_CLK[Com], ENABLE); GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[Com]; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(COM_TX_PORT[Com], &GPIO_InitStructure); GPIO_PinAFConfig(COM_TX_PORT[Com],COM_TX_AF_PIN[Com],COM_AF_NAME[Com]); GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[Com]; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(COM_RX_PORT[Com], &GPIO_InitStructure); GPIO_PinAFConfig(COM_RX_PORT[Com],COM_RX_AF_PIN[Com],COM_AF_NAME[Com]); }

串口3配置
USART_DeInit(COM_USART[Com]); //使能8倍过采样:配置8倍过采样后,对于波特率偏低的要求会低一些 USART3->CR1 |= 0x8000; //USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = 4800; USART_InitStructure.USART_WordLength = USART_WordLength_9b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_Even; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(COM_USART[Com],&USART_InitStructure); USART_ITConfig(COM_USART[Com],USART_IT_IDLE,ENABLE); USART_ITConfig(COM_USART[Com],USART_IT_TC,ENABLE); USART_DMACmd(COM_USART[Com],USART_DMAReq_Tx, ENABLE); USART_DMACmd(COM_USART[Com],USART_DMAReq_Rx, ENABLE); USART_Cmd(COM_USART[Com],ENABLE);

dma配置
#define COM3_FRAM_MAN_SIZE(50) u8 Com3DmaSendBuf[COM3_FRAM_MAN_SIZE]; u8 Com3DmaRecBuf[COM3_FRAM_MAN_SIZE]; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE); DMA_DeInit(DMA1_Stream3); DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道4 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR; //数据寄存器 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Com3DmaSendBuf; //内存地址 DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //外设地址 DMA_InitStructure.DMA_BufferSize = COM3_FRAM_MAN_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_InitStructure.DMA_Priority = DMA_Priority_High; //DMA等级高度 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不使用FIFO DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO溢出个数 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //单次突发模式 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //单次突发模式 DMA_Init(DMA1_Stream3,&DMA_InitStructure); // dma发送 DMA_DeInit(DMA1_Stream1); DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道4 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR; //数据寄存器 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Com3DmaRecBuf; //内存地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //外设地址 DMA_InitStructure.DMA_BufferSize = COM3_FRAM_MAN_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_InitStructure.DMA_Priority = DMA_Priority_High; //DMA等级高度 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不使用FIFO DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO溢出个数 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //单次突发模式 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //单次突发模式 DMA_Init(DMA1_Stream1,&DMA_InitStructure); // dma接收 DMA_Cmd(DMA1_Stream3,DISABLE); DMA_Cmd(DMA1_Stream1,ENABLE);

串口发送
void BSP_Com3_OpenTxInt(uint16_t DateLen) { DMA_SetCurrDataCounter(DMA1_Stream3,DateLen); DMA_Cmd(DMA1_Stream3 , ENABLE); }

串口中断接口
void USART3_IRQHandler(void) { if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) { USART_ClearITPendingBit(USART3, USART_IT_IDLE); /* 清除空闲中断标志位 */ USART_ReceiveData(USART3); /* 清除接收完成标志位 */ //u16_DmaClearTemp = USART3->SR; //u16_DmaClearTemp = USART3->DR; BSP_Com3RecvFcb(); }if(USART_GetITStatus(USART3, USART_IT_TC) != RESET) { USART_ClearITPendingBit(USART3, USART_IT_TC); ComDmaSendBusyflag[COM3] = 0; iRqSend++; } }voidBSP_Com3RecvFcb(void) { uint16_t Len; DMA_Cmd(DMA1_Stream1, DISABLE); DMA_ClearITPendingBit(DMA1_Stream1,DMA_IT_TCIF1); DMA_ClearITPendingBit(DMA1_Stream1,DMA_IT_HTIF1); Len=COM3_FRAM_MAN_SIZE-DMA_GetCurrDataCounter(DMA1_Stream1); if((0 < Len) && (Len<=COM3_FRAM_MAN_SIZE)) { Dat_enUnFixQueueNode(qHandleCom3Rx,Com3DmaRecBuf,Len); } memset(Com3DmaRecBuf, 0, COM3_FRAM_MAN_SIZE); DMA_SetCurrDataCounter(DMA1_Stream1,COM3_FRAM_MAN_SIZE); DMA_Cmd(DMA1_Stream1, ENABLE); }

目前存在问题:dma接收数据-长度跟内容都不对。
解决:调试跟踪后,使能8倍过采样,配置8倍过采样后,对于波特率偏低的要求会低一些。之后收发正常。

    推荐阅读