#include <stdio.h>
#include <unistd.h>
#include "exo_ahw_vdp_data_logger_ad7291.h"
#include "exo_ahw_al_linux_wrpr.h"
#include "ad7291.h"
#include "exo_common.h"
#include "exo_io_al_i2c_common.h"

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

ad7291_dev_t vd_dl_bp_ps1;
ad7291_dev_t vd_dl_bp_ps2;
ad7291_dev_t vd_dl_bp_oc;


uint8_t ad7291_i2c_write(void* intf_hdl, uint16_t slv_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
    ioal_i2c_hdle *hi2c = (ioal_i2c_hdle*)intf_hdl;
    int8_t rslt;
    uint8_t tx_arr[3];
    tx_arr[0]= reg_addr;
    tx_arr[1]= data[0];
    if (len>1)
        tx_arr[2]= data[1];
    len++;
    if(HAL_SCS == io_hal_i2c_transmit(hi2c, (uint16)slv_addr, tx_arr, len, 500))
    {
        rslt =AD7291_SCS;

    }
    else
    {
        rslt =AD7291_ERR;
    }
    return rslt;
}

 uint8_t ad7291_i2c_read(void* intf_hdl, uint16_t slv_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
    ioal_i2c_hdle *hi2c = (ioal_i2c_hdle*)intf_hdl;
    int8_t rslt;
    if(HAL_SCS == io_hal_i2c_transmit(hi2c, (uint16)slv_addr, &reg_addr, 1, 500))
    {
        if(HAL_SCS == io_hal_i2c_receive(hi2c, (uint16)slv_addr, data, len, 500))
        {
            rslt =AD7291_SCS;
        }
        else
        {
            rslt =AD7291_ERR;
        }
    }
    else
    {
        rslt =AD7291_ERR;
    }
    return rslt;
}

hal_ret_sts ahw_vdp_dl_ad7291_init(ahw_al_ad7291_hdl* al_dl_h)
{
    hal_ret_sts sts = HAL_SCS;

    ad7291_dev_t* vdh_dl = NULL;
    switch(al_dl_h->ahw_gen_info.ahw_inst_id)
    {
        case ADC_AD7291_PS_1:
        	vdh_dl = (void*)&vd_dl_bp_ps1;
            break;
        case ADC_AD7291_PS_2:
        	vdh_dl = (void*)&vd_dl_bp_ps2;
            break;
        case ADC_AD7291_ORING_CIRCUIT:
        	vdh_dl = (void*)&vd_dl_bp_oc;
            break;

        default:
            sts = HAL_AH_INVLD_ARG;
            break;
    }

    if(vdh_dl != NULL)
    {
#ifdef LINUX_TEMP_PORT
    vdh_dl->write = skt_intf_write;
    vdh_dl->read = skt_intf_read;
#else
    vdh_dl->write = ad7291_i2c_write;
    vdh_dl->read = ad7291_i2c_read;
#endif
    vdh_dl->io_intf_hdle = intf_inst_hdle_ptr[ahw_io_lookup_tble[al_dl_h->ahw_gen_info.ahw_inst_id]->io_instance_id];
    vdh_dl->slave_address=ahw_io_lookup_tble[al_dl_h->ahw_gen_info.ahw_inst_id]->slv_addr;
    al_dl_h->ahw_gen_info.vdp_inst_hdle = (void*)vdh_dl;
    al_dl_h->ahw_gen_info.io_intf_hdle=vdh_dl->io_intf_hdle;
    al_dl_h->ahw_gen_info.slave_address=vdh_dl->slave_address;

    DEBUG_CPRINT(("\n core temp sns slave addr = 0x%x\n",vdh_dl->slave_address));

    if(HAL_SCS == (hal_ret_sts)ad7291_init(vdh_dl))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }

#ifdef LINUX_TEMP_PORT
    sts = HAL_SCS;
#endif
    }
    else
    {
        sts = HAL_AH_INVLD_ARG;
    }
    return sts;
}
hal_ret_sts ahw_vdp_dl_ad7291_get_voltage_convertion(ahw_al_ad7291_hdl* al_dl_h, adc_channel_id chnl,float *data)
{
	hal_ret_sts sts;
	ad7291_dev_t *vdh_dl;
    vdh_dl = (ad7291_dev_t*) al_dl_h->ahw_gen_info.vdp_inst_hdle;

    if(HAL_SCS == (hal_ret_sts)ad7291_get_voltage_convertion(vdh_dl,chnl,data))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;

}

hal_ret_sts ahw_vdp_data_logger_self_test(ahw_al_ad7291_hdl* hdata)
{
    hal_ret_sts sts;
    ad7291_dev_t *adc_self_test;
    adc_self_test = (ad7291_dev_t*)hdata->ahw_gen_info.vdp_inst_hdle;
    if(HAL_SCS == (hal_ret_sts)ad7291_self_test(adc_self_test))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}
