ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

IIC驱动移植在linux3.14.78上的实现和在linux2.6.29上实现对比

2021-04-16 12:06:54  阅读:166  来源: 互联网

标签:I2C linux3.14 29 dev platform IIC device pdev i2c


首先说明下为什么写这篇文章,网上有许多博客也是介绍I2C驱动在linux上移植的实现,但是笔者认为他们相当一部分没有分清所写的驱动时的驱动模型,是基于device tree, 还是基于传统的Platform模型,有些文章只是把代码移植到平台上调试测试下,并没有理清内部逻辑调用关系,所以觉得有必要把两种驱动模型阐述剖析清楚,本文阅读者必须以在单片机上调试过IIC总线为前提,能够分析从芯片datasheet和其工作原理和总线的基本操作,虽然I2C硬件体系结构比较简单,但是I2C体系结构在Linux中的实现却相当复杂,作为驱动工程师,编写具体的I2C驱动时,主要工作如下:

1)、提供I2C适配器的硬件驱动,探测,初始化I2C适配器(如申请I2C的I/O地址和中断号),驱动CPU控制的I2C适配器从硬件上产生各种信号以及处理I2C中断(I2C总线驱动);

2)、提供I2C控制的algorithm, 用具体适配器的xxx_xfer()函数填充i2c_algorithm的master_xfer指针,并把i2c_algorithm指针赋给i2c_adapter的algo指针(I2C总线驱动),用于产生I2C访问从设备周期所需要的信号;

3)、实现I2C设备驱动中的i2c_driver接口,用具体yyy的yyy_probe(),yyy_remove(),yyy_suspend(),yyy_resume()函数指针和i2c_device_id设备ID表赋给i2c_driver的probe,remove,suspend,resume和id_table指针(I2C设备驱动);

4)、实现I2C设备所对应类型的具体驱动,i2c_driver只是实现设备与总线的挂接(I2C设备驱动)。

Step1,必须理清platform_device和platform_driver之间的匹配方式

  对比linux2.6.29实现方式见图1,linux3.14.78的实现方式见图2,可以发现,传统的Platform驱动模型只是通过匹配platform_device设备名和驱动的名字来实现,而对于3.0以后的内核,通过一下四种方式实现,首先时基于设备树风格的匹配(设备树是一种描述硬件的数据结构),第二种是基于ACPI风格的匹配,第三种是匹配ID表(platform_device设备名是否出现在platform_driver的ID表内),第四种方式才采用传统的匹配设备与驱动的名字来实现,我们先通过匹配platform_device设备名和驱动的名字来实现IIC在2.6.29上移植,然后再分析通过设备树的方式实现IIC在3.14.78上移植,下面我们开始:

复制代码

 1 //linux2.6.29系统中为platform总线定义了一个bus_type的实例platform_bus_type,
 2 struct bus_type platform_bus_type = {
 3     .name = “platform”,
 4     .dev_attrs = platform_dev_attrs,
 5     .match = platform_match,
 6     .uevent = platform_uevent,
 7     .pm = PLATFORM_PM_OPS_PTR,
 8 };
 9 EXPORT_SYMBOL_GPL(platform_bus_type);
10  
11 //这里要重点关注其match()成员函数,正是此成员表明了platform_device和platform_driver之间如何匹配。
12 static int platform_match(struct device *dev, struct device_driver *drv)
13 {
14     struct platform_device *pdev;
15 
16     pdev = container_of(dev, struct platform_device, dev);
17     return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
18 }
19 //匹配platform_device和platform_driver主要看二者的name字段是否相同。
20 //对platform_device的定义通常在BSP的板文件中实现,在板文件中,将platform_device归纳为一个数组,最终通过platform_add_devices()函数统一注册。
21 //platform_add_devices()函数可以将平台设备添加到系统中,这个函数的 原型为:
22 int platform_add_devices(struct platform_device **devs, int num);
23 //该函数的第一个参数为平台设备数组的指针,第二个参数为平台设备的数量,它内部调用了platform_device_register()函 数用于注册单个的平台设备。

复制代码

复制代码

//linux3.14.78内核
 1 static int platform_match(struct device *dev, struct device_driver *drv)
 2 {
 3     struct platform_device *pdev = to_platform_device(dev);
 4     struct platform_driver *pdrv = to_platform_driver(drv);
 5 
 6     /* Attempt an OF style match first */
 7     if (of_driver_match_device(dev, drv))
 8         return 1;
 9 
10     /* Then try ACPI style match */
11     if (acpi_driver_match_device(dev, drv))
12         return 1;
13 
14     /* Then try to match against the id table */
15     if (pdrv->id_table)
16         return platform_match_id(pdrv->id_table, pdev) != NULL;
17 
18     /* fall-back to driver name match */
19     return (strcmp(pdev->name, drv->name) == 0);
20 }

复制代码

 Step2,添加设备对象硬件信息,对应i2c_client,i2c_client的信息通常在BSP的板文件中通过i2c_board_info填充,在系统启动之处静态地进行i2c设备注册(设备注册分为动态发现注册和静态注册)下面的代码定义了一个I2C设备的ID为“24c08”,地址为0x50的i2c_client,使用i2c_register_board_info将所有注册i2c_board_info对象添加到一个名为__i2c_board_list的链表上,具体实现在mach_s3c2440.c中调用i2c_register_board_info,详见下面代码;

复制代码

1 static struct i2c_board_info i2c_devices[] __initdata = {
 2 {I2C_BOARD_INFO("24c08", 0x50), },
 3 };
 4 static void __init My2440_machine_init(void)
 5 {
 6     s3c24xx_fb_set_platdata(&My2440_fb_info);
 7     s3c_i2c0_set_platdata(NULL);
 8     i2c_register_board_info(0, i2c_devices, ARRAY_SIZE(i2c_devices));
 9     
10     s3c_device_spi0.dev.platform_data= &s3c2410_spi0_platdata;  
11     spi_register_board_info(s3c2410_spi0_board, ARRAY_SIZE(s3c2410_spi0_board));  
12     s3c_device_spi1.dev.platform_data= &s3c2410_spi1_platdata;  
13     spi_register_board_info(s3c2410_spi1_board, ARRAY_SIZE(s3c2410_spi1_board));  
14     s3c_device_nand.dev.platform_data = &My2440_nand_info;
15     s3c_device_sdi.dev.platform_data = &My2440_mmc_cfg;
16 
17     platform_add_devices(My2440_devices, ARRAY_SIZE(My2440_devices));
18 }
19 
20 MACHINE_START(MY2440, "MY2440")
21     /* Maintainer: Ben Dooks <ben@fluff.org> */
22     .phys_io    = S3C2410_PA_UART,
23     .io_pg_offst    = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
24     .boot_params    = S3C2410_SDRAM_PA + 0x100,
25 
26     .init_irq    = s3c24xx_init_irq,
27     .map_io        = My2440_map_io,
28     .init_machine    = My2440_machine_init,
29     .timer        = &s3c24xx_timer,
30 MACHINE_END

复制代码

 Step3,概念理清楚,i2c_adapter对应物理上的一个适配器,i2c_client对应真实的物理设备,每个i2c都需要一个i2c_client来描述,i2c_client依附于i2c_adapter,由于一个适配器可以连接多个I2C设备,所以一个i2c_adapter可以被多个i2c_client所依附;而i2c_driver对应与一套驱动方法,他们之间的数据结构之间的关系见下图。在系统启动或者模块加载时,i2c适配器设备驱动i2cdev_driver被添加到系统中,具体实现在drivers/i2c/i2c-dev.c中,现在分析它的实现,在i2c-dev.c中,定义了一个名为i2c_dev_init()的初始化函数,从module_init(i2c_dev_init)可以看出,该函数在系统启动或模块加载时执行,主要完成3种操作

  first. 调用register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops),为I2C适配器注册主设备号为I2C_MAJOR(89)、次设备号为0~255、文件操作集合为i2cdev_fops的字符设备,也就是针对每个I2C适配器生成一个主设备号为89的设备文件,实现了i2c_driver的成员函数以及文件操作接口;

  second. 调用class_create(THIS_MODULE, "i2c-dev")注册名为“i2c-dev”的设备类,然后又注册了一个i2cdev_notifier;

  Third. 调用i2c_for_each_dev(NULL, i2cdev_attach_adapter)遍历i2c_bus_type总线上的设备,对找到的设备执行i2cdev_attach_adapter()函数,它首先调用get_free_i2c_dev()分配并初始化一个struct i2c_dev结构,使i2c_dev->adap指向操作的adapter之后,该i2c_dev会被插入到连边i2c_dev_list中,再创建一个device,即绑定adapter并在/dev/目录下创建字符设备节点;

复制代码

static int __init i2c_dev_init(void)
 {
     int res;
     printk(KERN_INFO "i2c /dev entries driver\n");
    res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);
    if (res)
         goto out;
     i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
     if (IS_ERR(i2c_dev_class)) {
         res = PTR_ERR(i2c_dev_class);
         goto out_unreg_chrdev;
     }
     i2c_dev_class->dev_groups = i2c_groups;
     /* Keep track of adapters which will be added or removed later */
     res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
     if (res)
         goto out_unreg_class;
     /* Bind to already existing adapters right away */
     i2c_for_each_dev(NULL, i2cdev_attach_adapter); 
     return 0;
 out_unreg_class:
     class_destroy(i2c_dev_class);
 out_unreg_chrdev:
     unregister_chrdev(I2C_MAJOR, "i2c");
 out:
     printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
     return res;
 }

复制代码

Step4,添加i2c总线驱动(I2C适配器驱动的注册),由于I2C总线控制器通常是在内存上的,所以它本身也连接在platform总线上,要通过platform_driver和platform_device的匹配来执行。尽管I2C适配器给别人提供了总线,它自己也是连接在platform总线上的一个客户。在文件drivers/i2c/busses/i2c-s3c2410.c中,platform_driver的注册通过调用初始化函数i2c_adapter_s3c_init函数来完成,与I2C适配器所对应的platform_driver的probe函数主要完成以下两个工作:

  1、初始化I2C适配器所使用的硬件资源,如申请I/O地址、中断号、时钟等;2、通过i2c_add_numbered_adapter()添加i2c_adapter的数据结构,当然这个i2c_adapter数据结构的成员已经被对应的适配器的相应函数指针所初始化具体实现

i2c_add_numbered_adapter(&i2c->adap)-->i2c_register_adapter(adap)-->i2c_scan_static_board_info(adap)-->list_for_each_entry(devinfo, &__i2c_board_list, list)-->i2c_new_device(adapter,&devinfo->board_info)),以adapter结构体和找到的devinfo结构体中的i2c_board_info结构体为参数在i2c_bus_type总线上添加client设备,即添加i2c_client.

复制代码

1 static int s3c24xx_i2c_probe(struct platform_device *pdev)
  2 {
  3     struct s3c24xx_i2c *i2c;
  4     struct s3c2410_platform_i2c *pdata = NULL;
  5     struct resource *res;
  6     int ret;
  7     if (!pdev->dev.of_node) {
  8         pdata = dev_get_platdata(&pdev->dev);
  9         if (!pdata) {
 10             dev_err(&pdev->dev, "no platform data\n");
 11             return -EINVAL;
 12         }
 13     }
 14     i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
 15     if (!i2c) {
 16         dev_err(&pdev->dev, "no memory for state\n");
 17         return -ENOMEM;
 18     }
 19     i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 20     if (!i2c->pdata) {
 21         dev_err(&pdev->dev, "no memory for platform data\n");
 22         return -ENOMEM;
 23     }
 24     i2c->quirks = s3c24xx_get_device_quirks(pdev);
 25     if (pdata)
 26         memcpy(i2c->pdata, pdata, sizeof(*pdata));
 27     else
 28         s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
 29     strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
 30     i2c->adap.owner   = THIS_MODULE;
 31     i2c->adap.algo    = &s3c24xx_i2c_algorithm;   //设置适配器的通信方法 32  
        i2c->adap.retries = 2;
 33     i2c->adap.class   = I2C_CLASS_HWMON | I2C_CLASS_SPD;
 34     i2c->tx_setup     = 50;
 35     init_waitqueue_head(&i2c->wait);
 36     /* find the clock and enable it */
 37     i2c->dev = &pdev->dev;
 38     i2c->clk = devm_clk_get(&pdev->dev, "i2c");
 39     if (IS_ERR(i2c->clk)) {
 40         dev_err(&pdev->dev, "cannot get clock\n");
 41         return -ENOENT;
 42     }
 43     dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
 44     /* map the registers */
 45     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 46     i2c->regs = devm_ioremap_resource(&pdev->dev, res);
 47     if (IS_ERR(i2c->regs))
 48         return PTR_ERR(i2c->regs);
 49     dev_dbg(&pdev->dev, "registers %p (%p)\n",
 50         i2c->regs, res);
 51     /* setup info block for the i2c core ,将s3c24xx_i2c类型的对象I2C作为通信方法的私有数据存放在algo_data中,
      这样master_xfer等方法就能通过适配器的algo_data获得这个对象
     */
 52     i2c->adap.algo_data = i2c;
 53     i2c->adap.dev.parent = &pdev->dev;
 54     i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
 55     /* inititalise the i2c gpio lines */
 56     if (i2c->pdata->cfg_gpio) {
 57         i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
 58     } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) {
 59         return -EINVAL;
 60     }
 61     /* initialise the i2c controller*/
 62     clk_prepare_enable(i2c->clk);
 63     ret = s3c24xx_i2c_init(i2c); 
 64     clk_disable(i2c->clk);
 65     if (ret != 0) {
 66         dev_err(&pdev->dev, "I2C controller init failed\n");
 67         return ret;
 68     }
 69     /* find the IRQ for this unit (note, this relies on the init call to
 70      * ensure no current IRQs pending,获取IRQ资源并注册该中断,s3c24xx_i2c_irq是用来后续传输的中断处理函数
 71      */
 72     if (!(i2c->quirks & QUIRK_POLL)) {
 73         i2c->irq = ret = platform_get_irq(pdev, 0);
 74         if (ret <= 0) {
 75             dev_err(&pdev->dev, "cannot find IRQ\n");
 76             clk_unprepare(i2c->clk);
 77             return ret;
 78         }
 79     ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0,
 80                 dev_name(&pdev->dev), i2c);
 81         if (ret != 0) {
 82             dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
 83             clk_unprepare(i2c->clk);
 84             return ret;
 85         }
 86     }
 87     ret = s3c24xx_i2c_register_cpufreq(i2c);
 88     if (ret < 0) {
 89         dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
 90         clk_unprepare(i2c->clk);
 91         return ret;
 92     }
 93     /* Note, previous versions of the driver used i2c_add_adapter()
 94      * to add the bus at any number. We now pass the bus number via
 95      * the platform data, so if unset it will now default to always
 96      * being bus 0.
 97      */
 98     i2c->adap.nr = i2c->pdata->bus_num;
 99     i2c->adap.dev.of_node = pdev->dev.of_node;
100     platform_set_drvdata(pdev, i2c);
101     pm_runtime_enable(&pdev->dev);
102     ret = i2c_add_numbered_adapter(&i2c->adap);   //静态方式添加适配器
103     if (ret < 0) {
104         dev_err(&pdev->dev, "failed to add bus to i2c core\n");
105         pm_runtime_disable(&pdev->dev);
106         s3c24xx_i2c_deregister_cpufreq(i2c);
107         clk_unprepare(i2c->clk);
108         return ret;
109     }
110     pm_runtime_enable(&i2c->adap.dev);
111     dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
112     return 0;
113 }

复制代码

 Step5,编写i2c设备驱动,这部分工作就完全由驱动工程师来完成了。之前所完成的工作:I2C适配器所在驱动的platform_driver与arch/arm/mach-s3c2440中的platform(或者设备树中的节点)通过platform总线的match()函数导致s3c24xx_i2c_probe的执行,从而完成I2C适配器控制器的注册;而挂载在I2C上面的设备,以陀螺仪MPU6050为对象,它所依附的i2c_driver与arch/arm/mach-s3c2440中的i2c_board_info指向的设备(或设备树中的节点)通过I2C总线的match()函数匹配导致i2c_driver.i2c_probe执行。

  设备树信息

复制代码

 1 i2c@138B0000 {
 2     #address-cells = <1>;
 3     #size-cells = <0>;
 4     samsung,i2c-sda-delay = <100>;
 5     samsung,i2c-max-bus-freq = <20000>;
 6     pinctrl-0 = <&i2c5_bus>;
 7     pinctrl-names = "default";
 8     status = "okay";
 9 
10     mpu6050@68 {
11         compatible = "fs4412,mpu6050";
12         reg = <0x68>;
13     };
14 };

复制代码

复制代码

//i2c_driver.c
  1 #include <linux/kernel.h>
  2 #include <linux/module.h>
  3 #include <linux/i2c.h>
  4 #include <linux/device.h>
  5 #include <linux/cdev.h>
  6 #include <linux/fs.h>
  7 #include <linux/slab.h>
  8 #include <asm/uaccess.h>
  9 #include "mpu6050.h"
 10 
 11 
 12 #define DEVICE_MAJOR 665
 13 #define DEVICE_MINOR 0
 14 #define DEV_NUM      1
 15 #define DEVICE_NAME "mpu6050"
 16 
 17 struct mpu6050_device{
 18     struct cdev i2c_cdev;
 19     struct i2c_client *client;
 20 };
 21 
 22 struct mpu6050_device* mpu6050_device;
 23 struct class* cls;
 24 
 25 dev_t devno;
 26 int ret;
 27 
 28 MODULE_LICENSE("GPL");
 29 
 30 static const struct of_device_id i2c_dt_table[]={
 31 
 32     {
 33         .compatible="i2c,mpu6050",
 34     },
 35     {
 36     },
 37     
 38 };
 39 static const struct i2c_device_id i2c_id_table[]={
 40     {
 41         "mpu6050",0
 42     },
 43     {
 44     },
 45 
 46 };
 47 
 48 static int mpu6050_read_byte(struct i2c_client* client,char reg)
 49 {
 50 
 51     char txbuf[1]={reg};
 52     char rxbuf[1];
 53     struct i2c_msg msg[2]={
 54         {
 55             client->addr,0,1,txbuf
 56         
 57         },
 58         {
 59             client->addr,1,1,rxbuf
 60         }
 61 
 62     };
 63     ret=i2c_transfer(client->adapter,msg,ARRAY_SIZE(msg));
 64     if(ret<0)
 65     {  
 66         return -EFAULT;
 67     }
 68     return rxbuf[0];
 69 
 70 }
 71 
 72 static int mpu6050_write_byte(struct i2c_client* client,char reg,char val)
 73 {
 74 
 75     char txbuf[2]={reg,val};
 76     struct i2c_msg msg[1]={
 77         {
 78             client->addr,0,2,txbuf
 79         
 80         },
 81     };
 82     ret=i2c_transfer(client->adapter,msg,ARRAY_SIZE(msg));
 83     if(ret<0)
 84     {  
 85         printk("i2c_transfer failed!!\n");
 86         return -EFAULT;
 87     }
 88     return 0;
 89 }
 90 
 91 static int i2c_open(struct inode *inode,struct file* file)
 92 {
 93     return 0;
 94 }
 95 static int i2c_release(struct inode *inode,struct file* file)
 96 {
 97     return 0;
 98 }
 99 static long i2c_ioctl(struct file* file,unsigned int cmd,unsigned long arg)
100 {
101     union mpu6050_data data;
102     struct i2c_client *client=mpu6050_device->client;
103     switch (cmd)
104     {
105     case GET_ACCEL:
106         data.accel.x=mpu6050_read_byte(client,ACCEL_XOUT_L);
107         data.accel.x|=mpu6050_read_byte(client,ACCEL_XOUT_H)<<8;
108         data.accel.y=mpu6050_read_byte(client,ACCEL_YOUT_L);
109         data.accel.y|=mpu6050_read_byte(client,ACCEL_YOUT_H)<<8;
110         data.accel.z=mpu6050_read_byte(client,ACCEL_ZOUT_L);
111         data.accel.z|=mpu6050_read_byte(client,ACCEL_ZOUT_H)<<8;
112         break;
113     case GET_GYRO:
114         data.gyro.x=mpu6050_read_byte(client,GYRO_XOUT_L);
115         data.gyro.x|=mpu6050_read_byte(client,GYRO_XOUT_H)<<8;
116         data.gyro.y=mpu6050_read_byte(client,GYRO_YOUT_L);
117         data.gyro.y|=mpu6050_read_byte(client,GYRO_YOUT_H)<<8;
118         data.gyro.z=mpu6050_read_byte(client,GYRO_ZOUT_L);
119         data.gyro.z|=mpu6050_read_byte(client,GYRO_ZOUT_H)<<8;
120         break;
121     case GET_TEMP:
122         data.temp=mpu6050_read_byte(client,TEMP_OUT_L);
123         data.temp|=mpu6050_read_byte(client,TEMP_OUT_H)<<8;
124         break;
125     default :
126         printk("invalid argument\n");
127         return -EINVAL;
128 
129     }
130     
131     if(copy_to_user((void*)arg,&data,sizeof(data)))
132     {
133         return -EFAULT;
134     }
135     return sizeof(data);
136 }
137 
138 const struct file_operations fops={
139     .owner=THIS_MODULE,
140     .open=i2c_open,
141     .release=i2c_release,
142     .unlocked_ioctl=i2c_ioctl,
143 };
144 
145 
146 int i2c_probe(struct i2c_client* client,const struct i2c_device_id* device_id)
147 {
148     printk("i2c_mpu6050  probe!!!\n");
149     devno=MKDEV(DEVICE_MAJOR,DEVICE_MINOR);
150     mpu6050_device=kzalloc(sizeof(mpu6050_device),GFP_KERNEL);
151     if(mpu6050_device==NULL)
152     {
153         return -ENOMEM;
154     }
155     mpu6050_device->client=client;
156 
157     ret=register_chrdev_region(devno,DEV_NUM,DEVICE_NAME);
158     if(ret<0)
159     {
160         printk("register_chrdev_region failed!!!\n");
161         return -1;
162     }
163 
164     cls=class_create(THIS_MODULE,"I2C");
165     if(IS_ERR(cls))
166     {
167         printk("class:I2C create failed!!!\n");
168     }
169     printk("class:I2C create succeed!!!\n");
170     device_create(cls,NULL,devno,NULL,"MPU6050");
171     cdev_init(&mpu6050_device->i2c_cdev,&fops);
172     mpu6050_device->i2c_cdev.owner=THIS_MODULE;
173     printk("i2c_cdev init succeed!!!\n");
174     cdev_add(&mpu6050_device->i2c_cdev,devno,DEV_NUM);
175     printk("i2c_cdev add succeed!!!\n");
176 
177     mpu6050_write_byte(client,SMPLRT_DIV,0X07);
178     mpu6050_write_byte(client,CONFIG,0X06);
179     mpu6050_write_byte(client,GYRO_CONFIG,0XF8);
180     mpu6050_write_byte(client,ACCEL_CONFIG,0X19);
181     mpu6050_write_byte(client,PWR_MGMT_1,0X00);
182     mpu6050_write_byte(client,WHO_AM_I,0x68);
183 
184 
185     return 0;
186 }
187 int i2c_remove(struct i2c_client*client)
188 {
189 
190     device_destroy(cls,devno);
191     class_destroy(cls);
192     printk("class_destroy succeed!!!\n");
193     cdev_del(&mpu6050_device->i2c_cdev);
194     unregister_chrdev_region(devno,DEV_NUM);
195     
196     kfree(mpu6050_device);
197     return 0;
198 }
199 static struct i2c_driver i2c_dr ={
200 
201     .driver={
202             .name="mpu6050",
203             .of_match_table=i2c_dt_table,
204     },
205     .id_table=i2c_id_table,
206     .probe=i2c_probe,
207     .remove=i2c_remove,
208 };
209 
210 module_i2c_driver(i2c_dr);

复制代码

  test.c

复制代码

 1 #include <stdio.h>
 2 #include <sys/ioctl.h>
 3 #include <sys/types.h>
 4 #include <sys/stat.h>
 5 #include <fcntl.h>
 6 #include <unistd.h>
 7 
 8 #include "mpu6050.h"
 9 int main(int argc, const char *argv[])
10 {
11     int fd;
12     float temperature;
13 //    unsigned long data;
14     union mpu6050_data data;
15     int ret;
16     fd=open("/dev/MPU6050",O_RDWR);
17     if(fd<0)
18     {
19         perror("fail to open MPU6050");
20         return -1;
21     }
22     printf("open /dev/MPU6050 succeed!\n");
23     while(1)
24     {
25 
26         printf("****************data from mpu6050*************************\n");
27         printf("\n");
28 
29         ioctl(fd,GET_ACCEL,&data);    
30         printf("accel:x=%-5d,y=%-5d,z=%-5d\n",data.accel.x,data.accel.y,data.accel.z);
31         printf("\n");
32 
33         ioctl(fd,GET_GYRO,&data);    
34         printf("gyro :x=%-5d,y=%-5d,z=%-5d\n",data.gyro.x,data.gyro.y,data.gyro.z);
35         printf("\n");
36 
37         ioctl(fd,GET_TEMP,&data);
38         temperature=((float)data.temp)/340+36.53;
39         printf("temperature=%f\n",temperature);
40         printf("\n");
41         
42         sleep(1);
43 
44     }

复制代码

  实际采集到的数据:

 

 

参考文献:1.《深入Linux内核架构》 2.《Linux设备驱动开发详解》 

标签:I2C,linux3.14,29,dev,platform,IIC,device,pdev,i2c
来源: https://blog.51cto.com/u_15169172/2710545

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有