编译选择2410 的ADAPTER , 自己写了个I2C驱动的小程序, 代码风格比较难看,只是用来自己看看。
#include
#include
#include
#include
#include
#include /* get the user-level API */
#include
#include
static unsigned short normal_i2c[] = { 0x50, I2C_CLIENT_END };
I2C_CLIENT_INSMOD_1(at24);
static struct i2c_client new_client;
/*
static ssize_t si2c_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
}
static ssize_t i2c_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
}
static DEVICE_ATTR(AT24,S_IRUGO,i2c_show,i2c_store);
*/
static int write_i2c(void)
{
int ret;
char buf[5]={0,1,2,3,4};
ret = i2c_master_send(&new_client,buf,4);
return ret;
}
static int read_i2c(void)
{
int ret;
char buf1[4]={0};
char temp[1]={0};
/*或者直接使用 i2c_transfer(adap, &msg, 2) 传递2个消息;*/
i2c_master_send(&new_client,temp,1); //读需要两次操作,第一次为写inner reg.
ret=i2c_master_recv(&new_client,buf1,4);
int i;
for (i=0;i<4;i++)
printk("buf1[%d]=%d,",i,buf1[i]);
return ret;
}
static int at24_detect(struct i2c_adapter *adapter, int address, int kind)
{
printk("detect 24 i2c,address=%d\n",address);
new_client.adapter=adapter;
new_client.addr=address;
write_i2c();
msleep(1000);
read_i2c();
return 0;
}
static int at24_adatper(struct i2c_adapter *adapter)
{
return i2c_probe(adapter, &addr_data, at24_detect);
}
static struct i2c_driver at24_driver={
.driver={
.name="at24",
},
.attach_adapter=at24_adatper,
};
static int at24_init(void)
{
return i2c_add_driver(&at24_driver);
}
static void at24_exit(void)
{
i2c_del_driver(&at24_driver);
}
module_init(at24_init);
module_exit(at24_exit);
阅读(1841) | 评论(0) | 转发(0) |