【嵌入式】使用Linux自带的i2c-dev操作EEPROM(Userspace Driver)

.5BG?    
?@@@@:   推荐一款 求职面试、刷题学习 的神器:👉*点击跳转* ,快来看看吧!
J@@@@^                                  
~@@@@P                                  
 Y@@@@G:                   ~5PY^        
  5@@@@&Y^                .#@@@&J.      
   7&@@@@@G7^.             ~#@@@@#?.    
    :Y#@@@@@@#PY?!~^:...    .5@@@@@#Y^  
      .7P&@@@@@@@@@@&&##BBBBB#@@@@@@@@B^
         .~JPB&@@@@@@@@@@@@@@@@@@@@@@@&!    👉*点击跳转* 
              :^!7JY5PPPB&@@@@@@@@@&G?: 
                      ~5#@@@@@@@&P?^    
                     .#@@@@@&BY!:       
                      !G#BY!:           

Linux系统自带了I2C总线的子系统,没有了解这个I2C的子系统是如何工作的不过既然系统提供了这个功能就可以使用文件操作的方式直接使用I2C总线了。说到I2C总线第一个想到的还是EEPROM存储芯片,例如AT24C02这个芯片有256字节的存储空间,使用范围较广。

AT24C02芯片采用I2C总线与主机通信,具体细节参见数据手册。Linux的I2C设备为/dev/i2c-0和/dev/i2c-1,不同的板子可能不一样。使用I2C设备只需要简单的打开文件即可,十分的方便,使用ioctl函数控制I2C总线的一些参数,详见/usr/include/linux/i2c-dev.h头文件中的定义。

测试程序如下:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <errno.h>
#include "gpio.h"  

#define I2C_ADDR 0x50

char buf[256];
char val,value;
float flight;

int IIC_Write(int fd,unsigned char addr,unsigned char value)
{
	unsigned char buf[2];
	buf[0] = addr;
	buf[1] = value;
	return write(fd,buf,2);
}

int IIC_Read(int fd,unsigned char addr)
{
	int temp = 0;
	if(write(fd,&addr,1) < 0)
		return -1;
	if(read(fd,&temp,1) < 0)
		return -1;
	return temp;
}

int IIC_Read2(int fd,unsigned char addr,unsigned char *buff,int len)
{
	int temp = 0;
	write(fd,&addr,1);
	temp = read(fd,buff,len);
	return temp;
}

int main(void)
{
	int fd;
	GPIO_Init();
	GPIO_ConfigPin(PA,15,OUT);
	int i = 2;
	while(i--)
	{
		GPIO_SetPin(PA,15,1);
		usleep(50000);
		GPIO_SetPin(PA,15,0);
		usleep(50000);
	}

	fd=open("/dev/i2c-0",O_RDWR);
	if(fd<0)
	{
		printf("err open file:%s\r\n",strerror(errno));
		return 1;
	}
	if(ioctl( fd,I2C_SLAVE_FORCE,I2C_ADDR) < 0)
	{
		printf("ioctl error : %s\r\n",strerror(errno));
		return 1;
	}

	for(i=0;i<256;i++)
	{
		if(IIC_Write(fd,i, i) < 0)
			printf("%d ",i);
		usleep(1000);
	}
	
	while(1)
	{
		if(IIC_Read2(fd,0x00,buf,256) > 0)
		{
			for(i=0;i<256;i++)
			{
				if(i % 16 == 0)
					printf("\r\n");
				printf("%.2X ",buf[i]);
			}
			return ;
		}
		else
			printf("read error\r\n");
		printf("\r\n");
		// usleep(1000000);//sleep 0.1s
	}
}


需要注意的一点是EEPROM的写入是有时间限制的,Atmel官方数据手册上详细介绍了写时序规范:

A write operation requires an 8-bit data word address following the
device address word and acknowledgment. Upon receipt of this address, the EEPROM
will again respond with a zero and then clock in the first 8-bit data word. Following
receipt of the 8-bit data word, the EEPROM will output a zero and the addressing
device, such as a microcontroller, must terminate the write sequence with a stop condi-
tion. At this time the EEPROM enters an internally timed write cycle, t WR , to the
nonvolatile memory. All inputs are disabled during this write cycle and the EEPROM will
not respond until the write is complete
(see Figure 8 on page 11).

可知当主机将数据送到EEPROM中之后芯片内部需要一些时间来执行写操作,所以必须加上适当延时,不然会出现写入断断续续的情况甚至是完全写入不了,这个延时时间是t_wr,数据手册很坑,只给了这个值的max值为5ms但是没有给最小值和典型值,估计是随外界因素改变而变化的,测试得到写入间隔大于1ms时可以正常写入,如果是100us的话则出现每隔两个才能成功写入一个数据的情况,延时越小越糟糕。

测试程序的运行结果如下:(忽略GPIO部分的输出)

root@orangepione:/mnt/nfs_server/orangePi/i2c-test/eeprom# ./target
PageSize:4096,PageMask:FFFFF000
addr_start:01C20000,addr_offset:00000800
gpio_map:B6F0F000
PIO:B6F0F800


00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F
50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F
60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F
70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F
80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F
90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF
B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF
E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值