1.效果图:
应用程序:
#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/ioctl.h>
//构建功能码
#define LED_ON _IOW('1',1,int)
#define LED_OFF _IOW('1',0,int)
int main(int argc, char const *argv[])
{
int a,b;
int fd=open("/dev/myled0", O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
return -1;
}
while(1)
{
printf("想要控制哪盏灯\n");
printf("1(LED1) 2(LED2) 3(LED3)\n");
printf("请输入>");
scanf("%d", &a);
printf("请输入要实现的功能\n");
printf("0(关灯) 1(开灯)\n");
printf("请输入>");
scanf(" %d", &b);
getchar();
switch (b)
{
case 0:
ioctl(fd, LED_OFF, a);
break;
case 1:
ioctl(fd, LED_ON, a);
break;
}
}
close(fd);
return 0;
}
源代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/of_gpio.h>
struct class *cls;
struct device *dev;
//构建功能码
#define LED_ON _IOW('1',1,int)
#define LED_OFF _IOW('1',0,int)
struct device_node *dnode_led;//保存解析到的设备树节点地址
struct gpio_desc *gpiono_led1;
struct gpio_desc *gpiono_led2;
struct gpio_desc *gpiono_led3;
int major;
char kbuf[128]=" ";
int myled_open(struct inode *inode, struct file *file)
{
printk("myled_open success\n");
return 0;
}
ssize_t myled_read(struct file * file, char __user * buf, size_t size, loff_t * lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
unsigned long ret = copy_to_user(kbuf, buf, size);
if(ret)
{
printk("copy_to_user error\n");
return -ret;
}
return 0;
}
long myled_ioctl(struct file * file, unsigned int cmd, unsigned long arg)
{
switch (arg)
{
case 1:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono_led1, 1);
break;
case LED_OFF:
gpiod_set_value(gpiono_led1, 0);
break;
}
break;
case 2:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono_led2, 1);
break;
case LED_OFF:
gpiod_set_value(gpiono_led2, 0);
break;
}
break;
case 3:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono_led3, 1);
break;
case LED_OFF:
gpiod_set_value(gpiono_led3, 0);
break;
}
break;
}
return 0;
}
struct file_operations fops={
.open=myled_open,
.read=myled_read,
.unlocked_ioctl=myled_ioctl,
};
static int __init mycdev_init(void)
{
major=register_chrdev(0, "mychrdev", &fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n",major);
dnode_led = of_find_node_by_path("/leds");
if(dnode_led == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
//向上提交目录
cls=class_create(THIS_MODULE,"mychrdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点信息
int i;//向上提交三次设备节点信息
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(dev);
}
}
printk("向上提交设备节点成功\n");
//***********************led1***********************
//解析LED1的gpio编号
gpiono_led1 = gpiod_get_from_of_node(dnode_led, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
if(gpiono_led1 == NULL)
{
printk("解析LED1的gpio编号失败\n");
return -ENXIO;
}
printk("解析LED1的gpio编号成功\n");
//***********************led2***********************
//解析LED2的gpio编号
gpiono_led2 = gpiod_get_from_of_node(dnode_led, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
if(gpiono_led2 == NULL)
{
printk("解析LED2的gpio编号失败\n");
return -ENXIO;
}
printk("解析LED2的gpio编号成功\n");
//***********************led3***********************
//解析LED3的gpio编号
gpiono_led3 = gpiod_get_from_of_node(dnode_led, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
if(gpiono_led3 == NULL)
{
printk("解析LED3的gpio编号失败\n");
return -ENXIO;
}
printk("解析LED3的gpio编号成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
gpiod_put(gpiono_led1);
gpiod_put(gpiono_led2);
gpiod_put(gpiono_led3);
//注销字符设备驱动
unregister_chrdev(major,"myled");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
2.效果图:
源代码:
#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;//保存解析到的中断节点地址
unsigned int irqno;
unsigned int irqno_key2;
unsigned int irqno_key3;
irqreturn_t myirq_handler(int irqno, void *dev)
{
printk("key1_irq\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler_key2(int irqno_key2, void *dev)
{
printk("key2_irq\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler_key3(int irqno_key3, void *dev)
{
printk("key3_irq\n");
return IRQ_HANDLED;
}
static int __init mycdev_init(void)
{
//1.解析按键的设备树节点
dnode = of_find_compatible_node(NULL, NULL, "hqyj,myirq");
if(NULL == dnode)
{
printk("设备树节点解析失败\n");
return -ENXIO;
}
printk("设备树节点解析成功\n");
//2.解析按键1的软中断号
irqno = irq_of_parse_and_map(dnode, 0);
if(irqno == 0)
{
printk("解析按键1的中断号失败\n");
return -ENXIO;
}
printk("解析按键1软中断号成功\n");
//3.注册按键1中断
int ret = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "key1", (void *)1);
if(ret < 0)
{
printk("注册按键1软中断号失败\n");
return -ret;
}
printk("注册按键1软中断号成功\n");
//解析按键2的软中断号
irqno_key2 = irq_of_parse_and_map(dnode, 1);
if(irqno_key2 == 0)
{
printk("解析按键2的中断号失败\n");
return -ENXIO;
}
printk("解析按键2软中断号成功\n");
//注册按键2中断
ret = request_irq(irqno_key2, myirq_handler_key2, IRQF_TRIGGER_FALLING, "key2", (void *)2);
if(ret < 0)
{
printk("注册按键2软中断号失败\n");
return -ret;
}
printk("注册按键2软中断号成功\n");
//解析按键3的软中断号
irqno_key3 = irq_of_parse_and_map(dnode, 2);
if(irqno_key3 == 0)
{
printk("解析按键3的中断号失败\n");
return -ENXIO;
}
printk("解析按键3软中断号成功\n");
//注册按键3中断
ret = request_irq(irqno_key3, myirq_handler_key3, IRQF_TRIGGER_FALLING, "key3", (void *)3);
if(ret < 0)
{
printk("注册按键3软中断号失败\n");
return -ret;
}
printk("注册按键3软中断号成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//销毁终端
free_irq(irqno, (void *)1);
free_irq(irqno_key2, (void *)2);
free_irq(irqno_key3, (void *)3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");