串口dma发送库函数代码如何编写,已经配好初始化的代码
时间: 2023-07-03 15:04:33 浏览: 205
下面是一个基于STM32的串口DMA发送库函数代码示例:
```c
#include "stm32f4xx.h"
// 定义串口号和DMA通道号
#define UART_PORT USART1
#define UART_DMA_CHANNEL DMA_CHANNEL_4
// 定义发送缓存区大小
#define TX_BUFFER_SIZE 128
// 定义发送缓存区和指针
static uint8_t tx_buffer[TX_BUFFER_SIZE];
static uint16_t tx_head = 0;
static uint16_t tx_tail = 0;
// DMA发送完成中断处理函数
void DMA2_Stream7_IRQHandler(void)
{
// 判断发送完成中断标志位
if (DMA_GetFlagStatus(DMA2_Stream7, DMA_FLAG_TCIF7) == SET)
{
// 清除发送完成中断标志位
DMA_ClearFlag(DMA2_Stream7, DMA_FLAG_TCIF7);
// 更新发送缓存区指针
tx_tail = (tx_tail + DMA_GetCurrDataCounter(DMA2_Stream7)) % TX_BUFFER_SIZE;
// 判断是否还有待发送数据
if (tx_head != tx_tail)
{
// 配置DMA发送
DMA_Cmd(DMA2_Stream7, DISABLE);
DMA_SetCurrDataCounter(DMA2_Stream7, tx_head < tx_tail ? (tx_tail - tx_head) : (TX_BUFFER_SIZE - tx_head));
DMA_SetMemory0Address(DMA2_Stream7, (uint32_t)&tx_buffer[tx_head]);
DMA_Cmd(DMA2_Stream7, ENABLE);
}
}
}
// 初始化串口和DMA
void uart_dma_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 使能USART1和DMA2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
// 配置USART1引脚
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART1参数
USART_InitStructure.USART_BaudRate = 115200;
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_Init(UART_PORT, &USART_InitStructure);
// 配置USART1中断
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 配置DMA发送
DMA_InitStructure.DMA_Channel = UART_DMA_CHANNEL;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART_PORT->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)tx_buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = 0;
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_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream7, &DMA_InitStructure);
// 使能USART1和DMA发送
USART_Cmd(UART_PORT, ENABLE);
DMA_Cmd(DMA2_Stream7, ENABLE);
}
// 向发送缓存区写入数据
void uart_dma_write(uint8_t *data, uint16_t len)
{
uint16_t i;
// 判断发送缓存区是否已满
if ((tx_head - tx_tail + TX_BUFFER_SIZE) % TX_BUFFER_SIZE >= len)
{
// 向发送缓存区写入数据
for (i = 0; i < len; i++)
{
tx_buffer[(tx_head + i) % TX_BUFFER_SIZE] = data[i];
}
// 更新发送缓存区指针
tx_head = (tx_head + len) % TX_BUFFER_SIZE;
// 判断DMA发送是否空闲
if (DMA_GetFlagStatus(DMA2_Stream7, DMA_FLAG_TCIF7) == SET)
{
// 配置DMA发送
DMA_Cmd(DMA2_Stream7, DISABLE);
DMA_SetCurrDataCounter(DMA2_Stream7, tx_head < tx_tail ? (tx_tail - tx_head) : (TX_BUFFER_SIZE - tx_head));
DMA_SetMemory0Address(DMA2_Stream7, (uint32_t)&tx_buffer[tx_head]);
DMA_Cmd(DMA2_Stream7, ENABLE);
}
}
}
```
使用方法:
1. 在初始化函数中调用`uart_dma_init()`函数初始化串口和DMA。
2. 调用`uart_dma_write(data, len)`函数向发送缓存区写入数据。
3. 数据将自动通过DMA发送出去,无需额外操作。
注意事项:
1. 该代码仅适用于STM32系列单片机。
2. 代码中的串口号和DMA通道号需要根据实际情况修改。
3. 发送缓存区的大小需要根据实际情况确定。
4. 使用时需要根据实际情况在`stm32f4xx_it.c`文件中编写DMA发送完成中断处理函数`DMA2_Stream7_IRQHandler()`。
阅读全文