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 <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include "mpu6050.h"
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/delay.h>

#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<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#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

發佈了102 篇原創文章 · 獲贊 110 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章