当前位置:网站首页>IIC bus realizes client device
IIC bus realizes client device
2022-07-05 22:10:00 【wwwlyj123321】
One 、Linux I2C Introduction to drive frame
I2C Bus and platform Bus is similar , It's just platform Bus is a virtual bus , and I2C The bus actually exists .
Linux Medium l2C It is also designed according to the platform bus model , Since it is also designed according to the platform bus model , Is it also divided into one device And a driver Well ? however I2C there device Don't cry device, But is called client.
among I2C Bus driver is SOC Of I2C Controller drive , Generally speaking, it is SOC The manufacturer has achieved good . and I2C The device driver is the user according to their own different devices actualized . The drive of each device is different , But the writing process is one . Let's analyze it in detail .
Two 、 Device side code writing
1、 Do not use device tree files
When you start writing I2C When the device is driven , First, add device information . First, let's take a look at not using the device tree , When using platform files , How to add I2C Equipment information . Pass in the platform file i2c_board_info Structure to describe a specific I2C equipment .
struct i2c_board_info
{
char type[I2C_NAME_SIZE]; /* I2C Device name */
unsigned short flags; /* sign */
unsigned short addr; /* I2C Device address */
void *platform_data;
struct dev_archdata *archdata;
struct device_node *of_node;
struct fwnode_handle *fwnode;
int irq;
};static struct i2c_board_info mx27_3ds_i2c_camera =
{
I2C_BOARD_INFO("ov2640", 0x30),
};Example :
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
struct i2c adapter*i2c_ada;
// Allocate one I2C_client The pointer
struct i2c_cllent*i2c_client;
// Supported by I2C Device list for
struct i2c board_info ft5x06_info[]=
{
// Each item represents a I2C equipment , This sentence means that the name of this device is ft5×06_test, The device address is 0×38
{I2C_BOARD_INFO("ft5×06_test",0x38)},
}
static int ft5x06_client_init(void)
{
// call i2c_get_adapter To obtain a I2C Bus . because t5x06 It is attached to I2C2 On ,
// So this parameter is 1, from 0 Numbered starting
i2c_ada=i2c_get_adapter(1);
// hold I2C CLIENT and I2C Devices are associated
i2c_client=i2c_new_device(i2c_ada,ft5x06_info);
i2c_put_adapter(i2c_ada);// Release I2q
return e;
}
static void ft5x06_client_exit(void)
{
i2c_unregister_device(i2c_client);
}
module_init(ft5x06_client_init);
module_exit(ft5x06_client_exit);
MODULE_LICENSE("GPL");

2、 Use the device tree file
After loading the device tree , stay sys/bus/i2c/devices You can see it under the directory iic Equipment

In the device tree reg It's settings IIC The address of the device , therefore ,edt-ft5x06 The device address of is 0x38

3、 ... and 、driver Side code writing
The basic flow :
- i2c_driver Structure initialization
- After the initialization is completed, the i2c_driver Register into the kernel ,i2c_add_driver
- When the device and driver match successfully , Will execute probe function ,probe Function is to execute a set of processes driven by character devices .
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
static const struct i2c_device_id ft5x06_id_ts[]={
{"xxx",0},
{}
};
static const struct of_device_id ft5x06_id[]=
{
{.compatible="edt,edt-ft5306",0,},
{.compatible="edt,edt-ft5x06",0,},
{.compatible="edt,edt-ft5406",0,},
{}
};
static struct i2c_driver ft5x06_driver={
.driver={
.owner=THIS_MODULE,
.name="ft5×06 test",
.of_match_table=ft5×06_id,// and device Match to use
},
.probe=ft5x06_probe,
.remove=ft5x06_remove,
.idtable=ft5x06_id_ts// It is also used for matching , Usually use of_match_table.
};
int(*probe)(struct l2c_client *i2c_cllent, const struct 12c_device_id * id)
{
// You can register one here misc Equipment or char Equipment
}
static int ft5x06_driver_init(void)
{
int ret;
ret=i2c_add_driver(&ft5x06_driver);
if(ret<0){
printk("i2c_add_ driver is error\n");
return ret;
}
}
static void ft5x06_driver_exit(void)
{
i2c_del_driver(&ft5xo6_driver);
}
module_init(ft5x06_driver_init);
module_exit(ft5x06_driver_exit);
MODULE_LICENSE("GPL"); 边栏推荐
- Blocking protocol for concurrency control
- Promql demo service
- Performance monitoring of database tuning solutions
- Performance testing of software testing
- Matlab draws a cute fat doll
- boundary IoU 的计算方式
- 大约SQL现场“这包括”与“包括在”字符串的写法
- Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
- Overview of concurrency control
- 数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
猜你喜欢

笔记本电脑蓝牙怎么用来连接耳机

CA certificate trampled pit

Deeply convinced plan X - network protocol basic DNS

Cobaltstrike builds an intranet tunnel

Overview of concurrency control

Reptile practice

What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method

装饰器学习01

元宇宙中的三大“派系”

数据泄露怎么办?'华生·K'7招消灭安全威胁
随机推荐
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
如何開發引入小程序插件
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
ICMP introduction
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
数据泄露怎么办?'华生·K'7招消灭安全威胁
Oracle advanced query
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
CRM creates its own custom report based on fetch
The real situation of programmers
Poj3414 extensive search
Storage optimization of performance tuning methodology
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Microservice link risk analysis
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
让开发效率提升的跨端方案
The American Championship is about to start. Are you ready?
Performance monitoring of database tuning solutions