如果我不想使用 Raspberry 或 Arduino,我怎样才能访问 I2C 总线以与设备通信。我看到很多关于树莓派的例子和很多关于 Arduino 的例子,但没有一个使用下面显示的代码。下面的代码探测并使用 struct i2c_driver
。该代码还使用 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);
(显然)有一种方法可以使用 python 和串行端口(通过光隔离器)或并行端口来形成 I2C 总线 pyi2c
我不知道该库是否有效 - 但我认为它满足要求。每个 linux 主机至少有一个串口。
如果只有 USB 端口,则有 FTDI 和 Silicon Labs 制作的 USB-I2C/SPI/UART 桥接器,它们在最近的内核中具有驱动程序支持。您以后始终可以使用此策略添加 I2C 或 SPI 支持。
我是一名优秀的程序员,十分优秀!