/*************************************************************************************/
#include "UWater_eeprom_driver.h"
#include "UWater_timer1uS_driver.h" // Device header
#include "wdt.h"

/*------------------------------ Variable Define -------------------------------------*/
u8 s_g_EE_main_state = cst_EE_M_S0; //主状态机状态
static u8 s_g_EE_timeout_num = 0; //超时次数
static u8 s_g_I2C_State = cst_I2C_S0; //子状态机状态
u8* g_pData; //全局数据传递指针
u8* g_pData_Temp; //用于保存申请者原始数据指针
u8 ee_len;//数据长度
u8 ee=0;
//u8 total_len;       //数据总长度
u8* g_pMsg; //全局Message指针
u32 Timer1uSBase;
u32 odd_timer=0;
u32 readend_timer=0;
static u16 s_g_I2C_IO_Idx = 0; //申请空间下标
static u16 s_g_I2C_Len = 0; //要读些的字节数
static u16 s_g_StartPageAddr = 0; //起始页的相对地址
static u16 s_g_StartPageNum = 0; //起始页要写入数据数量
static u16 s_g_PageNum = 0; //要写入的页数
static u16 s_g_TurnPage = 0;
static u16 s_g_LastPage_Num = 0; //不足一页的数据个数
static u16 s_g_MultPageflag = ERROR; //跨页写入标志
TWO_BYTE_TO_WORD target_addr; //目标地址(2字节)
EE_Message g_EE_Msg; //Message信息结构
M0P_I2C_TypeDef *hi2c1;

/*------------------------------ Function Define -------------------------------------*/
static void PointToIicSeqread(void); //启动I2C读EEP
static void PointToIicSeqwrite(void); //启动I2C写EEP
static void GetIicResult(void); // 返回I2C状态机结果给申请者指针
static u8 I2cCheckCompleted(void); //查寻I2C状态机是否完成
static u8 I2cCheckRorW(void); //查寻I2C状态机是读还是写
static u8 CheckTimer(u32 timer);
static void SetTimer(u32 timer);

/*************************************************************************************
 * @fun_name:	EepInit(void)
 * @brief   :
 * @param[in] :
 * @param[out] :None
 * @retval     :None
 * @other     :  I2C初始化
 *************************************************************************************/
void EepInit(void)
{
    stc_gpio_cfg_t stcGpioCfg;
    stc_i2c_cfg_t stcI2cCfg;

    DDL_ZERO_STRUCT(stcGpioCfg);
    DDL_ZERO_STRUCT(stcI2cCfg);
     
    stcGpioCfg.enDir = GpioDirOut;
	stcGpioCfg.enDrv = GpioDrvH;      ///< 端口驱动能力配置->高驱动能力 
    stcGpioCfg.enPu = GpioPuDisable;
	stcGpioCfg.enPd = GpioPdDisable; 
	stcGpioCfg.enOD = GpioOdDisable;  ///< 端口开漏输出配置->无
	
    Gpio_Init(EE_POWER_GPIOx, EE_POWER_PIN, &stcGpioCfg);

    stcGpioCfg.enDir = GpioDirOut;                         ///< 端口方向配置
    stcGpioCfg.enOD = GpioOdEnable;                        ///< 端口开漏输出配置
    stcGpioCfg.enPu = GpioPuDisable;                        ///< 端口上拉配置
    stcGpioCfg.enPd = GpioPdDisable;                       ///< 端口下拉配置->禁止
    Gpio_Init(EE_SCL_GPIOx, EE_SCL_PIN, &stcGpioCfg);
    Gpio_Init(EE_SDA_GPIOx, EE_SDA_PIN, &stcGpioCfg);

    Gpio_SetAfMode(EE_SCL_GPIOx, EE_SCL_PIN, EE_SCL_AFSEL);
    Gpio_SetAfMode(EE_SDA_GPIOx, EE_SDA_PIN, EE_SDA_AFSEL);

    POWER_EE_DOWN;

#if i2c0
    hi2c1 = M0P_I2C0;

    MCU_I2C_CLK_ENABLE();

    Reset_RstPeripheral0(ResetMskI2c0);

    stcI2cCfg.u32Pclk = Sysctrl_GetPClkFreq();  ///< ??PCLK??
    stcI2cCfg.u32Baud = I2C_CLK;                ///< 10KHz
    stcI2cCfg.enMode = I2cMasterMode;           ///< ????
    stcI2cCfg.u8SlaveAddr = 0x55;               ///< ???,?????
    stcI2cCfg.bGc = FALSE;                      ///< ??????????
    I2C_Init(hi2c1,&stcI2cCfg);                 ///< ?????

    /* Peripheral interrupt init*/
    //设置中断优先级
    EnableNvic(I2C0_IRQn, IrqLevel2, TRUE);     ///< ??????
#endif

#if i2c1
    hi2c1 = M0P_I2C1;

    MCU_I2C_CLK_ENABLE();

    Reset_RstPeripheral0(ResetMskI2c1);
       
    stcI2cCfg.u32Pclk = Sysctrl_GetPClkFreq();  ///< 获取PCLK时钟
    stcI2cCfg.u32Baud = I2C_CLK;                ///< 10KHz
    stcI2cCfg.enMode = I2cMasterMode;           ///< 主机模式
    stcI2cCfg.u8SlaveAddr = 0x55;               ///< 从地址，主模式无效
    stcI2cCfg.bGc = FALSE;                      ///< 广播地址应答使能关闭
    I2C_Init(hi2c1,&stcI2cCfg);                 ///< 模块初始化

    /* Peripheral interrupt init*/
    //设置中断优先级
    EnableNvic(I2C0_IRQn, IrqLevel2, TRUE);     ///< 
#endif

}
/*************************************************************************************
 * @fun_name:	EepMachineDriver(void)
 * @brief   :
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :  前台主状态机
 *************************************************************************************/
void EepMachineDriver(void)
{
    switch (s_g_EE_main_state) //IDLE状态
    {
        case cst_EE_M_S0:
        break;
		
        case cst_EE_M_S1: //启动读任务
        {
            s_g_EE_main_state = cst_EE_M_S3;
            PointToIicSeqread();
            SetTimer(600000); //设定600ms超时时间
        }
        break;
        case cst_EE_M_S2: //启动写任务
        {
            s_g_EE_main_state = cst_EE_M_S3;
            PointToIicSeqwrite();
            SetTimer(600000); //设定600ms超时时间
        }
        break;
        case cst_EE_M_S3: //查询超时时间内是否完成
        {
            if(CheckTimer(600000)) //定时时间到
            {
			I2C_ClearIrq(hi2c1);//关闭一下中断
            if (s_g_EE_timeout_num == 0)
            {
                if (SUCCESS == I2cCheckRorW()) //超时未完成，重新执行写任务
                {
                    ++s_g_EE_timeout_num;
                    GetIicResult();
                    I2CIE_CLOSE;
                    POWER_EE_DOWN;
                    MCU_I2C_CLK_DISABLE();
                    SetTimer(600000);
                    s_g_EE_main_state = cst_EE_M_S7;
                }
                else //超时未完成，重新执行读任务
                {
                    ++s_g_EE_timeout_num;
                    GetIicResult();
                    I2CIE_CLOSE;
                    POWER_EE_DOWN;
                    MCU_I2C_CLK_DISABLE();
                    SetTimer(600000);
                    s_g_EE_main_state = cst_EE_M_S8;
                }
            }
            else //两次均超时，重启！
            {
                s_g_EE_timeout_num = 0;
                GetIicResult();
                POWER_EE_DOWN;
                SetTimer(600000);
                s_g_EE_main_state = cst_EE_M_S4;
            }
        }
        else
        {
            if (SUCCESS == I2cCheckCompleted())
            {
                if (SUCCESS == I2cCheckRorW()) //写任务，等50mS
                {
                    GetIicResult();
                    s_g_EE_timeout_num = 0;
                    SetTimer(50000);
                    s_g_EE_main_state = cst_EE_M_S6;
                }
                else
                {
                    if((Timer1usGetTick() - readend_timer) > 200) //读中断任务结束延时200us后EE断电
                    {
                        GetIicResult(); //读任务，返回IDLE
                        s_g_EE_timeout_num = 0;
                        I2CIE_CLOSE;
                        POWER_EE_DOWN;
                        MCU_I2C_CLK_DISABLE();
                        s_g_EE_main_state = cst_EE_M_S0;
                    }
                }
            }
            else
            {
                if(s_g_TurnPage == TRUE)
                {
                    if((Timer1usGetTick() - odd_timer) > 5000)
                    {
                        s_g_TurnPage = FALSE;
                        I2C_SetFunc(hi2c1,I2cStart_En);
                    }
                }
                s_g_EE_main_state = cst_EE_M_S3;
            }
        }
    }
    break;
    case cst_EE_M_S4: //重启后发送Start
    {
        if(CheckTimer(600000))
        {
            EepInit();
            POWER_EE_UP;           
            I2CIE_OPEN;
            I2C_SetFunc(hi2c1, I2cStart_En); //发送起始条件
            SetTimer(600000);
            s_g_EE_main_state = cst_EE_M_S5;
        }
        else
            s_g_EE_main_state = cst_EE_M_S4;
    }
    break;
    case cst_EE_M_S5: //恢复正常后发送Stop
    {
        if(CheckTimer(600000))
        {
            if( I2c_GET_FLAG(hi2c1, I2C_ISR_START))
            {
                I2C_SetFunc(hi2c1, I2cStop_En);
                I2CIE_CLOSE;
                POWER_EE_DOWN;
                MCU_I2C_CLK_DISABLE();
                s_g_EE_main_state = cst_EE_M_S0;
            }
            else //不正常，则正常位清0
            {
                g_EE_Msg.structs.EE_normal = ERROR;
                I2CIE_CLOSE;
                POWER_EE_DOWN;
                MCU_I2C_CLK_DISABLE();
                s_g_EE_main_state = cst_EE_M_S0;
            }
        }
        else
        {
            s_g_EE_main_state = cst_EE_M_S5;
        }
       break;
    }

    case cst_EE_M_S6:
    {
        if (CheckTimer(50000))
        {
            I2CIE_CLOSE;
            POWER_EE_DOWN;
            MCU_I2C_CLK_DISABLE();
            s_g_EE_main_state = cst_EE_M_S0;
        }
        else
        {
            s_g_EE_main_state = cst_EE_M_S6;
        }
        break;   
    }
        case cst_EE_M_S7://超时重新延时上电，写
    {
        Wdt_Feed();//喂狗是为了当EEP操作超时导致表计复位 
        if (CheckTimer(600000))
        {	
            EepInit();
            POWER_EE_UP; 
            I2CIE_OPEN; 
            MCU_I2C_CLK_ENABLE();
            s_g_EE_main_state = cst_EE_M_S2;
        }
        break;
    }		
    case cst_EE_M_S8://超时重新延时上电，读
    {
        Wdt_Feed();//喂狗是为了当EEP操作超时导致表计复位
        if (CheckTimer(600000))
        {           
            EepInit();
            POWER_EE_UP;
            I2CIE_OPEN;
            MCU_I2C_CLK_ENABLE();
            s_g_EE_main_state = cst_EE_M_S1;
        }
        break;
    }
	default:
	{
		s_g_EE_main_state = cst_EE_M_S0;
		break;
	}

  }
}

/*************************************************************************************
 * @fun_name:	void  SetTimer(u32 timer)
 * @brief   :
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :    内部接口
 *************************************************************************************/
void  SetTimer(u32 timer)
{
    Timer1uSBase=Timer1usGetTick();
}
/*************************************************************************************
* @fun_name:	void  SetTimer(u32 timer)
* @brief   :
* @param[in] :	 None
* @param[out] :None
* @retval     :    None
* @other     :    内部接口
*************************************************************************************/
u8 CheckTimer(u32 timer)
{
    u32 NowTimer=0;
    u32 a=0;
    a=Timer1usGetTick();
    NowTimer=a-Timer1uSBase;
    if(timer==50000 && (NowTimer>=timer))
    {
        return 1;
    }
    if(timer == 600000 && (NowTimer >= timer))
    {
        return 1;
    }
    else
        return 0;

}
/*************************************************************************************
 * @fun_name:	void I2cIsrMachine(void)
 * @brief   :
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :    内部接口
 *************************************************************************************/
void I2c0_IRQHandler(void)
{

    switch (s_g_I2C_State)
    {
    case cst_I2C_S0:
    {
        I2C_ClearFunc(hi2c1, I2cStart_En);
        I2C_SetFunc(hi2c1,I2cStop_En);
    }
    break;
    case cst_I2C_SR_S1://发送目标地址高字节
    {
        if( I2c_GET_FLAG(hi2c1, I2C_ISR_START))
        {
            I2C_ClearFunc(hi2c1,I2cStart_En);
            I2C_ClearFunc(hi2c1, I2cStop_En);
            I2C_WriteByte(hi2c1, Write_Addr);
        }
        else if( I2c_GET_FLAG(hi2c1, I2C_ISR_TXADDR))//
        {
            s_g_I2C_State = cst_I2C_SR_S2;
            I2C_ClearFunc(hi2c1, I2cStart_En);
            I2C_ClearFunc(hi2c1, I2cStop_En);
            I2C_WriteByte(hi2c1, I2c_MEM_ADD_MSB(target_addr.word_form));
        }

    }
    break;
    case cst_I2C_SR_S2://发送目标地址低字节
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_TXDATA))
        {
            s_g_I2C_State = cst_I2C_SR_S3;
//					I2C_ClearFunc(hi2c1, I2cStart_En);
//					I2C_ClearFunc(hi2c1, I2cStop_En);
            I2C_WriteByte(hi2c1, I2c_MEM_ADD_LSB(target_addr.word_form));
        }
    }
    break;
    case cst_I2C_SR_S3:
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_TXDATA))
        {
            I2C_SetFunc(hi2c1,I2cStart_En);
            I2C_ClearFunc(hi2c1,I2cStop_En);
        }
        else if(I2c_GET_FLAG(hi2c1, I2C_ISR_REPSTART))
        {
            I2C_ClearFunc(hi2c1, I2cStart_En);
            I2C_ClearFunc(hi2c1, I2cStop_En);
            I2C_WriteByte(hi2c1, Read_Addr);
            if(s_g_I2C_Len == 1) //只读1个字节的数据
            {
                I2C_ClearFunc(hi2c1, I2cAck_En); //NACK
            }
            else
            {
                I2C_SetFunc(hi2c1, I2cAck_En); //ACK
            }
        }
        else if(I2c_GET_FLAG(hi2c1, I2C_ISR_READADDR))
        {
            s_g_I2C_State = cst_I2C_SR_S4;
        }
    }
    break;
    case cst_I2C_SR_S4:
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_READDATA))
        {
            /* Read data from RXDR */
            if(s_g_I2C_Len)
            {
                (*g_pData++) = I2C_ReadByte(hi2c1);
                I2C_ClearFunc(hi2c1, I2cStart_En);
                I2C_ClearFunc(hi2c1, I2cStop_En);
                s_g_I2C_Len--;
                if(s_g_I2C_Len == 1) //接下来要读的是最后1个字节的数据
                {
                    s_g_I2C_State = cst_I2C_SR_S5;
                    I2C_ClearFunc(hi2c1, I2cAck_En);
                }
                else
                {
                    I2C_SetFunc(hi2c1,I2cAck_En); //ACK
                }
            }
        }
        else if(I2c_GET_FLAG(hi2c1, I2C_ISR_READDATA_NOACK)) //已接收数据字节，非ACK已返回
        {
            (*g_pData++) = I2C_ReadByte(hi2c1);
            s_g_I2C_Len--;
            if(0 == s_g_I2C_Len)
            {
                I2C_ClearFunc(hi2c1, I2cStart_En);
                I2C_SetFunc(hi2c1, I2cStop_En);
                readend_timer = Timer1usGetTick(); //延时保证停止条件发送成功
                g_EE_Msg.structs.Completed = SUCCESS;
                s_g_I2C_State = cst_I2C_S0;
            }
        }
    }
    break;
    case cst_I2C_SR_S5:
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_READDATA_NOACK)) //已接收数据字节，非ACK已返回
        {
            (*g_pData++) = I2C_ReadByte(hi2c1);
            s_g_I2C_Len--;
            if(s_g_I2C_Len == 0)
            {
                I2C_ClearFunc(hi2c1, I2cStart_En);
                I2C_SetFunc(hi2c1,I2cStop_En);
                readend_timer = Timer1usGetTick(); //延时保证停止条件发送成功
                g_EE_Msg.structs.Completed = SUCCESS;
                s_g_I2C_State = cst_I2C_S0;
            }
        }
    }
    break;

    case cst_I2C_SW_S1: //发送目标地址高字节
    {
        s_g_I2C_IO_Idx = 0;
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_START))
        {
            I2C_ClearFunc(hi2c1,I2cStart_En);
            I2C_ClearFunc(hi2c1, I2cStop_En);
            I2C_WriteByte(hi2c1, Write_Addr);

        }
        else if(I2c_GET_FLAG(hi2c1, I2C_ISR_TXADDR))
        {
            s_g_I2C_State = cst_I2C_SW_S2;
            I2C_ClearFunc(hi2c1, I2cStart_En);
            I2C_ClearFunc(hi2c1, I2cStop_En);
            I2C_WriteByte(hi2c1, I2c_MEM_ADD_MSB(target_addr.word_form));
        }
//			if((I2c_GET_FLAG(hi2c1, I2C_ISR_NACKF) == TRUE) || (I2c_GET_FLAG(hi2c1, I2C_ISR_STOPF) == TRUE)	)
//			{
//				I2c_CLEAR_FLAG(hi2c1, I2C_ISR_NACKF);
//				I2c_CLEAR_FLAG(hi2c1, I2C_ISR_STOPF);
//			}
    }
    break;
    case cst_I2C_SW_S2://发送目标地址低字节
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_TXDATA))
        {
            s_g_I2C_State = cst_I2C_SW_S3;
            I2C_WriteByte(hi2c1, I2c_MEM_ADD_LSB(target_addr.word_form));
        }
//			if(I2c_GET_FLAG(hi2c1, I2C_ISR_NACKF) == TRUE	)
//			{
//				s_g_I2C_State = cst_I2C_SW_S1;
//				I2c_CLEAR_FLAG(hi2c1, I2C_ISR_NACKF);
//				I2c_CLEAR_FLAG(hi2c1, I2C_ISR_STOPF);
//			}
    }
    break;
    case cst_I2C_SW_S3:
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_TXDATA))
        {
            if(s_g_I2C_IO_Idx == (s_g_I2C_Len - 1)) //内存当前页写入的最后一个字节
            {
                odd_timer = Timer1usGetTick();
                I2C_WriteByte(hi2c1, *(g_pData+ s_g_I2C_IO_Idx));
                if((s_g_MultPageflag == SUCCESS) && (s_g_StartPageAddr == 0) && (s_g_PageNum > 0)) //跨页&起始页相对地址为零&不止一页
                {
                    s_g_PageNum -= 1;
                }
                s_g_I2C_State = cst_I2C_SW_S4;
            }
            else if(s_g_I2C_IO_Idx < (s_g_I2C_Len - 1)) //内存当前页写入的非最后一个字节
            {
                s_g_I2C_State = cst_I2C_SW_S3;
                I2C_WriteByte(hi2c1, *(g_pData+ s_g_I2C_IO_Idx));
                ++s_g_I2C_IO_Idx;
            }
        }
//			else if((I2c_GET_FLAG(hi2c1, I2C_ISR_WRITEADDR_NOACK))||(I2c_GET_FLAG(hi2c1, I2C_ISR_WRITEFAIL)))
//			{
//				I2C_SetFunc(hi2c1,I2cStart_En);
//			}
//			else if(I2c_GET_FLAG(hi2c1, I2C_ISR_WRITEDATA_NOACK))
//			{
//				s_g_I2C_State = cst_I2C_S0;
//				I2C_SetFunc(hi2c1,I2cStop_En);
//			}

    }
    break;

    case cst_I2C_SW_S4:
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_TXDATA))
        {
            if (s_g_MultPageflag == ERROR) //数据小于1页，已经写完。返回IDEL
            {
                s_g_I2C_State = cst_I2C_S0;
                g_EE_Msg.structs.Completed = SUCCESS;
                I2C_ClearFunc(hi2c1, I2cStart_En);
                I2C_SetFunc(hi2c1,I2cStop_En);
            }
            else
            {
                if (s_g_StartPageAddr != 0) //不从页起始位置写入
                {
                    target_addr.word_form += s_g_StartPageNum;
                    g_pData += s_g_StartPageNum;
                    if (s_g_PageNum > 0)
                    {
                        s_g_I2C_Len = EEPageSize;
                    }
                    else
                    {
                        s_g_I2C_Len = s_g_LastPage_Num;
                        s_g_LastPage_Num = 0;
                    }
                    s_g_StartPageAddr = 0;
                    s_g_I2C_State = cst_I2C_SW_S5;
                    s_g_TurnPage = TRUE;
                    I2C_ClearFunc(hi2c1,I2cStart_En);
                    I2C_SetFunc(hi2c1, I2cStop_En);
                }
                else
                {
                    if (s_g_PageNum > 0)
                    {
                        target_addr.word_form += EEPageSize;
                        g_pData += EEPageSize;
                        s_g_I2C_Len = EEPageSize;
                        s_g_I2C_State = cst_I2C_SW_S5;
                        s_g_TurnPage = TRUE;
                        I2C_ClearFunc(hi2c1,I2cStart_En);
                        I2C_SetFunc(hi2c1, I2cStop_En);
                    }
                    else //还剩最后一页
                    {
                        if (s_g_LastPage_Num > 0)
                        {
                            target_addr.word_form += EEPageSize;
                            g_pData += EEPageSize;
                            s_g_I2C_Len = s_g_LastPage_Num;
                            s_g_LastPage_Num = 0;
                            s_g_I2C_State = cst_I2C_SW_S5;
                            s_g_TurnPage = TRUE;
                            I2C_ClearFunc(hi2c1,I2cStart_En);
                            I2C_SetFunc(hi2c1, I2cStop_En);
                        }
                        else if (s_g_LastPage_Num == 0) //
                        {
                            g_EE_Msg.structs.Completed = SUCCESS;
                            s_g_I2C_State = cst_I2C_S0;
                            I2C_ClearFunc(hi2c1,I2cStart_En);
                            I2C_SetFunc(hi2c1,I2cStop_En);
                        }
                    }
                }
            }
        }
//			else if(I2c_GET_FLAG(hi2c1, I2C_ISR_WRITEDATA_NOACK))
//			{
//				s_g_I2C_State = cst_I2C_S0;
//				I2C_SetFunc(hi2c1,I2cStop_En);
//			}
    }
    break;
    case cst_I2C_SW_S5:
    {
        if(I2c_GET_FLAG(hi2c1, I2C_ISR_REPSTART))
        {
            s_g_I2C_State = cst_I2C_SW_S1;
            I2C_ClearFunc(hi2c1,I2cStart_En);
            I2C_ClearFunc(hi2c1, I2cStop_En);
            I2C_WriteByte(hi2c1, Write_Addr);
        }
    }
    break;

    default:
    {
        s_g_I2C_State = cst_I2C_S0;
        I2C_ClearFunc(hi2c1, I2cStart_En);
        I2C_SetFunc(hi2c1,I2cStop_En);
    }
    break;
    }
    I2C_ClearIrq(hi2c1);
}

/*************************************************************************************
 * @fun_name:	void I2C1_EV_IRQHandler(void)
 * @brief   :
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :  I2C1中断
 *************************************************************************************/
//void I2c0_IRQHandler(void)
//{
//	I2cIsrMachine();
//}

/*************************************************************************************
 * @fun_name:	u8 EepIfIdle (void)
 * @brief   :
 * @param[in] :	 None
 * @param[out] :TASK_IDLE:空闲;TASK_BUSY:忙碌
 * @retval     :    None
 * @other     :   工作接口
 *************************************************************************************/
u8 EepIfIdle(void)
{
    if (s_g_EE_main_state == cst_EE_M_S0)// && I2c_GET_FLAG(hi2c1, I2C_ISR_IDLE) == TRUE)
        return TASK_IDLE;
    else
        return TASK_BUSY;
}

/*************************************************************************************
 * @fun_name:	u8 EepRead(u16 EEaddr,u8 * Data,u8 Num,u8* pMsg)
 * @brief   :      申请启动连续读EEPROM任务
 * @param[in] :	 存储首字节地址(2字节)EEaddr,读出地址要存储的位置指针data,读出的字节数num,读取过程中发生的状态信息指针pMsg
 * @param[out] :SUCCESS:数据有效性判断成功，允许读取；ERROR：数据有效性判断失败，不允许读取
                          读取的数据内容 (*data)，传递出的message内容 (*pMsg)
 * @retval     :    None
 * @other     :    工作接口
 *************************************************************************************/
u8 EepRead(u16 EEaddr, u8* Data, u16 Num, u8* pMsg)
{
    g_EE_Msg.MessageInfo=0x40;
    *pMsg = 0;
    if (EEaddr + Num <= (EE_ADDR_MAX+1))
    {
        if (Num > 0 && Num <= READ_LIMITSIZE_MAX) //???????
        {
            target_addr.word_form = EEaddr;
            g_pData_Temp = Data;
            s_g_I2C_Len = Num;
            g_pMsg = pMsg;
			MCU_I2C_CLK_ENABLE();
			POWER_EE_UP;  //EEP上拉
			I2CIE_OPEN;
            s_g_EE_main_state = cst_EE_M_S1;
            return SUCCESS;
        }
        else
            return ERROR;
    }
    else
        return ERROR;
}
/*************************************************************************************
 * @fun_name:	void PointToIicSeqread(void)
 * @brief   :        启动I2C读EEPROM中断状态机
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :    内部接口
 *************************************************************************************/
void PointToIicSeqread(void)
{
    s_g_I2C_State = cst_I2C_SR_S1;
    g_pData = g_pData_Temp;

    I2C_ClearFunc(hi2c1, I2cStop_En);
    I2C_SetFunc(hi2c1, I2cStart_En);
}
/*************************************************************************************
 * @fun_name:	u8 EepWrite (u8* Data,u16 EEaddr,u16 Num,u8* pMsg)
 * @brief   :       申请启动连续写EEPROM任务
 * @param[in] :	存储写入数据的位置指针data，存储首字节地址（2个字节）EEaddr，写入的字节数num,写入过程中发生的状态信息指针pMsg
 * @param[out] :_SUCCESS:数据有效性判断成功，允许写入；_ERROR：数据有效性判断失败，不允许写入
                          传递出的message内容 (*pMsg)
 * @retval     :    None
 * @other     :   工作接口
 *************************************************************************************/
u8 EepWrite(u8* Data, u16 EEaddr, u16 Num, u8* pMsg)
{
    g_EE_Msg.MessageInfo=0x60;
    *pMsg = 0;
    if (EEaddr + Num <= (EE_ADDR_MAX+1))
    {
        if (Num > 0 && Num <=WRITE_LIMITSIZE_MAX)
        {
            target_addr.word_form = EEaddr;
            g_pData_Temp = Data;
            s_g_StartPageAddr = EEaddr % EEPageSize;
            s_g_StartPageNum = EEPageSize - s_g_StartPageAddr;
            s_g_PageNum = Num / EEPageSize;
            s_g_LastPage_Num = Num % EEPageSize;
            if (0 == s_g_PageNum)
            {
                if ((s_g_StartPageAddr + Num) > EEPageSize)
                {
                    s_g_MultPageflag = SUCCESS;
                    Num -= s_g_StartPageNum;
                    s_g_PageNum = Num / EEPageSize;
                    s_g_LastPage_Num = Num % EEPageSize;
                    s_g_I2C_Len = s_g_StartPageNum;
                }
                else
                {
                    s_g_MultPageflag = ERROR; //数据小于一页
                    s_g_I2C_Len = s_g_LastPage_Num;
                }
            }
            else
            {
                s_g_MultPageflag = SUCCESS; //数据大于等于一页
                if (s_g_StartPageAddr == 0)
                {
                    s_g_I2C_Len = EEPageSize;
                }
                else
                {
                    Num -= s_g_StartPageNum;
                    s_g_PageNum = Num / EEPageSize;
                    s_g_LastPage_Num = Num % EEPageSize;
                    s_g_I2C_Len = s_g_StartPageNum;
                }
            }
            g_pMsg = pMsg;
			MCU_I2C_CLK_ENABLE();
			POWER_EE_UP; //EEP上拉
			I2CIE_OPEN;
            s_g_EE_main_state = cst_EE_M_S2;
            return SUCCESS;
        }
        else
            return ERROR;
    }
    else
        return ERROR;
}
/*************************************************************************************
 * @fun_name:	void PointToIicSeqwrite(void)
 * @brief   :        启动I2C写EEPROM中断状态机
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :    内部接口
 *************************************************************************************/
void PointToIicSeqwrite(void)
{
    s_g_I2C_State = cst_I2C_SW_S1;
    g_pData = g_pData_Temp;

    I2C_ClearFunc(hi2c1, I2cStop_En);
    I2C_SetFunc(hi2c1, I2cStart_En);
    
}
/*************************************************************************************
 * @fun_name:	u8 EepCheckMsg (u8 Msg)
 * @brief   :       根据自己定义的Msg查询任务是否完成
 * @param[in] :	申请者自己定义的状态信息变量Msg
 * @param[out] :SUCCESS:任务完成；ERROR：任务未完成
 * @retval     :    None
 * @other     :     处于MainSpace，第二类接口：工作接口
 *************************************************************************************/
u8 EepCheckMsg(u8 Msg)
{
    if ((SUCCESS == I2cCheckCompleted())&&EepIfIdle())
        return SUCCESS;
    else
        return ERROR;
}

/*************************************************************************************
 * @fun_name:	u8 EepCheckState(u8 Msg)
* @brief   :        根据自己定义的Msg查询EEPROM是否异常
 * @param[in] :	 申请者自己定义的状态信息变量Msg
 * @param[out] :SUCCESS:EEPROM正常；ERROR_IES：EEPROM异常
 * @retval     :    None
 * @other     :    处于MainSpace，第二类接口：工作接口
 *************************************************************************************/
u8 EepCheckState(void)
{
    return g_EE_Msg.structs.EE_normal;
}

/*************************************************************************************
 * @fun_name:	void EepPreSleep(void)
 * @brief   :        休眠EEP模块
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :   处于MainSpace，第三类接口：休眠接口
 *************************************************************************************/
void EepPreSleep(void)
{

}

/*************************************************************************************
 * @fun_name:	u8 EepIfSleep(void)
 * @brief   :        查询EEPROM模块是否可以休眠
 * @param[in] :	 None
 * @param[out] :TRUE:EEPROM模块可以休眠；FALSE：EEPROM模块不可以休眠
 * @retval     :    None
 * @other     :   处于MainSpace，第三类接口：休眠接口
 *************************************************************************************/
u8 EepIfSleep(void)
{
    if(cst_EE_M_S0 == s_g_EE_main_state)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

/*************************************************************************************
 * @fun_name:	void EepWakeSleep(void)
 * @brief   :        唤醒EEPROM模块
 * @param[in] :	 None
 * @param[out] :None
 * @retval     :    None
 * @other     :    处于MainSpace，第四类接口：唤醒接口
 *************************************************************************************/
void EepWakeSleep(void)
{

}

/********************************************************
 * @fun_name:	void GetIicResult(void)
 * @brief   :   返回I2C中断状态机任务结果给申请者指针
 * @param[in] :
 * @param[out] :
 * @retval     :    None
 * @other     :    内部接口
 *******************************************************/
void GetIicResult(void)
{
    *g_pMsg = g_EE_Msg.MessageInfo;
}

/*************************************************************************************
 * @fun_name:	u8 I2cCheckCompleted(void)
 * @brief   :        查询中断状态机任务是否完成
 * @param[in] :	 None
 * @param[out] :SUCCESS:任务完成；ERROR：任务未完成
 * @retval     :    None
 * @other     :    内部接口
 *************************************************************************************/
u8 I2cCheckCompleted(void)
{
    return g_EE_Msg.structs.Completed;
}

/*************************************************************************************
 * @fun_name:	u8 I2cCheckRorW(void)
 * @brief   :        查询中断状态机是读任务还是写任务
 * @param[in] :	 None
 * @param[out] :ERROR:读任务；SUCCESS：写任务
 * @retval     :    None
 * @other     :    内部接口
 *************************************************************************************/
u8 I2cCheckRorW(void)
{
    return g_EE_Msg.structs.EorW;
}

