驱动程序
#include <linux/init.h>
#include <linux/of.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/io.h>
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct device_node *dnode;
struct class *cls;
struct device *dev;
int major;
char kbuf[128] = {0};
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 *lof)
{
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
// 从用户空间读取数据
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_to_user filed\n");
return ret;
}
// 开灯
switch(kbuf[0])
{
case '1':
switch(kbuf[1])
{
case '1':
gpiod_set_value(gpiono1, 1);
break;
case '2':
gpiod_set_value(gpiono2, 1);
break;
case '3':
gpiod_set_value(gpiono3, 1);
break;
}
break;
//关灯
case '0':
switch(kbuf[1])
{
case '1':
gpiod_set_value(gpiono1, 0);
break;
case '2':
gpiod_set_value(gpiono2, 0);
break;
case '3':
gpiod_set_value(gpiono3, 0);
break;
}
break;
}
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,"myleds",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n",major);
//向上提交目录
cls=class_create(THIS_MODULE,"myleds");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点信息
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myleds");
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 -ENOMEM;
}
printk("解析设备树节点成功\n");
gpiono1 = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("led3解析设备号失败\n");
return -PTR_ERR(gpiono1);
}
// 申请gpio编号
printk("led1申请编号成功\n");
gpiono2 = gpiod_get_from_of_node(dnode, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("led3解析设备号失败\n");
return -PTR_ERR(gpiono2);
}
// 申请gpio编号
printk("led2申请编号成功\n");
gpiono3 = gpiod_get_from_of_node(dnode, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("led3解析设备号失败\n");
return -PTR_ERR(gpiono3);
}
// 申请gpio编号
printk("led3申请编号成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
// 灭灯
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
// 释放gpio编号
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
//销毁设备节点信息
device_destroy(cls,MKDEV(major,0));
//销毁目录空间
class_destroy(cls);
//注销字符设备驱动
unregister_chrdev(major,"myleds");
}
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 <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
char buf[128]={0};
int fd=open("/dev/myleds",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
//从终端读取
printf("请输入两个字符\n");
printf("第一个字符:1(开灯) 0(关灯)\n");
printf("第二个字符:1(LED1) 2(LED2) 3(LED3)\n");
printf("请输入>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
//向设备文件中写
write(fd,buf,sizeof(buf));
}
return 0;
}