1、在内核模块中启用定时器,定时1s,让led1 闪烁
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>
struct device_node* dnode;
struct gpio_desc* gpiono;
// 定义定时器对象
struct timer_list mytimer;
unsigned int state = 0;
// 定义定时器处理函数
void mytimer_func(struct timer_list* timer)
{
if (state == 0)
state = 1;
else
state = 0;
gpiod_set_value(gpiono, state);
mod_timer(timer, jiffies + HZ);
}
static int __init mycdev_init(void)
{
// 解析led灯的设备树节点
dnode = of_find_node_by_path("/myleds");
if (dnode == NULL) {
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功 \n");
// 根据设备树节点解析led1gpio编号
gpiono = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono)) {
printk("gpio编号解析失败\n");
return -PTR_ERR(gpiono);
}
// 初始化定时器对象
mytimer.expires = jiffies + HZ;
timer_setup(&mytimer, mytimer_func, 0);
add_timer(&mytimer);
return 0;
}
static void __exit mycdev_exit(void)
{
del_timer(&mytimer);
// gpio_free(gpiono);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
2、基于gpio子系统完成led灯驱动的注册,应用程序测试文章来源:https://www.toymoban.com/news/detail-554112.html
#include "head.h"
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
struct cdev* cdev;
struct class* cls;
struct device* dev;
dev_t devno;
unsigned int major = 500;
unsigned int minor = 0;
struct device_node* dnode;
struct gpio_desc* gpiono[10];
int mycdev_open(struct inode* inode, struct file* file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
int mycdev_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case LED_ON: // 开灯
switch (arg) {
case 1:
gpiono[0] = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono[0])) {
printk("申请gpio失败\n");
return -PTR_ERR(gpiono[0]);
}
gpiod_set_value(gpiono[0], 1);
break;
case 2:
gpiono[1] = gpiod_get_from_of_node(dnode, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono[1])) {
printk("申请gpio失败\n");
return -PTR_ERR(gpiono[1]);
}
gpiod_set_value(gpiono[1], 1);
break;
case 3:
gpiono[2] = gpiod_get_from_of_node(dnode, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono[2])) {
printk("申请gpio失败\n");
return -PTR_ERR(gpiono[2]);
}
gpiod_set_value(gpiono[2], 1);
break;
default:
break;
}
break;
case LED_OFF: // 关灯
switch (arg) {
case 1:
gpiod_set_value(gpiono[0], 0);
break;
case 2:
gpiod_set_value(gpiono[1], 0);
break;
case 3:
gpiod_set_value(gpiono[3], 0);
break;
default:
break;
}
break;
default:
break;
}
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;
// 1、分配字符设备驱动空间
cdev = cdev_alloc();
if (cdev == NULL) {
printk("申请字符设备驱动对象空间失败\n");
ret = -EFAULT;
goto out1;
}
// 2、字符设备驱动对象部分初始化
cdev_init(cdev, &fops);
// 3、申请设备号
if (major > 0) {
ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
if (ret) {
printk("静态指定设备号失败\n");
}
} else {
ret = alloc_chrdev_region(&devno, minor, 3, "myled");
if (ret) {
printk("动态申请设备号失败\n");
goto out2;
}
major = MAJOR(devno); // 根据设备号得到主设备号
minor = MINOR(devno); // 根据设备号得到次设备号
}
printk("申请设备号成功\n");
// 4、注册字符设备驱动对象
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret) {
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
// 5、向上提交目录
cls = (class_create(THIS_MODULE, "myled"));
if (IS_ERR(cls)) {
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
// 6、向上提交设备结点
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");
}
// 解析led灯的设备树节点
dnode = of_find_node_by_path("/myleds");
if (dnode == NULL) {
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功");
return 0;
out5:
for (--i; i >= 0; i++) {
// 撤销上面提交的设备信息
device_destroy(cls, MKDEV(major, i));
}
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major, minor), 3);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
// 灭灯
gpiod_set_value(gpiono[0], 0);
gpiod_set_value(gpiono[1], 0);
gpiod_set_value(gpiono[2], 0);
// 注销gpio信息
gpiod_put(gpiono[0]);
gpiod_put(gpiono[1]);
gpiod_put(gpiono[2]);
// 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");
应用层代码:文章来源地址https://www.toymoban.com/news/detail-554112.html
#include "head.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const* argv[])
{
int n, f, fd;
char buf[128] = { 0 };
// 打开LED
fd = open("/dev/myled0", O_RDWR);
if (fd < 0) {
printf("打开设备文件失败\n");
exit(-1);
}
while (1) {
printf("请选择要控制的LED灯\n");
printf("1(LED1) 2(LED2) 3(LED3)");
scanf("%d", &n);
printf("请选择要实现的功能\n");
printf("0(关灯) 1(开灯)");
scanf("%d", &f);
switch (n) {
case 1:
if (f == 1) {
ioctl(fd, LED_ON, n);
} else if (f == 0) {
ioctl(fd, LED_OFF, n);
}
break;
case 2:
if (f == 1) {
ioctl(fd, LED_ON, n);
} else if (f == 0) {
ioctl(fd, LED_OFF, n);
}
break;
case 3:
if (f == 1) {
ioctl(fd, LED_ON, n);
} else if (f == 0) {
ioctl(fd, LED_OFF, n);
}
break;
default:
break;
}
}
close(fd);
return 0;
}
到了这里,关于驱动开发—day8的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!