前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >STM32F407-USART1+DMA收发

STM32F407-USART1+DMA收发

作者头像
用户11186929
发布2024-07-01 12:30:28
1400
发布2024-07-01 12:30:28
举报
文章被收录于专栏:技术干货

usart1.c

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


#define     G_DMA_RX_LEN 100
#define        G_DMA_TX_LEN 100
uint8_t     g_dma_rx_buff[G_DMA_RX_LEN];
uint8_t     g_dma_tx_buff[G_DMA_TX_LEN];
uint16_t     g_tx_buff_len = 0;


static void Usart1GpioInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(USART1_GPIO_CLK,ENABLE);                             //使能GPIOA时钟
    GPIO_InitStructure.GPIO_Pin = USART1_GPIO_PIN;                                 //GPIOA9与GPIOA10
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                                //复用功能
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                            //速度50MHz
    GPIO_Init(USART1_GPIO_PORT,&GPIO_InitStructure);                             //初始化PA9,PA10
    
    //串口1对应引脚复用映射
    GPIO_PinAFConfig(USART1_GPIO_PORT,USART1_TX_SOURCE,USART1_TX_AF);             //GPIOA9复用为USART1
    GPIO_PinAFConfig(USART1_GPIO_PORT,USART1_RX_SOURCE,USART1_RX_AF);             //GPIOA10复用为USART1
}

static void Usart1Config(void)
{
    USART_InitTypeDef USART_InitStructure;
    RCC_APB2PeriphClockCmd(USART1_CLK,ENABLE);                                    //使能USART1时钟
    //USART1 初始化设置
    USART_InitStructure.USART_BaudRate = USART1_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(USART1, &USART_InitStructure);                                     //初始化串口1    
}
static void Usart1NvicInit(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    //Usart1 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;                            //串口1中断通道
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;                        //抢占优先级1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;                            //子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);    
}

static void Usart1DmaRxInit(void)
{
    DMA_InitTypeDef  DMA_InitStructure;
    RCC_AHB1PeriphClockCmd(USART1_DMA_CLK,ENABLE);                                //DMA2时钟使能 
    DMA_DeInit(USART1_RX_DMA_STREAM);
    while (DMA_GetCmdStatus(USART1_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) (&(USART1->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 = USART1_RX_DMA_CHANNEL ;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;                               // DMA_Mode_Circular  DMA_Mode_Normal
    DMA_InitStructure.DMA_BufferSize = G_DMA_RX_LEN;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;
    DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)g_dma_rx_buff; 
    DMA_Init(USART1_RX_DMA_STREAM,&DMA_InitStructure);

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

    USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);

}
static void Usart1DmaTxInit(void)
{
    DMA_InitTypeDef  DMA_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);                            //DMA2时钟使能 
    DMA_DeInit(USART1_TX_DMA_STREAM);
    while (DMA_GetCmdStatus(USART1_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) (&(USART1->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 = USART1_RX_DMA_CHANNEL ;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                               // DMA_Mode_Circular  DMA_Mode_Normal
    DMA_InitStructure.DMA_BufferSize = G_DMA_TX_LEN;
    DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;
    DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)g_dma_tx_buff ;
    DMA_Init(USART1_TX_DMA_STREAM,&DMA_InitStructure);

    USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);

}
void Usart1Init(void)
{
    Usart1GpioInit();
    Usart1Config();
    Usart1NvicInit();
    Usart1DmaRxInit();
    Usart1DmaTxInit();
    
    USART_ClearITPendingBit(USART1, USART_IT_IDLE);                                //开启串口接受中断
    USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);                                //开启空闲中断
    USART_Cmd(USART1, ENABLE);                                                  //使能串口1 
}



void Usart1DmaSend(uint8_t *data_buf, uint16_t lengh)
{
    DMA_Cmd(USART1_TX_DMA_STREAM,DISABLE);
    Waiting_Disable_DMA(USART1_TX_DMA_STREAM);
    //DMA_ITConfig(USART1_TX_DMA_STREAM,DMA_IT_FE,ENABLE);
    
    //解析接收到的数据
    memset(g_dma_tx_buff, 0,G_DMA_TX_LEN );
    memcpy(g_dma_tx_buff,data_buf,lengh);
    
    /* Clear DMA Transfer Complete Flags */
    DMA_ClearFlag(USART1_TX_DMA_STREAM,DMA_FLAG_TCIF7);
    /* Clear USART Transfer Complete Flags */
    USART_ClearFlag(USART1,USART_FLAG_TC); 
    
    DMA_SetCurrDataCounter(USART1_TX_DMA_STREAM,lengh); 
    
    DMA_Cmd(USART1_TX_DMA_STREAM,ENABLE);    

}


void DMA2_Stream2_IRQHandler(void)
{
    uint16_t lengh=0;
    //清除标志
    if(DMA_GetFlagStatus(USART1_RX_DMA_STREAM,DMA_FLAG_TCIF2)!=RESET)//等待DMA2_Steam7传输完成 
    {
        DMA_Cmd(USART1_RX_DMA_STREAM, DISABLE); //关闭DMA,防止处理其间有数据   
        lengh = G_DMA_RX_LEN - DMA_GetCurrDataCounter(USART1_RX_DMA_STREAM);    
        DMA_ClearFlag(USART1_RX_DMA_STREAM,DMA_FLAG_TCIF2 | DMA_FLAG_FEIF2 | DMA_FLAG_DMEIF2 | DMA_FLAG_TEIF2 | DMA_FLAG_HTIF2);//清除DMA2_Steam7传输完成标志  
        DMA_SetCurrDataCounter(USART1_RX_DMA_STREAM, G_DMA_RX_LEN);
        
        memcpy(g_dma_tx_buff,g_dma_rx_buff,lengh);
        g_tx_buff_len = lengh;
        
        //发送数据
        Usart1DmaSend(g_dma_tx_buff,g_tx_buff_len);
        
        DMA_Cmd(USART1_RX_DMA_STREAM, ENABLE);     //打开DMA, 
    }
}
代码语言:javascript
复制
#ifndef __USART1_H
#define __USART1_H

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

//GPIO  
#define USART1_GPIO_PIN                    (GPIO_Pin_9 | GPIO_Pin_10)
#define USART1_GPIO_CLK                    RCC_AHB1Periph_GPIOA
#define USART1_GPIO_PORT                GPIOA
#define USART1_RX_AF                    GPIO_AF_USART1
#define USART1_RX_SOURCE                GPIO_PinSource10
#define USART1_TX_AF                    GPIO_AF_USART1
#define USART1_TX_SOURCE                GPIO_PinSource9

// USART1 
#define USART1_CLK                        RCC_APB2Periph_USART1
#define USART1_BAUDRATE                    115200

// DMA  
#define    USART1_DMA_CLK                    RCC_AHB1Periph_DMA2
#define USART1_RX_DMA_CHANNEL            DMA_Channel_4
#define USART1_RX_DMA_STREAM            DMA2_Stream2
#define USART1_TX_DMA_CHANNEL            DMA_Channel_4
#define USART1_TX_DMA_STREAM            DMA2_Stream7

void Usart1Init(void);
uint8_t Usart1IsReady(void);
void Usart1DmaSend(uint8_t *data_buf, uint16_t lengh);


#endif /* __USART1_H */

main.c

代码语言:javascript
复制
#include "stm32f4xx.h"
#include "systick.h"
#include "usart1.h"


int main()
{
    
    uint8_t data_Buffer[8]= {01,02,03,04,05,06,07};
    Usart1Init();
    
    //printf("That OK!\n");
    while(1)
    {
        
        Usart1DmaSend(data_Buffer,8);
        Delay_ms(500);
        Usart1IsReady();
        Delay_ms(500);                    
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档