简体   繁体   English

与Linux中没有微控制器的I2C设备进行通讯

[英]Talking to a I2C device without a microcontroller in linux

If I do not want to use Raspberry or Arduino, how can I get access to a I2C bus to talk to a device. 如果我不想使用Raspberry或Arduino,如何访问I2C总线与设备进行通讯。 I see a lot of examples for raspberry and lots for Arduino but none of them use the code that is shown below. 我看到了很多树莓派的示例和Arduino的很多示例,但是它们都没有使用下面显示的代码。 The code below probes and uses struct i2c_driver . 下面的代码探查并使用struct i2c_driver The code also initializes in kernel with module_init : 该代码还使用module_init在内核中初始化:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/i2c-id.h>
#include <linux/videodev2.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/log2.h>

#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#include <media/v4l2-chip-ident.h>
#include <media/v4l2-subdev.h>
#include <media/soc_camera.h>

#include "adv7403_regs.h"

#define DRIVER_NAME "adv7403"

struct adv7403_state {
          struct v4l2_subdev subdev;
};

static __devinit int adv7403_probe(struct i2c_client *client,
                              const struct i2c_device_id *id)
{
          struct adv7403_state *state;
          int ret;

          /* Check if the adapter supports the needed features */
          if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
                    return -EIO;

          v4l_info(client, "chip found @ 0x%02x (%s)\n",
                              client->addr << 1, client->adapter->name);

          state = kzalloc(sizeof(struct adv7403_state), GFP_KERNEL);
          if (state == NULL) {
                    ret = -ENOMEM;
                    goto err;
          }
        else{
                    printk(KERN_ERR DRIVER_NAME ": Detected %d\n");
          }

err:
          printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret);
          return ret;
}


static __devexit int adv7403_remove(struct i2c_client *client)
{
          struct v4l2_subdev *sd = i2c_get_clientdata(client);

          v4l2_device_unregister_subdev(sd);
          return 0;
}

static const struct i2c_device_id adv7403_id[] = {
          {DRIVER_NAME, 0},
          {},
};

MODULE_DEVICE_TABLE(i2c, adv7403_id);

static struct i2c_driver adv7403_driver = {
          .driver = {
                    .owner          = THIS_MODULE,
                    .name          = DRIVER_NAME,
          },
        .probe                    = adv7403_probe,
        .remove                    = adv7403_remove,
          .id_table          = adv7403_id
};

static int __init adv7403_mod_init(void)
{
          printk(" ADV7403 Video Decoder Device Driver inserted to kernel \n");
          return i2c_add_driver(&adv7403_driver);
}

static void __exit adv7403_mod_exit(void)
{
        printk(" ADV7403 Video Decoder Device Driver removed from kernel \n");
          i2c_del_driver(&adv7403_driver);
}

module_init(adv7403_mod_init);
module_exit(adv7403_mod_exit);

There is (apparently) a way to use python and either a serial port (through optoisolators) or a parallel port to form a I2C bus pyi2c (显然)有一种方法可以使用python和一个串行端口(通过光隔离器)或一个并行端口来形成I2C总线pyi2c

I have no idea if the library works - however I think it satisfies the requirement. 我不知道图书馆是否可以工作-但是我认为它可以满足要求。 Every linux host has at least a serial port. 每个Linux主机至少都有一个串行端口。

If there are only USB ports, there are USB-I2C/SPI/UART bridges made by FTDI and Silicon Labs that have driver support in recent kernels. 如果只有USB端口,则有FTDI和Silicon Labs制作的USB-I2C / SPI / UART桥,这些桥在最新的内核中具有驱动程序支持。 You can always add I2C or SPI support later with this strategy. 以后总是可以使用此策略添加I2C或SPI支持。

You have to have registered I2C bus driver first to talk to the I2C clients. 您必须先注册I2C总线驱动程序才能与I2C客户端通信。

This is not a bus (adapter) driver but a chip (client) driver. 这不是总线(适配器)驱动程序,而是芯片(客户端)驱动程序。 Which when registered just calls a probe() function which checks if I2C client is present. 在注册时哪个仅调用probe()函数,该函数检查是否存在I2C客户端。

There is no other functionality in this driver eg callbacks registered with the kernel I2C layer, which are used to access HW registers on the chip, so what is the purpose of this driver? 该驱动程序中没有其他功能,例如在内核I2C层中注册的回调,这些回调用于访问芯片上的硬件寄存器,那么此驱动程序的作用是什么?

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

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