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
#include <linux/i2c.h> /*added for AT24C02 driver */
#include <linux/i2c/at24.h> /* Added for AT24C02 driver */
/*added for i2c driver */
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的设备地址是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的读写,我们来测试一下:
接下来测试一下。
/*********************************************************************************
* Copyright: (C) 2017 ZhangYan<zhangyan@emaill.com>
* All rights reserved.
*
* Filename: eepromtest.c
* Description: This file
*
* Version: 1.0.0(2017年09月13日)
* Author: ZhangYan <zhangyan@emaill.com>
* ChangeLog: 1, Release initial version on "2017年09月13日 14时55分10秒"
*
********************************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
int main(void)
{
int i;
int fd ;
int rv;
int read_fd;
char write_buf[]="hello zy"; // wait to be written
char read_buf[32];
// snprintf(write_buf,sizeof(write_buf),"hello zy");
fd=open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom",O_RDWR,0);
if(fd<0)
{ printf("fail to open eeprom\n");
return -1;
}
lseek(fd,0,SEEK_SET);
rv=write(fd,write_buf,sizeof(write_buf));
if(rv<0)
printf("fail to write\n");
printf("write ok %d\n",rv);
lseek(fd,0,SEEK_SET);
read_fd=read(fd,read_buf,sizeof(write_buf));
if(read_fd<0)
printf("fail to read \n");
printf("read ok %d\n",read_fd) ;
for(i=0;i<sizeof(write_buf);i++)
{
printf("%03d\n",read_buf[i]); //此处最好用 %c,直接打印字符
}
close(fd);
return 0;
}
本文参考crazyboy资料,仅作学习用。