STM32F407-USART2+DMA空闲中断收发

2024-07-01 12:31:00 浏览数 (1)

usart2.c

代码语言:javascript复制
#include "usart2.h"



#define     G_DMA_RX2_LEN 100
#define        G_DMA_TX2_LEN 100
uint8_t     g_dma_rx2_buff[G_DMA_RX2_LEN];
uint8_t     g_dma_tx2_buff[G_DMA_TX2_LEN];
uint16_t     g_tx2_buff_len = 0;



static void Usart2GpioInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(USART2_GPIO_CLK,ENABLE);                             //使能GPIOA时钟
    GPIO_InitStructure.GPIO_Pin = USART2_GPIO_PIN;                                 //GPIOD5与GPIOD6
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                                //复用功能
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                            //速度50MHz
    GPIO_Init(USART2_GPIO_PORT,&GPIO_InitStructure);                             //初始化PD5/PD6
    
    //串口2对应引脚复用映射
    GPIO_PinAFConfig(USART2_GPIO_PORT,USART2_TX_SOURCE,USART2_TX_AF);             //GPIOD5复用为USART2
    GPIO_PinAFConfig(USART2_GPIO_PORT,USART2_RX_SOURCE,USART2_RX_AF);             //GPIOD6复用为USART2
}

static void Usart2Config(void)
{
    USART_InitTypeDef USART_InitStructure;
    RCC_APB1PeriphClockCmd(USART2_CLK,ENABLE);                                    //使能USART2时钟
    //USART2 初始化设置
    USART_InitStructure.USART_BaudRate = USART2_BAUDRATE;                        //波特率设置
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;                    //字长为8位数据格式
    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_Init(USART2, &USART_InitStructure);                                     //初始化串口2    
}
static void Usart2NvicInit(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    //Usart2 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;                            //串口2中断通道
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;                        //抢占优先级1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;                            //子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                                                //根据指定的参数初始化VIC寄存器
}

static void Usart2DmaRxInit(void)
{
    DMA_InitTypeDef  DMA_InitStructure;
    RCC_AHB1PeriphClockCmd(USART2_DMA_CLK,ENABLE);                                //DMA1时钟使能 
    DMA_DeInit(USART2_RX_DMA_STREAM);
    while (DMA_GetCmdStatus(USART2_RX_DMA_STREAM) != DISABLE){}                    //等待DMA可配置 

    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full ;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;                 //存储器突发单次传输
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    //  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;   // DMA_Mode_Circular  DMA_Mode_Normal
    DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(USART2->DR)) ;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;

    /* Configure RX DMA */
    DMA_InitStructure.DMA_Channel = USART2_RX_DMA_CHANNEL ;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;                               // DMA_Mode_Circular  DMA_Mode_Normal
    DMA_InitStructure.DMA_BufferSize = G_DMA_RX2_LEN;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;
    DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)g_dma_rx2_buff; 
    DMA_Init(USART2_RX_DMA_STREAM,&DMA_InitStructure);

    DMA_Cmd(USART2_RX_DMA_STREAM,ENABLE);                                          //开启接收

    USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);

}
static void Usart2DmaTxInit(void)
{
    DMA_InitTypeDef  DMA_InitStructure;
    RCC_AHB1PeriphClockCmd(USART2_DMA_CLK,ENABLE);                                //DMA1时钟使能 
    DMA_DeInit(USART2_TX_DMA_STREAM);
    while (DMA_GetCmdStatus(USART2_TX_DMA_STREAM) != DISABLE){}                    //等待DMA可配置 

    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full ;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;                 //存储器突发单次传输
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    //  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;   // DMA_Mode_Circular  DMA_Mode_Normal
    DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(USART2->DR)) ;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
    /* Configure TX DMA */
    DMA_InitStructure.DMA_Channel = USART2_RX_DMA_CHANNEL ;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                               // DMA_Mode_Circular  DMA_Mode_Normal
    DMA_InitStructure.DMA_BufferSize = G_DMA_TX2_LEN;
    DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;
    DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)g_dma_tx2_buff ;
    DMA_Init(USART2_TX_DMA_STREAM,&DMA_InitStructure);

    USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);

}
void Usart2Init(void)
{
    Usart2GpioInit();
    Usart2Config();
    Usart2NvicInit();
    Usart2DmaRxInit();
    Usart2DmaTxInit();
    
    USART_ClearITPendingBit(USART2, USART_IT_IDLE);                                //开启串口接受中断
    USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);                                //开启空闲中断
    USART_Cmd(USART2, ENABLE);                                                      //使能串口2 
}



void Usart2DmaSend(uint8_t *data_buf, uint16_t lengh)
{
    DMA_Cmd(USART2_TX_DMA_STREAM,DISABLE);
    Waiting_Disable_DMA(USART2_TX_DMA_STREAM);
    //DMA_ITConfig(USART1_TX_DMA_STREAM,DMA_IT_FE,ENABLE);
    
    //解析接收到的数据
    memset(g_dma_tx2_buff, 0,G_DMA_TX2_LEN );
    memcpy(g_dma_tx2_buff,data_buf,lengh);
    
    /* Clear DMA Transfer Complete Flags */
    DMA_ClearFlag(USART2_TX_DMA_STREAM,DMA_FLAG_TCIF6);
    /* Clear USART Transfer Complete Flags */
    USART_ClearFlag(USART2,USART_FLAG_TC); 
    
    DMA_SetCurrDataCounter(USART2_TX_DMA_STREAM,lengh); 
    
    DMA_Cmd(USART2_TX_DMA_STREAM,ENABLE);    

}

void USART2_IRQHandler(void)
{
    uint16_t lengh=0; 
    if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)                          //接收中断
    {
        USART_ClearITPendingBit(USART2, USART_IT_IDLE);                            //清除空闲中断标志位(无效,持续进中断)
        USART_ReceiveData(USART2);                                               //清除空闲中断标志位
        DMA_Cmd(USART2_RX_DMA_STREAM,DISABLE);
        lengh= G_DMA_RX2_LEN - DMA_GetCurrDataCounter(USART2_RX_DMA_STREAM);    //获得传输的数据个数
        DMA_SetCurrDataCounter(USART2_RX_DMA_STREAM, G_DMA_RX2_LEN);
        
        //解析接收到的数据
        memcpy(g_dma_tx2_buff,g_dma_rx2_buff,lengh);
        g_tx2_buff_len = lengh;
        
        //发送数据
        Usart2DmaSend(g_dma_tx2_buff,g_tx2_buff_len);
        
        DMA_Cmd(USART2_RX_DMA_STREAM,ENABLE);        
        memset(g_dma_rx2_buff, 0, G_DMA_RX2_LEN);    //清空接受区
        memset(g_dma_tx2_buff, 0, G_DMA_TX2_LEN);    //清空发送区
        g_tx2_buff_len = 0;
    }


}

usart2.h

代码语言:javascript复制
#ifndef __USART2_H
#define __USART2_H

#include "stm32f4xx.h"
#include <stdio.h>
#include <string.h>

//GPIO  
#define USART2_GPIO_PIN                    (GPIO_Pin_5| GPIO_Pin_6)
#define USART2_GPIO_CLK                    RCC_AHB1Periph_GPIOD
#define USART2_GPIO_PORT                GPIOD
#define USART2_RX_AF                    GPIO_AF_USART2
#define USART2_RX_SOURCE                GPIO_PinSource6
#define USART2_TX_AF                    GPIO_AF_USART2
#define USART2_TX_SOURCE                GPIO_PinSource5

// USART2 
#define USART2_CLK                        RCC_APB1Periph_USART2
#define USART2_BAUDRATE                    115200

// DMA  
#define    USART2_DMA_CLK                    RCC_AHB1Periph_DMA1
#define USART2_RX_DMA_CHANNEL            DMA_Channel_4
#define USART2_RX_DMA_STREAM            DMA1_Stream5
#define USART2_TX_DMA_CHANNEL            DMA_Channel_4
#define USART2_TX_DMA_STREAM            DMA1_Stream6

void Usart2Init(void);
uint8_t Usart2IsReady(void);
void Usart2DmaSend(uint8_t *data_buf, uint16_t lengh);



#endif /* __USART2_H */

main.c

代码语言:javascript复制
#include "stm32f4xx.h"
#include "systick.h"
#include "usart2.h"
 
 
int main()
{
    
    uint8_t data_Buffer[8]= {01,02,03,04,05,06,07};
    Usart2Init();
    
    //printf("That OK!n");
    while(1)
    {
        
        Usart2DmaSend(data_Buffer,8);
        Delay_ms(500);
        Usart2IsReady();
        Delay_ms(500);                    
    }
 
}

0 人点赞