/*
 ** author: jimbo
 ** date: 2019-03-21
 ** version: 1.00
 ** e-mail:  zhouchao@storlead.com  1959353124@qq.com
 ** brief: INA220 driver
 *
*/

#include "bmc_main.h"
#include "i2c_api.h"
/* 
 *  ** sensor name: INA220
 *   ** brief:Volts
 *    **/
int INA220_3V3_Volts_init(hal_t *phal)
{
        printf("init INA220 3V3 Volts!\n");
        return 0;
}

int INA220_3V3_Volts_write(hal_t *phal)
{
        printf("write INA220 3V3 Volts!\n");
        return 0;
}

int INA220_3V3_Volts_read(hal_t *phal)
{
	uint8_t		pData[2]={0x00,0x00};
	uint16_t	*Volt;
	uint8_t		temp;
	I2C_DEV_T i2c_dev;
	i2c_dev.bus = I2C3;
	I2C_Master_Receiver(&i2c_dev,0x88,pData,0x02,0x02);
//        printf("read INA220 3V3 Volts!\n");
	temp=pData[1];
	pData[1]=pData[0];
	pData[0]=temp;
	Volt=pData;
//	printf("3V3 Volt is %x\n",*Volt);
//	printf("%x , %x \n",pData[0],pData[1]);
	if((*Volt>>5)>255)
		*phal->pbyte =255;
	else
        	*phal->pbyte =*Volt>>5;
        return 0;
}


/* 
 *  *  ** sensor name: INA220
 *   *   ** brief: Volts
 *    *    **/
int INA220_5V0_Volts_init(hal_t *phal)
{
        printf("init INA220 5V0 Volts!\n");
        return 0;
}

int INA220_5V0_Volts_write(hal_t *phal)
{
        printf("write INA220 5V0 Volts!\n");
        return 0;
}

int INA220_5V0_Volts_read(hal_t *phal)
{
        uint8_t         pData[2]={0x00,0x00};
        uint16_t        *Volt;
        uint8_t         temp;
        I2C_DEV_T i2c_dev;
        i2c_dev.bus = I2C3;
        I2C_Master_Receiver(&i2c_dev,0x82,pData,0x02,0x02);
	temp=pData[1];
        pData[1]=pData[0];
        pData[0]=temp;
        Volt=pData;

//        printf("read INA220 5V0 Volts!\n");
	if((*Volt>>6)>255)
		*phal->pbyte = 255;
	else
        	*phal->pbyte = *Volt>>6;
        return 0;
}

/* 
 *  *  *  ** sensor name: INA220
 *   *   *   ** brief: Volts
 *    *    *    **/
int INA220_12V0_Volts_init(hal_t *phal)
{
        printf("init INA220 12V0 Volts!\n");
        return 0;
}

int INA220_12V0_Volts_write(hal_t *phal)
{
        printf("write INA220 12V0 Volts!\n");
        return 0;
}

int INA220_12V0_Volts_read(hal_t *phal)
{
        uint8_t         pData[2]={0x00,0x00};
        uint16_t        *Volt;
        uint8_t         temp;
        I2C_DEV_T i2c_dev;
        i2c_dev.bus = I2C3;
        I2C_Master_Receiver(&i2c_dev,0x80,pData,0x02,0x02);
        temp=pData[1];
        pData[1]=pData[0];
        pData[0]=temp;
        Volt=pData;

//        printf("read INA220 12V0 Volts!\n");
        if((*Volt>>7)>255)
                *phal->pbyte = 255;
        else
                *phal->pbyte = *Volt>>7;
        return 0;

}


/* 
 *  *  *  *  *  *  ** sensor name: INA220
 *   *   *   *   *   *   ** brief: Amps
 *    *    *    *    *    *    **/
int INA220_3V3_Amps_init(hal_t *phal)
{
        printf("init INA220 3V3 Amps!\n");
        return 0;
}
        
int INA220_3V3_Amps_write(hal_t *phal)
{
        printf("write INA220 3V3 Amps!\n");
        return 0;
}
        
int INA220_3V3_Amps_read(hal_t *phal)
{
        uint8_t         pData[2]={0x00,0x00};
        uint16_t        *Amps;
        uint8_t         temp;
        I2C_DEV_T i2c_dev;
        i2c_dev.bus = I2C3;
        I2C_Master_Receiver(&i2c_dev,0x88,pData,0x04,0x02);
        temp=pData[1];
        pData[1]=pData[0];
        pData[0]=temp;
        Amps=pData;

//        printf("read INA220 3V3 Volts!\n");
        if((*Amps>>1)>255)
                *phal->pbyte = 255;
        else
                *phal->pbyte = *Amps>>1;
        return 0;

}


/* 
 *  *  *  *  *  ** sensor name: INA220
 *   *   *   *   *   ** brief: Amps
 *    *    *    *    *    **/
int INA220_5V0_Amps_init(hal_t *phal)
{
        printf("init INA220 5V0 Amps!\n");
        return 0;
}

int INA220_5V0_Amps_write(hal_t *phal)
{
        printf("write INA220 5V0 Amps!\n");
        return 0;
}

int INA220_5V0_Amps_read(hal_t *phal)
{
        uint8_t         pData[2]={0x00,0x00};
        uint16_t        *Amps;
        uint8_t         temp;
        I2C_DEV_T i2c_dev;
        i2c_dev.bus = I2C3;
        I2C_Master_Receiver(&i2c_dev,0x82,pData,0x04,0x02);
        temp=pData[1];
        pData[1]=pData[0];
        pData[0]=temp;
        Amps=pData;

//        printf("read INA220 5V0 Volts!\n");
        if((*Amps>>1)>255)
                *phal->pbyte = 255;
        else
                *phal->pbyte = *Amps>>1;
        return 0;

}


/* 
 *  *  *  *  ** sensor name: INA220
 *   *   *   *   ** brief: Amps
 *    *    *    *    **/
int INA220_12V0_Amps_init(hal_t *phal)
{
        printf("init INA220 12V0 Amps!\n");
        return 0;
}

int INA220_12V0_Amps_write(hal_t *phal)
{
        printf("write INA220 12V0 Amps!\n");
        return 0;
}

int INA220_12V0_Amps_read(hal_t *phal)
{
        uint8_t         pData[2]={0x00,0x00};
        uint16_t        *Amps;
        uint8_t         temp;
        I2C_DEV_T i2c_dev;
        i2c_dev.bus = I2C3;
        I2C_Master_Receiver(&i2c_dev,0x80,pData,0x04,0x02);
        temp=pData[1];
        pData[1]=pData[0];
        pData[0]=temp;
        Amps=pData;

//        printf("read INA220 12V0 Volts!\n");
        if((*Amps>>1)>255)
                *phal->pbyte = 255;
        else
                *phal->pbyte = *Amps>>1;
        return 0;

}


