关于串口DMA接收问题

2019-10-16 00:33发布

今天在捣鼓串口DMA方式接收数据时,偶然发现自己没有对串口的IO口进行初始化,但是奇怪的是,从串口助手发送数据竟然能产生DMA中断,特此来发帖求助.#define UART_RX_LEN     6  //#define UART_TX_LEN     5
uint8_t Uart_Rx[UART_RX_LEN] = {0};  
//uint8_t Uart_Tx[UART_TX_LEN] = {0};

void DMA_IO_Init()
{  
                DMA_InitTypeDef DMA_InitStructure;
                NVIC_InitTypeDef NVIC_InitStructure;

        USART_InitTypeDef USART_InitStructure;


    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);  

    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;  
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;  
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
    NVIC_Init(&NVIC_InitStructure);  

//    DMA_DeInit(DMA1_Channel4);   
//    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);  
//    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Tx;  
//    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  
//    DMA_InitStructure.DMA_BufferSize = 100;  
//    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_PeripheralDataSize_Byte;  
//    //??DMA?????  
//    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
//    //??DMA?????  
//    DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
//    //??DMA?2?memory????????  
//    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  
//    DMA_Init(DMA1_Channel4,&DMA_InitStructure);  
//    DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);  


    DMA_DeInit(DMA1_Channel5);   //????  
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);  //????  
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Rx;  //dma??????  
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;  //??DMA??????????  
    DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;  //??DMA???????,????  
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  //??DMA???????  
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  //??????  
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  //??????  
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  //??DMA?????  
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  //??DMA?????  
    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;  //??DMA?2?memory????????  
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  
    DMA_Init(DMA1_Channel5,&DMA_InitStructure);  //????5
                DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);               
    DMA_Cmd(DMA1_Channel5,ENABLE);  

    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_BaudRate = 115200;   
    USART_Init(USART1,&USART_InitStructure);   
   // USART_ITConfig(USART1,USART_IT_TC,DISABLE);  
   // USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);  
  //  USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);   

//    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);  
//    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;               //???????1??   
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;       //??????0   
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;              //???????0   
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                 //????   
//    NVIC_Init(&NVIC_InitStructure);     

  //  USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);  
    USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);  
    USART_Cmd(USART1, ENABLE);   
        }

int main()
{

        DMA_IO_Init();
        LED_Init();
        LED0 = 0;
        while(1)
        {


        }
}       

void DMA1_Channel5_IRQHandler()
{
        if(SET ==  DMA_GetITStatus(DMA1_IT_TC5))
        {
                DMA_ClearITPendingBit(DMA1_IT_TC5);
                //i++;
                LED0 = !LED0;
        }
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。