#include "CommandsDefine.h"
#include "ProjectConfig.h"
#include "AnalogChannelCtrl.h"
#include "AnalogChannelCtrl_2d5g_base.h"
#include "sys.h"
#include "timer.h"
#include "delay.h"
#include "iwdg.h"
#include "DS18B20.h"
#include "iap.h"
#include "Helper.h"
//#include "OuterPanel_LED.h"
//#include "Probe.h"
int  flag = 0;
int port1 = 4;
//int port2 = 7;
int dacid = 1;
//u8 testLed =0;
//u8 ledTestEnable =0;
int main(void)
{
    MainInit();
		Helper_CRC32_Init();	
		//LED_ALL(0xFF);
#ifndef RUNNING_WHERE_AT_BOOT	
		int i=0;
		DS18B20_Get_Temp();
		DS18B20_Get_Temp();
		DAC_AD5675((1<<(16+5))|( unsigned short)(0x7FFF),1);
		DAC_AD5675((1<<(16+5))|( unsigned short)(0x7FFF),2);
		u8 setData1= 0x1f;
		u8 setData2= 0x00;
		for(int i=0; i<MAX_CHANNEL_NUM; i++)
		{			 
			DSA_PE43711(LMH6401_ADDR_TEMPCORRECT_GAIN,setData1,1+i);	
			DSA_PE43711(LMH6401_ADDR_TEMPCORRECT_FREQ,setData2,1+i);				 
		}		
	
		
		//启动后通知APP，需要重新发送通道等信息，是一个Mcu意外重新启动后的补救措施
		Command_SendbackAppStartTime();
		int delay50usTimes=0;

		while(delay50usTimes<1000)
		{
				if(i%100==0)	
				{
					IWDG_Feed();//喂狗
				}		
				i++;
				delay_us(50);
				delay50usTimes++;
		}			
		i=0;		
		Command_SendbackAppStartTime();//间隔500ms发两次				
#else
	  iap_WriteBootVersionInfo();
#endif

    while(1)
    {			
			delay_us(50);
#ifndef RUNNING_WHERE_AT_BOOT	
				if(i%100==0)	
				{
					IWDG_Feed();//喂狗
				}
				
				AnalogChCtrl_ProtectProcess();	
        Command_ProcessRequestCmds();
				if(IsChannelSetting==1)	
				{
							continue;
				}		
				if(i>=1000) 
				{
					Timer_ReadBack_ProbeStatus(1,1);
					DS18B20_Get_Temp();
					i=0;	  
				}
				i++;
#else				
        Command_ProcessRequestCmds();
#endif
    }
}

