开发板:TQ2440
内核:Linux 2.6.32
PC OS:Ubuntu 11.04
1.配置内核
打开I2C功能:
打开杂项设备,该选项打开后,EEPROM也就打开了。
2. 修改代码
修改文件: linux/arch/arm/mach-s3c2440/mach-smdk2440.c
增加如下代码片段:
#include
static struct at24_platform_data at24c02 = {
.byte_len = SZ_2K / 8,
.page_size = 8,
.flags = 0,
};
static struct i2c_board_info __initdata smdk_i2c_devices[] = {
/* more devices can be added using expansion connectors */
{
I2C_BOARD_INFO("24c02", 0x50),
.platform_data = &at24c02,
},
};
在smdk2440_machine_init函数中增加如下:
i2c_register_board_info(0, smdk_i2c_devices, ARRAY_SIZE(smdk_i2c_devices));
注意:上面许多参数是根据at24c02的参数来设置的,at24c02使用8位地址,内存大小2K比特位,也就是256K字节,页大小为8字节。
最后,需要注意,手册中at24c02的设备地址是0b 1 0 1 0 0 0 0 R/W, 其最低位是读写标志位,
但是在Linux中,I2C设备地址的最高位为0,而低七位地址就是手册中去掉R/W的剩余7位。因此,地址为0b 01010000(0x50)
3. 测试代码
系统启动后,如果一切正常。会在/sys文件系统下展示出该设备,如下:
[root@yj4230-0050]#pwd
/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050
[root@yj4230-0050]#ls
bus eeprom name subsystem
driver modalias power uevent
[root@yj4230-0050]#cat name
24c02
其中eeprom即为驱动导出的bin属性,通过读写eeprom即可访问设备,如下:
[root@yj4230-0050]#cat eeprom
i2ci2c-0: master_xfer[0] W, addr=0x50, len=1
i2ci2c-0: master_xfer[1] R, addr=0x50, len=128
i2ci2c-0: master_xfer[0] W, addr=0x50, len=1
i2ci2c-0: master_xfer[1] R, addr=0x50, len=128
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€亗儎厗噲墛媽崕彁憭摂晼棙櫄洔潪煚、¥ウЖ┆辈炒刀犯购患骄坷谅媚牌侨墒颂臀闲岩釉罩棕仝圮蒉哙徕沅彐玷殛腱眍镳耱篝貊鼬
接着,编写代码进行测试,如下:
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int ret, fd, i, j;
char read_data[256];
char write_data[256];
char offset;
fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom", O_RDWR);
if(fd < 0){
printf("Open at24c02 fail\n");
return -1;
}
ret = read(fd, &offset, 1);
if(ret < 0){
printf("Read error\n");
return -1;
}else if(ret < 1){
perror("Incomplete read\n");
printf("%d\n", ret);
return -1;
}
for(i = 0; i < 256; i++)
write_data[i] = offset+ 1 + i;
lseek(fd, 0 , SEEK_SET); //It's a must, or something wierd will happen
ret = write(fd, write_data, 256);
if(ret < 0){
printf("Write error\n");
return -1;
}
lseek(fd, 0 , SEEK_SET); //It's a must, or something wierd will happen
ret = read(fd, read_data, 256);
if(ret < 0){
printf("Read error\n");
return -1;
}else if(ret < 256){
perror("Incomplete read\n");
printf("%d\n", ret);
return -1;
}
for(i = 0; i < 256; i++){
if(i %16 == 0)
printf("\n");
printf(" %03d ", read_data[i]);
}
printf("\n");
}
代码结果如下:
上一篇:S3C2440 Linux驱动移植——SD卡驱动
下一篇:S3C2440挂载NFS根文件系统
推荐阅读最新更新时间:2024-03-16 15:38