👇 星标「嵌入式大杂烩」,一起进步!👇
来源:https://blog.csdn.net/qq_20553613
直接存储器访问(Direct Memory Access),简称DMA。DMA是CPU一个用于数据从一个地址空间到另一地址空间“搬运”(拷贝)的组件,数据拷贝过程不需CPU干预,数据拷贝结束则通知CPU处理。因此,大量数据拷贝时,使用DMA可以释放CPU资源。DMA数据拷贝过程,典型的有:
串口(uart)是一种低速的串行异步通信,适用于低速通信场景,通常使用的波特率小于或等于115200bps。对于小于或者等于115200bps波特率的,而且数据量不大的通信场景,一般没必要使用DMA,或者说使用DMA并未能充分发挥出DMA的作用。
对于数量大,或者波特率提高时,必须使用DMA以释放CPU资源,因为高波特率可能带来这样的问题:
因此,高波特率场景下,串口非常有必要使用DMA。
关于STM32串口使用DMA,不乏一些开发板例程及网络上一些博主的使用教程。使用步骤、流程、配置基本大同小异,正确性也没什么毛病,但都是一些基本的Demo例子,作为学习过程没问题;实际项目使用缺乏严谨性,数据量大时可能导致数据异常。
测试平台:
关键步骤
【1】初始化串口
【2】使能串口DMA接收模式,使能串口空闲中断
【3】配置DMA参数,使能DMA通道buf半满(传输一半数据)中断、buf溢满(传输数据完成)中断
为什么需要使用DMA 通道buf半满中断?
很多串口DMA模式接收的教程、例子,基本是使用了“空间中断”+“DMA传输完成中断”来接收数据。实质上这是存在风险的,当DMA传输数据完成,CPU介入开始拷贝DMA通道buf数据,如果此时串口继续有数据进来,DMA继续搬运数据到buf,就有可能将数据覆盖,因为DMA数据搬运是不受CPU控制的,即使你关闭了CPU中断。
严谨的做法需要做双buf,CPU和DMA各自一块内存交替访问,即是"乒乓缓存” ,处理流程步骤应该是这样:
【1】第一步,DMA先将数据搬运到buf1,搬运完成通知CPU来拷贝buf1数据 【2】第二步,DMA将数据搬运到buf2,与CPU拷贝buf1数据不会冲突 【3】第三步,buf2数据搬运完成,通知CPU来拷贝buf2数据 【4】执行完第三步,DMA返回执行第一步,一直循环
STM32F0系列DMA不支持双缓存(以具体型号为准)机制,但提供了一个buf"半满中断"
,即是数据搬运到buf大小的一半时,可以产生一个中断信号。基于这个机制,我们可以实现双缓存功能,只需将buf空间开辟大一点即可。
【1】第一步,DMA将数据搬运完成buf的前一半时,产生“半满中断”,CPU来拷贝buf前半部分数据 【2】第二步,DMA继续将数据搬运到buf的后半部分,与CPU拷贝buf前半部数据不会冲突 【3】第三步,buf后半部分数据搬运完成,触发“溢满中断”,CPU来拷贝buf后半部分数据 【4】执行完第三步,DMA返回执行第一步,一直循环
UART2 DMA模式接收配置代码如下,与其他外设使用DMA的配置基本一致,留意关键配置:
void bsp_uart2_dmarx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->RDR);/* UART2接收数据地址 */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr; /* 接收buf */
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /* 传输方向:外设->内存 */
DMA_InitStructure.DMA_BufferSize = mem_size; /* 接收buf大小 */
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_Circular; /* 连续模式 */
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC|DMA_IT_HT|DMA_IT_TE, ENABLE);/* 使能DMA半满、溢满、错误中断 */
DMA_ClearFlag(DMA1_IT_TC5); /* 清除相关状态标识 */
DMA_ClearFlag(DMA1_IT_HT5);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
DMA 错误中断
“DMA_IT_TE”
,一般用于前期调试使用,用于检查DMA出现错误的次数,发布软件可以不使能该中断。
基于上述描述机制,DMA方式接收串口数据,有三种中断场景需要CPU去将buf数据拷贝到fifo中,分别是:
前两者场景,前面文章已经描述。串口空闲中断指的是,数据传输完成后,串口监测到一段时间内没有数据进来,则触发产生的中断信号。
数据传输过程是随机的,数据大小也是不定的,存在几类情况:
因此,我们需根据“DMA通道buf大小”、“DMA通道buf剩余空间大小”、“上一次接收的总数据大小”
来计算当前接收的数据大小。
/* 获取DMA通道接收buf剩余空间大小 */
uint16_t DMA_GetCurrDataCounter(DMA_Channel_TypeDef* DMAy_Channelx);
DMA通道buf溢满场景计算
接收数据大小 = DMA通道buf大小 - 上一次接收的总数据大小
DMA通道buf溢满中断处理函数:
void uart_dmarx_done_isr(uint8_t uart_id)
{
uint16_t recv_size;
recv_size = s_uart_dev[uart_id].dmarx_buf_size - s_uart_dev[uart_id].last_dmarx_size;
fifo_write(&s_uart_dev[uart_id].rx_fifo,
(const uint8_t *)&(s_uart_dev[uart_id].dmarx_buf[s_uart_dev[uart_id].last_dmarx_size]), recv_size);
s_uart_dev[uart_id].last_dmarx_size = 0;
}
DMA通道buf半满场景计算
接收数据大小 = DMA通道接收总数据大小 - 上一次接收的总数据大小
DMA通道接收总数据大小 = DMA通道buf大小 - DMA通道buf剩余空间大小
DMA通道buf半满中断处理函数:
void uart_dmarx_half_done_isr(uint8_t uart_id)
{
uint16_t recv_total_size;
uint16_t recv_size;
if(uart_id == 0)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart1_get_dmarx_buf_remain_size();
}
else if (uart_id == 1)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart2_get_dmarx_buf_remain_size();
}
recv_size = recv_total_size - s_uart_dev[uart_id].last_dmarx_size;
fifo_write(&s_uart_dev[uart_id].rx_fifo,
(const uint8_t *)&(s_uart_dev[uart_id].dmarx_buf[s_uart_dev[uart_id].last_dmarx_size]), recv_size);
s_uart_dev[uart_id].last_dmarx_size = recv_total_size;/* 记录接收总数据大小 */
}
串口空闲中断场景计算
串口空闲中断场景的接收数据计算与“DMA通道buf半满场景”计算方式是一样的。
串口空闲中断处理函数:
void uart_dmarx_idle_isr(uint8_t uart_id)
{
uint16_t recv_total_size;
uint16_t recv_size;
if(uart_id == 0)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart1_get_dmarx_buf_remain_size();
}
else if (uart_id == 1)
{
recv_total_size = s_uart_dev[uart_id].dmarx_buf_size - bsp_uart2_get_dmarx_buf_remain_size();
}
recv_size = recv_total_size - s_uart_dev[uart_id].last_dmarx_size;
s_UartTxRxCount[uart_id*2+1] += recv_size;
fifo_write(&s_uart_dev[uart_id].rx_fifo,
(const uint8_t *)&(s_uart_dev[uart_id].dmarx_buf[s_uart_dev[uart_id].last_dmarx_size]), recv_size);
s_uart_dev[uart_id].last_dmarx_size = recv_total_size;
}
注:串口空闲中断处理函数,除了将数据拷贝到串口接收fifo中,还可以增加特殊处理,如作为串口数据传输完成标识、不定长度数据处理等等。
将有效数据拷贝到fifo中,除了需知道有效数据大小外,还需知道数据存储于DMA 接收buf的偏移地址。有效数据偏移地址只需记录上一次接收的总大小即,可,在DMA通道buf全满中断处理函数将该值清零,因为下一次数据将从buf的开头存储。
在DMA通道buf溢满中断处理函数中将数据偏移地址清零:
void uart_dmarx_done_isr(uint8_t uart_id)
{
/* todo */
s_uart_dev[uart_id].last_dmarx_size = 0;
}
经过前面的处理步骤,已将串口数据拷贝至接收fifo,应用程序任务只需从fifo获取数据进行处理。前提是,处理效率必须大于DAM接收搬运数据的效率,否则导致数据丢失或者被覆盖处理。
关键步骤
【1】初始化串口
【2】使能串口DMA发送模式
【3】配置DMA发送通道,这一步无需在初始化设置,有数据需要发送时才配置使能DMA发送通道
UART2 DMA模式发送配置代码如下,与其他外设使用DMA的配置基本一致,留意关键配置:
void bsp_uart2_dmatx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->TDR);/* UART2发送数据地址 */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr; /* 发送数据buf */
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* 传输方向:内存->外设 */
DMA_InitStructure.DMA_BufferSize = mem_size; /* 发送数据buf大小 */
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_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC|DMA_IT_TE, ENABLE); /* 使能传输完成中断、错误中断 */
DMA_ClearFlag(DMA1_IT_TC4); /* 清除发送完成标识 */
DMA_Cmd(DMA1_Channel4, ENABLE); /* 启动DMA发送 */
}
串口待发送数据存于发送fifo中,发送处理函数需要做的的任务就是循环查询发送fifo是否存在数据,如存在则将该数据拷贝到DMA发送buf中,然后启动DMA传输。前提是需要等待上一次DMA传输完毕,即是DMA接收到DMA传输完成中断信号"DMA_IT_TC"
。
串口发送处理函数:
void uart_poll_dma_tx(uint8_t uart_id)
{
uint16_t size = 0;
if (0x01 == s_uart_dev[uart_id].status)
{
return;
}
size = fifo_read(&s_uart_dev[uart_id].tx_fifo, s_uart_dev[uart_id].dmatx_buf,
s_uart_dev[uart_id].dmatx_buf_size);
if (size != 0)
{
s_UartTxRxCount[uart_id*2+0] += size;
if (uart_id == 0)
{
s_uart_dev[uart_id].status = 0x01; /* DMA发送状态 */
bsp_uart1_dmatx_config(s_uart_dev[uart_id].dmatx_buf, size);
}
else if (uart_id == 1)
{
s_uart_dev[uart_id].status = 0x01; /* DMA发送状态,必须在使能DMA传输前置位,否则有可能DMA已经传输并进入中断 */
bsp_uart2_dmatx_config(s_uart_dev[uart_id].dmatx_buf, size);
}
}
}
“DMA_IT_TC”
中断可能比“发送状态标识置位”先执行,导致程序误判DMA一直处理发送状态(发送标识无法被清除)。注:关于DMA发送数据启动函数,有些博客文章描述只需改变DMA发送buf的大小即可;经过测试发现,该方法在发送数据量较小时可行,数据量大后,导致发送失败,而且不会触发DMA发送完成中断。因此,可靠办法是:每次启动DMA发送,重新配置DMA通道所有参数。该步骤只是配置寄存器过程,实质上不会占用很多CPU执行时间。
DMA传输完成中断处理函数:
void uart_dmatx_done_isr(uint8_t uart_id)
{
s_uart_dev[uart_id].status = 0; /* 清空DMA发送状态标识 */
}
上述串口发送处理函数可以在几种情况调用:
void thread(void)
{
uart_poll_dma_tx(DEV_UART1);
uart_poll_dma_tx(DEV_UART2);
}
void TIMx_IRQHandler(void)
{
uart_poll_dma_tx(DEV_UART1);
uart_poll_dma_tx(DEV_UART2);
}
void DMA1_Channel4_5_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC4))
{
UartDmaSendDoneIsr(UART_2);
DMA_ClearFlag(DMA1_FLAG_TC4);
uart_poll_dma_tx(DEV_UART2);
}
}
每次拷贝多少数据量到DMA发送buf:
关于这个问题,与具体应用场景有关,遵循的原则就是:只要发送fifo的数据量大于等于DMA发送buf的大小,就应该填满DMA发送buf,然后启动DMA传输,这样才能充分发挥会DMA性能。因此,需兼顾每次DMA传输的效率和串口数据流实时性,参考着几类实现:
/* 串口设备数据结构 */
typedef struct
{
uint8_t status; /* 发送状态 */
_fifo_t tx_fifo; /* 发送fifo */
_fifo_t rx_fifo; /* 接收fifo */
uint8_t *dmarx_buf; /* dma接收缓存 */
uint16_t dmarx_buf_size;/* dma接收缓存大小*/
uint8_t *dmatx_buf; /* dma发送缓存 */
uint16_t dmatx_buf_size;/* dma发送缓存大小 */
uint16_t last_dmarx_size;/* dma上一次接收数据大小 */
}uart_device_t;
/* 串口注册初始化函数 */
void uart_device_init(uint8_t uart_id)
{
if (uart_id == 1)
{
/* 配置串口2收发fifo */
fifo_register(&s_uart_dev[uart_id].tx_fifo, &s_uart2_tx_buf[0],
sizeof(s_uart2_tx_buf), fifo_lock, fifo_unlock);
fifo_register(&s_uart_dev[uart_id].rx_fifo, &s_uart2_rx_buf[0],
sizeof(s_uart2_rx_buf), fifo_lock, fifo_unlock);
/* 配置串口2 DMA收发buf */
s_uart_dev[uart_id].dmarx_buf = &s_uart2_dmarx_buf[0];
s_uart_dev[uart_id].dmarx_buf_size = sizeof(s_uart2_dmarx_buf);
s_uart_dev[uart_id].dmatx_buf = &s_uart2_dmatx_buf[0];
s_uart_dev[uart_id].dmatx_buf_size = sizeof(s_uart2_dmatx_buf);
bsp_uart2_dmarx_config(s_uart_dev[uart_id].dmarx_buf,
sizeof(s_uart2_dmarx_buf));
s_uart_dev[uart_id].status = 0;
}
}
/* 串口发送函数 */
uint16_t uart_write(uint8_t uart_id, const uint8_t *buf, uint16_t size)
{
return fifo_write(&s_uart_dev[uart_id].tx_fifo, buf, size);
}
/* 串口读取函数 */
uint16_t uart_read(uint8_t uart_id, uint8_t *buf, uint16_t size)
{
return fifo_read(&s_uart_dev[uart_id].rx_fifo, buf, size);
}
依赖的fifo参考该文章:
【1】?通用环形缓冲区模块
代码仓库:https://github.com/Prry/stm32f0-uart-dma
串口&DMA底层配置:
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "stm32f0xx.h"
#include "bsp_uart.h"
/**
* @brief
* @param
* @retval
*/
static void bsp_uart1_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
#if 0
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#else
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
}
/**
* @brief
* @param
* @retval
*/
static void bsp_uart2_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**
* @brief
* @param
* @retval
*/
void bsp_uart1_init(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
bsp_uart1_gpio_init();
/* 使能串口和DMA时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 57600;
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_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); /* 使能空闲中断 */
USART_OverrunDetectionConfig(USART1, USART_OVRDetection_Disable);
USART_Cmd(USART1, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx|USART_DMAReq_Tx, ENABLE); /* 使能DMA收发 */
/* 串口中断 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* DMA中断 */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief
* @param
* @retval
*/
void bsp_uart2_init(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
bsp_uart2_gpio_init();
/* 使能串口和DMA时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 57600;
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_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); /* 使能空闲中断 */
USART_OverrunDetectionConfig(USART2, USART_OVRDetection_Disable);
USART_Cmd(USART2, ENABLE);
USART_DMACmd(USART2, USART_DMAReq_Rx|USART_DMAReq_Tx, ENABLE); /* 使能DMA收发 */
/* 串口中断 */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* DMA中断 */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void bsp_uart1_dmatx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel2);
DMA_Cmd(DMA1_Channel2, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->TDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* 传输方向:内存->外设 */
DMA_InitStructure.DMA_BufferSize = mem_size;
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_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC|DMA_IT_TE, ENABLE);
DMA_ClearFlag(DMA1_IT_TC2); /* 清除发送完成标识 */
DMA_Cmd(DMA1_Channel2, ENABLE);
}
void bsp_uart1_dmarx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel3);
DMA_Cmd(DMA1_Channel3, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->RDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /* 传输方向:外设->内存 */
DMA_InitStructure.DMA_BufferSize = mem_size;
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_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel3, DMA_IT_TC|DMA_IT_HT|DMA_IT_TE, ENABLE);/* 使能DMA半满、全满、错误中断 */
DMA_ClearFlag(DMA1_IT_TC3);
DMA_ClearFlag(DMA1_IT_HT3);
DMA_Cmd(DMA1_Channel3, ENABLE);
}
uint16_t bsp_uart1_get_dmarx_buf_remain_size(void)
{
return DMA_GetCurrDataCounter(DMA1_Channel3); /* 获取DMA接收buf剩余空间 */
}
void bsp_uart2_dmatx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_Cmd(DMA1_Channel4, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->TDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* 传输方向:内存->外设 */
DMA_InitStructure.DMA_BufferSize = mem_size;
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_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC|DMA_IT_TE, ENABLE);
DMA_ClearFlag(DMA1_IT_TC4); /* 清除发送完成标识 */
DMA_Cmd(DMA1_Channel4, ENABLE);
}
void bsp_uart2_dmarx_config(uint8_t *mem_addr, uint32_t mem_size)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_Cmd(DMA1_Channel5, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->RDR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)mem_addr;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /* 传输方向:外设->内存 */
DMA_InitStructure.DMA_BufferSize = mem_size;
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_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC|DMA_IT_HT|DMA_IT_TE, ENABLE);/* 使能DMA半满、全满、错误中断 */
DMA_ClearFlag(DMA1_IT_TC5);
DMA_ClearFlag(DMA1_IT_HT5);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
uint16_t bsp_uart2_get_dmarx_buf_remain_size(void)
{
return DMA_GetCurrDataCounter(DMA1_Channel5); /* 获取DMA接收buf剩余空间 */
}
压力测试: