#ifndef __BSP_UART_H
#define __BSP_UART_H
#define USART1_TX_LEN 1024
#define USART1_RX_LEN 1024
#define USART2_TX_LEN 1024
#define USART2_RX_LEN 1024
#define USART3_TX_LEN 1024
#define USART3_RX_LEN 1024
void Uart1_Init(void);
void Uart1_DMA_TxConfig(uint8_t TransSize);
void Uart1_DMA_RxConfig(void);
void Uart2_Init(void);
void Uart2_DMA_TxConfig(uint8_t TransSize);
void Uart2_DMA_RxConfig(void);
void Uart3_Init(void);
void Uart3_DMA_TxConfig(uint8_t TransSize);
void Uart3_DMA_RxConfig(void);
#endif
//接收缓存区
uint8_t Uart1_Rx_Flag;//接收到数据请求标志
uint8_t Uart1_Rx_Buf[USART1_RX_LEN];//接收缓冲,最USART1_RX_LEN个字节
//发送缓存区
uint8_t Uart1_Tx_Buf[USART1_TX_LEN];//发送缓冲,最USART1_TX_LEN个字节
uint16_t Uart1_ReceiveSize;
//接收缓存区
uint8_t Uart2_Rx_Flag;//接收到数据请求标志
uint8_t Uart2_Rx_Buf[USART2_RX_LEN];//接收缓冲,最USART1_RX_LEN个字节
//发送缓存区
uint8_t Uart2_Tx_Buf[USART2_TX_LEN];//发送缓冲,最USART1_TX_LEN个字节
uint16_t Uart2_ReceiveSize;
//接收缓存区
uint8_t Uart3_Rx_Flag;//接收到数据请求标志
uint8_t Uart3_Rx_Buf[USART3_RX_LEN];//接收缓冲,最USART1_RX_LEN个字节
//发送缓存区
uint8_t Uart3_Tx_Buf[USART3_TX_LEN];//发送缓冲,最USART1_TX_LEN个字节
uint16_t Uart3_ReceiveSize;
/**
* @brief Configures the UART1.
* @param None
* @retval None
*/
static void UART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//使能GPIOA时钟
//打开 UART 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//Enable DMA1 clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//USART1_TX PA9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
//USART1_RX PA10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
//配置串口硬件参数
USART_InitStructure.USART_BaudRate = 115200;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
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);
}
/**
* @brief Configures the UART2.
* @param None
* @retval None
*/
static void UART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//使能GPIOA时钟
//打开 UART 时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//Enable DMA1 clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
//USART2_TX PA2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2
//USART2_RX PA3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA3
//配置串口硬件参数
USART_InitStructure.USART_BaudRate = 115200;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
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);
}
/**
* @brief Configures the UART3.
* @param None
* @retval None
*/
static void UART3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);//使能GPIOB时钟
//打开 UART 时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//Enable DMA1 clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
//USART3_TX PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB10
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB11
//配置串口硬件参数
USART_InitStructure.USART_BaudRate = 115200;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
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(USART3, &USART_InitStructure);
}
/**
* @brief 初始化CPU的USART1串口硬件设备
* @param None
* @retval None
*/
void Uart1_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
UART1_Configuration();
//设置中断
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//配置串口中断
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART1, USART_IT_TC, DISABLE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//使能IDLE中断
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);//清发送完成标志,Transmission Complete flag
}
/**
* @brief 初始化CPU的USART2串口硬件设备
* @param None
* @retval None
*/
void Uart2_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
UART2_Configuration();
//设置中断
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//配置串口中断
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART2, USART_IT_TC, DISABLE);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//使能IDLE中断
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2, USART_FLAG_TC);//清发送完成标志,Transmission Complete flag
}
/**
* @brief 初始化CPU的USART3串口硬件设备
* @param None
* @retval None
*/
void Uart3_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
UART3_Configuration();
//设置中断
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//配置串口中断
USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART3, USART_IT_TC, DISABLE);
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);//使能IDLE中断
USART_Cmd(USART3, ENABLE);
USART_ClearFlag(USART3, USART_FLAG_TC);//清发送完成标志,Transmission Complete flag
}
//
void Uart1_DMA_TxConfig(uint8_t TransSize)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_DeInit(DMA1_Channel4);
//DMA外设USART1基地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR);
//DMA内存基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart1_Tx_Buf;
//外设作为数据传输的目的地
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_BufferSize = TransSize;
//外设地址寄存器不变
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//内存地址寄存器递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//数据宽度为8位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//工作在正常模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//DMA通道 x拥有高优先级
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//根据DMA_InitStruct中指定的参数初始化DMA的通道
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
//使能 USART DMA TX 请求
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
//使能 DMA Stream 中断通道
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Enable DMA1_Channel4
DMA_Cmd(DMA1_Channel4, DISABLE);
}
//
void Uart2_DMA_TxConfig(uint8_t TransSize)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_DeInit(DMA1_Channel7);
//DMA外设USART1基地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);
//DMA内存基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart2_Tx_Buf;
//外设作为数据传输的目的地
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_BufferSize = TransSize;
//外设地址寄存器不变
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//内存地址寄存器递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//数据宽度为8位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//工作在正常模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//DMA通道 x拥有高优先级
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//根据DMA_InitStruct中指定的参数初始化DMA的通道
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE);
//使能 USART DMA TX 请求
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
//使能 DMA Stream 中断通道
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Enable DMA1_Channel
DMA_Cmd(DMA1_Channel7, DISABLE);
}
//
void Uart3_DMA_TxConfig(uint8_t TransSize)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_DeInit(DMA1_Channel2);
//DMA外设USART1基地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);
//DMA内存基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart3_Tx_Buf;
//外设作为数据传输的目的地
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_BufferSize = TransSize;
//外设地址寄存器不变
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//内存地址寄存器递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//数据宽度为8位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//工作在正常模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//DMA通道 x拥有高优先级
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//根据DMA_InitStruct中指定的参数初始化DMA的通道
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
//使能 USART DMA TX 请求
USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
//使能 DMA Stream 中断通道
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Enable DMA1_Channel
DMA_Cmd(DMA1_Channel2, DISABLE);
}
//
void Uart1_DMA_RxConfig(void)
{
DMA_InitTypeDef DMA_InitStructure;
//DMA1的通道配置
DMA_DeInit(DMA1_Channel5);
//DMA外设USART1基地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR);
//DMA内存基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart1_Rx_Buf;
//内存作为数据传输的目的地
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_BufferSize = USART1_RX_LEN;
//外设地址寄存器不变
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//内存地址寄存器递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//数据宽度为8位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//工作在正常模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//DMA通道 x拥有中优先级
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
//
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//根据DMA_InitStruct中指定的参数初始化DMA的通道
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
//使能 USART DMA RX 请求
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE);
//Enable DMA1_Channel5
DMA_Cmd(DMA1_Channel5,ENABLE);
}
//
void Uart2_DMA_RxConfig(void)
{
DMA_InitTypeDef DMA_InitStructure;
//DMA1的通道配置
DMA_DeInit(DMA1_Channel6);
//DMA外设USART1基地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);
//DMA内存基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart2_Rx_Buf;
//内存作为数据传输的目的地
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_BufferSize = USART2_RX_LEN;
//外设地址寄存器不变
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//内存地址寄存器递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//数据宽度为8位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//工作在正常模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//DMA通道 x拥有中优先级
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
//
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//根据DMA_InitStruct中指定的参数初始化DMA的通道
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
//使能 USART DMA RX 请求
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
DMA_ITConfig(DMA1_Channel6,DMA_IT_TC,ENABLE);
//Enable DMA1_Channel
DMA_Cmd(DMA1_Channel6,ENABLE);
}
//
void Uart3_DMA_RxConfig(void)
{
DMA_InitTypeDef DMA_InitStructure;
//DMA1的通道配置
DMA_DeInit(DMA1_Channel3);
//DMA外设USART1基地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);
//DMA内存基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart3_Rx_Buf;
//内存作为数据传输的目的地
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_BufferSize = USART3_RX_LEN;
//外设地址寄存器不变
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//内存地址寄存器递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//数据宽度为8位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//数据宽度为8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//工作在正常模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//DMA通道 x拥有中优先级
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
//
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//根据DMA_InitStruct中指定的参数初始化DMA的通道
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
//使能 USART DMA RX 请求
USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);
DMA_ITConfig(DMA1_Channel3,DMA_IT_TC,ENABLE);
//Enable DMA1_Channel
DMA_Cmd(DMA1_Channel3,ENABLE);
}
/*
*********************************************************************************************************
* 函 数 名: DMA1_Channel4_IRQHandler
* 功能说明: 串口1在DMA方式下的发送中断
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void DMA1_Channel4_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC4)==SET) //UART Trans Complete
{
DMA_ClearFlag(DMA1_FLAG_TC4);
//DataHold=0;
DMA_Cmd(DMA1_Channel4, DISABLE);
}
}
/*
*********************************************************************************************************
* 函 数 名: DMA1_Channel7_IRQHandler
* 功能说明: 串口2在DMA方式下的发送中断
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void DMA1_Channel7_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC7)==SET) //UART Trans Complete
{
DMA_ClearFlag(DMA1_FLAG_TC7);
//DataHold=0;
DMA_Cmd(DMA1_Channel7, DISABLE);
}
}
/*
*********************************************************************************************************
* 函 数 名: DMA1_Channel2_IRQHandler
* 功能说明: 串口3在DMA方式下的发送中断
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void DMA1_Channel2_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC2)==SET) //UART Trans Complete
{
DMA_ClearFlag(DMA1_FLAG_TC2);
//DataHold=0;
DMA_Cmd(DMA1_Channel2, DISABLE);
}
}
/*
*********************************************************************************************************
* 函 数 名: USART1_IRQHandler
* 功能说明: 串口1空闲中断
* 形 参: 无
* 返 回 值: 无
* IDLE要由软件序列清除依次读取USART1->SR和USART1->DR
*********************************************************************************************************
*/
void USART1_IRQHandler(void)
{
uint16_t tmp;
if(USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel5, DISABLE); //关闭DMA,防止处理其间有数据
tmp = USART1->SR;
tmp = USART1->DR;
tmp++;
Uart1_ReceiveSize =USART1_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5);
if(Uart1_ReceiveSize!=0)
Uart1_Rx_Flag=1;
DMA_ClearFlag(DMA1_FLAG_GL5 | DMA1_FLAG_TC5 | DMA1_FLAG_HT5 | DMA1_FLAG_TE5);//清DMA1_Channel5传输完成标志
DMA_SetCurrDataCounter(DMA1_Channel5, USART1_RX_LEN);
DMA_Cmd(DMA1_Channel5, ENABLE); //打开DMA,防止处理其间有数据
}
}
/*
*********************************************************************************************************
* 函 数 名: USART2_IRQHandler
* 功能说明: 串口2空闲中断
* 形 参: 无
* 返 回 值: 无
* IDLE要由软件序列清除依次读取USART2->SR和USART2->DR
*********************************************************************************************************
*/
void USART2_IRQHandler(void)
{
uint16_t tmp;
if(USART_GetITStatus(USART2,USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel6, DISABLE); //关闭DMA,防止处理其间有数据
tmp = USART2->SR;
tmp = USART2->DR;
tmp++;
Uart2_ReceiveSize =USART2_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel6);
if(Uart2_ReceiveSize!=0)
Uart2_Rx_Flag=1;
DMA_ClearFlag(DMA1_FLAG_GL6 | DMA1_FLAG_TC6 | DMA1_FLAG_HT6 | DMA1_FLAG_TE6);//清DMA1_Channel6传输完成标志
DMA_SetCurrDataCounter(DMA1_Channel6, USART2_RX_LEN);
DMA_Cmd(DMA1_Channel6, ENABLE); //打开DMA,防止处理其间有数据
}
}
/*
*********************************************************************************************************
* 函 数 名: USART3_IRQHandler
* 功能说明: 串口3空闲中断
* 形 参: 无
* 返 回 值: 无
* IDLE要由软件序列清除依次读取USART3->SR和USART3->DR
*********************************************************************************************************
*/
void USART3_IRQHandler(void)
{
uint16_t tmp;
if(USART_GetITStatus(USART3,USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel3, DISABLE); //关闭DMA,防止处理其间有数据
tmp = USART3->SR;
tmp = USART3->DR;
tmp++;
Uart3_ReceiveSize =USART3_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel3);
if(Uart3_ReceiveSize!=0)
Uart3_Rx_Flag=1;
DMA_ClearFlag(DMA1_FLAG_GL3 | DMA1_FLAG_TC3 | DMA1_FLAG_HT3 | DMA1_FLAG_TE3);//清DMA1_Channel3传输完成标志
DMA_SetCurrDataCounter(DMA1_Channel3, USART3_RX_LEN);
DMA_Cmd(DMA1_Channel3, ENABLE); //打开DMA,防止处理其间有数据
}
}
/**********************************************(END OF FILE)**********************************************/
//初始化示例,使用前必须调用
Uart1_Init();
Uart1_DMA_TxConfig(40);
Uart1_DMA_RxConfig();
Uart2_Init();
Uart2_DMA_TxConfig(40);
Uart2_DMA_RxConfig();
Uart3_Init();
Uart3_DMA_TxConfig(40);
Uart3_DMA_RxConfig();
//Uart Echo Test
/*Uart Data*/
void Uart_Data_Process(void)
{
uint16_t i;
if(Uart1_Rx_Flag==1)
{
Uart1_Rx_Flag=0;
for(i=0;i<Uart1_ReceiveSize;i++)
Uart1_Tx_Buf[i]=Uart1_Rx_Buf[i];
DMA_SetCurrDataCounter(DMA1_Channel4,Uart1_ReceiveSize);
DMA_Cmd(DMA1_Channel4, ENABLE);
}
if(Uart2_Rx_Flag==1)
{
Uart2_Rx_Flag=0;
for(i=0;i<Uart2_ReceiveSize;i++)
Uart2_Tx_Buf[i]=Uart2_Rx_Buf[i];
DMA_SetCurrDataCounter(DMA1_Channel7,Uart2_ReceiveSize);
DMA_Cmd(DMA1_Channel7, ENABLE);
}
if(Uart3_Rx_Flag==1)
{
Uart3_Rx_Flag=0;
for(i=0;i<Uart3_ReceiveSize;i++)
Uart3_Tx_Buf[i]=Uart3_Rx_Buf[i];
DMA_SetCurrDataCounter(DMA1_Channel2,Uart3_ReceiveSize);
DMA_Cmd(DMA1_Channel2, ENABLE);
}
}
|