本帖最后由 muyichuan2012 于 2021-5-24 11:31 编辑
请教 AT32F421 USART DMA 接收不定长数据 ,空闲中断正常,能正常进入,但设置的接收内存RxBuffer数据全为0,找不到原因,不知怎么回事?
代码如下:
-------------------------------------------------------------------------------------------------
void USART1_DMA(void)
{
DMA_InitType DMA_InitStructure;
DMA_Reset(DMA1_Channel3);
DMA_DefaultInitParaConfig(&DMA_InitStructure);
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40013804;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
DMA_InitStructure.DMA_Direction = DMA_DIR_PERIPHERALSRC;
DMA_InitStructure.DMA_BufferSize = LENGTH;
DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_ENABLE;
DMA_InitStructure.DMA_PeripheralDataWidth = DMA_PERIPHERALDATAWIDTH_BYTE;
DMA_InitStructure.DMA_MemoryDataWidth = DMA_MEMORYDATAWIDTH_BYTE;
DMA_InitStructure.DMA_Mode = DMA_MODE_NORMAL;
DMA_InitStructure.DMA_Priority = DMA_PRIORITY_VERYHIGH;
DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
}
void usart1_init( uint32_t BaudRate, uint32_t USART_Parity )
{
GPIO_InitType GPIO_InitStructure;
NVIC_InitType NVIC_InitStructure;
USART_InitType USART_InitStructure;
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd( RCC_AHBPERIPH_GPIOA, ENABLE );
/* Enable USART1 Clock */
RCC_APB2PeriphClockCmd( RCC_APB2PERIPH_USART1, ENABLE );
/* Connect PXx to USART1_Tx */
GPIO_PinAFConfig( GPIOA, GPIO_PinsSource9, GPIO_AF_1 );
/* Connect PXx to USART1_Rx */
GPIO_PinAFConfig( GPIOA, GPIO_PinsSource10, GPIO_AF_1 );
/* Configure USART1 Tx/Rx */
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_9 | GPIO_Pins_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
GPIO_Init( GPIOA, &GPIO_InitStructure );
USART_InitStructure.USART_BaudRate = BaudRate;
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;
/* Configure USARTx */
USART_Init( USART1, &USART_InitStructure );
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure );
USART1_DMA();
USART_Cmd(USART1, ENABLE);
USART_INTConfig(USART1, USART_INT_IDLEF, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
DMA_ChannelEnable(DMA1_Channel3, ENABLE);
}
void USART1_IRQHandler(void)
{
u8 i;
if(USART_GetITStatus(USART1, USART_INT_IDLEF) != RESET)
{
i =USART1->STS;
i =USART1->DT;
DMA_ChannelEnable(DMA1_Channel3, DISABLE); //关闭DMA
get_len = LENGTH - DMA_GetCurrDataCounter(DMA1_Channel3); //获取字符长度
DMA_SetCurrDataCounter(DMA1_Channel3,LENGTH); //设置DMA字符长度
DMA_ChannelEnable(DMA1_Channel3, ENABLE); //开启DMA
}
}
|