新手入门
- 积分
- 8
- 金钱
- 8
- 注册时间
- 2016-6-29
- 在线时间
- 0 小时
|
1金钱
各位好: 我使用STM32F411的平台,USART1的接收由DMA控制,每次上电后,接收的第一包数据的前面多了一个没用的字符‘\0’,如下图:
求救:哪位有遇到过相关的问题?
DMA的配置:
/*******************************************************************************
* Function Name : GKI_dma_UartConfig
* Description : Configure DMA for Uart
*******************************************************************************/
void GKI_dma_UartConfig(u16 u)
{
static uint16 StatusRegister, DataRegister;
uint32 DMA_RX_IT, DMA_TX_IT;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
ENTER_CRITICAL();
// Vector interrupts to use
DMA_TX_IT = DMA_IT_TC;
DMA_RX_IT = (u==eTM_MainUart) ?
(DMA_IT_TC | DMA_IT_TE | DMA_IT_HT) : (DMA_IT_TC | DMA_IT_HT);
// Setup Interrupt Vectors for USART Tx DMA
DMA_ITConfig(TxDmaStream, DMA_TX_IT, DISABLE);
DMA_ClearITPendingBit(TxDmaStream, TxITPending);
NVIC_InitStructure.NVIC_IRQChannel = DmaTxIRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pUart->IRQPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(TxDmaStream, DMA_TX_IT, ENABLE);
// If Rx DMA is available...
if (DMA_RX_IT)
{
// DMA Rx interrupt vector
NVIC_InitStructure.NVIC_IRQChannel = DmaRxIRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pUart->IRQPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Setup Rx DMA
DMA_Cmd(RxDmaStream, DISABLE);
while(!(DMA_GetCmdStatus(RxDmaStream) ==DISABLE));
DMA_ClearFlag(RxDmaStream,DmaRxPendingFlags);
//DMA_DeInit(RxDmaStream);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32)&UART->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32)(TM_UartDev.Rx.DmaBuffer); // RX.DmaBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = TM_UartDev.Rx.DmaSize;
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_Circular;
//DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
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(RxDmaStream, &DMA_InitStructure);
// Turn on Vectors for RX DMA
DMA_ITConfig(RxDmaStream, DMA_RX_IT, ENABLE);
DMA_Cmd(RxDmaStream, ENABLE);
}
RX.DmaPnt = 0;
// Enable USART DMA for Tx & Rx
USART_DMACmd(UART, USART_DMAReq_Tx, ENABLE);
USART_DMACmd(UART, USART_DMAReq_Rx, ENABLE);
// Enable IDLE interrupts
NVIC_InitStructure.NVIC_IRQChannel = RxIRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pUart->IRQPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART, USART_IT_IDLE, ENABLE);
// Clear DMA Flags
DMA_ClearFlag(TxDmaStream, DmaRxPendingFlags);
DMA_ClearFlag(RxDmaStream, DmaRxPendingFlags);
// Clear status flags
StatusRegister = UART->SR;
DataRegister = UART->DR;
EXIT_CRITICAL();
}
UART的配置:
/*******************************************************************************
* Function Name : GKI_uart_Init
* Description : Initialize the uart hardware
*******************************************************************************/
void GKI_uart_Init(u16 u, tGKI_uart_Init *GKI_uart_InitStruct)
{
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = GKI_uart_InitStruct->USART_BaudRate;
USART_InitStruct.USART_WordLength = GKI_uart_InitStruct->USART_WordLength;
USART_InitStruct.USART_StopBits = GKI_uart_InitStruct->USART_StopBits;
USART_InitStruct.USART_Parity = GKI_uart_InitStruct->USART_Parity;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = GKI_uart_InitStruct->USART_HardwareFlowControl;
USART_Init(UART, &USART_InitStruct);
}
Uart的IO配置:
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = _MASK(IO_MAIN_RX);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(_BASE(IO_MAIN_RX), &GPIO_InitStructure);
//GPIO_Init(GPIOA, &GPIO_InitStructure);
#if defined(STM32F2xx) || defined(STM32F4xx)
GPIO_PinAFConfig(_BASE(IO_MAIN_TX), _PIN(IO_MAIN_TX), GPIO_AF_USART1);
GPIO_PinAFConfig(_BASE(IO_MAIN_RX), _PIN(IO_MAIN_RX), GPIO_AF_USART1);
GPIO_PinAFConfig(_BASE(IO_MAIN_CTS), _PIN(IO_MAIN_CTS), GPIO_AF_USART1);
GPIO_PinAFConfig(_BASE(IO_MAIN_RTS), _PIN(IO_MAIN_RTS), GPIO_AF_USART1);
#endif
GKI_io_Config(IO_MAIN_RX, GKI_IO_AF_IN);
GKI_io_Config(IO_MAIN_TX, GKI_IO_AF_PP);
GKI_io_Config(IO_MAIN_CTS, GKI_IO_AF_IN);//GKI_IO_IPD);
GKI_io_Config(IO_MAIN_RTS, GKI_IO_OUT_PP);//GKI_IO_OUT_PP);
GKI_io_WriteBit(IO_MAIN_RTS, 1);
GKI_io_WriteBit(IO_MAIN_RTS, 0);
因为代码很多,我只截取了关键点。
|
最佳答案
查看完整内容[请看2#楼]
串口初始化完,先清一下TC标志就好了
USART_InitStructure.USART_BaudRate = bound;
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_ ...
|