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


#include "i2c_api.h"
#include "./../../driver/I2C/i2c.h"
#include <stdint.h>
#include <stdio.h>
#include "./../../stm32f429xx.h"
#include "./../../stm32f429.h"

/* Send a frame data to i2c device.
 * Start + Addr/W(7bit+1bit) + data*Size + Stop
 */
int I2C_Master_Transmit(I2C_DEV_T *dev, uint8_t DevAddress, uint8_t *pData, uint8_t start_reg, uint16_t Size)
{
	int fd;
	char dev_name[15] = {0};
	int ret = 0;
	int i;


	switch((uint32_t)dev->bus)
	{
	case (uint32_t)I2C1:
		sprintf(dev_name,"/dev/i2c1");
		break;
	case (uint32_t)I2C2:
		sprintf(dev_name,"/dev/i2c2");
		break;
	case (uint32_t)I2C3:
		sprintf(dev_name,"/dev/i2c3");
		break;
	default:
		printf("Error: %s %d Invalid device! %#x\n",__FILE__,__LINE__,(uint32_t)dev->bus);
		return -1;
	}
	
	/* open device	*/
	fd = open(dev_name,O_RDWR);
	if(fd == -1)
	{
		printf("Error:%s %d open %s fail!\n",__FILE__,__LINE__,dev_name);
		return -1;
	}

	/* Start signal */
	if(ioctl(fd,GENERATE_START,dev) != 0)
	{
		printf("Error %s %d: Generate start fail!\n",__FILE__,__LINE__);
		ret = -1;
		goto STOP;
	}
//	printf("start ok --->%#x\n",dev->bus);
	
	/* Send slave address */
	dev->slaveAddr = DevAddress;
	if(ioctl(fd,SEND_SLAVE_ADDR,dev) != 0)
	{
		printf("Error %s %d: Send slave addr %#x fail!\n",__FILE__,__LINE__,dev->slaveAddr);
		ret = -1;
		goto STOP;
	}	

        /* Send data address */
        dev->reg = start_reg;
        if(ioctl(fd,SEND_DATA_ADDR,dev) != 0)
        {
                printf("Error %s %d: Send data addr %#x fail!\n",__FILE__,__LINE__,dev->reg);
                ret = -1;
                goto STOP;
        }

	/* Send data */
	for(i=0;i<Size;i++)
	{
		dev->data = pData[i];
		if(ioctl(fd,SEND_DATA,dev) != 0)
		{
			printf("Error %s %d: Send data fail!\n",__FILE__,__LINE__);
			ret = -1;
			goto STOP;
		}
	}
//	printf("data ok ---> %#x\n",dev->bus);
STOP:
	/* Stop signal */
	if(ioctl(fd,GENERATE_STOP,dev) != 0)
	{
		printf("Error %s %d: Generate stop fail!\n",__FILE__,__LINE__);
		ret = -1;
	}
//	printf("stop ok ---> %#x\n",dev->bus);
	/* Close device */
	close(fd);
	return ret;
}


int I2C_Master_Receiver(I2C_DEV_T *dev, uint8_t DevAddress, uint8_t *pData, uint8_t start_reg, uint16_t Size)
{
        int fd;
        char dev_name[15] = {0};
        int ret = 0;
        int i;


        switch((uint32_t)dev->bus)
        {
        case (uint32_t)I2C1:
                sprintf(dev_name,"/dev/i2c1");
                break;
        case (uint32_t)I2C2:
                sprintf(dev_name,"/dev/i2c2");
                break;
        case (uint32_t)I2C3:
                sprintf(dev_name,"/dev/i2c3");
                break;
        default:
                printf("Error: %s %d Invalid device! %#x\n",__FILE__,__LINE__,(uint32_t)dev->bus);
                return -1;
        }

        /* open device  */
        fd = open(dev_name,O_RDWR);
        if(fd == -1)
        {
                printf("Error:%s %d open %s fail!\n",__FILE__,__LINE__,dev_name);
                return -1;
        }

        /* Start signal */
        if(ioctl(fd,GENERATE_START,dev) != 0)
        {
                printf("Error %s %d: Generate start fail!\n",__FILE__,__LINE__);
                ret = -1;
                goto STOP;
        }
//        printf("start ok --->%#x\n",dev->bus);

        /* Send slave address */
        dev->slaveAddr = DevAddress;
        if(ioctl(fd,SEND_SLAVE_ADDR,dev) != 0)
        {
                printf("Error %s %d: Send slave addr %#x fail!\n",__FILE__,__LINE__,dev->slaveAddr);
                ret = -1;
                goto STOP;
        }

        /* Send data address */
        dev->reg = start_reg;
        if(ioctl(fd,SEND_DATA_ADDR,dev) != 0)
        {
                printf("Error %s %d: Send data addr %#x fail!\n",__FILE__,__LINE__,dev->reg);
                ret = -1;
                goto STOP;
        }
	ioctl(fd,ACK_ENABLE,dev);
        /* Start signal */
        if(ioctl(fd,GENERATE_START,dev) != 0)
        {
                printf("Error %s %d: Generate start fail!\n",__FILE__,__LINE__);
                ret = -1;
                goto STOP;
        }

        /* receiver slave address */
        dev->slaveAddr = DevAddress;
        if(ioctl(fd,RECEIVER_SLAVE_ADDR,dev) != 0)
        {
                printf("Error %s %d: receiver slave addr %#x fail!\n",__FILE__,__LINE__,dev->slaveAddr);
                ret = -1;
                goto STOP;
        }

	for(i=0;i<Size;i++)
	{
		if(i==Size-1)
		{
			ioctl(fd,ACK_DISABLE,dev);
			ioctl(fd,GENERATE_STOP,dev);
		}
        /* receiver data address */
        	if(ioctl(fd,READ_DATA,dev) != 0)
        	{
                	printf("Error %s %d: Send slave addr %#x fail!\n",__FILE__,__LINE__,dev->data);
                	ret = -1;
                	goto STOP;
        	}
		pData[i]=dev->data;		

	}
//	ioctl(fd,GENERATE_STOP,dev);
//	ioctl(fd,ACK_ENABLE,dev);
//      ioctl(fd,GENERATE_STOP,dev);
	close(fd);
//	printf("read data is %d ,%d",pData[0],pData[1]);
	return 0;
STOP:
	ioctl(fd,GENERATE_STOP,dev);
	//ioctl(fd,ACK_ENABLE,dev);
	close(fd);
	return -1;
}


