440 lines
16 KiB
C
440 lines
16 KiB
C
#include "usart4.h"
|
||
char temp_data[100];
|
||
//串口接收缓存区
|
||
//Usart4_Fram_Record_Struct Usart4_Fram_Record_Structs;
|
||
u8 USART4_TX_BUF[USART4_MAX_SEND_LEN]; //发送缓冲
|
||
void USART4_Configuration(u32 baund)
|
||
{
|
||
USART_InitTypeDef USART_InitStructure;
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); //使能USART4
|
||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); //GPIOc时钟
|
||
|
||
//NVIC_Configuration();
|
||
|
||
USART_DeInit(UART4); //复位串口1
|
||
|
||
//USART1_TX PA.9
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
//USART1_RX PA.10
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||
|
||
|
||
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
|
||
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
|
||
|
||
|
||
USART_InitStructure.USART_BaudRate = baund;
|
||
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_Init(UART4,&USART_InitStructure);
|
||
|
||
USART_Cmd(UART4,ENABLE);
|
||
|
||
|
||
//Usart1 NVIC 配置
|
||
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//串口1中断通道
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=5;//抢占优先级3
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
|
||
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
|
||
USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);//开启USART1的接收中断和空闲中断
|
||
USART_ClearFlag(UART4,USART_FLAG_TC); //清除发送完成标志位
|
||
USART_ITConfig(UART4,USART_IT_IDLE,ENABLE);//开启USART1的接收中断和空闲中断
|
||
}
|
||
void timer4_init(void)
|
||
{
|
||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
||
TIM_TimeBaseStructure.TIM_Period = 999;
|
||
TIM_TimeBaseStructure.TIM_Prescaler = 3199;//中断时间10ms
|
||
TIM_TimeBaseStructure.TIM_ClockDivision = 1;
|
||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||
|
||
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
|
||
|
||
/* TIM IT enable */
|
||
TIM_ITConfig(TIM4,TIM_IT_Update, ENABLE);
|
||
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
|
||
/* TIM4 enable counter */
|
||
TIM_Cmd(TIM4, DISABLE);
|
||
|
||
/* Enable the TIM4 Interrupt */
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
}
|
||
//设置TIM4的开关
|
||
//sta:0,关闭;1,开启;
|
||
void TIM4_Set(unsigned char sta)
|
||
{
|
||
if(sta)
|
||
{
|
||
|
||
TIM_SetCounter(TIM4,0);//计数器清空
|
||
TIM_Cmd(TIM4, ENABLE); //使能TIMx
|
||
}else TIM_Cmd(TIM4, DISABLE);//关闭定时器2
|
||
}
|
||
|
||
/**
|
||
* @brief This function handles TIM4 global interrupt request.
|
||
* @param None
|
||
* @retval None
|
||
*/
|
||
void TIM4_IRQHandler(void)
|
||
{
|
||
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
|
||
{
|
||
ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag = 1;
|
||
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
|
||
TIM4_Set(0); //关闭TIM4
|
||
|
||
}
|
||
}
|
||
|
||
void Usart4_ReceiveBuff_Clear(void)
|
||
{
|
||
ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag=0;
|
||
ESP8266_Fram_Record_Struct .InfBit .FramLength=0;
|
||
memset(ESP8266_Fram_Record_Struct.Data_RX_BUF,0,RX_BUF_MAX_LEN);
|
||
//memset(USART4_TX_BUF,0,USART4_MAX_SEND_LEN);
|
||
}
|
||
|
||
void UART4_IRQHandler(void)
|
||
{
|
||
u8 ucCh;
|
||
|
||
if(USART_GetITStatus( UART4, USART_IT_RXNE ) != RESET )
|
||
{
|
||
ucCh = USART_ReceiveData( UART4 );
|
||
|
||
if(ESP8266_Fram_Record_Struct .InfBit .FramLength < ( RX_BUF_MAX_LEN - 1 ) )
|
||
{
|
||
//留最后一位做结束位
|
||
ESP8266_Fram_Record_Struct .Data_RX_BUF[ ESP8266_Fram_Record_Struct .InfBit .FramLength ++ ] = ucCh;
|
||
// TIM_SetCounter(TIM4,0);//计数器清空
|
||
// TIM4_Set(1); //使能定时器2的中断
|
||
}
|
||
else
|
||
{
|
||
ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag = 1;
|
||
}
|
||
}
|
||
|
||
if(USART_GetITStatus( UART4, USART_FLAG_ORE ) != RESET )
|
||
{
|
||
USART_ReceiveData(UART4);
|
||
USART_ClearFlag(UART4, USART_FLAG_ORE);
|
||
}
|
||
|
||
// if(USART_GetITStatus( UART4, USART_FLAG_FE ) != RESET )
|
||
// {
|
||
// USART_ReceiveData(UART4);
|
||
// USART_ClearFlag(UART4, USART_FLAG_FE);
|
||
// }
|
||
//
|
||
// if(USART_GetITStatus( UART4, USART_FLAG_PE ) != RESET )
|
||
// {
|
||
// USART_ReceiveData(UART4);
|
||
// USART_ClearFlag(UART4, USART_FLAG_PE);
|
||
// }
|
||
if( USART_GetITStatus( UART4, USART_IT_IDLE ) != RESET ) //如果总线空闲
|
||
{
|
||
ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag = 1;
|
||
ucCh = USART_ReceiveData( UART4 ); //由软件序列清除中断标志位(先读USART_SR,然后读USART_DR)
|
||
}
|
||
}
|
||
|
||
//串口4,printf 函数
|
||
//确保一次发送数据不超过USART4_MAX_SEND_LEN字节
|
||
void u4_printf(char* fmt,...)
|
||
{
|
||
u16 i,j;
|
||
va_list ap;
|
||
va_start(ap,fmt);
|
||
vsprintf((char*)USART4_TX_BUF,fmt,ap);
|
||
va_end(ap);
|
||
i=strlen((const char*)USART4_TX_BUF); //此次发送数据的长度
|
||
for(j=0;j<i;j++) //循环发送数据
|
||
{
|
||
while(USART_GetFlagStatus(UART4,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕
|
||
USART_SendData(UART4,USART4_TX_BUF[j]);
|
||
}
|
||
}
|
||
|
||
u8 USART4_Recieve_Ide(void)
|
||
{
|
||
u8 i = 0;
|
||
u32 RTC_T1,Alarm_T2;//Uptime_T3;
|
||
RTC_AlarmTypeDef RTC_AlarmStructs;
|
||
RTC_DateTypeDef nowdatas;
|
||
char *recString;
|
||
char *recStringNext;
|
||
char adda;
|
||
char hhmmss[7]={'\0'},yymmdd[10]={'\0'};
|
||
if( ESP8266_Fram_Record_Struct .InfBit .FramFinishFlag==1)
|
||
{
|
||
/************恢复出厂设置参数***********/
|
||
if((strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-rst")!=NULL))//接收恢复出厂设置
|
||
{
|
||
Write_Factory_Par();
|
||
EEPROM_ReadBytes(140,&Par_store_flag,1);
|
||
if(Par_store_flag)ESP8266_SendString(DISABLE,"parw-rst,OK\r\n",13,0);
|
||
else ESP8266_SendString(DISABLE,"parw-rst,fail\r\n",15,0);
|
||
|
||
}
|
||
/*写入******测量间隔、预约时间、测量模式、远传时间*********/
|
||
if((strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-1")!=NULL))//判断接收信息是否是命令参数设置
|
||
{
|
||
memset(temp_data,0,100);
|
||
for (i = 0 ; i < 5 ; i++)
|
||
{
|
||
if (i == 0)
|
||
{
|
||
if ((recString = strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-1")) == NULL)
|
||
;
|
||
recString+=6;
|
||
}
|
||
else
|
||
{
|
||
recString++;
|
||
if((recStringNext = strstr(recString, ",")) != NULL)
|
||
{
|
||
switch(i)
|
||
{
|
||
case 1: memset(Init_Data_Equipment.Measure_Interval,0,5);
|
||
memcpy(Init_Data_Equipment.Measure_Interval, recString, recStringNext - recString);break; //获取测量间隔
|
||
case 2: memset(Init_Data_Equipment.appointment_time,0,8);
|
||
memcpy(Init_Data_Equipment.appointment_time, recString, recStringNext - recString);break; //预约时间
|
||
case 3: memset(Init_Data_Equipment.measure_mode,0,1);
|
||
memcpy(Init_Data_Equipment.measure_mode, recString, recStringNext - recString);break; //测量模式
|
||
case 4: memset(Init_Data_Equipment.Pub_Time,0,6);
|
||
memcpy(Init_Data_Equipment.Pub_Time, recString, recStringNext - recString);break; //IOT传输时间
|
||
//case 5:memcpy(temp_data, recString, 12);break; //获取系统时间
|
||
default:break;
|
||
}
|
||
}
|
||
recString=recStringNext;
|
||
if(i==3)
|
||
{
|
||
recStringNext=strstr(recString,"\r\n");
|
||
recString++;
|
||
memset(Init_Data_Equipment.Pub_Time,0,6);
|
||
memcpy(Init_Data_Equipment.Pub_Time, recString, 6);
|
||
}
|
||
}
|
||
}
|
||
RTC_T1=(Init_Data_Equipment.Measure_Interval[0]*10000+
|
||
Init_Data_Equipment.Measure_Interval[1]*1000+
|
||
Init_Data_Equipment.Measure_Interval[2]*100+
|
||
Init_Data_Equipment.Measure_Interval[3]*10+
|
||
Init_Data_Equipment.Measure_Interval[4]*1);
|
||
if(RTC_T1<60)//RTC_T1表示测量间隔时间S
|
||
sprintf(Init_Data_Equipment.Measure_Interval,"%s","00060");//测量间隔最小60S
|
||
Write_Equipment_Par(Init_Data_Equipment);
|
||
ESP8266_SendString(DISABLE,"parw-1,OK\r\n",11,0);
|
||
|
||
}
|
||
/*写入******远传IP、远传端口、wifi名称、wifi密码、wifi端口*********/
|
||
if((strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-2")!=NULL)&(strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "\"")!=NULL))
|
||
{
|
||
for (i = 0 ; i < 5 ; i++)
|
||
{
|
||
if (i == 0)
|
||
{
|
||
if ((recString = strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-2")) != NULL)
|
||
;//解析错误
|
||
recString+=6;
|
||
}
|
||
else
|
||
{
|
||
recString++;
|
||
if((recStringNext = strstr(recString, ",")) != NULL)
|
||
{
|
||
switch(i)
|
||
{
|
||
case 1: memset(Init_Data_Equipment.Bc25_Ip,0,20);
|
||
memcpy(Init_Data_Equipment.Bc25_Ip, recString, recStringNext - recString);break; //iot ip
|
||
case 2: memset(Init_Data_Equipment.Bc25_Port,0,10);
|
||
memcpy(Init_Data_Equipment.Bc25_Port, recString, recStringNext - recString);break; //iot port
|
||
case 3: memset(Init_Data_Equipment.ESP8266_AP_SSIDs,0,20);
|
||
memcpy(Init_Data_Equipment.ESP8266_AP_SSIDs, recString, recStringNext - recString);break; //wifi 名称
|
||
case 4: memset(Init_Data_Equipment.ESP8266_AP_PWDs,0,10);
|
||
memcpy(Init_Data_Equipment.ESP8266_AP_PWDs, recString, recStringNext - recString);break; //wifi 密码
|
||
//case 5:memcpy(temp_data, recString, 12);break; //获取系统时间
|
||
default:break;
|
||
}
|
||
}
|
||
recString=recStringNext;
|
||
}
|
||
|
||
}
|
||
memcpy(Init_Data_Equipment.ESP8266_AP_Ports, recString+1, 4); //获取wifi port
|
||
Write_Equipment_Par(Init_Data_Equipment);
|
||
ESP8266_SendString(DISABLE,"parw-2,OK\r\n",11,0);
|
||
}
|
||
|
||
/*写入******立刻启动测量*********/
|
||
if((strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-3")!=NULL))
|
||
{
|
||
if ((recString = strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-3,1")) != NULL)
|
||
{
|
||
if(TASKRUNSS.APPO_RUN|TASKRUNSS.PROS1_RUN)//判断测量任务是否被占用
|
||
{
|
||
ESP8266_SendString(DISABLE,"parw-3,busy\r\n",13,0);
|
||
}else
|
||
{
|
||
MYITStatus1.WKUP_MEA_WIFI=1;
|
||
osThreadResume(ThreadIdProcsee1);//任务0恢复
|
||
ESP8266_SendString(DISABLE,"parw-3,OK\r\n",11,0);
|
||
}
|
||
}
|
||
if ((recString = strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-3,0")) != NULL)
|
||
{
|
||
// osThreadSuspend(ThreadIdProcsee0);//任务0挂起
|
||
// RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
|
||
// ESP8266_SendString(DISABLE,"OK\r\n",4,0);
|
||
}
|
||
|
||
}
|
||
/*设置******系统时间*********/
|
||
if((recString=strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-4")) !=NULL)
|
||
{
|
||
memset(temp_data,0,200);
|
||
recString+=7;
|
||
memcpy(temp_data, recString, 13); //获取系统时间
|
||
if(((temp_data[2]-'0')*10+(temp_data[3]-'0')*1)>12|
|
||
((temp_data[4]-'0')*10+(temp_data[5]-'0')*1)>31|
|
||
((temp_data[7]-'0')*10+(temp_data[8]-'0')*1)>23|
|
||
((temp_data[9]-'0')*10+(temp_data[10]-'0')*1)>59|
|
||
((temp_data[11]-'0')*10+(temp_data[12]-'0')*1)>59)
|
||
{
|
||
ESP8266_SendString(DISABLE,"parw-4,fail\r\n",13,0);
|
||
|
||
}else
|
||
{
|
||
RTC_T1=RTC_GetSeconds();//获取更新前RTC-time-S
|
||
RTC_GetDate(RTC_Format_BIN,&nowdatas);//获取日期
|
||
SetRTC(((temp_data[0]-'0')*10+(temp_data[1]-'0')*1),
|
||
((temp_data[2]-'0')*10+(temp_data[3]-'0')*1),
|
||
((temp_data[4]-'0')*10+(temp_data[5]-'0')*1),
|
||
((temp_data[7]-'0')*10+(temp_data[8]-'0')*1),
|
||
((temp_data[9]-'0')*10+(temp_data[10]-'0')*1),
|
||
((temp_data[11]-'0')*10+(temp_data[12]-'0')*1));
|
||
|
||
|
||
if((RTC->CR)&0x100)//已经开启了远传闹钟
|
||
{
|
||
//防止设置时间与闹钟时间差
|
||
|
||
RTC_GetAlarm(RTC_Format_BIN,RTC_Alarm_A,&RTC_AlarmStructs);
|
||
Alarm_T2=RTC_AlarmStructs.RTC_AlarmDateWeekDay*86400+
|
||
RTC_AlarmStructs.RTC_AlarmTime.RTC_Hours*3600+
|
||
RTC_AlarmStructs.RTC_AlarmTime.RTC_Minutes*60+
|
||
RTC_AlarmStructs.RTC_AlarmTime.RTC_Seconds*1;//获取ALARMA的值
|
||
RTC_AlarmAConfig(Alarm_T2-RTC_T1);//更新闹钟时间
|
||
}
|
||
if((RTC->CR)&0x200)//已经开启间隔闹钟
|
||
{
|
||
if(wut_rtc_s>RTC_T1)RTC_T1+=86400;
|
||
RtcWakeUpConfig(RTC_T1-wut_rtc_s);
|
||
}
|
||
ESP8266_SendString(DISABLE,"parw-4,OK\r\n",11,0);
|
||
}
|
||
}
|
||
|
||
/*写入******传感器地址修改*********/
|
||
if((recString=strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parw-5")) !=NULL) //parw-aAb,0A1
|
||
{
|
||
recString=strstr(recString, ",");
|
||
recString++;
|
||
recStringNext=recString+2;
|
||
Sdi_chanegadd(recString[0],recStringNext[0]);
|
||
adda=Sdi_Readone(recStringNext[0]);
|
||
if(adda==0)
|
||
{
|
||
ESP8266_SendString(DISABLE,"parw-5,OK\r\n",11,0);
|
||
//数值相同
|
||
}else
|
||
ESP8266_SendString(DISABLE,"parw-5,fail\r\n",13,0);
|
||
|
||
|
||
}
|
||
/*读取******测量间隔、预约时间、测量模式、远传时间*********/
|
||
if((recString=strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-1")) !=NULL)
|
||
{
|
||
//&&(strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "\r\n"))!=NULL)
|
||
memset(temp_data,0,200);
|
||
|
||
Init_Data_Equipment=Read_Equipment_Par();
|
||
sprintf(temp_data,"parr-1,%s,%s,%s,%s\r\n",
|
||
Init_Data_Equipment.Measure_Interval,
|
||
Init_Data_Equipment.appointment_time,
|
||
Init_Data_Equipment.measure_mode,
|
||
Init_Data_Equipment.Pub_Time
|
||
);
|
||
ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0);
|
||
}
|
||
|
||
/*读取******远传IP、远传端口、wifi名称、wifi密码、wifi端口*********/
|
||
if(strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-2"))
|
||
{
|
||
memset(temp_data,0,200);
|
||
GetRTC(hhmmss,yymmdd);
|
||
sprintf(temp_data,"%s,%s,%s,%s,%s,%s\r\n",
|
||
"parr-2",
|
||
Init_Data_Equipment.Bc25_Ip,
|
||
Init_Data_Equipment.Bc25_Port,
|
||
Init_Data_Equipment.ESP8266_AP_SSIDs,
|
||
Init_Data_Equipment.ESP8266_AP_PWDs,
|
||
Init_Data_Equipment.ESP8266_AP_Ports
|
||
);
|
||
ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0);
|
||
|
||
}
|
||
|
||
/*读取******系统时间 日期*********/
|
||
if(strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-4"))
|
||
{
|
||
memset(temp_data,0,200);
|
||
GetRTC(hhmmss,yymmdd);
|
||
sprintf(temp_data,"%s,%s,%s\r\n","parr-4",yymmdd,hhmmss);
|
||
ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0);
|
||
}
|
||
|
||
/*读取******传感器地址查询*********/
|
||
if((recString=strstr(ESP8266_Fram_Record_Struct.Data_RX_BUF, "parr-s?")) !=NULL)
|
||
{
|
||
memset(temp_data,0,200);
|
||
adda=Sdi_readaddress();
|
||
if(adda!=0x7f)
|
||
{
|
||
sprintf(temp_data,"Address:%c\r\n",adda);
|
||
ESP8266_SendString(DISABLE,temp_data,strlen(temp_data),0);
|
||
}
|
||
else
|
||
ESP8266_SendString(DISABLE,"Address,fail\r\n",14,0);
|
||
}
|
||
Usart4_ReceiveBuff_Clear();
|
||
}
|
||
return 0;
|
||
}
|