直接上代码就对了(_)
头文件
#ifndef __USART_H
#define __USART_H
#include "main.h"
#define USART1_RX_BUF_SIZE 128
#define USART1_TX_BUF_SIZE 64
#define USART3_RX_BUF_SIZE 128
#define USART3_TX_BUF_SIZE 64
extern uint8_t USART1_RX_Buff[USART1_RX_BUF_SIZE];
extern uint8_t USART1_TX_Buff[USART1_TX_BUF_SIZE];
extern uint8_t USART1_RC_Flag;
extern uint8_t USART1_TC_Flag;
extern uint8_t USART3_RX_Buff[USART3_RX_BUF_SIZE];
extern uint8_t USART3_TX_Buff[USART3_TX_BUF_SIZE];
extern uint8_t USART3_RC_Flag;
extern uint8_t USART3_TC_Flag;
void USART1_Init(uint32_t baud);
void USART1_DMA_ReceData(uint8_t *data);
void USART1_DMA_TranData(uint8_t *data,uint32_t dataLen);
void USART3_Init(uint32_t baud);
void USART3_DMA_ReceData(uint8_t *data);
void USART3_DMA_TranData(uint8_t *data,uint32_t dataLen);
void Usart_SendString(USART_TypeDef* USARTx,uint8_t *data,uint32_t dataLen);
#endif
源文件
#include "usart.h"
#include "main.h"
///
/******************************** 串口1 开始 **********************************/
uint32_t USART1_RX_Data_Size;
uint8_t USART1_RX_Buff[USART1_RX_BUF_SIZE] = {0};
uint8_t USART1_TX_Buff[USART1_TX_BUF_SIZE] = {0};
uint8_t USART1_RC_Flag = 0;
uint8_t USART1_TC_Flag = 0;
static void USART1_IO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
static void USART1_Config(uint32_t baud)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_DeInit(USART1);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ClearFlag(USART1,USART_FLAG_RXNE);
USART_ClearFlag(USART1,USART_FLAG_IDLE);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1,ENABLE);
}
static void USART1_DMA_RX_TX_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2时钟使能
//DMA2_USART1_RX:通道4,数据流5
DMA_Cmd(DMA2_Stream5, DISABLE);
DMA_DeInit(DMA2_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART1_RX_Buff;//接收基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = USART1_RX_BUF_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_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream5, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5);
DMA_ITConfig(DMA2_Stream5,DMA_IT_TC,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);//串口DMA接收使能
DMA_Cmd(DMA2_Stream5, ENABLE);//DMA传输使能
//DMA2_USART1_TX:通道4,数据流7
DMA_Cmd(DMA2_Stream7, DISABLE);//DMA传输失能
DMA_DeInit(DMA2_Stream7);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR; //DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART1_TX_Buff; //发送基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = USART1_TX_BUF_SIZE;
DMA_Init(DMA2_Stream7, &DMA_InitStructure);
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_ClearFlag(DMA2_Stream7,DMA_FLAG_TCIF7);
DMA_ITConfig(DMA2_Stream7,DMA_IT_TC,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//串口DMA发送使能
//DMA_Cmd(DMA2_Stream7, ENABLE);//DMA传输使能
}
/******** 串口1初始化配置 *********/
void USART1_Init(uint32_t baud)
{
USART1_IO_Config();
USART1_Config(baud);
USART1_DMA_RX_TX_Config();
}
/******** 串口1 DMA接收数据 *********/
void USART1_DMA_ReceData(uint8_t *data)
{
DMA_Cmd(DMA2_Stream5, DISABLE);//关闭DMA设备(需要在DMA关闭的状态下才能进行配置)
//DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5);
while(DMA_GetCmdStatus(DMA2_Stream5) != DISABLE);//等待关闭完成
DMA2_Stream5->M0AR = (uint32_t)data;//接收数据所放的地址
DMA_Cmd(DMA2_Stream5, ENABLE);//开启DMA(等待接收数据)
}
/******** 串口1 DMA发送数据 *********/
void USART1_DMA_TranData(uint8_t *data,uint32_t dataLen)
{
while(USART1_TC_Flag);//等待上一次数据传输完成
USART1_TC_Flag = 1;//将传输标志位置1表示正在传数据
//DMA_ClearFlag(DMA2_Stream7,DMA_FLAG_TCIF7);
DMA_Cmd(DMA2_Stream7, DISABLE);//关闭DMA设备(需要在DMA关闭的状态下才能进行配置)
while(DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);//等待关闭完成
DMA2_Stream7->M0AR = (uint32_t)data;//需要发送的数据地址
DMA2_Stream7->NDTR = dataLen;//需要发送的数据长度
//DMA_SetCurrDataCounter(DMA2_Stream7,dataLen);
DMA_Cmd(DMA2_Stream7, ENABLE);//开启DMA(开始发送数据)
}
///
/******** 串口1 中断服务函数 ***********/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_IDLE) == SET)
{
USART1_RC_Flag = 1;
USART1->SR;USART1->DR;//清除串口空闲中断标志位
}
}
/**** 串口1 DMA接收中断服务函数 *****/
void DMA2_Stream5_IRQHandler(void)
{
if(DMA_GetITStatus(DMA2_Stream5,DMA_IT_TCIF5) == SET)
{
//USART1_RC_Flag = 1;
DMA_ClearITPendingBit(DMA2_Stream5,DMA_IT_TCIF5);
}
}
/**** 串口1 DMA发送中断服务函数 *****/
void DMA2_Stream7_IRQHandler(void)
{
if(DMA_GetITStatus(DMA2_Stream7,DMA_IT_TCIF7) == SET)
{
USART1_TC_Flag = 0;
DMA_ClearITPendingBit(DMA2_Stream7,DMA_IT_TCIF7);
}
}
///
/******************************** 串口1 结束 **********************************/
///
///
/******************************** 串口3 开始 **********************************/
uint8_t USART3_RX_Buff[USART1_RX_BUF_SIZE] = {0};
uint8_t USART3_TX_Buff[USART1_TX_BUF_SIZE] = {0};
uint8_t USART3_RC_Flag = 0;
uint8_t USART3_TC_Flag = 0;
static void USART3_IO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
static void USART3_Config(uint32_t baud)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_DeInit(USART3);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART3,&USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ClearFlag(USART3,USART_FLAG_TC);
USART_ClearFlag(USART3,USART_FLAG_RXNE);
USART_ClearFlag(USART3,USART_FLAG_IDLE);
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3,ENABLE);
}
static void USART3_DMA_RX_TX_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA2时钟使能
//DMA1_USART3_RX:通道4,数据流1
DMA_Cmd(DMA1_Stream1, DISABLE);
DMA_DeInit(DMA1_Stream1);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR;//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART3_RX_Buff;//接收基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = USART3_RX_BUF_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_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream1, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearFlag(DMA1_Stream1,DMA_FLAG_TCIF1);
DMA_ITConfig(DMA1_Stream1,DMA_IT_TC,ENABLE);
USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);//串口DMA接收使能
DMA_Cmd(DMA1_Stream1, ENABLE);//DMA传输使能
//DMA1_USART3_TX:通道4,数据流3
DMA_Cmd(DMA1_Stream3, DISABLE);//DMA传输失能
DMA_DeInit(DMA1_Stream3);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR; //DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART3_TX_Buff; //发送基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = USART3_TX_BUF_SIZE;
DMA_Init(DMA1_Stream3, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearFlag(DMA1_Stream3,DMA_FLAG_TCIF3);
DMA_ITConfig(DMA1_Stream3,DMA_IT_TC,ENABLE);
USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);//串口DMA发送使能
//DMA_Cmd(DMA1_Stream3, ENABLE);//DMA传输使能
}
/******** 串口3初始化配置 *********/
void USART3_Init(uint32_t baud)
{
USART3_IO_Config();
USART3_Config(baud);
USART3_DMA_RX_TX_Config();
}
/******** 串口1 DMA接收数据 *********/
void USART3_DMA_ReceData(uint8_t *data)
{
DMA_Cmd(DMA1_Stream1, DISABLE);//关闭DMA设备(需要在DMA关闭的状态下才能进行配置)
//DMA_ClearFlag(DMA1_Stream1,DMA_FLAG_TCIF1);
while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);//等待关闭完成
DMA1_Stream1->M0AR = (uint32_t)data;//接收数据所放的地址
DMA_Cmd(DMA1_Stream1, ENABLE);//开启DMA(等待接收数据)
}
/******** 串口1 DMA发送数据 *********/
void USART3_DMA_TranData(uint8_t *data,uint32_t dataLen)
{
while(USART3_TC_Flag);//等待上一次数据传输完成
USART3_TC_Flag = 1;//将传输标志位置1表示正在传数据
//DMA_ClearFlag(DMA1_Stream3,DMA_FLAG_TCIF3);
DMA_Cmd(DMA1_Stream3, DISABLE);//关闭DMA设备(需要在DMA关闭的状态下才能进行配置)
while(DMA_GetCmdStatus(DMA1_Stream3) != DISABLE);//等待关闭完成
DMA1_Stream3->M0AR = (uint32_t)data;//需要发送的数据地址
DMA1_Stream3->NDTR = dataLen;//需要发送的数据长度
//DMA_SetCurrDataCounter(DMA1_Stream3,dataLen);
DMA_Cmd(DMA1_Stream3, ENABLE);//开启DMA(开始发送数据)
}
///
/******** 串口3 中断服务函数 ***********/
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_IDLE) == SET)
{
USART3_RC_Flag = 1;
USART3->SR;USART3->DR;//清除串口空闲中断标志位
}
}
/**** 串口3 DMA接收中断服务函数 *****/
void DMA1_Stream1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream1,DMA_IT_TCIF1) == SET)
{
//USART1_RC_Flag = 1;
DMA_ClearITPendingBit(DMA1_Stream1,DMA_IT_TCIF1);
}
}
/**** 串口3 DMA发送中断服务函数 *****/
void DMA1_Stream3_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream3,DMA_IT_TCIF3) == SET)
{
USART3_TC_Flag = 0;
DMA_ClearITPendingBit(DMA1_Stream3,DMA_IT_TCIF3);
}
}
///
/******************************** 串口3 结束 **********************************/
/******** 串口 发送字符串 *********/
void Usart_SendString(USART_TypeDef* USARTx,uint8_t *data,uint32_t dataLen)
{
uint32_t i;
for(i = 0;i < dataLen;i ++)
{
USART_SendData(USARTx,data[i]);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE) == RESET);
}
}
主函数
int main()
{
uint32_t temp = 0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
USART1_Init(115200);
USART3_Init(115200);
delay(10);
while (1)
{
if(USART1_RC_Flag == 1)
{
USART1_DMA_TranData(USART1_RX_Buff,strlen((char*)USART1_RX_Buff));
USART1_DMA_ReceData(USART1_RX_Buff);
USART1_RC_Flag = 0;
}
if(USART3_RC_Flag == 1)
{
USART3_DMA_TranData(USART3_RX_Buff,strlen((char*)USART3_RX_Buff));
USART3_DMA_ReceData(USART3_RX_Buff);
USART3_RC_Flag = 0;
}
delay(500);
}
return 0;
}