1.应用程序发送指令控制LED亮灭
2.按键1 按下,led1电位反转 按键2按下,led2电位反转 按键3 按下,led3电位反转
应用层代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128] = {0};
printf("调用open\n");
int fd = open("/dev/myled0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
printf("请输入控制灯命令:第一位代表控制哪盏灯(1,2,3)第二位代表亮灭(1,0)");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
驱动层代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
// 中断
struct device_node *dev;
unsigned int irqno[3];
int ret;
// 点灯
struct device_node *dev_led;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
// 中断处理函数
irqreturn_t myirq_handler(int irq, void *dev)
{
if (irq == irqno[0])
{
printk("KEY1_INTERRUPT\n");
// 亮灯
gpiod_set_value(gpiono1, !gpiod_get_value(gpiono1));
return IRQ_HANDLED;
}
else if (irq == irqno[1])
{
printk("KEY2_INTERRUPT\n");
// 亮灯
gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2));
return IRQ_HANDLED;
}
else
{
printk("KEY3_INTERRUPT\n");
// 亮灯
gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3));
return IRQ_HANDLED;
}
}
char kbuf[128]={0};
struct class *cls;
struct device *dev1;
unsigned int major;
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)
{
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 *lof)
{
long ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size>sizeof(kbuf))
{
size=sizeof(kbuf);
}
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user failed\n");
return -EIO;
}
//判断用户输入的数据
if(kbuf[0]=='1')
{
if(kbuf[1]=='0')//关灯
{
//关灯逻辑
gpiod_set_value(gpiono1, 0);
}
else if(kbuf[1]=='1')
{
//开灯逻辑
gpiod_set_value(gpiono1, 1);
}
}
else if(kbuf[0]=='2')
{
if(kbuf[1]=='0')//关灯
{
//关灯逻辑
gpiod_set_value(gpiono2, 0);
}
else if(kbuf[1]=='1')
{
//开灯逻辑
gpiod_set_value(gpiono2, 1);
}
}
else if(kbuf[0]=='3')
{
if(kbuf[1]=='0')//关灯
{
//关灯逻辑
gpiod_set_value(gpiono3, 0);
}
else if(kbuf[1]=='1')
{
//开灯逻辑
gpiod_set_value(gpiono3, 1);
}
}
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,
.release=mycdev_close,
.read=mycdev_read,
.write=mycdev_write,
};
static int __init mycdev_init(void)
{
int i;
major=register_chrdev(0,"mychrdev",&fops);
if(major<0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功\n");
//向上提交目录
cls=class_create(THIS_MODULE,"myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备结点信息
for(i=0;i<3;i++)
{
dev1=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev1))
{
printk("向上提交设备结点信息失败\n");
return -PTR_ERR(dev1);
}
}
printk("向上提交设备结点信息成功\n");
// 解析设备树节点
dev = of_find_node_by_path("/myirq");
if (dev == NULL)
{
printk("解析设备节点失败\n");
return -EFAULT;
}
printk("解析中断成功\n");
// 根据设备树节点的路径解析设备树信息
dev_led = of_find_node_by_path("/leds");
if (dev == NULL)
{
printk("解析设备树节点失败\n");
return -EFAULT;
}
printk("解析LED设备树节点成功\n");
// 解析中断的软中断号
for (i = 0; i < 3; i++)
{
irqno[i] = irq_of_parse_and_map(dev, i);
if (!irqno[i])
{
printk("获取软中断号失败\n");
return -ENXIO;
}
printk("获取中断号成功 irqno[%d]=%d\n", i, irqno[i]);
// 注册中断
ret = request_irq(irqno[i], myirq_handler, IRQF_TRIGGER_FALLING, "key", NULL);
if (ret)
{
printk("注册中断失败\n");
return ret;
}
printk("注册中断成功\n");
}
// 根据解析得到的设备树节点指针解析出LED1的gpio编号
gpiono1 = gpiod_get_from_of_node(dev_led, "led1-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("解析LED1失败\n");
return -PTR_ERR(gpiono1);
}
printk("申请LED1成功\n");
// 根据解析得到的设备树节点指针解析出LED2的gpio编号
gpiono2 = gpiod_get_from_of_node(dev_led, "led2-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("解析LED2失败\n");
return -PTR_ERR(gpiono2);
}
printk("申请LED2成功\n");
// 根据解析得到的设备树节点指针解析出LED3的gpio编号
gpiono3 = gpiod_get_from_of_node(dev_led, "led3-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("解析LED3失败\n");
return -PTR_ERR(gpiono3);
}
printk("申请LED3成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
//销毁结点信息
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录信息
class_destroy(cls);
unregister_chrdev(major,"mychrdev");
// 注销中断
for (i = 0; i < 3; i++)
{
free_irq(irqno[i], NULL);
}
// 灭灯
gpiod_set_value(gpiono1, 0);
// 释放gpio编号
gpiod_put(gpiono1);
// 灭灯
gpiod_set_value(gpiono2, 0);
// 释放gpio编号
gpiod_put(gpiono2);
// 灭灯
gpiod_set_value(gpiono3, 0);
// 释放gpio编号
gpiod_put(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");