I2C 通信 api

把之前的oled从rtos移植到linux时,抽了个IIC通信api粗来 ⊙▽⊙

Header: i2c-api.h

//
//  i2c-api.h
//  i2c-api
//
//  Created by MetalSeed on 15/4/11.
//  Copyright (c) 2015年 MetalSeed. All rights reserved.
//

#ifndef i2c_api_i2c_api_h
#define i2c_api_i2c_api_h


#include  // include I2C_FUNC_SMBUS_
#include 

#define IIC_TYPE_UNKNOWN    0
#define IIC_TPPE_OLED       1
#define IIC_TYPE_8BIT_ADDR  2
#define IIC_TYPE_16BIT_ADDR 3

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

/*
 * opens the i2c device at [dev_fqn] (i.e. /dev/i2c-N) whose address is
 * [addr] and set the i2c_object [e]
 */
int i2c_open(char *dev_fqn, int addr, int type, struct i2c_obj*);
/*
 * closees the i2c device [e]
 */
int i2c_close(struct i2c_obj *e);
/*
 * read and returns the i2c byte at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_byte(struct i2c_obj* e, __u16 mem_addr);
/*
 * read the current byte
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_current_byte(struct i2c_obj *e);
/*
 * writes [data] at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_write_byte(struct i2c_obj *e, __u16 mem_addr, __u8 data);

#endif

Source: i2c-api.c

//
//  main.c
//  i2c-api
//
//  Created by MetalSeed on 15/4/11.
//  Copyright (c) 2015年 MetalSeed. All rights reserved.
//

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "i2c-api.h"


//
//
// i2c smbus func
//
//

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;
}


/* Returns the number of read bytes */
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);
}

/* Returns the number of read bytes */
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);
}

/* Returns the number of read bytes */
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 i2c_obj *e, __u8 buf)
{
    int r;
    // we must simulate a plain I2C byte write with SMBus functions
    r = i2c_smbus_write_byte(e->fd, buf);
    if(r < 0)
        fprintf(stderr, "Error i2c_write_1b: %s\n", strerror(errno));
    usleep(10);
    return r;
}

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

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




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

/*
 * opens the i2c device at [dev_fqn] (i.e. /dev/i2c-N) whose address is
 * [addr] and set the i2c_object [e]
 */
int i2c_open(char *dev_fqn, int addr, int type, struct i2c_obj* e)
{
    int funcs, fd, r;
    e->fd = e->addr = 0;
    e->dev = 0;

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

    // get funcs list
    if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
    {
        fprintf(stderr, "Error i2c_obj_open: %s\n", 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( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)
    {
        fprintf(stderr, "Error i2c_obj_open: %s\n", strerror(errno));
        return -1;
    }
    e->fd = fd;
    e->addr = addr;
    e->dev = dev_fqn;
    e->type = type;
    return 0;
}

/*
 * closees the i2c device [e]
 */
int i2c_close(struct i2c_obj *e);
{
    close(e->fd);
    e->fd = -1;
    e->dev = 0;
    e->type = IIC_TYPE_UNKNOWN;
    return 0;
}

/*
 * read and returns the i2c byte at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_byte(struct i2c_obj* e, __u16 mem_addr);
{
    int r;
    ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
    if(e->type == IIC_TYPE_8BIT_ADDR)
    {
        __u8 buf =  mem_addr & 0x0ff;
        r = i2c_write_1b(e, buf);
    } else if(e->type == IIC_TYPE_16BIT_ADDR) {
        __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
        r = i2c_write_2b(e, buf);
    } else {
        fprintf(stderr, "ERR: unknown i2c_obj type\n");
        return -1;
    }
    if (r < 0)
        return r;
    r = i2c_smbus_read_byte(e->fd);
    return r;
}

/*
 * read the current byte
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_current_byte(struct i2c_obj *e)
{
    ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
    return i2c_smbus_read_byte(e->fd);
}


/*
 * writes [data] at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_write_byte(struct i2c_obj *e, __u16 mem_addr, __u8 data)
{
    if(e->type == IIC_TYPE_8BIT_ADDR) {
        __u8 buf[2] = { mem_addr & 0x00ff, data };
        return i2c_write_2b(e, buf);
    } else if(e->type == IIC_TYPE_16BIT_ADDR) {
        __u8 buf[3] =
        { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
        return i2c_write_3b(e, buf);
    }
    fprintf(stderr, "ERR: unknown i2c_obj type\n");
    return -1;
}

细节不完善的地方,日后有空的时候再补充。 >.<

你可能感兴趣的:(嵌入式linux)