驱动13.i2c设备驱动程序
1 分析i2c设备的识别过程
i2c_add_driver
i2c_register_driver
driver->driver.bus = &i2c_bus_type;
driver_register(&driver->driver);
list_for_each_entry(adapter, &adapters, list) {
driver->attach_adapter(adapter);
i2c_probe(adapter, &addr_data, eeprom_detect);
i2c_probe_address // 发出S信号,发出设备地址(来自addr_data)
i2c_smbus_xfer
i2c_smbus_xfer_emulated
i2c_transfer
adap->algo->master_xfer // s3c24xx_i2c_xfer
2 怎么写I2C设备驱动程序?
2.1 分配一个i2c_driver结构体
2.2 设置
attach_adapter // 它直接调用 i2c_probe(adap, 设备地址, 发现这个设备后要调用的函数);
detach_client // 卸载这个驱动后,如果之前发现能够支持的设备,则调用它来清理
2.3 注册:i2c_add_driver
3 写代码
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <asm/uaccess.h> static unsigned short ignore[] = { I2C_CLIENT_END };
static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; /* 地址值是7位 */
/* 改为0x60的话, 由于不存在设备地址为0x60的设备, 所以at24cxx_detect不被调用 */ static unsigned short force_addr[] = {ANY_I2C_BUS, 0x60, I2C_CLIENT_END};
static unsigned short * forces[] = {force_addr, NULL}; static struct i2c_client_address_data addr_data = {
.normal_i2c = normal_addr, /* 要发出S信号和设备地址并得到ACK信号,才能确定存在这个设备 */
.probe = ignore,
.ignore = ignore,
//.forces = forces, /* 强制认为存在这个设备 */
}; static struct i2c_driver at24cxx_driver; static int major;
static struct class *cls;
struct i2c_client *at24cxx_client; static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset)
{
unsigned char address;
unsigned char data;
struct i2c_msg msg[];
int ret; /* address = buf[0]
* data = buf[1]
*/
if (size != )
return -EINVAL; copy_from_user(&address, buf, ); /* 数据传输三要素: 源,目的,长度 */ /* 读AT24CXX时,要先把要读的存储空间的地址发给它 */
msg[].addr = at24cxx_client->addr; /* 目的 */
msg[].buf = &address; /* 源 */
msg[].len = ; /* 地址=1 byte */
msg[].flags = ; /* 表示写 */ /* 然后启动读操作 */
msg[].addr = at24cxx_client->addr; /* 源 */
msg[].buf = &data; /* 目的 */
msg[].len = ; /* 数据=1 byte */
msg[].flags = I2C_M_RD; /* 表示读 */ ret = i2c_transfer(at24cxx_client->adapter, msg, );
if (ret == )
{
copy_to_user(buf, &data, );
return ;
}
else
return -EIO;
} static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
unsigned char val[];
struct i2c_msg msg[];
int ret; /* address = buf[0]
* data = buf[1]
*/
if (size != )
return -EINVAL; copy_from_user(val, buf, ); /* 数据传输三要素: 源,目的,长度 */
msg[].addr = at24cxx_client->addr; /* 目的 */
msg[].buf = val; /* 源 */
msg[].len = ; /* 地址+数据=2 byte */
msg[].flags = ; /* 表示写 */ ret = i2c_transfer(at24cxx_client->adapter, msg, );
if (ret == )
return ;
else
return -EIO;
} static struct file_operations at24cxx_fops = {
.owner = THIS_MODULE,
.read = at24cxx_read,
.write = at24cxx_write,
}; static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind)
{
printk("at24cxx_detect\n"); /* 构构一个i2c_client结构体: 以后收改数据时会用到它 */
at24cxx_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
at24cxx_client->addr = address;
at24cxx_client->adapter = adapter;
at24cxx_client->driver = &at24cxx_driver;
strcpy(at24cxx_client->name, "at24cxx");
i2c_attach_client(at24cxx_client); major = register_chrdev(, "at24cxx", &at24cxx_fops); cls = class_create(THIS_MODULE, "at24cxx");
class_device_create(cls, NULL, MKDEV(major, ), NULL, "at24cxx"); /* /dev/at24cxx */ return ;
} static int at24cxx_attach(struct i2c_adapter *adapter)
{
return i2c_probe(adapter, &addr_data, at24cxx_detect);
} static int at24cxx_detach(struct i2c_client *client)
{
printk("at24cxx_detach\n");
class_device_destroy(cls, MKDEV(major, ));
class_destroy(cls);
unregister_chrdev(major, "at24cxx"); i2c_detach_client(client);
kfree(i2c_get_clientdata(client)); return ;
} /* 1. 分配一个i2c_driver结构体 */
/* 2. 设置i2c_driver结构体 */
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = "at24cxx",
},
.attach_adapter = at24cxx_attach,
.detach_client = at24cxx_detach,
}; static int at24cxx_init(void)
{
i2c_add_driver(&at24cxx_driver);
return ;
} static void at24cxx_exit(void)
{
i2c_del_driver(&at24cxx_driver);
} module_init(at24cxx_init);
module_exit(at24cxx_exit); MODULE_LICENSE("GPL");
i2c设备驱动程序
驱动13.i2c设备驱动程序的更多相关文章
- 驱动05.lcd设备驱动程序
参考s3c2410fb.c总结出框架 1.代码分析 1.1 入口函数 int __devinit s3c2410fb_init(void) { return platform_driver_regis ...
- linux设备驱动程序-i2c(0)-i2c设备驱动源码实现
(基于4.14内核版本) 为了梳理清楚linux内核中的i2c实现框架,从本文开始,博主将分几个章节分别解析i2c总线在linux内核中的形成过程.匹配过程.以及设备驱动程序源码实现. 在介绍linu ...
- linux设备驱动程序-i2c(1):i2c总线的添加与实现
linux设备驱动程序-i2c(1):i2c总线的添加与实现 (基于4.14内核版本) 在上一章节linux设备驱动程序-i2c(0)-i2c设备驱动源码实现中,我们演示了i2c设备驱动程序的源码实现 ...
- linux设备驱动程序--串行通信驱动框架分析
linux 串行通信接口驱动框架 在学习linux内核驱动时,不论是看linux相关的书籍,又或者是直接看linux的源码,总是能在linux中看到各种各样的框架,linux内核极其庞杂,linux各 ...
- Linux驱动之I2C总线设备以及驱动
[ 导读] 本文通过阅读内核代码,来梳理一下I2C子系统的整体视图.在开发I2C设备驱动程序时,往往缺乏对于系统整体的认识,导致没有一个清晰的思路.所以从高层级来分析一下I2C系统的设计思路,将有助于 ...
- 乾坤合一~Linux设备驱动之I2C核心、总线以及设备驱动
我思念的城市已是黄昏 为何我总对你一往情深 曾经给我快乐 也给我创伤 曾经给我希望 也给我绝望 我在遥远的城市 陌生的人群 感觉着你遥远的忧伤 我的幻想 你的忧伤,像我的的绝望,那样漫长,,,,,这是 ...
- Linux 驱动框架---i2c驱动框架
i2c驱动在Linux通过一个周的学习后发现i2c总线的驱动框架还是和Linux整体的驱动框架是相同的,思想并不特殊比较复杂的内容如i2c核心的内容都是内核驱动框架实现完成的,今天我们暂时只分析驱动开 ...
- linux驱动之I2C
include/linux/i2c.h struct i2c_msg;struct i2c_algorithm;struct i2c_adapter;struct i2c_client;struct ...
- Linux I2C设备驱动编写(二)
在(一)中简述了Linux I2C子系统的三个主要成员i2c_adapter.i2c_driver.i2c_client.三者的关系也在上一节进行了描述.应该已经算是对Linux I2C子系统有了初步 ...
随机推荐
- python queue - 同步队列类
参考 官网 queue 模块 queue 模块实现多生产者,多消费者队列. 当必须在 ==多个线程之间安全地交换信息== 时,它在线程编程中特别有用. 此模块中的Queue类实现了所有必需的锁定语义. ...
- Windows10 关闭自动更新
win+R调出运行窗口: 输入services.msc,查找 跳出服务窗口,点击windows update设置禁用即可 Windows Update Medic Service没办法禁用,需要采用其 ...
- HashMap存储原理
1. HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. ...
- 用session模拟登陆,手动输入验证码
# 本练习是模拟登陆及验证码处理(把验证码下载到本地后手动输入) # 1 通过分析页面获得form表单的登陆接口为 action="https://www.douban.com/accoun ...
- LeetCode(282) Peeking Iterator
题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...
- Linux学习-服务器硬件数据的收集
以系统内建 dmidecode 解析硬件配备 系统有个名为 dmidecode 的软件,它可以解析 CPU 型号.主板型号与内存相 关的型号等等~ [root@study ~]# dmidecode ...
- 持续化集成Jenkins的系统配置
最近在研究selenium2自动化测试,用到持续化集成jenkins.由于之前仅限于使用,而没有真正动手配置过,所以现在学习从零开始,搭建持续化集成,故而有了这篇博客. 先介绍一下项目持续集成测试,这 ...
- P2598 [ZJOI2009]狼和羊的故事(最小割)
P2598 [ZJOI2009]狼和羊的故事 题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么 ...
- django_orm操作
查询操作和性能优化 1.基本操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 增 models.Tb1.object ...
- Html开发小结
html部分 1.html标签 标签不区分大小写. 如:<!doctype html>与<!DOCTYPE html > <div></div>与< ...