stm32f407 DMA接收、发送问题

进行串口收发时,可以发送数据,但是DMA无法读取数据,配置看了好几遍,不知道哪里出现了问题,特意贴个代码,希望大家帮忙看看

#include "stm32f4xx.h"
#include "myconst.h"
#include "myfunction.h"
#include
#include "eeprom.h"
extern uint8_t uploaddata[10];
extern uint8_t usart1_dma_received[55];
extern uint8_t usart1_dma_flag;

//Óë´«¸ÐÐÅÏ¢°åͨÐŵĴ®¿Ú£ºPA9---TX; PA10---RX
//½ÓÊÕ´«¸Ð°å·¢À´µÄ59¸ö×Ö½Ú£º3A+A3+±ÚÕÏ2¸ö×Ö½Ú+µçÁ¿2¸ö×Ö½Ú+¹ßµ¼12¸ö×Ö½Ú+GPS39¸ö×Ö½Ú+Éî¶È2¸ö×Ö½Ú
//Ïò´«¸Ð°å·¢ËÍË®ÖÊÐÅÏ¢£º10¸ö×Ö½Ú£ºA4+phÖµ2¸ö×Ö½Ú+µçµ¼ÂÊ3¸ö×Ö½Ú+ζÈ2¸ö×Ö½Ú+0A+0D;
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;    
GPIO_Init(GPIOA, &GPIO_InitStructure);  

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;      

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;        

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  


GPIO_Init(GPIOA, &GPIO_InitStructure);       


GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);



USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);   

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_Cmd(USART1, ENABLE);

/*DMA·¢ËÍÅäÖÃ*/
DMA_DeInit(DMA2_Stream7);
DMA_StructInit( &DMA_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;             
DMA_InitStructure.DMA_PeripheralBaseAddr =USART1_DR_Base; 
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0;  
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;    
DMA_InitStructure.DMA_BufferSize = 8;                    
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_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(DMA2_Stream7, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream7, DMA_IT_TC, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_ClearITPendingBit(DMA2_Stream7, DMA_IT_TCIF7);

/*DMA ½ÓÊÕÊý¾ÝÉèÖÃ*/
DMA_DeInit(DMA2_Stream5);
DMA_StructInit(&DMA_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;             //Ñ¡ÔñChannel_5
DMA_InitStructure.DMA_PeripheralBaseAddr =USART1_DR_Base; //Êý¾Ý´«ÊäµÄÍâÉèÊ×µØÖ·£¬Ïê½â¼ûÉÏ
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(&usart1_dma_received[0]);  //×Ô¼º¶¨Òå´ý·¢ËÍÊý×éµÄÊ×µØÖ·£¬ÒªÇ¿ÖÆת»»Îª32λ
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;    //Êý¾Ý´«Êä·½ÏòÑ¡ÔñΪÄÚ´æ<-ÍâÉè
DMA_InitStructure.DMA_BufferSize =8;                      //´«ÊäÊý¾Ý´óСΪ8£¬µ¥Î»ÓÉÒÔÏÂÈ·¶¨£¬´óСҪÅäºÏ¶¨ÒåµÄÊý×éÀàÐͺÍÍâÉèÊý¾ÝÀàÐÍ
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //ÍâÉèµØÖ·¼Ä´æÆ÷×Ô¶¯Ôö¼Ó½ûÖ¹£¬ÒòΪÕâÀïÖ»Óõ½ÁËDRÊý¾Ý¼Ä´æÆ÷
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;    //ÄÚ´æµØÖ·×ÔÔöÔÊÐí£¬ÒòΪҪ¶ÁÈ¡Ò»¸öÊý×é
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //ÍâÉèµÄÊý¾Ý´óС£¬ÒòΪUSART6_DRÊý¾Ý¼Ä´æÆ÷Ϊ8Ϊ£¬¹ÊÑ¡Byte
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  //ÕâÀïҲѡByte
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;       //DMA´«ÊäģʽΪNormal£¬Èç¹ûΪCircular,½«»áÑ­»·´«Êä
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //ÓÅÏȼ¶ÎªHigh

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(DMA2_Stream5, &DMA_InitStructure);
//DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE);
DMA_ClearITPendingBit(DMA2_Stream5, DMA_IT_TCIF5);

USART_DMACmd(USART1,USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA2_Stream5, ENABLE);  

}

/*dma ½ÓÊÕÖжÏ*/
void DMA2_Stream5_IRQHandler(void)
{
if (DMA_GetITStatus(DMA2_Stream5, DMA_IT_TCIF5) != RESET)
{
DMA_Cmd(DMA2_Stream5, DISABLE);
DMA_ClearITPendingBit(DMA2_Stream5, DMA_IT_TCIF5);
USART_DMACmd(USART1, USART_DMAReq_Rx, DISABLE);

    usart1_dma_flag=1;

}
}

//USART1 DMA ·¢ËÍÖжÏ
void DMA2_Stream7_IRQHandler(void)
{
if (DMA_GetITStatus(DMA2_Stream7, DMA_IT_TCIF7) != RESET) //·¢ËÍÍê³É
{
DMA_ClearITPendingBit(DMA2_Stream7, DMA_IT_TCIF7);
USART_DMACmd(USART1, USART_DMAReq_Tx, DISABLE);
DMA_Cmd(DMA2_Stream7, DISABLE);
}
}

void UASRT1_DMA_Senddata(u32 par,u16 ndtr)
{
DMA_Cmd(DMA2_Stream7, DISABLE);
DMA2_Stream7->M0AR = (uint32_t)(par);
DMA2_Stream7->NDTR = ndtr;
DMA_Cmd(DMA2_Stream7, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
}

void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //ÅжÏΪ½ÓÊÕÖжÏ
{
// uint8_t Received;
// float angle_temp=0;
// Received = USART_ReceiveData(USART1);
// USART_SendData(USART1,Received);
// usart1_dma_flag=1;

    USART_ClearITPendingBit(USART1,USART_IT_RXNE);          

}

}

int main(void)
{ while(1)
if(usart1_dma_flag==1)//Êý¾Ý½ÓÊÜ
{
//.........

DMA_Cmd(DMA2_Stream5, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);

}

}

void NVIC_Config()
{

NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the USARTx Interrupt */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //////
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //////

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //////

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);        //ǶÌ×ÓÅÏȼ¶·Ö×éΪ 1
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream5_IRQn;   //ǶÌ×ͨµÀΪDMA2_Stream7_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÇÀÕ¼ÓÅÏȼ¶Îª 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;    //ÏìÓ¦ÓÅÏȼ¶Îª 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;     //ͨµÀÖжÏʹÄÜ
NVIC_Init(&NVIC_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);        //ǶÌ×ÓÅÏȼ¶·Ö×éΪ 1
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn;   //ǶÌ×ͨµÀΪDMA2_Stream7_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÇÀÕ¼ÓÅÏȼ¶Îª 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;    //ÏìÓ¦ÓÅÏȼ¶Îª 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;     //ͨµÀÖжÏʹÄÜ
NVIC_Init(&NVIC_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //////

NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);        //ǶÌ×ÓÅÏȼ¶·Ö×éΪ 1
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;   //ǶÌ×ͨµÀΪDMA2_Stream7_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÇÀÕ¼ÓÅÏȼ¶Îª 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;    //ÏìÓ¦ÓÅÏȼ¶Îª 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;     //ͨµÀÖжÏʹÄÜ
NVIC_Init(&NVIC_InitStructure);

//

// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // ÇÀռʽÓÅÏȼ¶±ð
// NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;//Ö¸¶¨ÖжÏÔ´
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;// Ö¸¶¨ÏìÓ¦ÓÅÏȼ¶±ð1
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
//

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // ÇÀռʽÓÅÏȼ¶±ð
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;//Ö¸¶¨ÖжÏÔ´
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;// Ö¸¶¨ÏìÓ¦ÓÅÏȼ¶±ð1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);   // ÇÀռʽÓÅÏȼ¶±ð 
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问