/*
 * eeprom.c
 *
 *  Created on: 2020. 4. 22.
 *      Author: parkyj
 */
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "eeprom.h"
#include "Bluecell_operate.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_gpio.h"

/* Includes ------------------------------------------------------------------*/   
HAL_StatusTypeDef EEPROM_M24C08_ByteRead(uint16_t devid,uint16_t Address,uint8_t* data,uint8_t size);
extern void Table_Init();

extern BLUESTATUS_st bluecell_Currdatastatus;

extern uint8_t FRBT_UserCtrl;
extern uint8_t FRBT_UserCtrl_Get();
extern uint8_t FRBT_UserCtrl_Set();






uint8_t testdata[120];
/*
typedef enum{
    Test_DL1 = 0,
    Test_DL2 = 0,
    Test_DL3 = 0,
    Test_DL4 = 0,    
};
typedef enum{
    Test_UL1 = 0,
    Test_UL2 = 0,
    Test_UL3 = 0,
    Test_UL4 = 0,    
};*/
/**/
void EEPROM_DataCheck(){
    static uint8_t datacheck_array[sizeof(BLUESTATUS_st)];
    
    Bluecell_StructCpy(&datacheck_array[0],&bluecell_Currdatastatus.bluecell_header,sizeof(BLUESTATUS_st));
    for(int i = 0; i < sizeof(BLUESTATUS_st); i++){
        if(datacheck_array[i] == 0xFF){

        }
        else{
            return;
        }
    }
    EEPROM_M24C08_Zerowrite(EEPROM_M24C08_ID ,(EEPROM_ATT_BASE));
}
void EEPROM_M24C08_Init(void){
/*    uint8_t* data = 0;
    int16_t DL_Main_Atten[4] = {0,};
    int16_t DL_Offset_Atten[4] = {0,};

    int16_t UL_Main_Atten[4] = {0,};
    int16_t UL_Offset_Atten[4] = {0,};
    int16_t UL_ALC_Atten[4] = {0,};

    int16_t DL_High_ThreasHold = 0;
    int16_t DL_Low_ThreasHold = 0;

    int16_t UL_Hihh_ThreasHold = 0;

    bool DL_Path[4] = {0,};

    bool UL_Path[4] = {0,};    

    bool ALC_Path = 0;        
    bool AGC_Path = 0;            

    bool DL_shutdown_Path = 0;
    bool UL_shutdown_Path = 0;

    int16_t DL_Shutdown_ThreadHold = 0;        
    int16_t UL_Shutdown_ThreadHold = 0;*/

//    DL_High_ThreasHold = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten4_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten4_L;
//    ALC_TEMP[0] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten1_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten1_L;
//    ALC_TEMP[1] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten2_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten2_L;
//    ALC_TEMP[2] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten3_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten3_L;
//    ALC_TEMP[3] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten4_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten4_L;
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_WINDOW_STATUS_ADDRESDS,&bluecell_Currdatastatus.bluecell_header,sizeof(BLUESTATUS_st) );
    printf("bluecell_Currdatastatus.DLI_FRBT : %x \r\n",bluecell_Currdatastatus.DLI_FRBT_ON_OFF);
//    EEPROM_DataCheck();

    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_BASE              ,&Att_DL1.Table_0_0_dBm,sizeof(ATT_TABLE_st) );

    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_DL1_TABLE_ADDRESDS,&Att_DL2.Table_0_0_dBm,sizeof(ATT_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_DL2_TABLE_ADDRESDS,&Att_DL3.Table_0_0_dBm,sizeof(ATT_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_DL3_TABLE_ADDRESDS,&Att_DL4.Table_0_0_dBm,sizeof(ATT_TABLE_st) );

    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_DL4_TABLE_ADDRESDS,&Att_UL1.Table_0_0_dBm,sizeof(ATT_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_UL1_TABLE_ADDRESDS,&Att_UL2.Table_0_0_dBm,sizeof(ATT_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_UL2_TABLE_ADDRESDS,&Att_UL3.Table_0_0_dBm,sizeof(ATT_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_UL3_TABLE_ADDRESDS,&Att_UL4.Table_0_0_dBm,sizeof(ATT_TABLE_st) );

    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_UL4_TABLE_ADDRESDS,&Det_DL1.Table_Det5_dBm_H,sizeof(DET_TABLEDL_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_DL1_TABLE_ADDRESDS,&Det_DL2.Table_Det5_dBm_H,sizeof(DET_TABLEDL_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_DL2_TABLE_ADDRESDS,&Det_DL3.Table_Det5_dBm_H,sizeof(DET_TABLEDL_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_DL3_TABLE_ADDRESDS,&Det_DL4.Table_Det5_dBm_H,sizeof(DET_TABLEDL_st) );

//    uint8_t* tmpdata;
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_DL4_TABLE_ADDRESDS,&Det_UL1.Table_Det_15_dBm_H,sizeof(DET_TABLEUL_st) );
    //tmpdata = &Det_UL1.Table_Det_15_dBm_H;
//    for(int i =0; i < sizeof(DET_TABLEUL_st) ; i++){
//        printf("[%d]%f \r\n",i,(tmpdata[i * 2] << 8 | tmpdata[i*2+1])*0.001);
//    }
//    printf("");
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_UL1_TABLE_ADDRESDS,&Det_UL2.Table_Det_15_dBm_H,sizeof(DET_TABLEUL_st) );
//    data = &Det_UL2.Table_Det_15_dBm_H;
//    for(int i = 0; i < sizeof(DET_TABLEUL_st) ; i ++ ){
//        printf("[%d] %x \r\n",i,data[i]);       
//    }    
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_UL2_TABLE_ADDRESDS,&Det_UL3.Table_Det_15_dBm_H,sizeof(DET_TABLEUL_st) );
//    data = &Det_UL3.Table_Det_15_dBm_H;
//    for(int i = 0; i < sizeof(DET_TABLEUL_st) ; i ++ ){
//        printf("[%d] %x \r\n",i,data[i]);       
//    }    
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_UL3_TABLE_ADDRESDS,&Det_UL4.Table_Det_15_dBm_H,sizeof(DET_TABLEUL_st) );


    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_DET_UL4_TABLE_ADDRESDS,&Temp_DL1.Table_1_Temp,sizeof(TEMP_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_TEMP_DL1_TABLE_ADDRESDS,&Temp_DL2.Table_1_Temp,sizeof(TEMP_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_TEMP_DL2_TABLE_ADDRESDS,&Temp_DL3.Table_1_Temp,sizeof(TEMP_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_TEMP_DL3_TABLE_ADDRESDS,&Temp_DL4.Table_1_Temp,sizeof(TEMP_TABLE_st) );


    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_TEMP_DL4_TABLE_ADDRESDS,&Temp_UL1.Table_1_Temp,sizeof(TEMP_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_TEMP_UL1_TABLE_ADDRESDS,&Temp_UL2.Table_1_Temp,sizeof(TEMP_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_TEMP_UL2_TABLE_ADDRESDS,&Temp_UL3.Table_1_Temp,sizeof(TEMP_TABLE_st) );
    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_TEMP_UL3_TABLE_ADDRESDS,&Temp_UL4.Table_1_Temp,sizeof(TEMP_TABLE_st) );
    Table_Init();


    
    HAL_Delay(200);
//    printf("MBIC_ULO_ALC_Atten1_H  %x / MBIC_ULO_ALC_Atten1_L: %x \r\n",bluecell_Currdatastatus.MBIC_ULO_ALC_Atten1_H,bluecell_Currdatastatus.MBIC_ULO_ALC_Atten1_L);        
//    printf("ATT_UL1_H  %x / ATT_UL1_L: %x \r\n",bluecell_Currdatastatus.ATT_UL1_H,bluecell_Currdatastatus.ATT_UL1_L);    
//    printf("bluecell_User_UL1_H  %x / bluecell_User_UL1_L: %x \r\n",bluecell_Currdatastatus.bluecell_User_UL1_H,bluecell_Currdatastatus.bluecell_User_UL1_L);

//    printf("===========================================================================\r\n");
//    printf("ATT TableDL 1_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_DL1.Table_Length,Att_DL1.Table_Ref);
//    printf("ATT TableDL 2_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_DL2.Table_Length,Att_DL2.Table_Ref);
//    printf("ATT TableDL 3_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_DL3.Table_Length,Att_DL3.Table_Ref);    
//    printf("ATT TableDL 4_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_DL4.Table_Length,Att_DL4.Table_Ref);
//    printf("ATT TableUL 1_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_UL1.Table_Length,Att_UL1.Table_Ref);
//    printf("ATT TableUL 2_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_UL2.Table_Length,Att_UL2.Table_Ref);
//    printf("ATT TableUL 3_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_UL3.Table_Length,Att_UL3.Table_Ref);    
//    printf("ATT TableUL 4_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Att_UL4.Table_Length,Att_UL4.Table_Ref);    
//
//    printf("DET TableDL 1_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_DL1.Table_Length,Det_DL1.Table_Ref);
//    printf("DET TableDL 2_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_DL2.Table_Length,Det_DL2.Table_Ref);
//    printf("DET TableDL 3_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_DL3.Table_Length,Det_DL3.Table_Ref);    
//    printf("DET TableDL 4_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_DL4.Table_Length,Det_DL4.Table_Ref);
//    printf("DET TableUL 1_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_UL1.Table_Length,Det_UL1.Table_Ref);
//    printf("DET TableUL 2_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_UL2.Table_Length,Det_UL2.Table_Ref);
//    printf("DET TableUL 3_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_UL3.Table_Length,Det_UL3.Table_Ref);    
//    printf("DET TableUL 4_Init START ORIGIN TABLE LENGTH  %d / Ref : %d \r\n",Det_UL4.Table_Length,Det_UL4.Table_Ref);    
//    printf("DL1\r\n");    Table_DET_DataSetting(&DL_Table_ref[AGC_Table_DL1_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_DL1.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_DL1.Table_Length);
//    printf("DL2\r\n");    Table_DET_DataSetting(&DL_Table_ref[AGC_Table_DL2_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_DL2.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_DL2.Table_Length);
//    printf("DL3\r\n");    Table_DET_DataSetting(&DL_Table_ref[AGC_Table_DL3_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_DL3.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_DL3.Table_Length);
//    printf("DL4\r\n");    Table_DET_DataSetting(&DL_Table_ref[AGC_Table_DL4_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_DL4.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_DL4.Table_Length);
//    printf("UL1\r\n");    Table_DET_DataSetting(&UL_Table_ref[ALC_Table_UL1_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_UL1.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_UL1.Table_Length);
//    printf("UL2\r\n");    Table_DET_DataSetting(&UL_Table_ref[ALC_Table_UL2_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_UL2.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_UL2.Table_Length);
//    printf("UL3\r\n");    Table_DET_DataSetting(&UL_Table_ref[ALC_Table_UL3_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_UL3.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_UL3.Table_Length);
//    printf("UL4\r\n");    Table_DET_DataSetting(&UL_Table_ref[ALC_Table_UL4_Ref_Index][DLUL_TABLE_MAX_VALUE],Det_UL4.Table_Ref ,TableDataSetting_ATT_DET_STEP,Det_UL4.Table_Length);

#if 0// PYJ.2020.06.28_BEGIN --
    DL_Main_Atten[Test_DL1] = (bluecell_Currdatastatus.ATT_DL1_H << 8) | bluecell_Currdatastatus.ATT_DL1_L;
    DL_Main_Atten[Test_DL2] = bluecell_Currdatastatus.ATT_DL2_H << 8 | bluecell_Currdatastatus.ATT_DL2_L;
    DL_Main_Atten[Test_DL3] = bluecell_Currdatastatus.ATT_DL3_H << 8 | bluecell_Currdatastatus.ATT_DL3_L;
    DL_Main_Atten[Test_DL4] = bluecell_Currdatastatus.ATT_DL4_H << 8 | bluecell_Currdatastatus.ATT_DL4_L;

    DL_Offset_Atten[Test_DL1] = bluecell_Currdatastatus.bluecell_User_DL1_H << 8 | bluecell_Currdatastatus.bluecell_User_DL1_L;
    DL_Offset_Atten[Test_DL2] = bluecell_Currdatastatus.bluecell_User_DL2_H << 8 | bluecell_Currdatastatus.bluecell_User_DL2_L;
    DL_Offset_Atten[Test_DL3] = bluecell_Currdatastatus.bluecell_User_DL3_H << 8 | bluecell_Currdatastatus.bluecell_User_DL3_L;
    DL_Offset_Atten[Test_DL4] = bluecell_Currdatastatus.bluecell_User_DL4_H << 8 | bluecell_Currdatastatus.bluecell_User_DL4_L;
    

    UL_Main_Atten[Test_UL1] = bluecell_Currdatastatus.ATT_UL1_H << 8 | bluecell_Currdatastatus.ATT_UL1_L;
    UL_Main_Atten[Test_UL2] = bluecell_Currdatastatus.ATT_UL2_H << 8 | bluecell_Currdatastatus.ATT_UL2_L;
    UL_Main_Atten[Test_UL3] = bluecell_Currdatastatus.ATT_UL3_H << 8 | bluecell_Currdatastatus.ATT_UL3_L;
    UL_Main_Atten[Test_UL4] = bluecell_Currdatastatus.ATT_UL4_H << 8 | bluecell_Currdatastatus.ATT_UL4_L;

    UL_Offset_Atten[Test_UL1] = bluecell_Currdatastatus.bluecell_User_UL1_H << 8 | bluecell_Currdatastatus.bluecell_User_UL1_L;
    UL_Offset_Atten[Test_UL2] = bluecell_Currdatastatus.bluecell_User_UL2_H << 8 | bluecell_Currdatastatus.bluecell_User_UL2_L;
    UL_Offset_Atten[Test_UL3] = bluecell_Currdatastatus.bluecell_User_UL3_H << 8 | bluecell_Currdatastatus.bluecell_User_UL3_L;
    UL_Offset_Atten[Test_UL4] = bluecell_Currdatastatus.bluecell_User_UL4_H << 8 | bluecell_Currdatastatus.bluecell_User_UL4_L;

    UL_ALC_Atten[Test_UL1] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten1_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten1_L;
    UL_ALC_Atten[Test_UL2] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten2_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten2_L;
    UL_ALC_Atten[Test_UL3] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten3_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten3_L;
    UL_ALC_Atten[Test_UL4] = bluecell_Currdatastatus.MBIC_ULO_ALC_Atten4_H << 8 | bluecell_Currdatastatus.MBIC_ULO_ALC_Atten4_L;
#endif // PYJ.2020.06.28_END -- 
#if 0 // PYJ.2020.06.28_BEGIN -- 

    for(int i = 0; i < 4; i++)
        printf("DL       : %d \r\n",DL_Main_Atten[Test_DL1+i]);
    for(int i = 0; i < 4; i++)
        printf("DL Offset: %d \r\n",DL_Offset_Atten[Test_DL1+i]);
    for(int i = 0; i < 4; i++)
        printf("UL       : %d \r\n",UL_Main_Atten[Test_DL1+i]);
    for(int i = 0; i < 4; i++)
        printf("UL Offset: %d \r\n",UL_Offset_Atten[Test_DL1+i]);
    for(int i = 0; i < 4; i++)
        printf("UL ALC: %d \r\n",UL_ALC_Atten[Test_DL1+i]);    
#endif // PYJ.2020.06.28_END -- 
    /*Table Initial Length Setting */
#if 0 // PYJ.2020.06.23_BEGIN -- 
        printf("Init Temp_UL3 Length : %d \r\n",Temp_UL3.Table_Length);
        Bluecell_DataCopy(testdata, &Temp_UL3.Table_1_Temp,sizeof(TEMP_TABLE_st) );
        for(int i = 0; i < sizeof(TEMP_TABLE_st); i++){
            printf("\r\n testdata[%d] : %x ",i,testdata[i]);
        }
#endif // PYJ.2020.06.23_END -- 
    bluecell_Currdatastatus.ALARM_TEMP_HIGH = 0; //bit
    bluecell_Currdatastatus.ALARM_DLI_Level = 0;
    bluecell_Currdatastatus.ALARM_DLI_SHTUTDOWN = 0;
    bluecell_Currdatastatus.ALARM_DLI_AGC_Alarm = 0;
    bluecell_Currdatastatus.ALARM_ULO_ALC_Alarm = 0;    
    bluecell_Currdatastatus.ALARM_ULO_Level = 0;
    bluecell_Currdatastatus.ALARM_ULO_SHTUTDOWN = 0;
    bluecell_Currdatastatus.DLI_Shutdown_Retry_Count1 = 0;    
    bluecell_Currdatastatus.DLI_Shutdown_Retry_Count2 = 0;    
    bluecell_Currdatastatus.DLI_Shutdown_Retry_Count3 = 0;    
    bluecell_Currdatastatus.DLI_Shutdown_Retry_Count4 = 0;    
    bluecell_Currdatastatus.ULO_Shutdown_Retry_Count1 = 0;        
    bluecell_Currdatastatus.ULO_Shutdown_Retry_Count2 = 0;        
    bluecell_Currdatastatus.ULO_Shutdown_Retry_Count3 = 0;            
    bluecell_Currdatastatus.ULO_Shutdown_Retry_Count4 = 0;     
    bluecell_Currdatastatus.DLI_FRBT_Atten1_H = 0;
    bluecell_Currdatastatus.DLI_FRBT_Atten2_H = 0;
    bluecell_Currdatastatus.DLI_FRBT_Atten3_H = 0;
    bluecell_Currdatastatus.DLI_FRBT_Atten4_H = 0;
    bluecell_Currdatastatus.DLI_FRBT_Atten1_L = 0;    
    bluecell_Currdatastatus.DLI_FRBT_Atten2_L = 0;    
    bluecell_Currdatastatus.DLI_FRBT_Atten3_L = 0;    
    bluecell_Currdatastatus.DLI_FRBT_Atten4_L = 0;
    bluecell_Currdatastatus.DLI_FRBT_D_Day = DLI_FRBT_DAY_DEFAULT;
    bluecell_Currdatastatus.DLI_FRBT_Status = FRBT_TRACKING; 
//    FRBT_UserCtrl = false;
    FRBT_UserCtrl_Set(false);

    if(bluecell_Currdatastatus.SelfTest == true){
        bluecell_Currdatastatus.ATT_DL1_H = bluecell_Currdatastatus.SelfTest_ATT_DL1_H ;
        bluecell_Currdatastatus.ATT_DL2_H = bluecell_Currdatastatus.SelfTest_ATT_DL2_H ;
        bluecell_Currdatastatus.ATT_DL3_H = bluecell_Currdatastatus.SelfTest_ATT_DL3_H ;
        bluecell_Currdatastatus.ATT_DL4_H = bluecell_Currdatastatus.SelfTest_ATT_DL4_H ;
        
        bluecell_Currdatastatus.ATT_DL1_L = bluecell_Currdatastatus.SelfTest_ATT_DL1_L ;
        bluecell_Currdatastatus.ATT_DL2_L = bluecell_Currdatastatus.SelfTest_ATT_DL2_L ;
        bluecell_Currdatastatus.ATT_DL3_L = bluecell_Currdatastatus.SelfTest_ATT_DL3_L ;
        bluecell_Currdatastatus.ATT_DL4_L = bluecell_Currdatastatus.SelfTest_ATT_DL4_L ;                    
        
        bluecell_Currdatastatus.ATT_UL1_H = bluecell_Currdatastatus.SelfTest_ATT_UL1_H ;
        bluecell_Currdatastatus.ATT_UL2_H = bluecell_Currdatastatus.SelfTest_ATT_UL2_H ;
        bluecell_Currdatastatus.ATT_UL3_H = bluecell_Currdatastatus.SelfTest_ATT_UL3_H ;
        bluecell_Currdatastatus.ATT_UL4_H = bluecell_Currdatastatus.SelfTest_ATT_UL4_H ;
        
        bluecell_Currdatastatus.ATT_UL1_L = bluecell_Currdatastatus.SelfTest_ATT_UL1_L ;
        bluecell_Currdatastatus.ATT_UL2_L = bluecell_Currdatastatus.SelfTest_ATT_UL2_L ;
        bluecell_Currdatastatus.ATT_UL3_L = bluecell_Currdatastatus.SelfTest_ATT_UL3_L ;
        bluecell_Currdatastatus.ATT_UL4_L = bluecell_Currdatastatus.SelfTest_ATT_UL4_L ;                    
    }
    bluecell_Currdatastatus.SelfTest = false;
//printf("bluecell_Currdatastatus.DLI_Level_High_Threshold_H : %x \r\n",bluecell_Currdatastatus.DLI_Level_High_Threshold_H);
//printf("bluecell_Currdatastatus.DLI_Level_High_Threshold_L : %x \r\n",bluecell_Currdatastatus.DLI_Level_High_Threshold_L);
//    bluecell_Currdatastatus.DLI_FRBT_Status = 7;
    bluecell_Currdatastatus.ALARM_TESTMODE = 0;
    bluecell_Currdatastatus.Type = 0;
    printf("bluecell_Currdatastatus.Carrier_ON_OFF : %d \r\n",bluecell_Currdatastatus.Carrier_ON_OFF);
    if(bluecell_Currdatastatus.Carrier_ON_OFF != false){
        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x80)
            bluecell_Currdatastatus.ATT_DL1_PATH = true;
        else
            bluecell_Currdatastatus.ATT_DL1_PATH = false;

        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x40)
            bluecell_Currdatastatus.ATT_DL2_PATH = true;
        else
            bluecell_Currdatastatus.ATT_DL2_PATH = false;

        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x20)
            bluecell_Currdatastatus.ATT_DL3_PATH = true;
        else
            bluecell_Currdatastatus.ATT_DL3_PATH = false;

        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x10)
            bluecell_Currdatastatus.ATT_DL4_PATH = true;
        else
            bluecell_Currdatastatus.ATT_DL4_PATH = false;

        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x08)
            bluecell_Currdatastatus.ATT_UL1_PATH = true;
        else
            bluecell_Currdatastatus.ATT_UL1_PATH = false;

        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x04)
            bluecell_Currdatastatus.ATT_UL2_PATH = true;
        else
            bluecell_Currdatastatus.ATT_UL2_PATH = false;

        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x02)
            bluecell_Currdatastatus.ATT_UL3_PATH = true;
        else
            bluecell_Currdatastatus.ATT_UL3_PATH = false;

        if(bluecell_Currdatastatus.Path_TempSave_Bit & 0x01)
            bluecell_Currdatastatus.ATT_UL4_PATH = true;
        else
            bluecell_Currdatastatus.ATT_UL4_PATH = false;
    }
    else{
        bluecell_Currdatastatus.ATT_DL1_PATH = false;
        bluecell_Currdatastatus.ATT_DL2_PATH = false;
        bluecell_Currdatastatus.ATT_DL3_PATH = false;
        bluecell_Currdatastatus.ATT_DL4_PATH = false;
        bluecell_Currdatastatus.ATT_UL1_PATH = false;
        bluecell_Currdatastatus.ATT_UL2_PATH = false;
        bluecell_Currdatastatus.ATT_UL3_PATH = false;
        bluecell_Currdatastatus.ATT_UL4_PATH = false;
        bluecell_Currdatastatus.Path_TempSave_Bit = 0;
        
    }
//    printf("PCB Version : %d.%d \r\n",bluecell_Currdatastatus.PCB_Version[0],bluecell_Currdatastatus.PCB_Version[1]);
//    printf("Serial Number : ");
//    for(int a = 0; a < 20; a++){
//        printf("%c",bluecell_Currdatastatus.Serial_Number[a]);
//    }
//    printf("\r\n");
//    printf("Manufacture_Date : ");
//    for(int a = 0; a < 3; a++){
//        printf("%c",bluecell_Currdatastatus.Manufacture_Date[a]);
//    }
//    printf("\r\n");
    


    bluecell_Currdatastatus.ALARM_TEMP_HIGH          = 0;
    bluecell_Currdatastatus.ALARM_DLI_Level          = 0;
    bluecell_Currdatastatus.ALARM_DLI_SHTUTDOWN  = 0;
    bluecell_Currdatastatus.ALARM_DLI_AGC_Alarm      = 0;
    bluecell_Currdatastatus.ALARM_ULO_ALC_Alarm      = 0;
    bluecell_Currdatastatus.ALARM_ULO_Level          = 0;
    bluecell_Currdatastatus.ALARM_ULO_SHTUTDOWN  = 0;



}










#if 0 // PYJ.2020.04.23_BEGIN -- 


void eepromtest(){
    memset(&eepdata[0],0x33,100);
    for(int i = 0; i < 100; i ++ ){
        printf("data[%d] : %x \r\n",i,eepdata[i]);
        EEPROM_M24C08_Bytewrite(EEPROM_M24C08_ID,EEPROM_ATT_BASE + i,&eepdata[i],1);
    }  
    for(int i = 0; i < 100; i ++ ){
        EEPROM_M24C08_ByteRead(EEPROM_M24C08_ID,EEPROM_ATT_BASE + i,&eepdata[i],1);
        printf("data[%d] : %x \r\n",i,eepdata[i]);
    }  
//    EEPROM_M24C08_Read(EEPROM_M24C08_ID,EEPROM_ATT_BASE,&eepdata[0],100);
//    for(int i = 0; i < 100; i ++ ){
//        printf("data[%d] : %x \r\n",i,eepdata[i]);
//    }    

}
#endif // PYJ.2020.04.23_END -- 

#define MAXEEPROM_LENG 32

HAL_StatusTypeDef EEPROM_M24C08_Read(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
   HAL_StatusTypeDef ret = HAL_ERROR;
   uint8_t sizecnt = 0,sizeremain = 0; 
   uint16_t addrees_inc = 0;
   sizecnt = size /MAXEEPROM_LENG;
   sizeremain = size % MAXEEPROM_LENG;
   addrees_inc = 0;

 //  uint16_t sizecnt = 0,
   //uint16_t sizeremain = 0;
  // uint16_t addrees_inc = 0;
//   ret = HAL_I2C_Mem_Read(&hi2c2, devid | ((Address & 0x0300) >> 7),((Address )),  I2C_MEMADD_SIZE_8BIT, &data[0], size, 1024);


#if 1 // PYJ.2020.06.28_BEGIN -- 
   ret = HAL_I2C_Mem_Read(&hi2c2, devid ,((Address )),  I2C_MEMADD_SIZE_16BIT, &data[0], size, 0xFFFF);
//      EEPROM24XX_Load( Address,data, size);

//      if(ret == HAL_ERROR)
//            printf("Write ERR\r\n");
#else
    if(sizecnt > 0){
        for(int i = 0 ; i < sizecnt; i++ ){
             addrees_inc = i * MAXEEPROM_LENG;
             ret = HAL_I2C_Mem_Read(&hi2c2, devid  ,((Address + addrees_inc) & 0xFFFF) ,  I2C_MEMADD_SIZE_16BIT, &data[addrees_inc], MAXEEPROM_LENG, 1024);
    
             if(ret == HAL_ERROR)
                 printf("Write ERR\r\n");
             
             HAL_Delay(20);
        }
        addrees_inc += MAXEEPROM_LENG;
    }

#endif // PYJ.2020.06.28_END -- 


//    HAL_Delay(20);

    return ret;   
}
HAL_StatusTypeDef EEPROM_M24C08_write(uint8_t devid,uint16_t Address,uint8_t* data,uint16_t size){
    HAL_StatusTypeDef ret = HAL_ERROR;
    uint8_t sizecnt = 0,sizeremain = 0; 
    uint16_t addrees_inc = 0;
    sizecnt = size /MAXEEPROM_LENG;
    sizeremain = size % MAXEEPROM_LENG;
    addrees_inc = 0;
#if 0 // PYJ.2020.04.25_BEGIN -- 
       for(int i = 0 ; i <sizecnt; i++ ){
            addrees_inc = i * 16;
            ret = HAL_I2C_Mem_Write(&hi2c2, devid | (((Address  + addrees_inc) & 0x0300) >> 7),((Address + addrees_inc)) ,  I2C_MEMADD_SIZE_8BIT, &data[addrees_inc], 16, 1024);
            if(ret == HAL_ERROR)
                printf("Write ERR\r\n");
            else
                HAL_Delay(20);
       }
       addrees_inc += 16;
           ret = HAL_I2C_Mem_Write(&hi2c2, devid | (((Address  + addrees_inc) & 0x0300) >> 7),((Address + addrees_inc)) ,  I2C_MEMADD_SIZE_8BIT, &data[addrees_inc], sizeremain, 1024);
            //       EEPROM24XX_Save( Address,data, size);
            if(ret == HAL_ERROR)
                printf("Write ERR\r\n");
            else
                HAL_Delay(20);
#else
//    printf("size : %d sizecnt = %d sizeremain : %d\r\n",size,sizecnt,sizeremain);
    if(sizecnt > 0){
        for(int i = 0 ; i < sizecnt; i++ ){
             addrees_inc = i * MAXEEPROM_LENG;
             ret = HAL_I2C_Mem_Write(&hi2c2, devid  ,((Address + addrees_inc) & 0xFFFF) ,  I2C_MEMADD_SIZE_16BIT, &data[addrees_inc], MAXEEPROM_LENG, 1024);

//             if(ret == HAL_ERROR)
//                 printf("Write ERR\r\n");
             
             HAL_Delay(20);
        }
        addrees_inc += MAXEEPROM_LENG;
    }

//    printf("Remain Data Index : %d \r\n",sizeremain);
    
    if(sizeremain > 0){
//         printf("Remain Data Write Start ");
        for(int i = 0; i < sizeremain; i++){
            ret = HAL_I2C_Mem_Write(&hi2c2, devid  ,((Address + addrees_inc + i)& 0xFFFF) ,  I2C_MEMADD_SIZE_16BIT, &data[addrees_inc + i], 1, 0xFFFF);
         //       EEPROM24XX_Save( Address,data, size);
//         if(ret == HAL_ERROR)
//             printf("Write ERR\r\n");
         
          HAL_Delay(20);
        }
    }
//    printf("EEPROM WRITE\r\n");

#endif // PYJ.2020.04.25_END -- 
    return ret;
}

HAL_StatusTypeDef EEPROM_M24C08_Zerowrite(uint8_t devid,uint16_t Address){
    HAL_StatusTypeDef ret = HAL_ERROR;
   // uint8_t sizeremain = 0;
    uint16_t addrees_inc = 0;
    addrees_inc = 0;
    static uint8_t data[1024] = {0,};
//    printf("EEPROM ALL ERASE \r\n");

    for(int i = 0 ; i < 128; i++ ){
         addrees_inc = i * MAXEEPROM_LENG;
         ret = HAL_I2C_Mem_Write(&hi2c2,
                                 devid  ,
                                 ((Address + addrees_inc) & 0xFFFF) ,
                                 I2C_MEMADD_SIZE_16BIT,
                                 &data[0],
                                 MAXEEPROM_LENG,
                                 0xFFFF);
    
//         if(ret == HAL_ERROR)
//             printf("Write ERR\r\n");
//         else
             HAL_Delay(20);
    }


    return ret;
}

