打印

stm32f407的usart2之dma发送和接收,采用空闲中断。

[复制链接]
1153|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
keer_zu|  楼主 | 2022-1-6 07:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

采用dma的usart2

static char usart2_dma_tx[256];
unsigned char usart2_tx_busy_flag = 0;
static char usart2_dma_rx[1024];
unsigned char usart2_rx_busy_flag = 0;


static void usart2_gpio_init(void)  
{  
        GPIO_InitTypeDef GPIO_InitStructure;  

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
        GPIO_Init(GPIOD, &GPIO_InitStructure);

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);  
        GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);         
}

static void usart2_usart_init(unsigned int bauderate, unsigned short wordLength, unsigned short stopBits, unsigned short Parity)
{
        USART_InitTypeDef USART_InitStructure;  

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
       
        USART_InitStructure.USART_BaudRate = bauderate;
        USART_InitStructure.USART_WordLength = wordLength;  
        USART_InitStructure.USART_StopBits = stopBits;  
        USART_InitStructure.USART_Parity = Parity;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  

        USART_Init(USART2, &USART_InitStructure);  
        USART_Cmd(USART2, ENABLE);
        USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);

        //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
        USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
}

static void uart2_dmarx_config(uint8_t *mem_addr, uint32_t mem_size)
{
        DMA_InitTypeDef DMA_InitStructure;

        //串口收DMA配置  
        //启动DMA时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
        //DMA通道配置
        DMA_DeInit(DMA1_Stream5);
        DMA_InitStructure.DMA_Channel = DMA_Channel_4;
        //外设地址
        DMA_InitStructure.DMA_PeripheralBaseAddr = (unsigned int)(&USART2->DR);
        //内存地址
        DMA_InitStructure.DMA_Memory0BaseAddr = (unsigned int)mem_addr;
        //dma传输方向
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
        //设置DMA在传输时缓冲区的长度
        DMA_InitStructure.DMA_BufferSize = mem_size;
        //设置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;
       
        //指定如果FIFO模式或直接模式将用于指定的流 : 不使能FIFO模式  
        DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;   
        //指定了FIFO阈值水平
        DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;        
        //指定的Burst转移配置内存传输
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;      
        //指定的Burst转移配置外围转移 */  
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
       
        //配置DMA1的通道         
        DMA_Init(DMA1_Stream5, &DMA_InitStructure);  
        //使能通道
        DMA_Cmd(DMA1_Stream5,ENABLE);
       

       
        USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);

}

static void usart2_dma_init(void)  
{  
        DMA_InitTypeDef  DMA_InitStructure;  

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
       
        DMA_DeInit(DMA1_Stream6);               
        DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;   
        DMA_InitStructure.DMA_BufferSize = 256;   
        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_1QuarterFull;  
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;        
        DMA_InitStructure.DMA_Channel = DMA_Channel_4;
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)usart2_dma_tx;        

        DMA_Init(DMA1_Stream6, &DMA_InitStructure);                
        DMA_ITConfig(DMA1_Stream6, DMA_IT_TC, ENABLE);

        uart2_dmarx_config(usart2_dma_rx,1024);

}

static void usart2_nvic_init(void)  
{  
        NVIC_InitTypeDef NVIC_InitStructure;   

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;  
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  

        NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream6_IRQn;
        NVIC_Init(&NVIC_InitStructure);

        NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
        NVIC_Init(&NVIC_InitStructure);

        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_Init(&NVIC_InitStructure);       
}  

void usart2_init(unsigned int bauderate, unsigned short wordLength, unsigned short stopBits, unsigned short Parity)
{
        usart2_nvic_init();
        usart2_gpio_init();
        usart2_usart_init(bauderate, wordLength, stopBits, Parity);
        usart2_dma_init();
       
}

void usart2_send(char *buf, unsigned char len)
{
        while(usart2_tx_busy_flag)        delay_ms(2);
        usart2_tx_busy_flag = 1;
        printf("== dma tx\n");
        memcpy(usart2_dma_tx, buf, len);
        DMA_SetCurrDataCounter(DMA1_Stream6, len);
        DMA_Cmd(DMA1_Stream6, ENABLE);               
        USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
}

/*****************USART2  TX******************/
void DMA1_Stream6_IRQHandler(void)  
{
        printf("== %d dma 1-6\n",__LINE__);

        /* Test on DMA Stream Transfer Complete interrupt */  
        if (DMA_GetITStatus(DMA1_Stream6, DMA_IT_TCIF6))  
        {  
                /* Clear DMA Stream Transfer Complete interrupt pending bit */  
                DMA_ClearITPendingBit(DMA1_Stream6, DMA_IT_TCIF6);
                USART_DMACmd(USART2, USART_DMAReq_Tx, DISABLE);        
                DMA_Cmd(DMA1_Stream6, DISABLE);

                usart2_tx_busy_flag = 0;                
        }
}

/*****************USART2  RX******************/
void DMA1_Stream5_IRQHandler(void)  
{
        printf("== %d dma rx\n",__LINE__);
        /* Test on DMA Stream Transfer Complete interrupt */  
        if (DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF6))  
        {  
                printf("== %d dma rx\n",__LINE__);
                /* Clear DMA Stream Transfer Complete interrupt pending bit */  
                DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5);
                USART_DMACmd(USART2, USART_DMAReq_Rx, DISABLE);        
                DMA_Cmd(DMA1_Stream5, DISABLE);

                usart2_rx_busy_flag = 0;                
        }
}


/*****************USART2  RX******************/
void USART2_IRQHandler(void)
{       
       
        unsigned char ch;
        unsigned int temp;
        int i;
        unsigned short l;
        /* USART in mode Sender   --------------------------------------------------*/
        if(USART_GetITStatus(USART2, USART_IT_TXE) == SET)
        {
                printf("== usart tx\n");
                USART_ClearITPendingBit(USART2, USART_IT_TXE);
        }
       
        /* USART in mode Receiver --------------------------------------------------*/
        //if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
        //{
        //        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
        //        ch = USART_ReceiveData(USART2);               
        //        printf("-%c",ch);
        //}
        if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET) {
                USART2->SR;
            USART2->DR; //清USART_IT_IDLE标志
            DMA_Cmd(DMA1_Stream5,DISABLE);
                DMA_ClearFlag(DMA1_Stream5,DMA_FLAG_TCIF5);
               
                l = DMA_GetCurrDataCounter(DMA1_Stream5);
                printf("++ rx:%d\n",l);
                temp = 1024 - l;//DMA_GetCurrDataCounter(DMA1_Stream5);
                for (i = 0;i < temp;i++)
                {
                         printf("%c",usart2_dma_rx[i]);
                }

                //设置传输数据长度
                DMA_SetCurrDataCounter(DMA1_Stream5,1024);
            //打开DMA
                DMA_Cmd(DMA1_Stream5,ENABLE);
        }
}


使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1349

主题

12425

帖子

53

粉丝