望大家帮我看看:DMA+USART1时,接收没问题,为什么发送老是掉倒数第二字节,倒数第一字节总是传在第一个字节上来了?具体配置如下:
USART配置
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 128000;
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_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USART1, &USART_InitStructure);
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, DISABLE);
DMA配置
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (UINT32)&(USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (UINT32)SoundDataBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 512;
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_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (UINT32)&(USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SoundDataBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 512;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
在主程序中启动第一次,然后在结束中断中将发送与接收交替开启:
void DMA1_Channel4_IRQHandler(void)
{
DMA1->IFCR = DMA1_FLAG_TC4;
DMA1_Channel4->CCR &= ((u32)0xFFFFFFFE);
*((PUINT32)0x40020064) = (UINT32)SoundDataBuffer;
DMA1_Channel5->CNDTR = 512;
DMA1_Channel5->CCR |= ((u32)0x00000001);
*((PUINT32)0x4001380c) = (UINT32)0x00002004;
}
/*******************************************************************************
* Function Name : DMA1_Channel5_IRQHandler
* Description : This function handles DMA Stream 5 interrupt request.
* Input : None
* Return : None
* Created By: 何宣见(hexuanjian2005@126.com)
* Created date: 2011.02.11
*-------------------------------------------------------------------------------
*******************************************************************************/
void DMA1_Channel5_IRQHandler(void)
{
DMA1->IFCR = DMA1_FLAG_TC5;
DMA1_Channel5->CCR &= ((u32)0xFFFFFFFE);
*((PUINT32)0x40020064) = (UINT32)SoundDataBuffer;
DMA1_Channel4->CNDTR = 512;
DMA1_Channel4->CCR |= ((u32)0x00000001);
*((PUINT32)0x4001380c) = (UINT32)0x00002008;
}
|