/********************************************************************************
* @file UART/UART_TwoBoards_ComIT/Src/uart.c
* @author jiahaoGao
* @brief This sample code shows how to use UART HAL API to transmit
* and receive a data buffer with a communication process based on
* IT transfer.
* The communication is done using 2 Boards.
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "uart.h"
#include "crc16.h"
UART_HandleTypeDef UartHandle;
UART_HandleTypeDef UartHandle2;
UART_HandleTypeDef UartHandle6;
uint8_t Usart1RxDataFlag;
uint8_t Usart2RxDataFlag;
uint8_t Usart6RxDataFlag;
uint8_t Usart1RxData;
uint8_t Usart2RxData;
uint8_t Usart6RxData;
static uint8_t Usart6RxDataBuffer[512]={0};
uint8_t Usart6RxBuffer[512];
uint8_t UART6SendBuffer[512];
uint16_t Rx6Counter=0;
uint16_t usart6framtlength=0;
uint16_t Usart6RxDataLength=0;
uint32_t UsartReceive_ToSendIradData_Count=0;
uint8_t IradSend_flag=0;
uint8_t iradSend_Flag[4]={0};
uint8_t iradSend_Primary_Bufeer232[4][236]={0};
uint8_t iradSend_length[4]={0};
uint32_t iradReceive_count[4]={0xffffffff,0xffffffff,0xffffffff,0xffffffff};
uint8_t LearningCode_Flag = 0;
uint8_t Rf433_Send_Usart1RxData_flag=0;
uint8_t Rf433_Send_DataLength;
uint8_t Send433Buffer[512];
static uint8_t Send433Buffer_Piecewise_stateData[10][230]={0};
uint8_t Send433Buffer_Piecewise[10][230];
uint8_t Rf433Rolling_code_Send_DataLength[10];
uint8_t Rf433_Send_format_Usart1RxData_flag=0;
uint8_t Rolling_code_count=0;
uint8_t Usart2RxBuffer[150];
uint8_t UART2SendBuffer[150];
static uint8_t Usart2RxDataBuffer[150];
uint8_t Usart2RxDataFlag=0;
uint8_t usart2framtlength=0;
uint8_t Rx2Counter;
uint32_t UsrtCRCValue;
uint32_t ChipUniqueID[2]={0x00000000,0x00000000};
uint8_t Upgrade_key[6]="ci09pq";
uint8_t firmware_update_Flag = 0;
uint8_t tianqiaoUartReveic_Flag=0;
void Get_ChipID(void)
{
/* 小端模式,低字节在前 */
ChipUniqueID[1] = *(__IO uint32_t *)(0x20003FE7); // 硬件软件版本
ChipUniqueID[0] = *(__IO uint32_t *)(0x20003FE3); // 硬件版本
}
void USART1_Configuration(void)//串口初始化函数
{
/*##-1- Configure the UART peripheral ######################################*/
UartHandle.Instance = USART1;
UartHandle.Init.BaudRate = 115200; //BaudRate = 115200 baud
UartHandle.Init.WordLength = UART_WORDLENGTH_8B; //Word Length = 8 Bits
UartHandle.Init.StopBits = UART_STOPBITS_1; //Stop Bit = One Stop bit
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; //Hardware flow control disabled (RTS and CTS signals) */
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
{
//Error_HandlerLed();
}
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
// Error_HandlerLed();
}
HAL_UART_Receive_IT(&UartHandle, &Usart1RxData, 1);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据�
Usart1RxDataFlag=0;
}
void USART2_Configuration(void)//串口初始化函数
{
/*##-1- Configure the UART peripheral ######################################*/
UartHandle2.Instance = USART2;
UartHandle2.Init.BaudRate = 115200; //BaudRate = 115200 baud
UartHandle2.Init.WordLength = UART_WORDLENGTH_8B; //Word Length = 8 Bits
UartHandle2.Init.StopBits = UART_STOPBITS_1; //Stop Bit = One Stop bit
UartHandle2.Init.Parity = UART_PARITY_NONE;
UartHandle2.Init.HwFlowCtl = UART_HWCONTROL_NONE; //Hardware flow control disabled (RTS and CTS signals)
UartHandle2.Init.Mode = UART_MODE_TX_RX;
UartHandle2.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_DeInit(&UartHandle2) != HAL_OK)
{
//Error_HandlerLed();
}
if(HAL_UART_Init(&UartHandle2) != HAL_OK)
{
// Error_HandlerLed();
}
HAL_UART_Receive_IT(&UartHandle2, &Usart2RxData, 1);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据�
}
void USART6_Configuration(void)//串口初始化函数
{
/*##-1- Configure the UART peripheral ######################################*/
UartHandle6.Instance = USART6;
UartHandle6.Init.BaudRate = 115200; //BaudRate = 115200 baud
UartHandle6.Init.WordLength = UART_WORDLENGTH_8B; //Word Length = 8 Bits
UartHandle6.Init.StopBits = UART_STOPBITS_1; //Stop Bit = One Stop bit
UartHandle6.Init.Parity = UART_PARITY_NONE;
UartHandle6.Init.HwFlowCtl = UART_HWCONTROL_NONE; //Hardware flow control disabled (RTS and CTS signals)
UartHandle6.Init.Mode = UART_MODE_TX_RX;
UartHandle6.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_DeInit(&UartHandle6) != HAL_OK)
{
//Error_HandlerLed();
}
if(HAL_UART_Init(&UartHandle6) != HAL_OK)
{
// Error_HandlerLed();
}
HAL_UART_Receive_IT(&UartHandle6, &Usart6RxData, 1);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据�
}
void UART1_Send(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit(&UartHandle, (uint8_t*)pData, Size,0x00ff)!= HAL_OK)
{
// Error_HandlerLed();
}
}
void UART2_Send(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit(&UartHandle2, (uint8_t*)pData, Size,0x00ff)!= HAL_OK)
{
// Error_HandlerLed();
}
}
void UART6_Send(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit(&UartHandle6, (uint8_t*)pData, Size,0x00ff)!= HAL_OK)
{
// Error_HandlerLed();
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
}
void USART6_IRQHandler(void)
{
uint8_t Res;
if((USART6->SR & USART_SR_TC)==USART_SR_TC)
{
HAL_UART_Receive_IT(&UartHandle6, &Usart6RxData, 1);
CLEAR_BIT(UartHandle6.Instance->SR, USART_SR_TC);
}
if((USART6->SR & USART_SR_ORE)==USART_SR_ORE)
{
HAL_UART_Receive_IT(&UartHandle6, &Usart6RxData, 1);
CLEAR_BIT(UartHandle6.Instance->SR, USART_SR_ORE);
}
if((USART6->SR & USART_SR_FE)==USART_SR_FE)
{
HAL_UART_Receive_IT(&UartHandle6, &Usart6RxData, 1);
CLEAR_BIT(UartHandle6.Instance->SR, USART_SR_FE);
}
if((USART6->SR & USART_SR_PE)==USART_SR_PE)
{
HAL_UART_Receive_IT(&UartHandle6, &Usart6RxData, 1);
CLEAR_BIT(UartHandle6.Instance->SR, USART_SR_PE);
}
if((__HAL_UART_GET_FLAG(&UartHandle6,UART_FLAG_RXNE)!=RESET)) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res = USART6->DR ;
Usart6RxBuffer[Rx6Counter] = Res; /* Receive data, clear flag */
Rx6Counter++;
if((Usart6RxBuffer[0]==0xfe)&&(Usart6RxBuffer[1]==0x88))
{
if(Rx6Counter>=4)
{
usart6framtlength=Usart6RxBuffer[4]+(((uint16_t)Usart6RxBuffer[3])<<8);
if(Rx6Counter==usart6framtlength)
{
Usart6RxDataFlag=1;
}
}
}
}
HAL_UART_Receive_IT(&UartHandle6, &Usart6RxData, 1);
//HAL_UART_IRQHandler(&UartHandle6);
}
/*********************