****************************************************************************************************************************************************
pc操作系统:CentOS 6.7
开发板cpu : s3c2440(arm9)
内核版本 : linux-3.0
开发板 : fl2440
开发模块 :EEPROM(AT24C02)
************************************************************************************************************************
1.EEPROM介绍
EEPROM (Electrically Erasable Programmable Read-Only Memory),电可擦可编程只读存储器--一种掉电后数据不丢失的存储芯片。 EEPROM 可以在电脑上或专用设备上擦除已有信息,重新编程。一般用在即插即用。
2.内核配置
[zoulei@CentOS linux-3.0]$ make menuconfig
Device Drivers-->
<*>I2C support -->
[*] Enable compatibility bits for old user-space
<*> I2C device interface
< > I2C bus multiplexing support
[*] Autoselect pertinent helper modules
I2C Hardware Bus support --->
[*] I2C Core debugging messages
[*] I2C Algorithm debugging messages
[ ] I2C Bus debugging messages
<*>GPIO support -->
<*>Hardware Monitoring support --->
<*>Misc devices-->
<*>EEPROM support-->
<*>I2C EEPROMS from most vendors
3.修改内核代码
[zoulei@CentOS linux-3.0]$ vim arch/arm/mach-s3c2440/mach-smdk2440.c
56 #include <linux/i2c.h> /*Added by zoulei 2017.04.27, for AT24C02 drive r */
57 #include <linux/i2c/at24.h> /* Added by zoulei 2017.4.27, for AT24C02 drive r */
148 /*added by zoulei for i2c driver 2017.04.27*/
149 static struct at24_platform_data at24c02 = {
150 .byte_len = SZ_2K / 8, //容量大小
151 .page_size = 8, //每页的字节数
152 .flags = 0,
153 };
154
155 static struct i2c_board_info __initdata smdk_i2c_devices[] = {
156 /* more devices can be added using expansion connectors */
157 {
158 I2C_BOARD_INFO("24c02", 0x50),
159 .platform_data = &at24c02,
160 },
161 };
在smdk2440_machine_init函数中增加如下:
307 i2c_register_board_info(0, smdk_i2c_devices, ARRAY_SIZE(smdk_i2c_dev ices)); /*added by zoulei 2017.04.27*/
********************************************************************************************************************************************************************
说明:上面许多参数是根据at24c02的参数来设置的,手册中at24c02的设备地址是0b 1 0 1 0 0 0 0 R/W, 其最低位是读写标志位
在Linux中,I2C设备地址的最高位为0,而低七位地址就是手册中去掉R/W的剩余7位。因此,地址为0b 01010000(0x50)
*********************************************************************************************************************************************************************
4.测试
内核make 编译之后,烧录到开发板然后在开发板上进行测试
系统为IIC创建的sysfs文件系统在/sys/bus/i2c/devices/0-0050/目录下,该目录下有一个eeprom文件,只要对该文件进行操作,就可以实现对eeprom的读写,我们来测试一下:
在命令行下输入上述命令,就会把eeprom中的地址的内容全部呈现出来,cat也具有同样的功能。我们也可以通过echo修改内容:
接下来进行代码测试:
/*********************************************************************************
* Copyright: (C) 2017 zoulei
* All rights reserved.
*
* Filename: eeprom_test.c
* Description: This file
*
* Version: 1.0.0(2017年04月27日)
* Author: zoulei <zoulei121@gmail.com>
* ChangeLog: 1, Release initial version on "2017年04月27日 05时42分04秒"
*
********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/io.h>
int main(int argc,char**argv)
{
int ret, fd,len,i;
char read_data[256];
char* write_data="Hi!I am zoulei";
len =strlen(write_data);
fd=open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom",O_RDWR);
if(fd<0)
{
printf("open device fail");
return -1;
}
lseek(fd,0,SEEK_SET);
if(ret=write(fd,write_data,len)<0)
{
printf("write error\n");
return -1;
}
printf("write ok!\n");
lseek(fd, 0 , SEEK_SET);
if(ret=read(fd,read_data,len)<0)
{
printf("read error\n");
return -1;
}
printf("read ok\n");
for(i=0;i<len;i++)
{
if(i%16==0)
{
printf("\n");
}
printf("%03d\n" ,read_data[i]);
}
close(fd);
return 0;
}
测试结果: