Linux驱动之I2C设备驱动完全解析

上一节介绍了I2C的相关协议,本节主要讲I2C的设备驱动的创建
在内核iTop4412_Kernel_3.0\Documentation\i2c\instantiating-devices这个文档介绍了4种创建I2C设备的方法
1设备的4种构建方法
1.1定义一个i2c_board_info,里面有名字,地址
然后i2c_register_board_info(busnum,……)(把他们放入链表)
list_add_tail(&devinfo->list, &__i2c_board_list);
链表何时使用
i2c_register_adapter->i2c_scan_static_board_info(struct i2c_adapter *adapter)>i2c_new_device

使用限制:必须i2c_register_adapter之前i2c_register_board_info
所以不适合我们动态加载insmod
1.2直接i2c_new_device,i2c_new_probe_device
1.2.1 i2c_new_device :认为设备肯定存在
1.2.2 i2c_new_probed_device :对于“已经识别出来的设备”probed__device”,才会(new)
probe(adap, addr_list[i] //确定设备是否真的存在
info->addr = addr_list[i];
return i2c_new_device(adap, info);
1.3从用户空间创建设备
创建设备
echo at24c02 0x50 > /sys/class/i2c-adapter/i2c-7/new_device
删除设备
echo 0x50 > /sys/class/i2c-adapter/i2c-7/new_device
导致i2c_unregister_device
1.4前面的3种方法都要确定适配器(I2C总线、I2C控制器
如果事先不知道这个I2C设备在哪个适配器,怎么办?去class表示的所有的适配器上查找,有一些I2C设备的地址是一样的,怎么继续分配它是哪一款?用detect函数确定

static struct i2c_driver at24cxx_driver = {
    .class=I2C_CLASS_HWMON//哪一类设备器查找支持的设备
    .driver = {
        .name   = "100ask",
    },
    .probe      = at24cxx_probe,
    .remove     = __devexit_p(at24cxx_remove),
    .id_table   = at24cxx_id_table,
    .detect=at24cxx_detect,//用这个函数来检测能否找到设备
    .address_list=addr_list,//这些设备的地址
};
去“class表示的这一类“i2c适配器,用“detect函数“来确定能否找到"address_list"里的设备
如果能找到就调用好i2c_new_device来注册i2c_client,这会和i2c_driver的id_table比较,如果匹配,调用probe
首先在入口函数中调用.i2c_add_driver
a.i2c_add_driver
        i2c_register_driver.
            at24cxx_driver放入i2c_bus_type的drv链表,
    并且从dev链表里取出能匹配的i2c_client并调用probe
        driver_register
    如果dev链表没有,此时则调用i2c_for_each_dev(driver,__process_new_driver);   对每一个适配器进行循环
b.对于每一个是适配器,调用__process_new_driver
对于每一个适配器,调用它的函数确定address_list里的设备是否存在
如果存在,在调用detect进一步确定、设置,然后i2c_new_device
i2c_for_each_dev(driver,__process_new_driver);  
    __process_new_driver
        i2c_do_add_adapter
        i2c_detect(adap,driver);
            for(i=0;address_list[i]!=I2C_CLIENT_END;i+=1)
            err=i2c_detect_address(temp_client,driver);
        //判断
        if(!i2c_default_probe(adapter,addr))
        return 0;
    memset(&info,0,sizeof(struct i2c_board_info));
    info.addr=addr;
    //设置info的结构体
    err=driver->detect(temp_client,&info);

2.实例驱动程序编写

/*
I2C设备:mpu6050
内核版本:3.0.15
开发板:itop4412
*/

mpu6050.h

#ifndef __MPU6050_H_
#define __MPU6050_H_
#define MPU6050_MAGIC 'K'
union mpu6050_data{
    struct{
        unsigned short x;
        unsigned short y;
        unsigned short z;
    }accel;
    struct{
        unsigned short x;
        unsigned short y;
        unsigned short z;
    }gyro;
    unsigned short temp;
};
#define GET_ACCEL _IOR(MPU6050_MAGIC,0,union mpu6050_data)
#define GET_GYRO _IOR(MPU6050_MAGIC,1,union mpu6050_data)
#define GET_TEMP _IOR(MPU6050_MAGIC,2,union mpu6050_data)
#endif

MPU6050.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "mpu6050.h"
#include 
#include 
#include 
#include 
#include 

#define SMPLRT_DIV      0x19  
#define CONFIG          0x1A  
#define GYRO_CONFIG     0x1B  
#define ACCEL_CONFIG    0x1C  
#define ACCEL_XOUT_H    0x3B  
#define ACCEL_XOUT_L    0x3C  
#define ACCEL_YOUT_H    0x3D  
#define ACCEL_YOUT_L    0x3E  
#define ACCEL_ZOUT_H    0x3F  
#define ACCEL_ZOUT_L    0x40  
#define TEMP_OUT_H      0x41  
#define TEMP_OUT_L      0x42  
#define GYRO_XOUT_H     0x43  
#define GYRO_XOUT_L     0x44  
#define GYRO_YOUT_H     0x45  
#define GYRO_YOUT_L     0x46  
#define GYRO_ZOUT_H     0x47  
#define GYRO_ZOUT_L     0x48  
#define PWR_MGMT_1      0x6B  

static int major;
struct cdev cdev;
static struct class *class;
static const unsigned short addr_list[] = { 0x68,I2C_CLIENT_END};
static struct i2c_client *mpu6050_client;
static struct i2c_client *my_client;
static int mpu6050_write_byte(struct i2c_client *client,unsigned char reg,unsigned char val)
{

    if(!i2c_smbus_write_byte_data(client,reg,val))
            return 2;
        else
            return -EIO;

    return 0;
}
static int mpu6050_read_byte(struct i2c_client *client,unsigned char reg)
{
     unsigned char data;
     data=i2c_smbus_read_byte_data(client,reg);
     return data;
}
static int mpu6050_open(struct inode * inode, struct file * file)
{
    return 0;
}
static int mpu6050_release(struct inode *inode, struct file *file)
{
    return 0;
}

static int mpu6050_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    union mpu6050_data data;
    struct i2c_client *client = my_client;
    switch(cmd){
    case GET_ACCEL:
    {
        data.accel.x=mpu6050_read_byte(client,ACCEL_XOUT_L);
        data.accel.x|=mpu6050_read_byte(client,ACCEL_XOUT_H)<<8;

        data.accel.y=mpu6050_read_byte(client,ACCEL_YOUT_L);
        data.accel.y|=mpu6050_read_byte(client,ACCEL_YOUT_H)<<8;


        data.accel.z=mpu6050_read_byte(client,ACCEL_ZOUT_L);
        data.accel.z|=mpu6050_read_byte(client,ACCEL_ZOUT_H)<<8;
        printk(KERN_EMERG"GET_ACCEL X = %04x\n",data.accel.x);
        printk(KERN_EMERG"GET_ACCEL y = %04x\n",data.accel.y);
        printk(KERN_EMERG"GET_ACCEL z = %04x\n",data.accel.z);
        printk("GET_ACCEL finished\n");
        break;
    }
    case GET_GYRO:
    {
        data.gyro.x=mpu6050_read_byte(client,GYRO_XOUT_L);
        data.gyro.x|=mpu6050_read_byte(client,GYRO_XOUT_H)<<8;

        data.gyro.y=mpu6050_read_byte(client,GYRO_YOUT_L);
        data.gyro.y|=mpu6050_read_byte(client,GYRO_YOUT_H)<<8;

        data.gyro.z=mpu6050_read_byte(client,GYRO_ZOUT_L);
        data.gyro.z|=mpu6050_read_byte(client,GYRO_ZOUT_H)<<8;
        printk("GET_GYRO finished\n");
        break;
    }
    case GET_TEMP:
    {
        data.temp=mpu6050_read_byte(client,TEMP_OUT_L);
        data.temp|=mpu6050_read_byte(client,TEMP_OUT_H)<<8;
        break;
    }
    default:
    {
        printk("magic error\n");
        return -EINVAL;
    }   
}
    if(copy_to_user((void*)arg,&data,sizeof(data)))
        return -EINVAL;
    return sizeof(data);

}

struct file_operations mpu6050_ops={
    .owner = THIS_MODULE,
    .open  = mpu6050_open,
    .release = mpu6050_release,
    .unlocked_ioctl = mpu6050_ioctl,
};
static int __devinit mpu6050_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
    printk("match\n");
    my_client=kzalloc(sizeof(struct i2c_client),GFP_KERNEL);
    if(my_client == NULL) 
     {  
        return -ENOMEM;  
    }  
    my_client=client;
    major = register_chrdev(0, "mpu6050", &mpu6050_ops);
    class = class_create(THIS_MODULE, "mpu6050");
    device_create(class, NULL, MKDEV(major, 0), NULL, "mpu6050");
    printk("create successful\n");
    /*mpu6050的初始化*/
    mpu6050_write_byte(my_client,PWR_MGMT_1,0x00);//解除睡眠
    mpu6050_write_byte(my_client,SMPLRT_DIV,0x07);//采样频率分频器
    mpu6050_write_byte(my_client,CONFIG,0x06);//配置EXT_SYNC_SET和FSYNC,初始化温度
    mpu6050_write_byte(my_client,GYRO_CONFIG,0xF8);//选通陀螺仪的三轴及量程
    mpu6050_write_byte(my_client,ACCEL_CONFIG,0x19);//执行加速度自检和量程
    printk("init finished\n");
    return 0;
}
static int __devexit mpu6050_remove(struct i2c_client *client)
{
    device_destroy(class,MKDEV(major, 0));
    class_destroy(class);
    unregister_chrdev(major, "mpu6050");
    return 0;
}

static const struct i2c_device_id mpu6050_id_table[] = {
    { "mpu6050", 0 },
    { }
};

static struct i2c_driver mpu6050_driver = {
    .driver = {
        .name   = "100ask",
    },
    .probe      = mpu6050_probe,
    .remove     = __devexit_p(mpu6050_remove),
    .id_table   = mpu6050_id_table,

};

static int mpu6050_init(void)
{
    i2c_add_driver(&mpu6050_driver);
 struct i2c_adapter *i2c_adap;
  struct i2c_board_info mpu6050_info;
  memset(&mpu6050_info,0,sizeof(struct i2c_board_info));
  strlcpy(mpu6050_info.type,"mpu6050",I2C_NAME_SIZE);
  i2c_adap=i2c_get_adapter(7);
  mpu6050_client=i2c_new_probed_device(i2c_adap,&mpu6050_info,addr_list,NULL);
  i2c_put_adapter(i2c_adap);
  if(mpu6050_client)
    return 0;
  else
    return -ENODEV;
   return 0;
}
static void mpu6050_exit(void)
{
      i2c_del_driver(&mpu6050_driver);//必须先注销驱动
      i2c_unregister_device(mpu6050_client);

}
module_init(mpu6050_init);
module_exit(mpu6050_exit);
MODULE_LICENSE("GPL");

测试程序:

#include
#include
#include
#include
#include
#include"mpu6050.h"
int main(int argc,char** argv)
{
    int fd;
    union mpu6050_data data;

    fd=open("/dev/mpu6050",O_RDWR);
    if(fd<0)
    {
        perror("open");
        exit(1);
    }
    while(1)
    {
        ioctl(fd,GET_ACCEL,&data);
        printf("acceleration data: x =  %04x,y = %04x,z = %04x\n",data.accel.x,data.accel.y,data.accel.z);

        ioctl(fd,GET_GYRO,&data);
        printf("gyroscope data : x = %04x,y = %04x,z = %04x\n",data.gyro.x,data.gyro.y,data.gyro.z);
        sleep(1);
    }
    close(fd);
    return 0;

}

测试程序编译
arm-none-Linux-gnueabi-gcc mpu6050_app.c -o mpu6050_app -static
下载到开发板中
insmod mpu6050.ko
./mpu6050_app
acceleration data: x = 0183,y = 001a,z = f814
gyroscope data : x = 07ea,y = 0932,z = 0c0e

你可能感兴趣的:(LINUX设备驱动实战)