我实现的功能,执行一次应用程序led灯 亮,再次执行应用程序led灯灭。
内核模块:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/wait.h>
#include <linux/device.h>
#include <linux/poll.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
unsigned int major;
char kbuf[128] = {0};
struct class *cls;
struct device *dev;// 设备树
int gpiono;
struct device_node *dnode;
// 新版设备树
struct gpio_desc *gpionn;
int mycdev_open(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *iof)
{int ret;// 向用户拷贝if (size > sizeof(kbuf))size = sizeof(kbuf);ret = copy_to_user(ubuf, kbuf, size);if (ret){printk("copy to user filed\n");return -EIO;}printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *iof)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);int ret;// 从用户拷贝if (size > sizeof(kbuf))size = sizeof(kbuf);ret = copy_from_user(kbuf, ubuf, size);if (ret){printk("copy from user filed\n");return -EIO;}// 开灯// 新版设备树// gpiod_set_value(gpionn,1);// 取反led灯状态gpio_set_value(gpiono, !gpio_get_value(gpiono));return 0;
}int mycdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {.open = mycdev_open,.read = mycdev_read,.write = mycdev_write,.release = mycdev_close,
};
static int __init mycdev_init(void)
{// 字符设备驱动的注册major = register_chrdev(0, "mycdev", &fops);if (major < 0){printk("字符设备驱动注册失败\n");return major;}printk("字符设备驱动注册成功major=%d\n", major);// 向上提交目录cls = class_create(THIS_MODULE, "mycdev");if (IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录成功\n");// 向上提交设备节点信息dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");if (IS_ERR(dev)){printk("向上提交设备节点失败\n");return -PTR_ERR(dev);}printk("向上提交设备节点成功\n");// 解析设备树节点dnode = of_find_node_by_name(NULL, "myleds");if (dnode == NULL){printk("解析设备树节点失败\n");return -1;}printk("解析设备树节点成功\n");// 根据设备树节点解析出gpio编号gpiono = of_get_named_gpio(dnode, "led1", 0);if (gpiono < 0){printk("解析设备号失败\n");return -1;}printk("解析设备号%d成功\n", gpiono);// 设置管脚为输出gpio_direction_output(gpiono, 0);// 新版设备树// 根据设备树节点解析出gpio编号并申请/*gpionn=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);if(IS_ERR(gpionn)){printk("申请设备号失败\n");return -1;}*/return 0;
}
static void __exit mycdev_exit(void)
{// 灭灯gpio_set_value(gpiono, 0);// 新版设备树// gpiod_set_value(gpiono, 0);// 释放gpio编号gpio_free(gpiono);// 新版设备树// gpiod_put(gpionn);// 销毁设备节点信息device_destroy(cls, MKDEV(major, 0));// 销毁目录空间class_destroy(cls);// 字符设备驱动的注销unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层程序:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>int main(int argc, char const *argv[])
{char buf[128] = "hello world";int fd = open("/dev/mycdev", O_RDWR);if (fd < 0){printf("设备文件打开失败\n");exit(-1);}write(fd, buf, sizeof(buf));return 0;
}
效果图: