/*
 * Implement sensor driver function in here.
 * Every sensor include init,write,read function.
 * Author:Jimbo
 */

#include <stdint.h>
#include <stdio.h>
#include "sensor_tbl.h"
#include "hal_interface_api.h"
#include "linux/fcntl.h"

static uint8_t i = 0;
/* 
 * sensor number: 1
 * sensor name: ltc2990_temp
 * brief: ltc2990 internal temperature
 */
int sensor_1_init(void)
{
	int ret = 0;
	uint8_t buf[2];
	int fd = open("/dev/i2c3", O_RDWR);
	//configure ltc2990
	buf[0] = 0x01;	//control register
	buf[1] = 0x18;	//register val
	ret = stm32_i2c_master_write(fd, 0x98, buf, 2);
	if(ret != 0)
	{
		printf("Initialize ltc2990 failed!\n");
		close(fd);
		return -1;
	}
	//start conversion
	buf[0] = 0x02;	//trigger register
	buf[1] = 0xAA;
	ret = stm32_i2c_master_write(fd, 0x98, buf, 2);
	if(ret != 0)
	{
		printf("Initialize ltc2990 failed!\n");
		close(fd);
		return -1;
	}
	close(fd);
	return 0;	
}

int sensor_1_write(void)
{
	return 0;
}

int sensor_1_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x4;	//internal temperature
		ret = stm32_i2c_master_write_read(fd, 0x98, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 1 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<4) | (buf[1]>>4);
	return 0;
}

/* 
 * sensor number: 2
 * sensor name: ltc2990_VCC
 * brief:  ltc2990 VCC
 */
int sensor_2_init(void)
{
	return 0;	
}

int sensor_2_write(void)
{
	return 0;
}

int sensor_2_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x0E;	//suply vcc
		ret = stm32_i2c_master_write_read(fd, 0x98, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 2 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<2) | (buf[1]>>6);
	return 0;
}

/* 
 * sensor number: 3
 * sensor name: ltc2990_V1_+5V
 * brief: ltc2990 single end V1 
 */
int sensor_3_init(void)
{
	return 0;	
}

int sensor_3_write(void)
{
	return 0;
}

int sensor_3_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x06;	//V1
		ret = stm32_i2c_master_write_read(fd, 0x98, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 3 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<2) | (buf[1]>>6);
	return 0;
}


/* 
 * sensor number: 4
 * sensor name: ltc2990_V2_+5V
 * brief: ltc2990 single end V2 
 */
int sensor_4_init(void)
{
	return 0;	
}

int sensor_4_write(void)
{
	return 0;
}

int sensor_4_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x08;	//V2
		ret = stm32_i2c_master_write_read(fd, 0x98, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 4 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<2) | (buf[1]>>6);
	return 0;
}

/* 
 * sensor number: 5
 * sensor name: ltc2990_TR2_Temp
 * brief: ltc2990 TR2
 */
int sensor_5_init(void)
{
	return 0;	
}

int sensor_5_write(void)
{
	return 0;
}

int sensor_5_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x0A;	//TR2
		ret = stm32_i2c_master_write_read(fd, 0x98, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 5 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else if(buf[0]&0x40)
		{
			printf("Detect ltc2990 TR2 Sensor Short!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		else if(buf[0]&0x20)
		{
			printf("Detect ltc2990 TR2 Sensor Open!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<4) | (buf[1]>>4);
	return 0;
}

/* 
 * sensor number: 6
 * sensor name: Current_+3.3V
 * brief: adc_ch1, STMC109
 */
int sensor_6_init(void)
{
	return 0;	
}

int sensor_6_write(void)
{
	return 0;
}

int sensor_6_read(uint8_t *reading)
{
	uint16_t buf;
	stm32_adc_get_value(1, &buf);
	*reading = buf>>4;
	return 0;
}

/* 
 * sensor number: 7
 * sensor name: ltc2991_Temp
 * brief: ltc2991 internal temperature
 */
int sensor_7_init(void)
{
	int ret = 0;
	uint8_t buf[2];
	int fd = open("/dev/i2c3", O_RDWR);
	
	//V1,V2,V3,V4 configuration
	buf[0] = 0x06;	
	buf[1] = 0x01;
	ret = stm32_i2c_master_write(fd, 0x9e, buf, 2);
	if(ret != 0)
	{
		printf("Initialize ltc2991 failed!\n");
		close(fd);
		return -1;
	}
	//V5, V6, V7, V8 
	buf[0] = 0x07;	
	buf[1] = 0x33;
	ret = stm32_i2c_master_write(fd, 0x9e, buf, 2);
	if(ret != 0)
	{
		printf("Initialize ltc2991 failed!\n");
		close(fd);
		return -1;
	}
	//VCC T_internal
	buf[0] = 0x08;	
	buf[1] = 0x10;
	ret = stm32_i2c_master_write(fd, 0x9e, buf, 2);
	if(ret != 0)
	{
		printf("Initialize ltc2991 failed!\n");
		close(fd);
		return -1;
	}

	//Channel Enable, start conversion
	buf[0] = 0x01;	//control register
	buf[1] = 0xE8;	//register val
	ret = stm32_i2c_master_write(fd, 0x9e, buf, 2);
	if(ret != 0)
	{
		printf("Initialize ltc2991 failed!\n");
		close(fd);
		return -1;
	}
	close(fd);
	return 0;		
}

int sensor_7_write(void)
{
	return 0;
}

int sensor_7_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x1A;	//internal temperature
		ret = stm32_i2c_master_write_read(fd, 0x9e, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 7 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<4) | (buf[1]>>4);
	return 0;
}

/* 
 * sensor number: 8
 * sensor name: ltc2991_VCC
 * brief:  ltc2991 VCC
 */
int sensor_8_init(void)
{
	return 0;	
}

int sensor_8_write(void)
{
	return 0;
}

int sensor_8_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x1c;	//suply vcc
		ret = stm32_i2c_master_write_read(fd, 0x9e, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 8 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<2) | (buf[1]>>6);
	return 0;
}

/* 
 * sensor number: 9
 * sensor name: ltc2991_V3_+5V
 * brief: ltc2991 single end V3 
 */
int sensor_9_init(void)
{
	return 0;	
}

int sensor_9_write(void)
{
	return 0;
}

int sensor_9_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x0E;	//V3
		ret = stm32_i2c_master_write_read(fd, 0x9e, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 9 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<2) | (buf[1]>>6);
	return 0;
}


/* 
 * sensor number: 10
 * sensor name: ltc2991_V4_+5V
 * brief: ltc2991 single end V4
 */
int sensor_10_init(void)
{
	return 0;	
}

int sensor_10_write(void)
{
	return 0;
}

int sensor_10_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x10;	//V4
		ret = stm32_i2c_master_write_read(fd, 0x9e, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 10 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<2) | (buf[1]>>6);
	return 0;
}

/* 
 * sensor number: 11
 * sensor name: ltc2991_TR3_Temp
 * brief: ltc2991 TR3
 */
int sensor_11_init(void)
{
	return 0;	
}

int sensor_11_write(void)
{
	return 0;
}

int sensor_11_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x12;	//TR3
		ret = stm32_i2c_master_write_read(fd, 0x9e, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 11 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else if(buf[0]&0x40)
		{
			printf("Detect ltc2991 TR3 Sensor Short!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		else if(buf[0]&0x20)
		{
			printf("Detect ltc2991 TR3 Sensor Open!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<4) | (buf[1]>>4);
	return 0;
}

/* 
 * sensor number: 12
 * sensor name: ltc2991_TR4_Temp
 * brief: ltc2991 TR4
 */
int sensor_12_init(void)
{
	return 0;	
}

int sensor_12_write(void)
{
	return 0;
}

int sensor_12_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int retry = 3;
	int fd = open("/dev/i2c3", O_RDWR);
	do
	{
		buf[0] = 0x16;	//TR4
		ret = stm32_i2c_master_write_read(fd, 0x9e, buf, 1, buf, 2);
		if(ret != 0)
		{
			printf("Sensor 12 read error!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		if(buf[0]&0x80)
		{
			//Read Valid data
			break;
		}
		else if(buf[0]&0x40)
		{
			printf("Detect ltc2991 TR4 Sensor Short!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		else if(buf[0]&0x20)
		{
			printf("Detect ltc2991 TR4 Sensor Open!\n");
			*reading = 0;
			close(fd);
			return -1;
		}
		else
		{
			retry--;
		}
	}while(retry>0);

	close(fd);

	*reading = (buf[0]<<4) | (buf[1]>>4);
	return 0;
}

/* 
 * sensor number: 13
 * sensor name: LM75A_Temp
 * brief: 
 */
int sensor_13_init(void)
{
	return 0;	
}

int sensor_13_write(void)
{
	return 0;
}

int sensor_13_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int fd = open("/dev/i2c3", O_RDWR);
	
	buf[0] = 0x00;	
	ret = stm32_i2c_master_write_read(fd, 0x94, buf, 1, buf, 2);
	if(ret != 0)
	{
		printf("Sensor 13 read error!\n");
		*reading = 0;
		close(fd);
		return -1;
	}

	close(fd);

	*reading = buf[0]; 
	return 0;
}

/* 
 * sensor number: 14
 * sensor name: LM73_Temp
 * brief: 
 */
int sensor_14_init(void)
{
	return 0;	
}

int sensor_14_write(void)
{
	return 0;
}

int sensor_14_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int fd = open("/dev/i2c3", O_RDWR);
	
	buf[0] = 0x00;	
	ret = stm32_i2c_master_write_read(fd, 0x90, buf, 1, buf, 2);
	if(ret != 0)
	{
		printf("Sensor 14 read error!\n");
		*reading = 0;
		close(fd);
		return -1;
	}

	close(fd);

	*reading = (buf[0]&0x80) | (buf[0]<<1) | (buf[1]>>7);
	return 0;
}

/* 
 * sensor number: 15
 * sensor name: NST175_Temp
 * brief: 
 */
int sensor_15_init(void)
{
	return 0;	
}

int sensor_15_write(void)
{
	return 0;
}

int sensor_15_read(uint8_t *reading)
{
	uint8_t buf[2];
	int ret;
	int fd = open("/dev/i2c3", O_RDWR);
	
	buf[0] = 0x00;	
	ret = stm32_i2c_master_write_read(fd, 0x92, buf, 1, buf, 2);
	if(ret != 0)
	{
		printf("Sensor 15 read error!\n");
		*reading = 0;
		close(fd);
		return -1;
	}

	close(fd);

	*reading = buf[0];
	return 0;
}


sensor_tbl_t sensor_tbl[]=
{
	{
		1,		//sensor number
		0,		//power on delay
		0,		//reset delay
		3,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_1_read,	//read function
		sensor_1_write,	//write functon
		sensor_1_init,	//init function
	},
	
	{
		2,		//sensor number
		0,		//power on delay
		0,		//reset delay
		5,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_2_read,
		sensor_2_write,
		sensor_2_init,
	},
	
	{	
		3,		//sensor number
		0,		//power on delay
		0,		//reset delay
		5,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_3_read,
		sensor_3_write,
		sensor_3_init,
	},
	
	{	
		4,		//sensor number
		0,		//power on delay
		0,		//reset delay
		5,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_4_read,
		sensor_4_write,
		sensor_4_init,
	},
	
	{	
		5,		//sensor number
		0,		//power on delay
		0,		//reset delay
		5,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_5_read,
		sensor_5_write,
		sensor_5_init,
	},
	
	{	
		6,		//sensor number
		0,		//power on delay
		0,		//reset delay
		5,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_6_read,
		sensor_6_write,
		sensor_6_init,
	},
	
	{	
		7,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_7_read,
		sensor_7_write,
		sensor_7_init,
	},
	
	{	
		8,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_8_read,
		sensor_8_write,
		sensor_8_init,
	},
	
	{	
		9,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_9_read,
		sensor_9_write,
		sensor_9_init,
	},
	
	{	
		10,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_10_read,
		sensor_10_write,
		sensor_10_init,
	},
	
	{	
		11,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_11_read,
		sensor_11_write,
		sensor_11_init,
	},
	
	{	
		12,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_12_read,
		sensor_12_write,
		sensor_12_init,
	},
	
	{	
		13,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_13_read,
		sensor_13_write,
		sensor_13_init,
	},
	
	{	
		14,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_14_read,
		sensor_14_write,
		sensor_14_init,
	},
	
	{	
		15,		//sensor number
		0,		//power on delay
		0,		//reset delay
		2,		//Monitor interval
		MonitorOnStandby,	//monitor states
		sensor_15_read,
		sensor_15_write,
		sensor_15_init,
	}	//最后的逗号不能有
	
	
};


sensor_tbl_t *getSensorDev(uint8_t sensorNum)
{
	uint16_t i = 0;
	uint16_t sensorCnt=0;
	sensorCnt = sizeof(sensor_tbl)/sizeof(sensor_tbl_t);
	
	for(i=0; i<sensorCnt; i++)
	{
		if(sensor_tbl[i].sensor_number == sensorNum)	
			return &sensor_tbl[i];
	}
	
	printf("Error: Can't find sensor, sensorNum = %#x\r\n", sensorNum);
	return NULL;
}
