Exemplo n.º 1
0
int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* e)
{
	int fd, r;
	unsigned long funcs;
	e->fd = e->addr = 0;
	e->dev = 0;
	
	fd = open(dev_fqn, O_RDWR);
	if(fd <= 0)
		return -1;

	// get funcs list
	if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
		return r;

	
	// check for req funcs
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );

	// set working device
	if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)
    {
		return r;
    }
	e->fd = fd;
	e->addr = addr;
	e->dev = dev_fqn;
	e->type = type;
	return 0;
}
Exemplo n.º 2
0
int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* e)
{
	int funcs, fd, r;
	e->fd = e->addr = 0;
	e->dev = 0;
	
	fd = open(dev_fqn, O_RDWR);
	if(fd <= 0)
	{
		fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
		return -1;
	}

	// get funcs list
	if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
	{
		fprintf(stderr, "Error ioctl(I2C_FUNCS): %s\n", strerror(errno));
		return -1;
	}

	
	// check for req funcs
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );
	CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );

	// set working device
	if( ( r = ioctl(fd, I2C_SLAVE_FORCE, addr)) < 0)
	{
		fprintf(stderr, "Error ioctl(I2C_SLAVE): %s\n", strerror(errno));
		return -1;
	}
	e->fd = fd;
	e->addr = addr;
	e->dev = dev_fqn;
	e->type = type;
	return 0;
}
Exemplo n.º 3
0
// 主函数
int main(int argc, char* argv[])
{
	int fd;
	int ret = 0;
	int funcs;
	int i;
	// 1  打开 ,从设备打开
	if (argc==2) {
		fd = open(argv[1], O_RDWR);
	} else {
		fd = open(DEV, O_RDWR);
	}
	if (fd<0) {
		perror("打开:");
		printf("fd=%d\n", fd);
		return -1;
	}
	// 1.1 获得支持函数列表<不必须>但是可以知道很多有用的信息
	// 支持的函数返回,并保存在 funcs 中.
	if ((ret = ioctl(fd, I2C_FUNCS, &funcs)<0))
	{
		perror("获得支持函数列表");
		return -1;
	}
	// check for req funcs 通过 & 操作得到是不是支持这些函数.
	// 应用层可以判断什么函数能用,什么不能用.类似向下兼容的作用
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_WORD_DATA);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA);
	// 2 设置从设备地址
	ret = ioctl(fd, I2C_SLAVE, ADDRESS);
	if (ret<0) {
		perror("ioctl:");
		printf("i2c_slave=%d\n", I2C_SLAVE);
		goto CLOSE;
	}
	//读取器件id
	ret = adxl_read(fd, REG_DEVID);
	if (ret<0) {
		perror("器件id");
	}
	printf("器件ID[0x%X]=0x%X\n", REG_DEVID, ret);
#if 0
	//其他寄存器测试
	printf("所有寄存器读取测试:\n");
	print_regs(fd, 0x1E, 0x39);
#endif
	adxl_write(fd, DATA_FORMAT, 0b01);
	adxl_write(fd, POWER_CTL, 0x28);
	adxl_write(fd, BW_RATE, 0x0B);
	adxl_write(fd, INT_ENABLE, 0x00);
#if 1
	__u16 x;
	for (;;) {
		x = adxl_read_byte(fd, DATAX0);
		printf("x=%X\n", x);
		usleep(100*1000);
	}
#endif

#if 0
	int x, y, z;
	int x0, x1, y0, y1, z0, z1;
	for (;;) {
		x0 = adxl_read(fd, DATAX0);
		x1 = adxl_read(fd, DATAX1);
		y0 = adxl_read(fd, DATAY0);
		y1 = adxl_read(fd, DATAY1);
		z0 = adxl_read(fd, DATAZ0);
		z1 = adxl_read(fd, DATAZ1);
		x = x0;
		y = y0;
		z = z0;
		guixyz('x', x);
		guixyz('y', y);
		guixyz('z', z);
		printf("\n");
		//printf("x0:%03d x1:%03d \n",x0,x1);
		//printf("y0:%03d y1:%03d \n",y0,y1);
		//printf("z0:%03d z1:%03d \n",z0,z1);
		//printf("x=%03d y=%03d z=%03d \n",x,y,z);
		usleep(100*1000);
		printf("\f");
	}
#endif
	CLOSE:
	close(fd);
	return 0;
}