找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6317|回复: 1
打印 上一主题 下一主题
收起左侧

学习笔记 - 在驱动中最灵活的 I2C 设备侦测方法

[复制链接]
跳转到指定楼层
楼主
ID:140343 发表于 2016-9-24 22:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在工作过程中,意外发现一种很灵活的 I2C 设备侦测方式,特别有意思,此种方式可以实现无论 I2C 设备挂在哪一条 I2C 总线上,都能够轻松找到该 I2C 设备,利用这种方式可以做到智能检测设备,即使硬件更换 I2C 总线也不需要修改驱动程序。为了方便日后调试,写了一个小驱动程序,用于通过往设备节点写 I2C 设备地址来检测 I2C 设备

写完之后在百度搜索了一下,看是否还有其他的方式,还真有:http://blog.csdn.net/yuanlulu/article/details/6557901


#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <linux/miscdevice.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/sysctl.h>
#include <linux/input.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <asm/uaccess.h>
#include <linux/wakelock.h>
#include <linux/slab.h>
#define __ISDEBUG__
#include <linux/zhc_public.h>

#define LMX_I2C_DETECT_DEVICE_NAME "zhc_i2c_detect"


// 此处可以写多个
static unsigned short s_Normal_I2c[] = {0x35, I2C_CLIENT_END};    // 芯片地址

/*
    struct i2c_board_info {
    char        type[I2C_NAME_SIZE];
    unsigned short    flags;
    unsigned short    addr;
    void        *platform_data;
    struct dev_archdata    *archdata;
    struct device_node *of_node;
    struct acpi_dev_node acpi_node;
    int        irq;
    int comp_addr_count;
    struct i2c_comp_address *comp_addrs;
    unsigned long irq_flags;
    };
*/

/**************************** I2C 设备驱动框架 ****************************/
static int i2c_detect_Detect(struct i2c_client *client, struct i2c_board_info *info)
{
    struct i2c_adapter *adapter = client->adapter;

    // 如果 I2C 设备挂在主控的 I2C控制器1 上,那么 adapter->nr = 1
    dprintk("adapter->nr = %d I2C_device = 0x%x", adapter->nr, info->addr);
   
    return -ENODEV;   

}


static const struct i2c_device_id i2c_detect_id[] = {
    {LMX_I2C_DETECT_DEVICE_NAME, 0},
    {}
};


static struct i2c_driver i2c_detect_driver = {
    .class = I2C_CLASS_HWMON,
    .driver = {
            .owner = THIS_MODULE,
            .name = LMX_I2C_DETECT_DEVICE_NAME,
            },
    .detect = i2c_detect_Detect,   // 会往所有的 I2C 控制器上寻指定的 I2C 设备地址的 I2C 设备,若有 ACK 回应则会调用该函数
    .id_table = i2c_detect_id,
    .address_list = s_Normal_I2c,  // 地址列表
};


/**************************** I2C 设备驱动框架 ****************************/

/* 重加载 */
static int I2c_Detect_Reload(void)
{   
    iprintk();
   
    if(i2c_add_driver(&i2c_detect_driver)!= 0)
    {
        eprintk("i2c_add_driver() failed");
        return -2;
    }
   
    i2c_del_driver(&i2c_detect_driver);
   
    return 0;
}


static ssize_t I2c_Detect_store(struct class *class, struct class_attribute *attr, const char *buf, size_t count)
{
    unsigned long data = 0;
    int ret;

    strict_strtoul(buf, 16,  &data);
    iprintk("data = 0x%x", data);
   
    s_Normal_I2c[0] = data;
   
    I2c_Detect_Reload();
   
    return count;
}


static ssize_t I2c_Detect_show(struct class *class, struct class_attribute *attr,    char *buf)
{
    return 1;
}


// 提供两个接口函数给上层使用
static struct class_attribute I2c_Detect_class_attrs[] = {
    __ATTR(i2c_addr, 0777, I2c_Detect_show, I2c_Detect_store),
    __ATTR_NULL
};


static struct class I2c_Detect_class = {
    .name = "zhc_i2c_detect",
    .class_attrs = I2c_Detect_class_attrs,
};


static int __init i2c_detect_init(void)
{
    int ret = 0;   
    dprintk();

    // 创建设备节点
    class_register(&I2c_Detect_class);   
    return ret;
}


static void __exit i2c_detect_exit(void)
{
    dprintk();
    class_unregister(&I2c_Detect_class);
}


module_init(i2c_detect_init);
module_exit(i2c_detect_exit);

MODULE_AUTHOR("lovemengx");
MODULE_DESCRIPTION("This I2C Devices Detect Driver Use Debug I2C devices");
MODULE_LICENSE("GPL");


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:477092 发表于 2020-12-1 09:38 | 只看该作者
你好,你这个头文件怎么定义的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表