102 lines
1.8 KiB
C
102 lines
1.8 KiB
C
#if 1
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h> //define O_WRONLY and O_RDONLY
|
|
|
|
#include <unistd.h> /* System V */
|
|
#include <sys/ioctl.h> /* BSD and Linux */
|
|
#include <stropts.h> /* XSI STREAMS */
|
|
|
|
#include "../inc/type.h"
|
|
#include "../inc/igpio.h"
|
|
#include "../inc/ii2c.h"
|
|
//#include <i2c-dev.h>
|
|
#include <linux/i2c-dev.h>
|
|
|
|
#define __use_imx_i2c__ 0
|
|
#if __use_imx_i2c__
|
|
int ii2c_com_init(const char*i2c_node_name,uint8 i2c_addr, int *pfd)
|
|
{
|
|
int fd=-1;
|
|
int err;
|
|
|
|
fd = open(i2c_node_name, O_RDWR);
|
|
if(fd < 0)
|
|
{
|
|
log_printf(LOG_ERR,I_NOUSED,"i2c open %s err.\n",i2c_node_name);
|
|
return -1;
|
|
}
|
|
|
|
err = ioctl(fd, I2C_SLAVE, i2c_addr);
|
|
if (err < 0)
|
|
{
|
|
log_printf(LOG_ERR,I_NOUSED,"can't set i2c dev slave addr.\n");
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
|
|
*pfd = fd;
|
|
}
|
|
|
|
uint8 i2c_write(int fd, uint8 reg, uint8 val)
|
|
{
|
|
int retries;
|
|
uint8 wdata[2];
|
|
uint8 retLen = 0;
|
|
|
|
if(fd<0)
|
|
return -1;
|
|
|
|
wdata[0] = reg;
|
|
wdata[1] = val;
|
|
|
|
for (retries = 2; retries; retries--)
|
|
{
|
|
retLen = write(fd, wdata, 2);
|
|
if(retLen == 2)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
log_printf(LOG_DEBUG,I_NOUSED,"ret writeLen = %d\n",retLen);
|
|
usleep(1000 * 2);
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
uint8 i2c_read(int fd, uint8 reg, uint8 *pval)
|
|
{
|
|
int retries;
|
|
uint8 retLen=0;
|
|
|
|
if(fd<0)
|
|
return -1;
|
|
|
|
for (retries = 2; retries; retries--)
|
|
{
|
|
retLen = write(fd, ®, 1);
|
|
|
|
if(retLen == 1)
|
|
{
|
|
if (read(fd, pval, 1) == 1)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
log_printf(LOG_DEBUG,I_NOUSED,"ret writeLen = %d\n",retLen);
|
|
usleep(1000 * 2);
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
#endif//__imx_i2c__
|
|
|
|
|
|
#endif
|
|
|