android IIC测试程序

/*************************************
NAME:i2c_rw.c
COPYRIGHT:www.embedsky.net
*************************************/
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <string.h>
#include "i2c.h"
#include "i2c-dev.h"
#define CHIP_ADDR 0x50 //设备地址
#define I2C_DEV "/dev/i2c/0"
struct eeprom
{
char *dev; // 设备名称,i2c-0
int addr; // 设备中地址
int fd; // 设备句柄
};
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, int size, union i2c_smbus_data
*data)
{
struct i2c_smbus_ioctl_data args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(file, I2C_SMBUS, &args);
}
int eeprom_read_byte(struct eeprom* e, __u16 mem_addr)
{
int r;
union i2c_smbus_data data;
ioctl(e->fd, BLKFLSBUF);
__u8 buf = mem_addr & 0x0ff;
r = i2c_smbus_access(e->fd, I2C_SMBUS_WRITE, buf, I2C_SMBUS_BYTE, NULL);;
if (r < 0)
return r;
if (i2c_smbus_access(e->fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
return -1;
else
return 0x0FF & data.byte;
}
int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
{
int r;
__u8 command = mem_addr & 0x00ff;
union i2c_smbus_data i2cdata;
i2cdata.byte = data;
r = i2c_smbus_access(e->fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &i2cdata);
if(r < 0)
printf("iic write error");
usleep(10);
return r;
}
int eeprom_open(char *dev_name, int addr, struct eeprom *e)
{
int funcs, fd, r;
e->fd = e->addr = 0;
e->dev = 0;
fd = open(dev_name, O_RDWR);
if(fd <= 0)
{
printf("iic open error\n");
return -1;
}
// set working device
ioctl(fd, I2C_TENBIT, 0);
ioctl(fd, I2C_SLAVE, addr);
e->fd = fd;
e->addr = addr;
e->dev = dev_name;
return 0;
}
int eeprom_close(struct eeprom *e)
{
close(e->fd);
e->fd = -1;
e->dev = 0;
return 0;
}
static int read_from_eeprom(struct eeprom *e, int addr, int size)
{
int temp, i, ret;

temp = eeprom_read_byte(e, addr);
if(temp < 0)
{
printf("iic read error !");
}

printf("\n %4x\n ", addr);

printf("%4x\n ", temp);
fflush(stdout);

printf("\n\n");
return 0;
}
static int write_to_eeprom(struct eeprom *e, int addr)
{
int ret;
printf("\n %4x ", addr);
fflush(stdout);
ret = eeprom_write_byte(e, addr, 0x25);
if(ret < 0)
{
printf("iic write error !");
}

fprintf(stderr, "\n\n");
return 0;
}

int main (void)
{
struct eeprom e;
int ret;
ret = eeprom_open(I2C_DEV, CHIP_ADDR, &e);
if(ret < 0)
{
printf("i2c device (AT24C02) open failed !\n");
return (-1);
}
printf(" Writing ox25 into AT24C02 \n");
write_to_eeprom(&e, 0x33);
printf(" Reading 1 bytes from 0x33\n");
read_from_eeprom(&e, 0x33,1 );
eeprom_close(&e);
return(0);
}

 

ANdroid.mk

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
 
LOCAL_STATIC_LIBRARIES := libcutils libc
LOCAL_MODULE := ii
LOCAL_MODULE_TAGS := eng
 
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_SRC_FILES:= \
        iic_rw.c\
  #i2c-core.c\
  #i2c-dev.c\
  i2c.h\
  i2c-dev.h
 
LOCAL_C_INCLUDES := bionic/libc/bionic
 
ifeq ($(HAVE_SELINUX),true)
LOCAL_CFLAGS += -DHAVE_SELINUX
LOCAL_SHARED_LIBRARIES += libselinux
LOCAL_C_INCLUDES += external/libselinux/include
endif
 
include $(BUILD_EXECUTABLE)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值