I2C设备linux驱动程序 [英] I2C device linux driver

查看:96
本文介绍了I2C设备linux驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为i2c设备制作具有打开,关闭,ioctl等功能的字符设备?最近两周我一直在寻找有关它的信息,但找不到任何有效的方法.我在Essential Linux设备驱动程序中找到了一些信息,但是它是针对2.6内核编写的,我使用3.4.79(我正尝试在cubian distr上为cubieboard2编写此驱动程序),因此本书有许多不推荐使用的功能,我试图在那里写我的驱动程序,但仍然不起作用(在我尝试使用cat字符设备时,请给我内核错误).谁能给我解释一下该怎么做,或者至少给我一个有效的例子.

所以,有我的代码: http://pastebin.com/T7PBTpym

我只是尝试使i2c总线正常工作,但是在我的示波器上我看不到i2c线路上的任何内容,而编译时却没有错误.我也找不到如何将设备驱动程序附加到i2c总线号.因为我的cubieboard2上有4条i2c总线,例如,如何将设备驱动程序连接到i2c-1总线?

解决方案

谢谢.几个小时前我已经找到了想要的东西.因此,正如我所看到的,有三种类型的驱动程序.应该将其中一个插入内核,并且您需要重建内核才能使用它们.也可以使用设备属性通过sysfs使用某些驱动程序(它们出现在/sys/bus/i2c/driver/your_driver/中).第三,我的类型-看起来像字符设备的驱动程序.实际上,您可以将它们组合在一起.

因此,如果要使用最后一种类型的设备,将很难找到正确的示例,因为几乎所有示例都专用于前两种类型的驱动程序.无论如何,如果要创建字符设备,则需要从file_operations结构描述功能.但是所有功能,例如i2c_transferi2c_smbus_read_byte.等(完整列表)需要struct i2c_adapterstruct i2c_client.还有两个问题,如何到达那里的结构,以及如何将驱动程序连接到适当的i2c总线(例如i2c-2)?

因此,此处中没有描述的功能:i2c_get_adapter.作为参数,您需要传递i2c总线号.它返回到i2c_adapter结构的链接.您可以使用i2c_new_dummy函数获得的i2c_client结构,将i2c_adapter和从属地址作为参数传递给它.

之后,您可以使用i2c_transferi2c_smbus_read_byte等功能.最后,您可以描述file_operations结构功能,并释放代表i2c设备的驱动程序,而无需使用sysfs和重建内核. /p>

结果代码如下:

u8 ret; 
struct i2c_client * my_client; 
struct i2c_adapter * my_adap = i2c_get_adapter(1); // 1 means i2c-1 bus
my_client = i2c_new_dummy (my_adap, 0x69); // 0x69 - slave address on i2c bus
i2c_smbus_write_byte(my_client, 0x0f); 
ret = i2c_smbus_read_byte(my_client);

您可以直接在用于file_operations结构的函数中使用此代码.

希望此信息对像我这样的初学者有用.

How to make a character device for i2c device, with open, close, ioctl etc. functions? I was looking for information about it last two weeks and couldn't find anything working. I found some information in Essential Linux Device Drivers, but it was written for 2.6 kernel, and i use 3.4.79 (i'm trying to write this driver for cubieboard2 on cubian distr) so this book has many deprecated functions, i tried to write my driver like there, but it still don't work (give me kernel errors while i'm truing to cat character device). Can anyone explain me what to do with it, or at least give me a working example.

so, there is my code: http://pastebin.com/T7PBTpym

i just try to get i2c bus works, but on my oscilloscope i don't see anything on i2c lines, while i get no errors when i compile it. Also i can't find how to attach device driver to i2c bus number. because i have 4 i2c buses on my cubieboard2, and how to attach device driver to i2c-1 bus, for example?

解决方案

Thank you, guys. I've found what I want a couple hours ago. So as I see there are three types of drivers. One is supposed to be inserted into the kernel, and you need to rebuild your kernel to use them. There are also drivers that you can use through the sysfs using device attributes (they appear in /sys/bus/i2c/driver/your_driver/). And, third - my type - drivers that look like character devices. Actually, you can combine them.

So, if you want to use last type of devices it'll be a bit difficult to find correct examples, because almost all examples are devoted to the first two types of drivers. Anyway, if you want to create a character device, you need to describe functions from the file_operations structure. But all functions like i2c_transfer, i2c_smbus_read_byte. Etc. (full list) required eitherstruct i2c_adapter or struct i2c_client. And there two questions, how to get there structures, and how to attach driver to the appropriate i2c bus, such as i2c-2?

So there is a function that is not described here: i2c_get_adapter. As a parameter, you need to pass the i2c bus number. It returns a link to a i2c_adapter structure. The i2c_client structure you can get with i2c_new_dummy function passing i2c_adapter and slave address to it as parameters.

After that you can use functions such as i2c_transfer, i2c_smbus_read_byte etc. In the end you can describe file_operations structure functions, and release driver, representing your i2c device, without using sysfs and rebuilding your kernel.

Result code looks like:

u8 ret; 
struct i2c_client * my_client; 
struct i2c_adapter * my_adap = i2c_get_adapter(1); // 1 means i2c-1 bus
my_client = i2c_new_dummy (my_adap, 0x69); // 0x69 - slave address on i2c bus
i2c_smbus_write_byte(my_client, 0x0f); 
ret = i2c_smbus_read_byte(my_client);

You can use this code directly in your functions for the file_operations structure.

Hope this info will be useful for beginners like me.

这篇关于I2C设备linux驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆