1.使用GPIO子系统编写LED灯驱动,应用程序测试
2.注册三个按键的中断,只需要写内核代码
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>
#include"head.h"
int main(int argc, char const *argv[])
{
int a,b;
int fd=open("/dev/myled0",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
//从终端读取
printf("请输入要实现的功能\n");
printf("0(关灯) 1(开灯)\n");
scanf("%d",&b);
printf("请输入>");
scanf("%d",&a);
printf("请选择LED1(1,LED2(2),LED3(3)\n");
scanf("%d",&b);
switch(a)
{
case 1:
ioctl(fd,LED_ON,&b);
break;
case 0:
ioctl(fd,LED_OFF,&b);
break;
}
}
close(fd);
return 0;
}
头文件:
#ifndef __HEAD_H__
#define __HEAD_H__
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
驱动代码:
#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>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include "head.h"
#include <linux/device.h>
struct class *cls;
struct device *dev;
struct device_node *dnone;
struct property *pr;
struct gpio_desc *gpio_node1;
struct gpio_desc *gpio_node2;
struct gpio_desc *gpio_node3;
struct timer_list timer;
int length;
int count;
u32 irqon;
u32 major;
char buf[128]={0};
ssize_t my_read (struct file *file, char *kbuf, size_t size, loff_t *loff)
{
unsigned long ret;
if(size>sizeof(buf)){
size=sizeof(buf);
}
ret=copy_to_user(kbuf,buf,size);
if(ret){
printk("读取失败\n");
return ret;
}
return 0;
}
ssize_t my_write (struct file *file, const char *kbuf, size_t size, loff_t *loff)
{
unsigned long ret;
if(size>sizeof(buf)){
size=sizeof(buf);
}
ret=copy_from_user(buf,kbuf,size);
if(ret){
printk("写入失败\n");
return ret;
}
return 0;
}
int my_open (struct inode *inode, struct file *file)
{
return 0;
}
int my_release (struct inode *inode, struct file *file)
{
return 0;
}
long my_ioctl (struct file *file, unsigned int cmd, unsigned long arg)
{
int wh;
int ret=copy_from_user(&wh,(void *)arg,4);
if(ret<0){
printk("copy_from_user error\n");
return ret;
}
switch (cmd)
{
case LED_ON:
switch (wh)
{
case 1:
gpiod_set_value(gpio_node1,1);
break;
case 2:
gpiod_set_value(gpio_node2,1);
break;
case 3:
gpiod_set_value(gpio_node3,1);
break;
}
break;
case LED_OFF:
switch (wh)
{
case 1:
gpiod_set_value(gpio_node1,0);
break;
case 2:
gpiod_set_value(gpio_node2,0);
break;
case 3:
gpiod_set_value(gpio_node3,0);
break;
}
break;
}
return 0;
}
struct file_operations fops={
.read=my_read,
.write=my_write,
.open=my_open,
.release=my_release,
.unlocked_ioctl=my_ioctl,
};
static int __init mycdev_init(void)
{
major=register_chrdev(0,"myled",&fops);
if(major<0){
printk("申请设备号失败\n");
return -EIO;
}
printk("申请设备号成功 major=%d\n",major);
cls=class_create(THIS_MODULE,"myled");
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");
dnone=of_find_node_by_path("/leds");
if(dnone<0){
printk("error\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
gpio_node1=gpiod_get_from_of_node(dnone,"led-gpios",0,GPIOD_OUT_LOW,NULL);
if(gpio_node1<0){
printk("led1获取gpio失败\n");
return -1;
}
gpio_node2=gpiod_get_from_of_node(dnone,"led-gpios",1,GPIOD_OUT_LOW,NULL);
if(gpio_node2<0){
printk("led2获取gpio失败\n");
return -1;
}
gpio_node3=gpiod_get_from_of_node(dnone,"led-gpios",2,GPIOD_OUT_LOW,NULL);
if(gpio_node3<0){
printk("led3获取gpio失败\n");
return -1;
}
gpiod_set_value(gpio_node1,1);
return 0;
}
static void __exit mycdev_exit(void)
{
gpiod_put(gpio_node1);
gpiod_put(gpio_node2);
gpiod_put(gpio_node3);
int i;
for(i=0;i<3;i++){
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
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 irqno2;
unsigned int irqno3;
unsigned int gpiono;
unsigned int gpiono2;
unsigned int gpiono3;
//中断处理函数
irqreturn_t myirq_handler(int irq , void *dev)
{
printk("key1_intc\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler2(int irq , void *dev)
{
printk("key2_intc\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler3(int irq , void *dev)
{
printk("key3_intc\n");
return IRQ_HANDLED;
}
static int __init mycdev_init(void)
{
//解析按键的设备树节点
dnode=of_find_compatible_node(NULL,NULL,"hqyj,myirq");
if(dnode==NULL)
{
printk("解析按键设备树节点失败\n");
return -ENXIO;
}
printk("解析按键设备树节点成功\n");
//解析按键1的软中断号
irqno=irq_of_parse_and_map(dnode,0);
if(!irqno)
{
printk("解析软中断号失败\n");
return -ENXIO;
}
printk("解析软中断号成功%d\n",irqno);
//解析按键2的软中断号
irqno2=irq_of_parse_and_map(dnode,1);
if(!irqno)
{
printk("解析软中断号失败\n");
return -ENXIO;
}
printk("解析软中断号成功%d\n",irqno2);
//解析按键3的软中断号
irqno3=irq_of_parse_and_map(dnode,2);
if(!irqno)
{
printk("解析软中断号失败\n");
return -ENXIO;
}
printk("解析软中断号成功%d\n",irqno3);
//注册按键1中断
int ret=request_irq(irqno,myirq_handler,IRQF_TRIGGER_FALLING,"key1",(void *)1);
if(ret)
{
printk("中断注册失败\n");
return ret;
}
printk("中断注册成功\n");
//注册按键2中断
int ret2=request_irq(irqno2,myirq_handler2,IRQF_TRIGGER_FALLING,"key2",(void *)2);
if(ret2)
{
printk("中断注册失败\n");
return ret;
}
printk("中断注册成功\n");
//注册按键1中断
int ret3=request_irq(irqno3,myirq_handler3,IRQF_TRIGGER_FALLING,"key3",(void *)3);
if(ret3)
{
printk("中断注册失败\n");
return ret;
}
printk("中断注册成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
free_irq(irqno,(void *)1);
free_irq(irqno2,(void *)2);
free_irq(irqno3,(void *)3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");