// #include "r_typedefs.h"
// #include "Internal_Flash.h"
#include "Emulated_EEPROM.h"
#include "Emulated_EEPROM_Access.h"



/****************************************************************************
 *   !!! The following code should be generated by configuration tool !!!   *
****************************************************************************/
#define   EEPROM_BLOCK0_BUFFER_SIZE         256U
uint32_t g_u32EEPROMBlock0Buffer[EEPROM_BLOCK0_BUFFER_SIZE / 4U];




// #define   EEPROM_BLOCK0_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_FLAG_SIZE)
// uint32_t g_u32EEPROMBlock0Buffer[EEPROM_BLOCK0_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK1_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_S27_SIZE)
// uint32_t g_u32EEPROMBlock1Buffer[EEPROM_BLOCK1_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK2_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_S2E_SIZE)
// uint32_t g_u32EEPROMBlock2Buffer[EEPROM_BLOCK2_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK3_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_CONFIG_SIZE)
// uint32_t g_u32EEPROMBlock3Buffer[EEPROM_BLOCK3_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK4_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_UDS_DTC_SIZE)
// uint32_t g_u32EEPROMBlock4Buffer[EEPROM_BLOCK4_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK5_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_ODO_SIZE)
// uint32_t g_u32EEPROMBlock5Buffer[EEPROM_BLOCK5_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK6_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_ODO_STAMP_SIZE)
// uint32_t g_u32EEPROMBlock6Buffer[EEPROM_BLOCK6_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK7_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_TRIP_STAMP_SIZE)
// uint32_t g_u32EEPROMBlock7Buffer[EEPROM_BLOCK7_BUFFER_SIZE / 4U];

// #define   EEPROM_BLOCK8_BUFFER_SIZE         (EEPROM_BLOCK_CTRL_BYTE_SIZE + EEPROM_BLOCK_SERVICE_INFO_SIZE)
// uint32_t g_u32EEPROMBlock8Buffer[EEPROM_BLOCK8_BUFFER_SIZE / 4U];

static const EEPROM_Block_st_t g_stEEPROMBlockTable[] = 
{
    /* u32StartAddr   u32EndAddr    u32DataSize                pu32DataBuffer */
    {SECTOR_14_START_ADDR,    0x0801E7FFUL, EEPROM_BLOCK0_BUFFER_SIZE, g_u32EEPROMBlock0Buffer,},
    // {0x08019080UL,    0x080190FFUL, EEPROM_BLOCK1_BUFFER_SIZE, g_u32EEPROMBlock1Buffer,},
    // {0x08019100UL,    0x0801917FUL, EEPROM_BLOCK2_BUFFER_SIZE, g_u32EEPROMBlock2Buffer,},
    // {0x08019180UL,    0x080191FFUL, EEPROM_BLOCK3_BUFFER_SIZE, g_u32EEPROMBlock3Buffer,},
    // {0x08019200UL,    0x0801927FUL, EEPROM_BLOCK4_BUFFER_SIZE, g_u32EEPROMBlock4Buffer,},
    // {0x08019280UL,    0x0801967FUL, EEPROM_BLOCK5_BUFFER_SIZE, g_u32EEPROMBlock5Buffer,},
    // {0x08019680UL,    0x08019B7FUL, EEPROM_BLOCK6_BUFFER_SIZE, g_u32EEPROMBlock6Buffer,},
    // {0x08019B80UL,    0x0802157FUL, EEPROM_BLOCK7_BUFFER_SIZE, g_u32EEPROMBlock7Buffer,},
    // {0x08021580UL,    0x080215FFUL, EEPROM_BLOCK8_BUFFER_SIZE, g_u32EEPROMBlock8Buffer,},
};

/****************************************************************************
 *             !!!          End of code generation          !!!             *
****************************************************************************/

EEPROM_Block_Access_st_t g_stEEPROMBlockAccess[EEPROM_TOTAL_BLOCK_NUM];

void EEPROM_Init(void)
{
    EEPROM_Media_Access_st_t stFlashAccessFunc;

    // TYW_FDL_Init();
    
    stFlashAccessFunc.pfnMemErase    = Test_Flash_AppointAddress_Erase;//Int_Data_Flash_Erase;
    stFlashAccessFunc.pfnMemBlankChk = Test_APP_FlashBlank;//Int_Data_Flash_Blank_Check;
    stFlashAccessFunc.pfnMemRead     = Test_Flash_AppointAddress_Read;//Int_Data_Flash_Read;
    stFlashAccessFunc.pfnMemWrite    = Test_Flash_AppointAddress_Write;//Int_Data_Flash_Write;
    
    // Int_Data_Flash_Register_P_E_Complete_Cb(EEPROM_Mem_Access_Complete_Callback);
    
    EEPROM_Access_Init( g_stEEPROMBlockTable,
                        g_stEEPROMBlockAccess,
                        &stFlashAccessFunc,
                        EEPROM_TOTAL_BLOCK_NUM);
}

EEPROM_Status_en_t EEPROM_Get_Status(void)
{
    EEPROM_Status_en_t enStatus;
    
    if(EEPROM_Access_Busy())
    {
        enStatus = EEPROM_STAT_BUSY;
    }
    else
    {
        enStatus = EEPROM_STAT_IDLE;
    }
    
    return enStatus;
}

EEPROM_Block_Status_en_t EEPROM_Get_Block_Status(uint16_t u16BlockID)
{
    uint8_t u8BlockStatus;
    EEPROM_Block_Status_en_t enStatus;
    
    u8BlockStatus = EEPROM_Block_Access_Status(u16BlockID);
    if (u8BlockStatus == EEPROM_BLOCK_BLANK)
    {
        enStatus = EEPROM_STAT_BLANK;
    }
    else if (u8BlockStatus == EEPROM_BLOCK_ACTIVE)
    {
        enStatus = EEPROM_STAT_ACTIVE;
    }
    else
    {
        enStatus = EEPROM_STAT_ERROR;
    }
    
    return enStatus;
}

/*u16Len是数组的长度*/
EEPROM_RW_Result_en_t EEPROM_Read_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len)
{
    uint8_t u8Error;
    EEPROM_RW_Result_en_t enResult;
    
    u8Error = EEPROM_Read_Block_Data(u16BlockID, u32Data, u16Len);
    if (u8Error)
    {
        enResult = EEPROM_RW_FAIL;
    }
    else
    {
        enResult = EEPROM_RW_PASS;
    }
    
    return enResult;
}

/*u16Len是数组的长度*/
EEPROM_RW_Result_en_t EEPROM_Write_Data(uint16_t u16BlockID, uint32_t u32Data[], uint16_t u16Len)
{
    uint8_t u8Error;
    EEPROM_RW_Result_en_t enResult;
    
    u8Error = EEPROM_Write_Block_Data(u16BlockID, u32Data, u16Len);
    if (u8Error)
    {
        enResult = EEPROM_RW_FAIL;
    }
    else
    {
        enResult = EEPROM_RW_PASS;
    }
    
    return enResult;
}

