1. 程式人生 > >linux i2c access in kernel and user space

linux i2c access in kernel and user space

http://bunniestudios.com/blog/images/infocast_i2c.c

http://www.ithao123.cn/content-1002018.html


1. i2c access in kernel

int zl380xx_write_reg(struct i2c_client *i2c,unsigned int reg,unsigned int value)
{
	unsigned char buf[6]={0};
	struct i2c_msg msg[2];

	if( reg >= 0x00 
	      && reg <= 0xEE)
	{
	buf[0] = (reg >> 1) | 0x80;
	buf[1] = 0x80;
	buf[2] = value >> 8;
	buf[3] = value & 0xFF;

	msg[0].addr = i2c->addr;
	msg[0].flags = 0;
	msg[0].buf = buf;
	msg[0].len = 4;
	i2c_transfer(i2c->adapter,msg,1);
	return 0;
	}
	else if( reg >= 0x100
		  && reg <= 0xFFE)
	{
	buf[0] = 0xFE;
	buf[1] = (reg >> 8) -1 ;
	buf[2] = (reg & 0x00FF) >> 1;
	buf[3] = 0x80;
	buf[4] = value >> 8;
	buf[5] = value & 0xFF;
	
	msg[0].addr = i2c->addr;
	msg[0].flags = 0;
	msg[0].buf = buf;
	msg[0].len = 6;
	i2c_transfer(i2c->adapter,msg,1);
	return 0;
	}
	else
	return -1;
}

int zl380xx_read_reg(struct i2c_client *i2c,unsigned int reg,unsigned int *value)
{
	unsigned char buf[4]={0x00};
	unsigned char bufRead[2]={0x0};
	struct i2c_msg msg[2];

	if( reg >= 0x00 
	      && reg <= 0xEE)
	{
	buf[0] = (reg >> 1) | 0x80;
	buf[1] = 0x00;

	msg[0].addr = i2c->addr;
	msg[0].flags = 0;
	msg[0].buf = buf;
	msg[0].len = 2 ;
	msg[1].addr = i2c->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].buf = bufRead;
	msg[1].len = 2;
	i2c_transfer(i2c->adapter,msg,2);
	*value = (bufRead[0] << 8)| bufRead[1];
	printk("read register 0x%x=0x%x\n",reg,*value);
	return 0;
	}
	else if( reg >= 0x100
		  && reg <= 0xFFE)
	{
	buf[0] = 0xFE;
	buf[1] = (reg >> 8) -1 ;
	buf[2] = (reg & 0x00FF) >> 1;
	buf[3] = 0x00;

	msg[0].addr = i2c->addr;
	msg[0].flags = 0;
	msg[0].buf = buf;
	msg[0].len = 4 ;
	msg[1].addr = i2c->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].buf = bufRead;
	msg[1].len = 2;
	i2c_transfer(i2c->adapter,msg,2);
	*value = (bufRead[0] << 8)| bufRead[1];
	printk("read register 0x%x=0x%x\n",reg,*value);
	return 0;
	}
	else
		return -1;
}

ps: this is demo source for zl380xx. the i2c data that zl380xx sent is different with normal device.


2. i2c access in user space

/*
 This software uses a BSD license.

Copyright (c) 2010, Sean Cross / chumby industries
All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the
   distribution.  
 * Neither the name of Sean Cross / chumby industries nor the names
   of its contributors may be used to endorse or promote products
   derived from this software without specific prior written
   permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 
 */

#include <stdio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>


#define I2C_FILE_NAME "/dev/i2c-0"
#define USAGE_MESSAGE \
    "Usage:\n" \
    "  %s r [addr] [register]   " \
        "to read value from [register]\n" \
    "  %s w [addr] [register] [value]   " \
        "to write a value [value] to register [register]\n" \
    ""

static int set_i2c_register(int file,
                            unsigned char addr,
                            unsigned char reg,
                            unsigned char value) {

    unsigned char outbuf[2];
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[1];

    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = sizeof(outbuf);
    messages[0].buf   = outbuf;

    /* The first byte indicates which register we'll write */
    outbuf[0] = reg;

    /* 
     * The second byte indicates the value to write.  Note that for many
     * devices, we can write multiple, sequential registers at once by
     * simply making outbuf bigger.
     */
    outbuf[1] = value;

    /* Transfer the i2c packets to the kernel and verify it worked */
    packets.msgs  = messages;
    packets.nmsgs = 1;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Unable to send data");
        return 1;
    }

    return 0;
}


static int get_i2c_register(int file,
                            unsigned char addr,
                            unsigned char reg,
                            unsigned char *val) {
    unsigned char inbuf, outbuf;
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[2];

    /*
     * In order to read a register, we first do a "dummy write" by writing
     * 0 bytes to the register we want to read from.  This is similar to
     * the packet in set_i2c_register, except it's 1 byte rather than 2.
     */
    outbuf = reg;
    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = sizeof(outbuf);
    messages[0].buf   = &outbuf;

    /* The data will get returned in this structure */
    messages[1].addr  = addr;
    messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
    messages[1].len   = sizeof(inbuf);
    messages[1].buf   = &inbuf;

    /* Send the request to the kernel and get the result back */
    packets.msgs      = messages;
    packets.nmsgs     = 2;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Unable to send data");
        return 1;
    }
    *val = inbuf;

    return 0;
}


int main(int argc, char **argv) {
    int i2c_file;

    // Open a connection to the I2C userspace control file.
    if ((i2c_file = open(I2C_FILE_NAME, O_RDWR)) < 0) {
        perror("Unable to open i2c control file");
        exit(1);
    }


    if(argc > 3 && !strcmp(argv[1], "r")) {
        int addr = strtol(argv[2], NULL, 0);
        int reg = strtol(argv[3], NULL, 0);
        unsigned char value;
        if(get_i2c_register(i2c_file, addr, reg, &value)) {
            printf("Unable to get register!\n");
        }
        else {
            printf("Register %d: %d (%x)\n", reg, (int)value, (int)value);
        }
    }
    else if(argc > 4 && !strcmp(argv[1], "w")) {
        int addr = strtol(argv[2], NULL, 0);
        int reg = strtol(argv[3], NULL, 0);
        int value = strtol(argv[4], NULL, 0);
        if(set_i2c_register(i2c_file, addr, reg, value)) {
            printf("Unable to get register!\n");
        }
        else {
            printf("Set register %x: %d (%x)\n", reg, value, value);
        }
    }
    else {
        fprintf(stderr, USAGE_MESSAGE, argv[0], argv[0]);
    }


    close(i2c_file);


    return 0;
}