1使用GPIO子系统编写LED灯驱动,应用程序测试
应用程序
#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");
printf("请输入>");
scanf("%d", &a);
switch (a)
{
case 1:
ioctl(fd, LED_ON);
break;
case 0:
ioctl(fd, LED_OFF);
break;
}
}
close(fd);
return 0;
}
驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include "head.h"
struct cdev *cdev;
char kbuf[128] = {0};
unsigned int major = 0; // 主设备号
unsigned int minor = 0; // 次设备号
dev_t devno;
struct class *cls;
struct device *dev;
// 保存解析到的设备树节点地址
struct device_node *dnode;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
// 封装操作方法
// 定义操作方法对象并初始化
int mycdev_open(struct inode *inode, struct file *file)
{
// 获取打开文件的设备号
dev_t d = inode->i_rdev;
// 保存次设备号
file->private_data = (void *)MINOR(d);
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__);
unsigned long ret;
// 向用户空间读取拷贝
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret) // 拷贝失败
{
printk("copy_to_user filed\n");
return ret;
}
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;
}
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
// 获取次设备号
unsigned int mon = (unsigned int)file->private_data;
switch (mon)
{
case 0:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono1, 1);
break;
case LED_OFF:
gpiod_set_value(gpiono1, 0);
break;
}
break;
case 1:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono2, 1);
break;
case LED_OFF:
gpiod_set_value(gpiono2, 0);
break;
}
break;
case 2:
switch (cmd)
{
case LED_ON:
gpiod_set_value(gpiono3, 1);
break;
case LED_OFF:
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int ret;
// 1.申请字符设备驱动对象空间
cdev = cdev_alloc();
if (cdev == NULL)
{
return -EFAULT;
}
printk("字符设备驱动对象申请成功\n");
// 2.初始化字符设备驱动对象
cdev_init(cdev, &fops);
// 3.申请设备号
if (major == 0) // 动态申请
{
ret = alloc_chrdev_region(&devno, minor, 3, "myled");
if (ret)
{
printk("动态申请设备号失败\n");
goto A;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
else // 静态指定
{
ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
if (ret)
{
printk("静态申请设备号失败\n");
goto A;
}
}
printk("设备号申请成功\n");
// 4.注册驱动
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("注册驱动失败\n");
goto B;
}
printk("注册驱动成功\n");
// 5.向上提交目录
cls = class_create(THIS_MODULE, "led");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto C;
}
printk("向上提交目录成功\n");
// 6.向上提交设备节点
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");
ret = -PTR_ERR(dev);
goto D;
}
}
printk("向上提交设备信息成功\n");
dnode = of_find_node_by_path("/leds");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
gpiono1 = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
if (gpiono1 == NULL)
{
printk("解析gpio编号失败\n");
return -ENXIO;
}
gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
if (gpiono1 == NULL)
{
printk("解析gpio编号失败\n");
return -ENXIO;
}
gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
if (gpiono1 == NULL)
{
printk("解析gpio编号失败\n");
return -ENXIO;
}
printk("解析gpio编号成功\n");
return 0;
D:
// 销毁提交成功的设备信息
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
// 销毁目录
class_destroy(cls);
C:
cdev_del(cdev);
B:
unregister_chrdev_region(MKDEV(major, minor), 3);
A:
kfree(cdev);
return ret;
}
static void __exit mycdev_exit(void)
{
gpiod_set_value(gpiono1, 0);
gpiod_put(gpiono1);
gpiod_set_value(gpiono2, 0);
gpiod_put(gpiono2);
gpiod_set_value(gpiono3, 0);
gpiod_put(gpiono3);
// 1.释放设备信息
int i;
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 2.销毁目录
class_destroy(cls);
// 3.注销驱动对象
cdev_del(cdev);
// 4.释放设备号
unregister_chrdev_region(MKDEV(major, minor), 3);
// 5.释放对象空间
kfree(cdev);
}
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>
// 保存解析到的设备树节点地址
struct device_node *dnode;
unsigned int irqno1;
unsigned int irqno2;
unsigned int irqno3;
irqreturn_t myirq_handler1(int irq, void *dev)
{
printk("按下按键1\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler2(int irq, void *dev)
{
printk("按下按键2\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler3(int irq, void *dev)
{
printk("按下按键3\n");
return IRQ_HANDLED;
}
static int __init mycdev_init(void)
{
dnode = of_find_node_by_path("/myirq");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
// 解析按键1的软中断号
irqno1 = irq_of_parse_and_map(dnode, 0);
irqno2 = irq_of_parse_and_map(dnode, 1);
irqno3 = irq_of_parse_and_map(dnode, 2);
if (!irqno1)
{
printk("解析软中断号1失败\n");
}
if (!irqno2)
{
printk("解析软中断号2失败\n");
}
if (!irqno3)
{
printk("解析软中断号3失败\n");
}
printk("解析软中断号1成功irqno1=%d\n", irqno1);
printk("解析软中断号2成功irqno2=%d\n", irqno2);
printk("解析软中断号3成功irqno3=%d\n", irqno3);
// 注册按键1中断
int ret1 = request_irq(irqno1, myirq_handler1, IRQF_TRIGGER_FALLING, "key1", (void *)1);
int ret2 = request_irq(irqno2, myirq_handler2, IRQF_TRIGGER_FALLING, "key2", (void *)2);
int ret3 = request_irq(irqno3, myirq_handler3, IRQF_TRIGGER_FALLING, "key3", (void *)3);
if (ret1)
{
printk("按键1中断注册失败\n");
}
printk("按键1中断注册成功\n");
if (ret2)
{
printk("按键2中断注册失败\n");
}
printk("按键2中断注册成功\n");
if (ret3)
{
printk("按键3中断注册失败\n");
}
printk("按键3中断注册成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
free_irq(irqno1, (void *)1);
free_irq(irqno2, (void *)2);
free_irq(irqno3, (void *)3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");