#include "m24c16.h"
#include "exo_hal_common.h"
#include "exo_ahw_al_ext_flash_common.h"

extern void *intf_inst_hdle_ptr[MAX_IO_INST_ID];
extern ahw_io_map *ahw_io_lookup_tble[MAX_AH_INST_ID];

hal_ret_sts ahw_al_vdp_m24c16_init(ahw_al_ext_mem_hdl *al_em_h)
{
    hal_ret_sts sts = HAL_SCS;
    m24c16_t* vdh_mem = (m24c16_t*)os_malloc(sizeof(m24c16_t));
    vdh_mem->intf_hdl = intf_inst_hdle_ptr[ahw_io_lookup_tble[al_em_h->ahw_gen_info.ahw_inst_id]->io_instance_id];

    /* update io interface & vdp handle */
    al_em_h->ahw_gen_info.vdp_inst_hdle = (void*)vdh_mem;
    al_em_h->ahw_gen_info.io_intf_hdle=vdh_mem->intf_hdl;
    al_em_h->flash_size = M24C16_TOTAL_SIZE;

    return sts;
}

hal_ret_sts ahw_vdp_m24c16_eeprom_write(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
    hal_ret_sts sts;
    m24c16_t vdh_mem;
    vdh_mem.intf_hdl = al_em_h->ahw_gen_info.io_intf_hdle;
    if(HAL_SCS == (hal_ret_sts)m24c16_eeprom_write_buffer(&vdh_mem, address, data, size))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}

hal_ret_sts ahw_vdp_m24c16_eeprom_read(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
    hal_ret_sts sts;
    m24c16_t vdh_mem;
    vdh_mem.intf_hdl = al_em_h->ahw_gen_info.io_intf_hdle;
    if(HAL_SCS == (hal_ret_sts)m24c16_eeprom_read_buffer(&vdh_mem, address, data, size))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}


hal_ret_sts ahw_vdp_m24c16_eeprom_erase_full_chip(ahw_al_ext_mem_hdl *al_em_h)
{
    hal_ret_sts sts;
    m24c16_t vdh_mem;
    vdh_mem.intf_hdl = al_em_h->ahw_gen_info.io_intf_hdle;

    if(HAL_SCS == (hal_ret_sts)m24c16_eeprom_erase_full_chip(&vdh_mem))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}
