栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux下的I2C-EEPROM应用

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux下的I2C-EEPROM应用

参考博主

http://t.csdn.cn/sIP6U

http://t.csdn.cn/cUWzo

感谢博主

一、I2C简介

I2C是很常用的总线协议。它使用两条线在主控制器与从机直接进行数据通信。一条是时钟线,一条是数据线,两条数据线都要接4.7K的上拉电阻,空闲的时候处于高电平。I2C总线的标准模式下传输速度可以达到100k/s,快速模式达到400k/s。

​ 常见的I2C驱动从设备有电容触摸屏,CMOS camera ,重力传感器,EEPROM,HDMI。

二、I2C的协议内容

起始位S:数据位电平拉低

​停止位P:数据位拉高

ACK1:MASTER发送完地址后,SLIVER将数据位拉低,响应主机。

​ACK2:读写完以后,主机拉低数据位,告诉SLIVER进行回应。

写数据:

主机发送起始位S信号,发送i2c从机的地址,发送写位,ACK1,写入从机的寄存器的8位数据,然后循环写数据,最后从机ACK2应答,并且主机发送停止位信号。

读数据:

写入设备地址,写入寄存器地址,读取设备地址,读取寄存器的值。

​ 阶段一:主机发送S,写入I2C设备地址,ACK1从机响应。

​ 阶段二:重发S,发送寄存器的地址,从机ACK1响应。

​ 阶段三:重发S,发送I2C设备地址,读位,ACK1响应。

​ 阶段四:读取数据,ACK2主机拉低信号读完8bit,主机发送P。

三、查看i2c源代码 1.24cXX.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "24cXX.h"


static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
    int size, union i2c_smbus_data* data)
{
    struct i2c_smbus_ioctl_data args;

    args.read_write = read_write;
    args.command = command;
    args.size = size;
    args.data = data;
    return ioctl(file, I2C_SMBUS, &args);
}


static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
{
    return i2c_smbus_access(file, value, 0, I2C_SMBUS_QUICK, NULL);
}

static inline __s32 i2c_smbus_read_byte(int file)
{
    union i2c_smbus_data data;
    if (i2c_smbus_access(file, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
        return -1;
    else
        return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
{
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, value,
        I2C_SMBUS_BYTE, NULL);
}

static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
{
    union i2c_smbus_data data;
    if (i2c_smbus_access(file, I2C_SMBUS_READ, command,
        I2C_SMBUS_BYTE_DATA, &data))
        return -1;
    else
        return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
    __u8 value)
{
    union i2c_smbus_data data;
    data.byte = value;
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_BYTE_DATA, &data);
}

static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
{
    union i2c_smbus_data data;
    if (i2c_smbus_access(file, I2C_SMBUS_READ, command,
        I2C_SMBUS_WORD_DATA, &data))
        return -1;
    else
        return 0x0FFFF & data.word;
}

static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
    __u16 value)
{
    union i2c_smbus_data data;
    data.word = value;
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_WORD_DATA, &data);
}

static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
{
    union i2c_smbus_data data;
    data.word = value;
    if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_PROC_CALL, &data))
        return -1;
    else
        return 0x0FFFF & data.word;
}



static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
    __u8* values)
{
    union i2c_smbus_data data;
    int i;
    if (i2c_smbus_access(file, I2C_SMBUS_READ, command,
        I2C_SMBUS_BLOCK_DATA, &data))
        return -1;
    else {
        for (i = 1; i <= data.block[0]; i++)
            values[i - 1] = data.block[i];
        return data.block[0];
    }
}

static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
    __u8 length, __u8* values)
{
    union i2c_smbus_data data;
    int i;
    if (length > 32)
        length = 32;
    for (i = 1; i <= length; i++)
        data.block[i] = values[i - 1];
    data.block[0] = length;
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_BLOCK_DATA, &data);
}


static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
    __u8* values)
{
    union i2c_smbus_data data;
    int i;
    if (i2c_smbus_access(file, I2C_SMBUS_READ, command,
        I2C_SMBUS_I2C_BLOCK_DATA, &data))
        return -1;
    else {
        for (i = 1; i <= data.block[0]; i++)
            values[i - 1] = data.block[i];
        return data.block[0];
    }
}

static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
    __u8 length, __u8* values)
{
    union i2c_smbus_data data;
    int i;
    if (length > 32)
        length = 32;
    for (i = 1; i <= length; i++)
        data.block[i] = values[i - 1];
    data.block[0] = length;
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_I2C_BLOCK_DATA, &data);
}


static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
    __u8 length, __u8* values)
{
    union i2c_smbus_data data;
    int i;
    if (length > 32)
        length = 32;
    for (i = 1; i <= length; i++)
        data.block[i] = values[i - 1];
    data.block[0] = length;
    if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_BLOCK_PROC_CALL, &data))
        return -1;
    else {
        for (i = 1; i <= data.block[0]; i++)
            values[i - 1] = data.block[i];
        return data.block[0];
    }
}

static int i2c_write_1b(struct eeprom* p_device_struct, __u8 buf)
{
    int byte;
    // we must simulate a plain I2C byte write with SMBus functions
    byte = i2c_smbus_write_byte(p_device_struct->fd, buf);
    if (byte < 0)
        fprintf(stderr, "Error i2c_write_1b: %sn", strerror(errno));
    usleep(10);
    return byte;
}

static int i2c_write_2b(struct eeprom* p_device_struct, __u8 buf[2])
{
    int byte;
    // we must simulate a plain I2C byte write with SMBus functions
    byte = i2c_smbus_write_byte_data(p_device_struct->fd, buf[0], buf[1]);
    if (byte < 0)
        fprintf(stderr, "Error i2c_write_2b: %sn", strerror(errno));
    usleep(10);
    return byte;
}

static int i2c_write_3b(struct eeprom* p_device_struct, __u8 buf[3])
{
    int byte;
    // we must simulate a plain I2C byte write with SMBus functions
    // the __u16 data field will be byte swapped by the SMBus protocol
    byte = i2c_smbus_write_word_data(p_device_struct->fd, buf[0], buf[2] << 8 | buf[1]);
    if (byte < 0)
        fprintf(stderr, "Error i2c_write_3b: %sn", strerror(errno));
    usleep(10);
    return byte;
}


#define CHECK_I2C_FUNC( var, label ) 
    do {    if(0 == (var & label)) { 
        fprintf(stderr, "nError: " 
            #label " function is required. Program halted.nn"); 
        exit(1); } 
    } while(0);

int eeprom_open(char* dev_fqn, int addr, int type, struct eeprom* p_device_struct)
{
    int funcs, fd, byte;
    p_device_struct->fd = p_device_struct->addr = 0;
    p_device_struct->dev = 0;

    fd = open(dev_fqn, O_RDWR);
    if (fd <= 0)
    {
        fprintf(stderr, "Error eeprom_open: %sn", strerror(errno));
        return -1;
    }

    // get funcs list
    if ((byte = ioctl(fd, I2C_FUNCS, &funcs) < 0))
    {
        fprintf(stderr, "Error eeprom_open: %sn", strerror(errno));
        return -1;
    }


    // check for req funcs
    CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE);
    CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE);
    CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA);
    CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA);
    CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_WORD_DATA);
    CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA);

    // set working device
    if ((byte = ioctl(fd, I2C_SLAVE, addr)) < 0)
    {
        fprintf(stderr, "Error eeprom_open: %sn", strerror(errno));
        return -1;
    }
    p_device_struct->fd = fd;
    p_device_struct->addr = addr;
    p_device_struct->dev = dev_fqn;
    p_device_struct->type = type;
    return 0;
}

int eeprom_close(struct eeprom* p_device_struct)
{
    close(p_device_struct->fd);
    p_device_struct->fd = -1;
    p_device_struct->dev = 0;
    p_device_struct->type = EEPROM_TYPE_UNKNOWN;
    return 0;
}

#if 0
int eeprom_24c32_write_byte(struct eeprom* p_device_struct, __u16 mem_addr, __u8 data)
{
    __u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
    return i2c_write_3b(p_device_struct, buf);
}


int eeprom_24c32_read_current_byte(struct eeprom* p_device_struct)
{
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    return i2c_smbus_read_byte(p_device_struct->fd);
}

int eeprom_24c32_read_byte(struct eeprom* p_device_struct, __u16 mem_addr)
{
    int byte;
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
    byte = i2c_write_2b(p_device_struct, buf);
    if (byte < 0)
        return byte;
    byte = i2c_smbus_read_byte(p_device_struct->fd);
    return byte;
}
#endif


int eeprom_read_current_byte(struct eeprom* p_device_struct)
{
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    return i2c_smbus_read_byte(p_device_struct->fd);
}

int eeprom_read_byte(struct eeprom* p_device_struct, __u16 mem_addr)
{
    int byte;
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    if (p_device_struct->type == EEPROM_TYPE_8BIT_ADDR)
    {
        __u8 buf = mem_addr & 0x0ff;
        byte = i2c_write_1b(p_device_struct, buf);
    }
    else if (p_device_struct->type == EEPROM_TYPE_16BIT_ADDR) {
        __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
        byte = i2c_write_2b(p_device_struct, buf);
    }
    else {
        fprintf(stderr, "ERR: unknown eeprom typen");
        return -1;
    }
    if (byte < 0)
        return byte;
    byte = i2c_smbus_read_byte(p_device_struct->fd);
    return byte;
}

int eeprom_write_byte(struct eeprom* p_device_struct, __u16 mem_addr, __u8 data)
{
    if (p_device_struct->type == EEPROM_TYPE_8BIT_ADDR) {
        __u8 buf[2] = { mem_addr & 0x00ff, data };
        return i2c_write_2b(p_device_struct, buf);
    }
    else if (p_device_struct->type == EEPROM_TYPE_16BIT_ADDR) {
        __u8 buf[3] =
        { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
        return i2c_write_3b(p_device_struct, buf);
    }
    fprintf(stderr, "ERR: unknown eeprom typen");
    return -1;
}
2.24cXX.h


#ifndef _24CXX_H_
#define _24CXX_H_
#include 
#include 

#define EEPROM_TYPE_UNKNOWN 0
#define EEPROM_TYPE_8BIT_ADDR   1
#define EEPROM_TYPE_16BIT_ADDR  2

struct eeprom
{
    char *dev;  // device file i.e. /dev/i2c-N
    int addr;   // i2c address
    int fd;     // file descriptor
    int type;   // eeprom type
};


int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom*);

int eeprom_close(struct eeprom *p_device_struct);

int eeprom_read_byte(struct eeprom* p_device_struct, __u16 mem_addr);

int eeprom_read_current_byte(struct eeprom *p_device_struct);

int eeprom_write_byte(struct eeprom *p_device_struct, __u16 mem_addr, __u8 data);

#endif
3.eeprog.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "24cXX.h"

#define DEVICE_FILE_STRING "/dev/i2c-0"
#define DEVICE_ADDRESS 0x50

#define usage_if(flag) do { do_usage_if( flag , __LINE__); } while(0);
void do_usage_if(int error_code, int line) {
    const static char* eeprog_usage =
        "I2C-24C08(256 bytes) Read/Write Program, ONLY FOR TEST!n"
        "FriendlyARM Computer Tech. 2009n";
    if (!error_code)
        return;
    fprintf(stderr, "%sn[line %d]n", eeprog_usage, line);
    exit(1);
}


#define die_if(flag, msg) do { do_die_if( flag , msg, __LINE__); } while(0);
void do_die_if(int error_code, char* msg, int line) {
    if (!error_code)
        return;
    fprintf(stderr, "Error at line %d: %sn", line, msg);
    fprintf(stderr, "   sysmsg: %sn", strerror(errno));
    exit(1);
}


static int read_from_eeprom(struct eeprom* e, int addr, int size) {
    int ch, i;
    for (i = 0; i < size; ++i, ++addr) {
        die_if((ch = eeprom_read_byte(e, addr)) < 0, "read error");
        if ((i % 16) == 0)
            printf("n %.4x|  ", addr);
        else if ((i % 8) == 0)
            printf("  ");
        printf("%.2x ", ch);
        fflush(stdout);
    }
    fprintf(stderr, "nn");
    return 0;
}

static int write_to_eeprom(struct eeprom* e, int addr) {
    int i;
    for (i = 0, addr = 0; i < 256; i++, addr++) {
        if ((i % 16) == 0)
            printf("n %.4x|  ", addr);
        else if ((i % 8) == 0)
            printf("  ");
        printf("%.2x ", i);
        fflush(stdout);
        die_if(eeprom_write_byte(e, addr, i), "write error");
    }
    fprintf(stderr, "nn");
    return 0;
}

int main(int argc, char** argv) {
    struct eeprom e;
    int op;

    op = 0;

    usage_if(argc != 2 || argv[1][0] != '-' || argv[1][2] != '');
    op = argv[1][1];

    //TODO: 将数字改为自己的学号。
    write(STDOUT_FILENO, "APP for 201930310066 ...n", strlen("APP for 201930310066 ...n"));
    fprintf(stderr, "Open %s with 8bit moden", DEVICE_FILE_STRING);
    die_if(eeprom_open(DEVICE_FILE_STRING, DEVICE_ADDRESS, EEPROM_TYPE_8BIT_ADDR, &e) < 0,
        "unable to open eeprom device file "
        "(check that the file exists and that it's readable)");
    switch (op) {
    case 'r':
        fprintf(stderr, "  Reading 256 bytes from 0x0n");
        read_from_eeprom(&e, 0, 256);
        break;
    case 'w':
        fprintf(stderr, "  Writing 0x00-0xff into 24C08 n");
        write_to_eeprom(&e, 0);
        break;
    default:
        usage_if(1);
        exit(1);
    }
    eeprom_close(&e);

    return 0;
}
4. 编译并在ubuntu下运行 使用make通过Makefile文件直接编译
make
arm-linux-gnueabihf-gcc -c -o eeprog.o eeprog.c
arm-linux-gnueabihf-gcc -c -o 24cXX.o 24cXX.c
arm-linux-gnueabihf-gcc -Wall -O2 -o i2c eeprog.o 24cXX.o

 

 

四、思考题  1.I2C总线的优点是什么?
  • 结构图的功能模块与实际的ic对应;设计快速从结构图向最后的原理图推进;
  • 不需要设计总线接口,因为i2c总线接口已经集成在片上;
  • 集成的寻址和数据传输协议允许系统完全由软件定义;
  • 相同类型的ic经常用于很多不同的应用;
  • 在系统中增加或删除ic不会影响总线的其他电路;
  • 故障诊断和调试都很简单,故障可被立即寻迹;
  • 通过聚集一个可再使用的软件模块的库减少软件开发的时间;
  • 极低的电流消耗;
  • 抗高噪声干扰;
  • 电源电压范围宽;
  • 工作的温度范围广;
  • 简单的两线串行i2c总线将互联减到最小,因此ic的管脚更少而且PCB的线路也减少。结果使PCB更小和更便宜。
  • 完全完整的i2c总线协议不需要地址译码器和其他“胶合逻辑”。
  • i2c总线的多主机功能允许通过外部连接到生产线快速测试和调整最终用户的设备。
  • 符合i2c总线的ic提供SO(小型)、VSO(超小型)以及DIL封装,甚至减少了IC的空间要求;

2.I2C总线的启动信号和结束信号有什么特点? 

启动条件:在SCL线路从高电平切换到低电平之前,SDA线路从高电平切换到低电平。

停止条件:SCL线路从低电平切换到高电平后,SDA线路从低电平切换到高电平。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835675.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号