STM32Fxx之串口DMA配置

简介:

        串口高波特率通讯时需要用DMA传输数据。本文档串口DMA驱动采样stm32标准库版本。

1、时钟配置

//时钟配置
static void USART_RCC_Config(void)
{
    //使能DMA外设
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    
    //使能GPIO时钟
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    //使能串口时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    //使能系统时钟配置
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
}

2、串口GPIO配置

//串口GPIO配置
static void USART_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    //usart引脚配置
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

    //配置 Tx pins
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //配置Rx pins
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

3、初始化串口1

//初始化串口1
void USART1_Init(uint32_t baudrate)
{
    USART_InitTypeDef USART_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;

    //使能外设时钟
    USART_RCC_Config();
    
    //配置串口GPIO
    USART_GPIO_Config();
    
    //DMA映射串口的发送与接收
    DMA_RemapConfig(DMA1,DMA1_CH2_USART1_TX);
    DMA_RemapConfig(DMA1,DMA1_CH3_USART1_RX);

    USART_OverSampling8cmd(USART1, ENABLE);    //高波特率开启8bit过采样

    USART_InitStructure.USART_BaudRate = baudrate;
    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_Tx | USART_Mode_Rx;
    USART_Init(USART1, &USART_InitStructure);

    //DMA配置
    DMA_InitStructure.DMA_BufferSize = MAX_LEN;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoreDataSize_Byte;
    DMA_InitStructure.DMA_PeripheralInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    //DMA1 channel2配置
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)test_buffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
    DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_TDR_Address;
    DMA_Init(DMA1_Channel2, &DMA_InitStructure);

    //DMA1 channel3配置
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)test_buffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
    DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_RDR_Address;
    DMA_Init(DMA1_Channel3, &DMA_InitStructure);

    //clear DMA1 TC flag
    DMA_ClearFlag(DMA1_FLAG_TC2);
    DMA_ClearFlag(DMA1_FLAG_TC3);

    //使能串口发送接收请求
    USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
    
    //使能DMA通道
    DMA_Cmd(DMA1_Channel2, ENABLE);
    DMA_Cmd(DMA1_Channel3, ENABLE);

    //使能串口
    USART_Cmd(USART1, ENABLE);
}

4、串口DMA发送

void USART1_DMA_Send(uint8_t *buff, uint16_t len)
{
    //失能DMA1通道
    DMA_Cmd(DMA1_Channel2, DISABLE);

    //clear DMA1 TC flags
    DMA_ClearFlag(DAM1_FLAG_TC2);
    DMA_SetCurrDataCounter(DMA1_Channel2, len);
    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

    DMA_Cmd(DMA1_Channel2, ENABLE);

    while(RESET == DMA_GetFlagStatus(DMA1_FLAG_TC2)){}
}

5、串口DMA接收

void USART1_DMA_Receive(void)
{
    //失能DMA1通道
    DMA_Cmd(DMA1_Channel3, DISABLE);

    //clear DMA1 TC flags
    DMA_ClearFlag(DAM1_FLAG_TC3);
    DMA_SetCurrDataCounter(DMA1_Channel3, MAX_LEN);
    USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);

    DMA_Cmd(DMA1_Channel3, ENABLE);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值