在内核模块中启用定时器,让LED1亮1s灭1s
实验效果
驱动程序
#include
#include
#include
#include
#include
#include
struct device_node *dnode;
struct gpio_desc *gpiono;
struct timer_list mytimer;
void mytimer_func(struct timer_list *timer)
{
gpiod_set_value(gpiono,!gpiod_get_value(gpiono));
mod_timer(timer,jiffies+HZ);
}
static int __init mycdev_init(void)
{
mytimer.expires=jiffies+HZ;
timer_setup(&mytimer,mytimer_func,0);
add_timer(&mytimer);
dnode=of_find_node_by_path("/myleds");
if(dnode==NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
gpiono=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono))
{
printk("gpio编号解析失败\n");
return -PTR_ERR(gpiono);
}
printk("解析gpio编号成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
gpiod_set_value(gpiono,0);
gpiod_put(gpiono);
del_timer(&mytimer);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
基于GPIO子系统完成LED驱动的注册,应用程序测试
实验效果
驱动程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
struct cdev *cdev;
unsigned int major = 500;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;
char kbuf[128] = {0};
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)
{
unsigned int aaa=MINOR(inode->i_rdev);
file->private_data=(void *)aaa;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which,ret;
ret = copy_from_user(&which,(void *)arg,4);
if(ret)
{
printk("Copy to user failed\n");
return -EIO;
}
printk("Copy to user successful\n");
switch(cmd)
{
case LED_ON:
switch (which)
{
case 1:
gpiod_set_value(gpiono1,1);
break;
case 2:
gpiod_set_value(gpiono2,1);
break;
case 3:
gpiod_set_value(gpiono3,1);
break;
default:
break;
}
break;
case LED_OFF:
switch(which)
{
case 1:
gpiod_set_value(gpiono1,0);
break;
case 2:
gpiod_set_value(gpiono2,0);
break;
case 3:
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int ret, i;
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret = -EFAULT;
goto out1;
}
printk("字符设备驱动对象申请成功\n");
cdev_init(cdev, &fops);
if (major > 0)
{
ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
if (ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else
{
ret = alloc_chrdev_region(&devno, minor, 3, "myled");
if (ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
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 out5;
}
}
printk("向上提交设备节点信息成功\n");
dnode = of_find_node_by_name(NULL, "myleds");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
}
printk("解析设备树节点成功\n");
gpiono1 = gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1))
{
printk("gpio编号解析失败\n");
return -PTR_ERR(gpiono1);
}
printk("解析gpio1编号成功gpiono1\n");
gpiono2 = gpiod_get_from_of_node(dnode,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2))
{
printk("gpio编号解析失败\n");
return -PTR_ERR(gpiono2);
}
printk("解析gpio2编号成功gpiono2\n");
gpiono3 = gpiod_get_from_of_node(dnode,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3))
{
printk("gpio编号解析失败\n");
return -PTR_ERR(gpiono3);
}
printk("解析gpio3编号成功gpiono3\n");
gpiod_set_value(gpiono1, 1);
gpiod_set_value(gpiono2, 1);
gpiod_set_value(gpiono3, 1);
return 0;
out5:
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major, minor), 3);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
int i;
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major, minor), 3);
kfree(cdev);
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include
#include
#include
#include
#include
#include
#include
#include
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)
void LED_control()
{
int a, b;
char buf[128] = {0};
int fd_led = open("/dev/myled0", O_RDWR);
if (fd_led < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请选择灯功能\n");
printf("0(关灯) 1(开灯)>");
scanf("%d", &a);
printf("请输入要控制的灯\n");
printf("1(LED1) 2(LED2) 3(LED3)>");
scanf("%d", &b);
if (a == 1)
{
ioctl(fd_led, LED_ON, &b);
}
else if (a == 0)
{
ioctl(fd_led, LED_OFF, &b);
}
}
close(fd_led);
}
int main(int argc, char const *argv[])
{
LED_control();
return 0;
}