搜索

18

主题

18

帖子

116

积分

注册会员

Rank: 2

积分
116
发表于 2020-7-31 10:37:36 4820 浏览 0 回复

在sys/class下创建访问节点

本帖最后由 xudr 于 2020-7-31 10:37 编辑

在驱动开发中我们经常要提供应用层或者debug的访问节点,内核中有一个接口class_register可以创建这种节点,下面是代码可供参考。

/*读取GPIO120 的状态*/

#define ACC           GPIO120
static int acc_status = 0;

static ssize_t acc_show(struct class *class, struct class_attribute *attr,char *buf)
{
      if(gpio_get_value(ACC) == 0)
        {
         acc_status = 1;
                  
        }
        else if(gpio_get_value(ACC) == 1)
        {
         acc_status = 0;   
        }   
    return(sprintf(buf, "%d\n", acc_status));
}

static struct class_attribute acc_class_attrs[] = {
    __ATTR(acc, 0664, acc_show, NULL),
    __ATTR_NULL,
};
static struct class acc_class = {
    .name = "acc",
    .owner = THIS_MODULE,
    .class_attrs = acc_class_attrs,
};
static int __init gpio_iacc_init(void)
{
    int ret = 0;
   ret = class_register(&acc_class);
    if (ret < 0) {
        printk("%s:Fail to creat test class file \n", __func__);
        return ret;
    }
   
    return ret ;
}

static void __exit gpio_acc_exit(void)
{   

   class_destroy(&acc_class);

}

module_init(gpio_iacc_init);
module_exit(gpio_acc_exit);


adb  cat sys/class/acc/acc  可查看到acc 的状态


回复

使用道具 举报

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

本版积分规则


登录或注册
快速回复 返回顶部 返回列表