嵌入式 Linux C ARM C语言&嵌入式嵌入式开发

聊聊设备节点

2017-11-25  本文已影响20人  SayidZhong
  • 怕什么真理无穷,进一寸有一寸的欢喜 ——胡适

设备节点

聊聊怎么通过 安卓用户空间 去和之间进行 内核沟通

我们通过一个例子去解释,设备节点的作用:

在user_space中去读取一个gpio(上拉 输入)的状态

static unsigned int GPIO_test;
signed int value_GPIO_test;


static ssize_t show_value_GPIO_test(struct device *dev, struct device_attribute *attr,
                                       char *buf)
{
       printk( " value_GPIO_test= %d\n", value_GPIO_test);
       return sprintf(buf, "%d\n", value_GPIO_test);
}

static ssize_t store_value_GPIO_test(struct device *dev, struct device_attribute *attr,
                                        const char *buf, size_t size)
{
       printk( " customeized  the  Write Function\n");
       return size;
}

static DEVICE_ATTR(value_GPIO_test, 0664, show_value_GPIO_test, store_value_GPIO_test);
/*使用DEVICE_ATTR,可以在 sys fs 中添加“文件”,通过修改该文件内容,可以实现在运行过程中动态控制device的目的*/

/*先看看DEVICE_ATTR的原型:
DEVICE_ATTR(_name, _mode, _show, _store)
_name:名称,也就是将在sys fs中生成的文件名称。
_mode:上述文件的访问权限,与普通文件相同,UGO的格式。
_show:显示函数,cat该文件时,此函数被调用。
_store:写函数,echo内容到该文件时,此函数被调用。

模式可以为只读0444,只写0222,或者读写都行的0666。当然也可以对User\Group\Other进行区别。
显示和写入函数就需要实现了。
*/

static int battery_probe(struct platform_device *dev)
{
    struct device_node *np;
    np = of_find_compatible_node(NULL, NULL, "mediatek,battery");
    GPIO_test=of_get_named_gpio(np, "test", 0);
    if (!np) {
        printk( "Failed to find device-tree node: test\n");
        return -ENODEV;
    }
    gpio_direction_input(GPIO_test);
    value_GPIO_test = gpio_get_value(GPIO_test);


ret_device_file = device_create_file(&(dev->dev), &dev_attr_value_GPIO_test);
  
  /*
  *利用device_create_file函数可以在/sys/下创建对应的属性文件,从而通过对该文件的读写实现特定的数据操作。

  *DEVICE_ATTR的功能就是定义一个device_attribute结构体对象。
  *device_create_file利用该对象在device下创建文件。
  */

/**
 * device_create_file - create sysfs attribute file for device.
 * @dev: device.
 * @attr: device attribute descriptor.
 */
int device_create_file(struct device *dev,
         const struct device_attribute *attr)
{
 int error = 0;
 if (dev)
  error = sysfs_create_file(&dev->kobj, &attr->attr);
 return error;
}

最后我们可以在上层去读取这个io的状态了

unsigned int test  = get_int_value(value_GPIO_test_PATH);//get方法在下面

#define value_GPIO_test_PATH  "/sys/devices/platform/battery/value_GPIO_test"


static int read_from_file(const char* path, char* buf, int size)
{
    if (!path) {    
        return 0;
    }
    int fd = open(path, O_RDONLY);
    if (fd == -1) {
        return 0;
    }
    int count = read(fd, buf, size);
    if (count > 0) {
        count = (count < size) ? count : size - 1;
        while (count > 0 && buf[count-1] == '\n') count--;
        buf[count] = '\0';
    } else {
        buf[0] = '\0';
    }
    close(fd);
    return count;
}
int get_int_value(const char * path)
{
        int size = 32;
    char buf[size];
    if(!read_from_file(path, buf, size))
        return 0;
    return atoi(buf);
};

上一篇下一篇

猜你喜欢

热点阅读