I2C驱动app

1.介绍

I2C(Inter-Integrated Circuit)总线是由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备。是微电子通信控制领域广泛采用的一种总线标准。它是同步通信的一种特殊形式,具有接口线少,控制方式简单,器件封装形式小,通信速率较高等优点。I2C 总线支持任何IC 生产过程(CMOS、双极性)。通过串行数据(SDA)线和串行时钟 (SCL)线在连接到总线的器件间传递信息。每个器件都有一个唯一的地址识别(无论是微控制器——MCU、LCD 驱动器、存储器或键盘接口),而且都可以作为一个发送器或接收器(由器件的功能决定)。LCD 驱动器只能作为接收器,而存储器则既可以接收又可以发送数据。除了发送器和接收器外,器件在执行数据传输时也可以被看作是主机或从机(见表1)。主机是初始化总线的数据传输并产生允许传输的时钟信号的器件。此时,任何被寻址的器件都被认为是从机。

2.思考

什么是i2c总线?
I2C即Inter IC,由Philips公司开发,是当今电子设计中应用非常广泛的串行总线之一,主要用于电压、温度监控,EEPROM(电可擦可编程只读存储器)数据的读写,光模块的管理等。I2C总线只有两根线,SCL和SDA,SCL即Serial Clock,串行时钟,SDA即Serial Data,串行数据。

I2C总线的速率能达到多少?
标准模式下:100Kbps
快速模式下:400Kbps
高速模式下:3.4Mbps


3.操作

i2c源代码:

文件.c
 
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include "24cXX.h"
 
 
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
    int size, union i2c_smbus_data* data)
&#123;
    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);
&#125;
 
 
static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
&#123;
    return i2c_smbus_access(file, value, 0, I2C_SMBUS_QUICK, NULL);
&#125;
 
static inline __s32 i2c_smbus_read_byte(int file)
&#123;
    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;
&#125;
 
static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
&#123;
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, value,
        I2C_SMBUS_BYTE, NULL);
&#125;
 
static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
&#123;
    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;
&#125;
 
static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
    __u8 value)
&#123;
    union i2c_smbus_data data;
    data.byte = value;
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_BYTE_DATA, &data);
&#125;
 
static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
&#123;
    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;
&#125;
 
static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
    __u16 value)
&#123;
    union i2c_smbus_data data;
    data.word = value;
    return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
        I2C_SMBUS_WORD_DATA, &data);
&#125;
 
static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
&#123;
    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;
&#125;
 
 
/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
    __u8* values)
&#123;
    union i2c_smbus_data data;
    int i;
    if (i2c_smbus_access(file, I2C_SMBUS_READ, command,
        I2C_SMBUS_BLOCK_DATA, &data))
        return -1;
    else &#123;
        for (i = 1; i <= data.block[0]; i++)
            values[i - 1] = data.block[i];
        return data.block[0];
    &#125;
&#125;
 
static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
    __u8 length, __u8* values)
&#123;
    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);
&#125;
 
/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
    __u8* values)
&#123;
    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 &#123;
        for (i = 1; i <= data.block[0]; i++)
            values[i - 1] = data.block[i];
        return data.block[0];
    &#125;
&#125;
 
static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
    __u8 length, __u8* values)
&#123;
    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);
&#125;
 
/* Returns the number of read bytes */
static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
    __u8 length, __u8* values)
&#123;
    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 &#123;
        for (i = 1; i <= data.block[0]; i++)
            values[i - 1] = data.block[i];
        return data.block[0];
    &#125;
&#125;
 
static int i2c_write_1b(struct eeprom* p_device_struct, __u8 buf)
&#123;
    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: %s\n", strerror(errno));
    usleep(10);
    return byte;
&#125;
 
static int i2c_write_2b(struct eeprom* p_device_struct, __u8 buf[2])
&#123;
    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: %s\n", strerror(errno));
    usleep(10);
    return byte;
&#125;
 
static int i2c_write_3b(struct eeprom* p_device_struct, __u8 buf[3])
&#123;
    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: %s\n", strerror(errno));
    usleep(10);
    return byte;
&#125;
 
 
#define CHECK_I2C_FUNC( var, label ) \
    do &#123;    if(0 == (var & label)) &#123; \
        fprintf(stderr, "\nError: " \
            #label " function is required. Program halted.\n\n"); \
        exit(1); &#125; \
    &#125; while(0);
 
int eeprom_open(char* dev_fqn, int addr, int type, struct eeprom* p_device_struct)
&#123;
    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)
    &#123;
        fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
        return -1;
    &#125;
 
    // get funcs list
    if ((byte = ioctl(fd, I2C_FUNCS, &funcs) < 0))
    &#123;
        fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
        return -1;
    &#125;
 
 
    // 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)
    &#123;
        fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
        return -1;
    &#125;
    p_device_struct->fd = fd;
    p_device_struct->addr = addr;
    p_device_struct->dev = dev_fqn;
    p_device_struct->type = type;
    return 0;
&#125;
 
int eeprom_close(struct eeprom* p_device_struct)
&#123;
    close(p_device_struct->fd);
    p_device_struct->fd = -1;
    p_device_struct->dev = 0;
    p_device_struct->type = EEPROM_TYPE_UNKNOWN;
    return 0;
&#125;
 
#if 0
int eeprom_24c32_write_byte(struct eeprom* p_device_struct, __u16 mem_addr, __u8 data)
&#123;
    __u8 buf[3] = &#123; (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data &#125;;
    return i2c_write_3b(p_device_struct, buf);
&#125;
 
 
int eeprom_24c32_read_current_byte(struct eeprom* p_device_struct)
&#123;
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    return i2c_smbus_read_byte(p_device_struct->fd);
&#125;
 
int eeprom_24c32_read_byte(struct eeprom* p_device_struct, __u16 mem_addr)
&#123;
    int byte;
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    __u8 buf[2] = &#123; (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff &#125;;
    byte = i2c_write_2b(p_device_struct, buf);
    if (byte < 0)
        return byte;
    byte = i2c_smbus_read_byte(p_device_struct->fd);
    return byte;
&#125;
#endif
 
 
int eeprom_read_current_byte(struct eeprom* p_device_struct)
&#123;
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    return i2c_smbus_read_byte(p_device_struct->fd);
&#125;
 
int eeprom_read_byte(struct eeprom* p_device_struct, __u16 mem_addr)
&#123;
    int byte;
    ioctl(p_device_struct->fd, BLKFLSBUF); // clear kernel read buffer
    if (p_device_struct->type == EEPROM_TYPE_8BIT_ADDR)
    &#123;
        __u8 buf = mem_addr & 0x0ff;
        byte = i2c_write_1b(p_device_struct, buf);
    &#125;
    else if (p_device_struct->type == EEPROM_TYPE_16BIT_ADDR) &#123;
        __u8 buf[2] = &#123; (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff &#125;;
        byte = i2c_write_2b(p_device_struct, buf);
    &#125;
    else &#123;
        fprintf(stderr, "ERR: unknown eeprom type\n");
        return -1;
    &#125;
    if (byte < 0)
        return byte;
    byte = i2c_smbus_read_byte(p_device_struct->fd);
    return byte;
&#125;
 
int eeprom_write_byte(struct eeprom* p_device_struct, __u16 mem_addr, __u8 data)
&#123;
    if (p_device_struct->type == EEPROM_TYPE_8BIT_ADDR) &#123;
        __u8 buf[2] = &#123; mem_addr & 0x00ff, data &#125;;
        return i2c_write_2b(p_device_struct, buf);
    &#125;
    else if (p_device_struct->type == EEPROM_TYPE_16BIT_ADDR) &#123;
        __u8 buf[3] =
        &#123; (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data &#125;;
        return i2c_write_3b(p_device_struct, buf);
    &#125;
    fprintf(stderr, "ERR: unknown eeprom type\n");
    return -1;
&#125;
 

文件.h


#ifndef _24CXX_H_
#define _24CXX_H_
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
 
#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
};
 
/*
 * opens the eeprom device at [dev_fqn] (i.e. /dev/i2c-N) whose address is
 * [addr] and set the eeprom_24c32 [e]
 */
int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom*);
/*
 * closees the eeprom device [e] 
 */
int eeprom_close(struct eeprom *e);
/*
 * read and returns the eeprom byte at memory address [mem_addr] 
 * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) 
 */
int eeprom_read_byte(struct eeprom* e, __u16 mem_addr);
/*
 * read the current byte
 * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) 
 */
int eeprom_read_current_byte(struct eeprom *e);
/*
 * writes [data] at memory address [mem_addr] 
 * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) 
 * int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data);
#endif


 
#include <stdio.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "24cXX.h"
 
#define DEVICE_FILE_STRING "/dev/i2c/0"
#define DEVICE_ADDRESS 0x50
 
#define usage_if(a) do { do_usage_if( a , __LINE__); } while(0);
void do_usage_if(int b, int line) {
        const static char *eeprog_usage =
                "I2C-24C08(256 bytes) Read/Write Program, ONLY FOR TEST!\n"
                "FriendlyARM Computer Tech. 2009\n";
        if(!b)
                return;
        fprintf(stderr, "%s\n[line %d]\n", eeprog_usage, line);
        exit(1);
}
 


 
#define die_if(a, msg) do { do_die_if( a , msg, __LINE__); } while(0);
void do_die_if(int b, char* msg, int line) {
        if(!b)
                return;
        fprintf(stderr, "Error at line %d: %s\n", line, msg);
        fprintf(stderr, "       sysmsg: %s\n", 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, "\n\n");
}
 
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, "\n\n");
        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] != '\0');
        op = argv[1][1];
 
        //TODO: 将数字改为自己的学号。
        write(STDOUT_FILENO, "APP for 123456789012345 ...\n", strlen("APP for 123456789012345 ...\n"));
        fprintf(stderr, "Open %s with 8bit mode\n", 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 0x0\n");
                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.思考题 

1.I2C总线的优点是什么?
      I2C总线最主要的优点是其简单性和有效性。由于接口直接在组件之上,因此I2C总线占用的空间非常小,减少了电路板的空间和芯片管脚的数量,降低了互联成本。总线的长度可高达25英尺,并且能够以10Kbps的最大传输速率支持40个组件。I2C总线的另一个优点是,它支持多主控(multimastering),其中任何能够进行发送和接收的设备都可以成为主总线。一个主控能够控制信号的传输和时钟频率。当然,在任何时间点上只能有一个主控。

2.I2C总线的启动信号和结束信号有什么特点?
启动条件:在SCL线路从高电平切换到低电平之前,SDA线路从高电平切换到低电平。
开始的建立时间:SCL上升至高电平与SDA下降至脱离高电平的时间间隔; 开始的保持时间:SDA下降至低电平与SCL下降至低电平的时间间隔;

停止条件:SCL线路从低电平切换到高电平后,SDA线路从低电平切换到高电平。
结束的建立时间:SCL上升至高电平与SDA上升至高电平之间的时间间隔。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值