简体   繁体   English

如何在kernel空间中使用i2c驱动

[英]how to use i2c driver in kernel space

I have wrote a character driver to control my embedded hardware with my application, In my driver there is a feature to send a command to an I2C device which is connected to my embedded device.我编写了一个字符驱动程序来使用我的应用程序控制我的嵌入式硬件,在我的驱动程序中有一个功能可以向连接到我的嵌入式设备的 I2C 设备发送命令。

in command line I am able to send the following code to my device:在命令行中,我可以将以下代码发送到我的设备:

i2cset  -y 0 0x2c 0x00 0x05

I want to do same thing in kernel space within my driver, but I did not find a sample, all I got was in userspace, how can I do that in kernel space?我想在我的驱动程序中的 kernel 空间中做同样的事情,但我没有找到样本,我得到的只是在用户空间中,我怎么能在 kernel 空间中做到这一点?

edit:编辑:

I know that with " i2c_master_send " or " i2c_smbus_read_byte " function I can send data to i2c devices, bu this function gets a structure called " i2c_client ", I don't know how should I suppose to fill this structure to send data.我知道使用“ i2c_master_send ”或“ i2c_smbus_read_byte ”function 我可以将数据发送到 i2c 设备,但是这个 function 得到一个名为“ i2c_client ”的结构,我不知道应该如何发送这个结构来填充数据。 It might be really silly but I could not figure it out how can I fill this structure.这可能真的很傻,但我不知道如何填充这个结构。

I have reached my goal with following code in my driver:我在驱动程序中使用以下代码达到了我的目标:

****Please read my edit before using this code***** ****请在使用此代码之前阅读我的编辑*****

#define VOL1_CHIP_ADDRESS 0x2c
#define VOL1_DATA_ADDRESS 0

struct i2c_client *clientstr;
struct file * fp;

...

mm_segment_t oldfs;

oldfs = get_fs();
set_fs(KERNEL_DS);
fp = filp_open("/dev/i2c-0",O_RDWR,0);
clientstr = (struct i2c_client *)fp->private_data;
clientstr->addr=VOL1_CHIP_ADDRESS;
i2c_smbus_write_byte_data(clientstr,VOL1_DATA_ADDRESS,0x05);
filp_close(fp,NULL);
set_fs(oldfs);

EDIT:编辑:

So I did what I wanted in kernel, opening a file in kernel space regardless of any suggestions not to do so.所以我在 kernel 中做了我想做的事,在 kernel 空间中打开一个文件,不管有什么建议不要这样做。

But I needed to open a file in kernel module...但我需要在 kernel 模块中打开一个文件...

So why every body said don't open user space files in kernel?那么为什么每个人都说不要在 kernel 中打开用户空间文件呢? In these two articles there are some reasons to not using files in kernel, this link and this link .在这两篇文章中,有一些原因不使用 kernel、此链接此链接中的文件。

there are some reasons like: 1- kernel module may lose CPU at any time and the file which is opened by kernel may close.有一些原因,如: 1- kernel 模块可能随时失去 CPU 并且由 kernel 打开的文件可能会关闭。

2- I am not sure really about this, But they said files needs to have a process to stay opened but kernel module itself is not a process(maybe I am wrong.). 2-我不确定这一点,但他们说文件需要有一个进程才能保持打开状态,但 kernel 模块本身不是一个进程(也许我错了。)。

3- If any error happens while working with files(open/close/read/write), kernel module can not handle it and causes kernel panic... 3- 如果在处理文件时发生任何错误(打开/关闭/读/写),kernel 模块无法处理它并导致 kernel 恐慌...

I have experienced a lot of kernel panics only while opening and closing the file I wanted to work with.只有在打开和关闭我想要使用的文件时,我才经历过很多 kernel 恐慌。 These are some of reasons why you should not use files in kernel modules, So as every body said before "If you need to use files in kernel module you probably did something wrong in your code!"这些是您不应该在 kernel 模块中使用文件的一些原因,因此正如每个人之前所说的“如果您需要在 kernel 模块中使用文件,您可能在代码中做错了!”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM