久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: 學習筆記 - 在驅動中最靈活的 I2C 設備偵測方法 [打印本頁]

作者: 51hei小林    時間: 2016-9-24 22:31
標題: 學習筆記 - 在驅動中最靈活的 I2C 設備偵測方法
在工作過程中,意外發現一種很靈活的 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");



作者: 201535030111    時間: 2020-12-1 09:38
你好,你這個頭文件怎么定義的




歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 黄色a网站| 性欧美bbw| 亚洲精品久久久久久久久 | 精品日韩在线观看 | 久久久精品一区二区 | 精品国产乱码一区二区三 | 日本a级大片 | 中文av网站| 国产91热爆ts人妖系列 | 国产一区二区福利 | 日韩不卡一区 | 欧美色婷婷 | 日本欧美久久久久免费播放网 | 成人国产精品视频 | 黄色片91 | 一道本av| 日本精品视频 | 一级免费毛片 | 色婷婷av一区二区 | 亚洲精品乱码久久久久久蜜桃91 | 成人免费视频国产免费麻豆 | 色婷婷香蕉在线一区二区 | 97免费在线 | 日日操夜夜爽 | 99精品欧美一区二区蜜桃免费 | 亚洲成人精品 | 成人免费毛片片v | 日本伊人久久 | 国产在线色 | 欧美激情第二页 | 福利视频网站 | 日韩精品成人 | 国产浮力第一页 | 中文字幕一区在线观看 | 日韩精品一区在线 | 午夜精品久久久久久 | 久久精品免费观看 | 91丨九色丨蝌蚪丨丝袜 | 日皮视频免费看 | 亚洲第一区视频 | av天天干|