#include "A_UART.h"
#include "includes.h"
#if USART1_Enable
COM_TypeDef COM1;
#endif
#if USART2_Enable
COM_TypeDef COM2;
#endif
#if USART3_Enable
COM_TypeDef COM3;
#endif
#if UART4_Enable
COM_TypeDef COM4;
#endif
#if UART5_Enable
COM_TypeDef COM5;
#endif
void Memery_Copy1(uint8_t *Destination,uint8_t *Source,uint16_t Len)
{
uint8_t i;
uint8_t *ptr;
ptr = Source;
for(i=0;i<Len;i++)
{
*Destination = *ptr;
Destination++;
ptr++;
}
}
//管脚配置
void GPIO_USARTx_Pin_Config(USART_TypeDef*uart)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
#if USART1_Enable
if(uart==USART1) //USART1(PB6,PB7)
{
RCC_APB2PeriphClockCmd(USART1_RCC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART1_T_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(USART1_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART1_R_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_Port, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
}
#endif
#if USART2_Enable
if(uart==USART2) //USART2(PA2,PA3)
{
RCC_APB2PeriphClockCmd(USART2_RCC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_T_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_R_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
#endif
#if USART3_Enable
if(uart==USART3) //USART3(PB10,PB11)
{
RCC_APB2PeriphClockCmd(USART3_RCC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART3_T_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(USART3_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART3_R_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART3_Port, &GPIO_InitStructure);
}
#endif
#if UART4_Enable
if(uart==UART4) //USART4(PC10,PC11)
{
RCC_APB2PeriphClockCmd(UART4_RCC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4 , ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = UART4_T_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(UART4_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = UART4_R_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(UART4_Port, &GPIO_InitStructure);
}
#endif
#if UART5_Enable
if(uart==UART5) //USART5(PC12,PD2)
{
RCC_APB2PeriphClockCmd(UART5_T_RCC, ENABLE);
RCC_APB2PeriphClockCmd(UART5_R_RCC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 , ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = UART5_T_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(UART5_T_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = UART5_R_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(UART5_R_Port, &GPIO_InitStructure);
}
#endif
}
//串口配置
void UART_init(USART_TypeDef*uart,uint32_t BaudRate)
{
USART_ClockInitTypeDef USART_ClockInitStruct;
USART_InitTypeDef USART_InitStructure;
GPIO_USARTx_Pin_Config(uart);
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_Rx | USART_Mode_Tx;
USART_ClockInitStruct.USART_Clock = USART_Clock_Disable;
USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable;
USART_Init(uart, &USART_InitStructure);
USART_ClockInit(uart,&USART_ClockInitStruct);
USART_ITConfig(uart, USART_IT_RXNE, ENABLE);
USART_ITConfig(uart, USART_IT_TXE, ENABLE);
USART_Cmd(uart, ENABLE);
}
//串口接收指针置0
void uartFameInPointerCheckClear(void)
{
#if USART1_Enable
if(COM1.Frame_Receive_Over_Time<5) //串口1
{
if(COM1.Frame_Receive_Over_Time==Max_Frame_Receive_Over_Time)
{
COM1.Frame_Received_OK=True;
COM1.Received_Number=COM1.Byte_In_Pointer;
COM1.Byte_In_Pointer=0;
}
COM1.Frame_Receive_Over_Time++;
}
#endif
#if USART2_Enable
if(COM2.Frame_Receive_Over_Time<5) //串口2
{
if(COM2.Frame_Receive_Over_Time==Max_Frame_Receive_Over_Time)
{
COM2.Frame_Received_OK=True;
COM2.Received_Number=COM2.Byte_In_Pointer;
COM2.Byte_In_Pointer=0;
}
COM2.Frame_Receive_Over_Time++;
}
#endif
#if USART3_Enable
if(COM3.Frame_Receive_Over_Time<5) //串口3
{
if(COM3.Frame_Receive_Over_Time==Max_Frame_Receive_Over_Time)
{
COM3.Frame_Received_OK=True;
COM3.Received_Number=COM3.Byte_In_Pointer;
COM3.Byte_In_Pointer=0;
}
COM3.Frame_Receive_Over_Time++;
}
#endif
#if UART4_Enable
if(COM4.Frame_Receive_Over_Time<5) //串口4
{
if(COM4.Frame_Receive_Over_Time==Max_Frame_Receive_Over_Time)
{
COM4.Frame_Received_OK=True;
COM4.Received_Number=COM4.Byte_In_Pointer;
COM4.Byte_In_Pointer=0;
}
COM4.Frame_Receive_Over_Time++;
}
#endif
#if UART5_Enable
if(COM5.Frame_Receive_Over_Time<5) //串口5
{
if(COM5.Frame_Receive_Over_Time==Max_Frame_Receive_Over_Time)
{
COM5.Frame_Received_OK=True;
COM5.Received_Number=COM5.Byte_In_Pointer;
COM5.Byte_In_Pointer=0;
}
COM5.Frame_Receive_Over_Time++;
}
#endif
}
//串口1中断
#if USART1_Enable
void USART1_IRQHandler(void)
{
OSIntEnter();
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
COM1.Frame_Receive_Over_Time=0;
COM1.RxBuf[COM1.Byte_In_Pointer]=USART_ReceiveData(USART1);
if(COM1.Byte_In_Pointer<RX_BUFFER_SIZE) COM1.Byte_In_Pointer++;
}
if(USART_GetITStatus(USART1, USART_IT_TXE)==SET)
{
if(COM1.Byte_Out_Pointer<COM1.Send_Number)
{
USART_SendData(USART1