#include "common_include.h"
#include "s6j3360_RIC.h"
#include "abstract.h"

/*-------------------------------------------------------------------------
* Function Name  : bsp_SDA_IN
* Description    :
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void bsp_SDA_IN(void)
{
    stc_port_pin_config_t stc_port_pin_config1_09 = { .enOutputFunction      = PortOutputResourceGPIO,
        .enOutputDrive         = PortOutputDriveA,
        .enPullResistor        = PortPullResistorNone,
        .enInputLevel          = PortInputLevelCmosA,
        .bInputEnable          = TRUE,
        .bNoiseFilterEnable    = FALSE,
        .enGpioDirection       = PortGpioInput,
    .enGpioInitOutputLevel = PortGpioLow};
    Port_SetPinConfig( 1, 9, &stc_port_pin_config1_09 );
    Port_EnableInput();
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_SDA_OUT
* Description    :
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void bsp_SDA_OUT(void)
{
    stc_port_pin_config_t stc_port_pin_config1_09 = { .enOutputFunction      = PortOutputResourceGPIO,
        .enOutputDrive         = PortOutputDriveA,
        .enPullResistor        = PortPullResistorNone,
        .enInputLevel          = PortInputLevelCmosA,
        .bInputEnable          = FALSE,
        .bNoiseFilterEnable    = TRUE,
        .enGpioDirection       = PortGpioOutput,
    .enGpioInitOutputLevel = PortGpioLow};
    Port_SetPinConfig( 1, 9, &stc_port_pin_config1_09 );
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_SDA_GetValue
* Description    :
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
unsigned char bsp_SDA_GetValue(void)
{
    unsigned char i = 0;

    en_port_gpio_level_t temp;
    Port_GetPortPinLevel(1,9,(en_port_gpio_level_t*)(&temp));
    i = (unsigned char)(temp);

    return i;
    
}

/*-------------------------------------------------------------------------
* Function Name  : bsp_SCL_Set
* Description    :
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void bsp_SCL_Set(unsigned char data)
{

   Port_SetPortPinLevel(1,8,(en_port_gpio_level_t)data);

}


/*-------------------------------------------------------------------------
* Function Name  : bsp_SDA_Set
* Description    :
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void bsp_SDA_Set(unsigned char data)
{

   Port_SetPortPinLevel(1,9,(en_port_gpio_level_t)data);

}

/*-------------------------------------------------------------------------
* Function Name  : ClearDog
* Description    : 清零 DOG
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void ClearDog(void)
{

    bsp_HDOG_Feed();

}

/*-------------------------------------------------------------------------
* Function Name  : ClearDog
* Description    : 清零 DOG
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void bsp_NOP(void)
{
    NOP();
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_Save_Check_Service
* Description    : 数据读-写-检查-擦除
* Input          : SaveSt -1：read -2：write -3：clear
* Output         : None
* Return         : -3:True -other:Flase
* onther         : xxhh
--------------------------------------------------------------------------*/
DATA_SAVE_PACK NewData;
uint8_t bsp_Save_Check_Service(uint8_t SaveS)
{
    DATA_SAVE_PACK CheckData;
    
    if(SaveS == 1)//read
    {
        memset((void*)&CheckData, 0, sizeof (CheckData));
        if(bsp_IIC_SequentialRead(0xa2,32,(uint8_t*)&CheckData,sizeof(CheckData)) == 1)
        {
            if((CheckData.Head1 == 0x5A) && (CheckData.Head2 == 0xA5) && (CheckData.Tail == 0xFF))
            {
                memcpy(&NewData, &CheckData, sizeof (CheckData));
                return 1;
            }
            else
                return 3; 
        }
        else
            return 2;  
    }
    else if(SaveS == 2)//write
    {
        if((NewData.Head1 == 0x5A) && (NewData.Head2 == 0xA5) && (NewData.Tail == 0xFF))
        {
            if(bsp_IIC_WritePage(0xa2,32,(uint8_t*)&NewData,sizeof(NewData)) == 1)
                return 1;
            else
                return 5;
        }
        else
            return 4;
    } 
    else if(SaveS == 3)//clear
    {
        if((NewData.Head1 == 0x5A) && (NewData.Head2 == 0xA5) && (NewData.Tail == 0xFF))
        {
            NewData.Data1 = 0;
            NewData.Data2 = 0;
            NewData.Data3 = 0;
            NewData.Data4 = 0;
          
            if(bsp_IIC_WritePage(0xa2,32,(uint8_t*)&NewData,sizeof(NewData)) == 1)
                return 1;
            else
                return 7;
        }
        else
            return 6;
    }
}

/*-------------------------------------------------------------------------
* Function Name  : SavePro
* Description    : 
* Input          : SaveState
* Output         : None
* Return         : None
* onther         : xxhh
--------------------------------------------------------------------------*/

uint8_t SaveState,OldSaveState;
void SavePro()
{
    static uint8_t i = 0;
    uint8_t Savebuf = 0;
    
    if(SaveState != OldSaveState)
    {
        Savebuf = bsp_Save_Check_Service(SaveState);
        if(Savebuf == 1)
        {
            i = 0;
            SaveState = 0;
            OldSaveState = 0;
        }
        else if(Savebuf == 3)
        {
            SaveState = 2;
            i++;
        }
        else
           i++; 
        if(i >= 10)
        {
            SaveState = 0;
            OldSaveState = 0;
        }
    }
}


