#include "type.h"
#include "UWater_adc_driver.h"
#include "UWater_power_app.h"
#include "UWater_eeprom_driver.h"
#include "UWater_frame_app.h"
#include "UWater_timer1us_driver.h"
#include "UWater_rtcc_app.h"
#include "UWater_ir_app.h"
#include "UWater_self_test.h"
#include "UWater_pt_app.h"
#include "UWater_lptimer_driver.h"

static MCU_MAC temp_mcu_mac;
static u8 g_testself_ee_msg_temp = 0;           
static u8 g_testself_ee_buf[7];
static u32 g_testself_tickstart = 0;
static u32 g_testself_adc_result=0;
//static u8  g_testself_adc_done=0;
static u8 g_test_self_buf[32];

u16 g_testself_result=0;

u8 step_TestSelf=TEST_SELF_MAC_GET;


static void McuUidCodeToMac(MCU_MAC *pmac)
{
	u8 temp_uid=0;
	//u8 temp_buf_uid[12];
	u8 i=0,j=0;
    for (i = 0; i < 10; i++)
	{
		temp_uid = *((u8*)(0x00100E74 + i));
		if(temp_uid != 0)
		{
			if(j<7)
			{
				pmac->buf[j] = temp_uid;
				j++;
			}
		}
	}
}

void SelfTest_Proc(void)
{
    switch(step_TestSelf)
    {
        case TEST_SELF_IDLE:
        {
            break;
        }
        case TEST_SELF_MAC_GET://生成物理地址，写入EEROM
        {
            g_testself_result =0;//测试结果清0
            McuUidCodeToMac(&temp_mcu_mac);
            //if(PowerIfSleepSys() == 1)
            {
                RtccSetTimer(RTCC_TIMER_BAT_NUM, 3); //3s后检测电池电压
            }
            step_TestSelf = TEST_SELF_EE_WR;
            break;
        }
        case TEST_SELF_EE_WR://写EE物理地址
        {
            if (TASK_IDLE == EepIfIdle())
            {
                g_testself_ee_msg_temp = 0;
                EepWrite(temp_mcu_mac.buf, EEP_FRAME_MAC_ADDR, 7, &g_testself_ee_msg_temp);
                step_TestSelf  =TEST_SELF_EE_WR_WT;
            }
            break;
        }
        case TEST_SELF_EE_WR_WT://等待写完成，EE掉电
        {
            if (TASK_IDLE == EepIfIdle())
            {
                g_testself_tickstart = Timer1usGetTick();//延时200ms
                step_TestSelf  =TEST_SELF_EE_OFF_WT;			
            }
            break;
        }
        case TEST_SELF_EE_OFF_WT://等待EE掉电200ms,读EE物理地址
        {
            if((Timer1usGetTick() - g_testself_tickstart)>200000)//100ms
            {
                EepRead(EEP_FRAME_MAC_ADDR, &g_testself_ee_buf[0], 7, &g_testself_ee_msg_temp);
                step_TestSelf  =TEST_SELF_EE_RD_CK;
            }
            break;
        }

        case TEST_SELF_EE_RD_CK://检测EE读取是否正确
        {
            if (TASK_IDLE == EepIfIdle())
            {
                int temp_i;
                for(temp_i=0; temp_i<7; temp_i++)
                {
                    g_test_self_buf[temp_i] = temp_mcu_mac.buf[temp_i];
                    if(g_testself_ee_buf[temp_i] != temp_mcu_mac.buf[temp_i])
                    {
	                     break;
                    } 	
                }
                if(temp_i == 7)
                {
                    g_testself_result |= 0x0001;
                }
                g_testself_tickstart = Timer1usGetTick();
                step_TestSelf  =TEST_SELF_HALL_CK;
            }
            break;
        }
        case TEST_SELF_HALL_CK://检测霍尔
        {
            //if((KEY_GPIO->IDR & KEY_PIN) == (u32)0)
            {
                g_testself_result |= 0x0002;
            }
            if((Timer1usGetTick() - g_testself_tickstart)>100000)//100ms
            {
                RtccSetTimer(TIMER_RTCC_TEST_SELF, 2);
                g_testself_tickstart = Timer1usGetTick();
                step_TestSelf  =TEST_SELF_RTCC_CK;
            }
            break;
        }
        case TEST_SELF_RTCC_CK://检测RTCC
        {
             if((0 == RtccCheckTimer(TIMER_RTCC_TEST_SELF))&&((Timer1usGetTick() - g_testself_tickstart)>1000000))  
             {
				 if((0 == RtccIsError()))//RTC无异常
				 {
					g_testself_result |= 0x0004;
				 }
				 else//如果RTC异常,则再次初始化,防止单晶帧首次上电GP30没初始化
				 {
					u8 rtcc_clk_source = RtccInit(); //再次初始化
					 LPTimer1Init(rtcc_clk_source);
					 if((0 == RtccIsError()))//RTC无异常
					 {
						g_testself_result |= 0x0004;
					 }
				 }
             }
             if((Timer1usGetTick() - g_testself_tickstart)>3000000)//3s
             {
				 step_TestSelf  = TEST_SELF_IR_CK1;
             }
             break;
        }
        case TEST_SELF_IR_CK1://检测蓝牙
        {
			#if (MODULE_BLE_USED)
				if(BleIfIdle()==TASK_IDLE)
				{
					if(BleGetPowerState()!=BlePowerWrong)
					{
						g_testself_result |= 0x0010;
					}
					step_TestSelf  = TEST_SELF_SEND_RESULT;
				}
			#else
				step_TestSelf  = TEST_SELF_SEND_RESULT;
			#endif
            break;
        }
        case TEST_SELF_IR_CK2:
        {
//            if (TRUE == g_testself_adc_done)
//            {
//                if (ADC_OK == AdcGetStatus())
//                {
//                    IrKey_AdcInitForUart();//ADC初始化串口使用
//                    IrInitApp();//打开串口
//                    if((g_testself_adc_result < 3000)&&((g_testself_adc_result > 2000)))
//                    {
//                         g_testself_result |= 0x0008;
//                    }
//                    step_TestSelf = TEST_SELF_SEND_RESULT;
//                }
//            }
            break;
        }
        case TEST_SELF_SEND_RESULT://??GP22??
		{
			UNION_FLOAT_STORAGE temp;
			g_test_self_buf[7]= g_testself_result&0xFF;   //??????
			g_test_self_buf[8]= (g_testself_result>>8)&0xFF;//??????
			g_test_self_buf[9]= PowerCheckValueSys()&0xFF;  //??????
			g_test_self_buf[10]= ( PowerCheckValueSys()>>8)&0xFF;//??????
			g_test_self_buf[11]= g_testself_adc_result&0xFF;  //??????
			g_test_self_buf[12]= ( g_testself_adc_result>>8)&0xFF;//??????
			temp.float_datas = TempCheckValueApp();
			g_test_self_buf[13]=temp.float_storage_format.data_buf[0];
			g_test_self_buf[14]=temp.float_storage_format.data_buf[1];
			g_test_self_buf[15]=temp.float_storage_format.data_buf[2];
			g_test_self_buf[16]=temp.float_storage_format.data_buf[3];

			g_testself_tickstart = Timer1usGetTick();

			step_TestSelf = TEST_SELF_SEND_WAIT;
            break;
        }
		case TEST_SELF_SEND_WAIT:
		{
			if((Timer1usGetTick() - g_testself_tickstart)>2000000)//2s
			{
				step_TestSelf  =TEST_SELF_IDLE;
			}
			break;
		}
    }
}

void SelfTest_Start(void)
{
	if(step_TestSelf == TEST_SELF_IDLE)
	{
		step_TestSelf = TEST_SELF_MAC_GET;
	}
}
void SelfTest_End(void)
{
    step_TestSelf = TEST_SELF_IDLE;
}

u8 SelfTest_GetResult(u8 *pBuf)
{
    if(step_TestSelf == TEST_SELF_SEND_WAIT)
    {
		u8 i=0;
		for(i=0;i<17;i++)
		{
			*pBuf++ = g_test_self_buf[i];
		}
		step_TestSelf = TEST_SELF_IDLE;
		return 17;
    }
		return 0;
}



















