1.使用GPIO子系统,编写LED驱动,应用程序测试
mychrdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
int major; // 保存申请的主设备号
char kbuf[128]={0};struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct device_node *dnode;//保存解析到的设备树节点地址
// 封装常用的操作方法
int mychrdev_open(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);return 0;
}
ssize_t mychrdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *lof)
{printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);unsigned int ret;ret=copy_to_user(ubuf,kbuf,size);if(ret){printk("copy_to_user err\n");return -ret;}return 0;
}
ssize_t mychrdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *lof)
{//ubuf就是应用程序中write函数第二个参数传过来的值//size就是应用程序中write函数第三个参数传过来的值printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);unsigned int ret;ret=copy_from_user(kbuf,ubuf,size);if(ret){printk("copy_from_user err\n");return -ret;}if(kbuf[0]=='0')//关灯{gpiod_set_value(gpiono1,0);//输出低电平gpiod_set_value(gpiono2,0);//输出低电平gpiod_set_value(gpiono3,0);//输出低电平}else if(kbuf[0]=='1')//开led1灯{gpiod_set_value(gpiono1,1);//输出高电平}else if(kbuf[0]=='2')//开led2灯{gpiod_set_value(gpiono2,1);//输出高电平}else if(kbuf[0]=='3')//开led3灯{gpiod_set_value(gpiono3,1);//输出高电平}return 0;
}
int mychrdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);return 0;
}
struct file_operations fops = {.open=mychrdev_open,.release=mychrdev_close,.read=mychrdev_read,.write=mychrdev_write,
};
static int __init mycdev_init(void)
{// 注册字符设备驱动major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("注册字符设备驱动失败%d\n", __LINE__);}printk("注册字符设备驱动成功 major=%d\n", major);//解析LED灯设备树节点dnode=of_find_node_by_path("/leds");if(dnode==NULL){printk("解析设备树节点失败\n");return -ENXIO;}printk("解析设备树节点成功\n");//解析LED1的gpio编号gpiono1=gpiod_get_from_of_node(dnode,"led1-gpio",0, GPIOD_OUT_LOW,NULL);if(gpiono1==NULL){printk("解析gpio编号失败\n");return -ENXIO;}printk("解析gpio编号成功\n");gpiono2=gpiod_get_from_of_node(dnode,"led2-gpio",0, GPIOD_OUT_LOW,NULL);if(gpiono2==NULL){printk("解析gpio编号失败\n");return -ENXIO;}printk("解析gpio编号成功\n");gpiono3=gpiod_get_from_of_node(dnode,"led3-gpio",0, GPIOD_OUT_LOW,NULL);if(gpiono3==NULL){printk("解析gpio编号失败\n");return -ENXIO;}printk("解析gpio编号成功\n");return 0;
}
static void __exit mycdev_exit(void)
{//释放gpio编号gpiod_put(gpiono1);gpiod_put(gpiono2);gpiod_put(gpiono3);// 注销字符设备驱动unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
led.c
#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] = {};int fd = open("/dev/mychrdev", O_RDWR);if (fd < 0){printf("打开设备文件失败\n");return -1;}while (1){printf("请输入控制码:1(led1开灯) 2(led2开灯) 3(led3开灯) 0(关灯)>");fgets(buf, sizeof(buf), stdin); // 从终端输入一个数据buf[strlen(buf) - 1] = '\0';write(fd, buf, sizeof(buf));}close(fd);return 0;
}
2.注册三个按键的中断,只需要写内核代码
myled.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
/*myirq{compatible = "hqyj,myirq";interrupt-parent = <&gpiof>;interrupts=<9 0>,<7 0>,<8 0>;
};
*/struct device_node *dnode_led; // 保存解析到的led设备树节点地址
struct device_node *dnode_int; // 保存解析到的中断设备树节点地址
unsigned int irqno1; // 按键1软中断号
unsigned int irqno2; // 按键2软中断号
unsigned int irqno3; // 按键3软中断号
struct gpio_desc *gpiono1; // led1设备号
struct gpio_desc *gpiono2; // led2设备号
struct gpio_desc *gpiono3; // led3设备号// 中断处理函数1
irqreturn_t myirq_handler1(int irq, void *dev)
{printk("key1_intc\n");//关灯三 开灯一gpiod_set_value(gpiono3, 0);gpiod_set_value(gpiono1, 1);return IRQ_HANDLED;
}
// 中断处理函数2
irqreturn_t myirq_handler2(int irq, void *dev)
{printk("key2_intc\n");//关灯一 开灯二gpiod_set_value(gpiono1, 0);gpiod_set_value(gpiono2, 1);return IRQ_HANDLED;
}
// 中断处理函数3
irqreturn_t myirq_handler3(int irq, void *dev)
{printk("key3_intc\n");//关灯二 开灯三gpiod_set_value(gpiono2, 0);gpiod_set_value(gpiono3, 1);return IRQ_HANDLED;
}static int __init mycdev_init(void)
{// 解析按键的设备树节点dnode_int = of_find_compatible_node(NULL, NULL, "hqyj,myirq");if (dnode_int == NULL){printk("解析设备树节点失败\n");return -ENXIO;}printk("解析设备树节点成功\n");// 解析LED灯设备树节点dnode_led = of_find_node_by_path("/leds");if (dnode_led == NULL){printk("解析设备树节点失败\n");return -ENXIO;}printk("解析设备树节点成功\n");// 解析按键1的软中断号irqno1 = irq_of_parse_and_map(dnode_int, 0);if (!irqno1){printk("解析按键1软中断号失败\n");return -ENXIO;}printk("解析按键1软中断号成功%d\n", irqno1);// 解析按键2的软中断号irqno2 = irq_of_parse_and_map(dnode_int, 1);if (!irqno2){printk("解析按键2软中断号失败\n");return -ENXIO;}printk("解析按键2软中断号成功%d\n", irqno2);// 解析按键3的软中断号irqno3 = irq_of_parse_and_map(dnode_int, 2);if (!irqno3){printk("解析按键3软中断号失败\n");return -ENXIO;}printk("解析按键3软中断号成功%d\n", irqno3);// 注册按键1中断int ret1 = request_irq(irqno1, myirq_handler1, IRQF_TRIGGER_FALLING, "key1", (void *)1);if (ret1){printk("中断注册失败\n");return ret1;}printk("中断注册成功\n");// 注册按键2中断int ret2 = request_irq(irqno2, myirq_handler2, IRQF_TRIGGER_FALLING, "key2", (void *)2);if (ret2){printk("中断注册失败\n");return ret2;}printk("中断注册成功\n");// 注册按键3中断int ret3 = request_irq(irqno3, myirq_handler3, IRQF_TRIGGER_FALLING, "key3", (void *)3);if (ret3){printk("中断注册失败\n");return ret3;}printk("中断注册成功\n");// 解析LED1的gpio编号gpiono1 = gpiod_get_from_of_node(dnode_led, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);if (gpiono1 == NULL){printk("解析gpio编号失败\n");return -ENXIO;}printk("解析gpio编号成功\n");// 解析LED2的gpio编号gpiono2 = gpiod_get_from_of_node(dnode_led, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);if (gpiono2 == NULL){printk("解析gpio编号失败\n");return -ENXIO;}printk("解析gpio编号成功\n");// 解析LED3的gpio编号gpiono3 = gpiod_get_from_of_node(dnode_led, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);if (gpiono3 == NULL){printk("解析gpio编号失败\n");return -ENXIO;}printk("解析gpio编号成功\n");return 0;
}
static void __exit mycdev_exit(void)
{// 释放软中断号free_irq(irqno1, (void *)1);free_irq(irqno2, (void *)2);free_irq(irqno3, (void *)3);// 释放gpio编号gpiod_put(gpiono1);gpiod_put(gpiono2);gpiod_put(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");