本文主要是介绍【驱动开发】注册字符设备使用gpio设备树节点控制led三盏灯的亮灭,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
注册字符设备使用gpio设备树节点控制led三盏灯的亮灭
设备树:
头文件:
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct
{unsigned int MODER;unsigned int OTYPER;unsigned int OSPEEDR;unsigned int PUPDR;unsigned int IDR;unsigned int ODR;
} gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28
// 构建开灯关灯的功能码
#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/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/kdev_t.h>
#include "head.h"
struct class *cls;
struct device *dev;
struct resource *res;
unsigned int irqno;
struct device_node *dnode;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
int mycdev_open(struct inode *inode, struct file *file)
{unsigned int min = MINOR(inode->i_rdev); // 获取打开的文件的次设备号file->private_data = (void *)min;printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
ssize_t mycdev_read(struct file *file, char __user *user, size_t size, loff_t *lof)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
ssize_t mycdev_write(struct file *file, const char __user *user, size_t size, loff_t *lof)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{int min = (int)file->private_data; // 获取文件的次设备号switch (min){case 0: // LED1switch (cmd){case LED_ON: // 开灯gpiod_set_value(gpiono1, 1);break;case LED_OFF: // 关灯gpiod_set_value(gpiono1, 0);break;}break;case 1: // LED2switch (cmd){case LED_ON: // 开灯gpiod_set_value(gpiono2, 1);break;case LED_OFF: // 关灯gpiod_set_value(gpiono2, 0);break;}break;case 2: // LED3switch (cmd){case LED_ON: // 开灯gpiod_set_value(gpiono3, 1);break;case LED_OFF: // 关灯gpiod_set_value(gpiono3, 0);break;}break;}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;
}
struct file_operations fops = {.open = mycdev_open,.read = mycdev_read,.write = mycdev_write,.release = mycdev_close,.unlocked_ioctl = mycdev_ioctl,
};
int pdrv_probe(struct platform_device *pdev) // 当驱动和设备匹配成功后执行
{int ret,// 1.实例化字符设备驱动对象cdev = cdev_alloc();if (cdev == NULL){printk("申请字符设备驱动对象空间失败\n");ret = -EFAULT;goto OUT1;}printk("申请字符设备驱动对象空间成功\n");// 2.部分初始化字符设备驱动对象cdev_init(cdev, &fops);// 3.申请设备号if (major == 0) // 动态申请设备号{ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");if (ret){printk("动态申请设备号失败\n");goto OUT2;}major = MAJOR(devno);minor = MINOR(devno);}else // 静态指定设备号{ret = register_chrdev_region(devno, 3, "mycdev");if (ret){printk("静态设备号失败\n");goto OUT2;}}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, "mycdev");if (IS_ERR(cls)){printk("向上提交目录失败\n");ret = -PTR_ERR(cls);goto OUT4;}printk("向上提交目录成功\n");// 向上提交设备节点int i;for (i = 0; i < 3; i++){dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);if (IS_ERR(dev)){printk("向上提交设备信息失败\n");ret = PTR_ERR(dev);goto OUT5;}}printk("自动创建设备节点成功\n");res = platform_get_resource(pdev, IORESOURCE_MEM, 0);if (res == NULL){printk("解析资源失败%d\n", __LINE__);return -ENOMEM;}irqno = platform_get_irq(pdev, 0);if (irqno < 0){printk("解析资源失败%d\n", __LINE__);return -ENOMEM;}printk("mem:%x irq:%d\n", res->start, irqno);// 使用gpio驱动dnode = of_find_node_by_path("/myplatform");if (dnode == NULL){printk("解析设备树节点失败\n");return -ENXIO;}// 解析出GPIO编号并申请gpiono1 = gpiod_get_from_of_node(pdev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);if (IS_ERR(gpiono1)){printk("GPIO信息解析失败\n");return -PTR_ERR(gpiono1);}gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);if (IS_ERR(gpiono2)){printk("GPIO信息解析失败\n");return -PTR_ERR(gpiono2);}gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);if (IS_ERR(gpiono3)){printk("GPIO信息解析失败\n");return -PTR_ERR(gpiono3);}/*gpiod_set_value(gpiono1,1);gpiod_set_value(gpiono2,1);gpiod_set_value(gpiono3,1);*/printk("GPIO信息解析成功\n");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;
}
int pdrv_remove(struct platform_device *pdev) // 当设备和驱动分离时执行
{// 释放GPIO信息gpiod_put(gpiono1);gpiod_put(gpiono2);gpiod_put(gpiono3);// 销毁节点信息int i;for (i = 0; i < 3; i++){device_destroy(cls, MKDEV(major, i));}// 销毁目录class_destroy(cls);// 注销驱动unregister_chrdev(major, "mychrdev");return 0;
}
// 构建设备树匹配表
struct of_device_id oftable[] = {{.compatible = "hqyj,myplatform",},{.compatible = "hqyj,myplatform1",},{}, // 防止数组越界
};
// 1.分配驱动信息对象
struct platform_driver pdrv = {.probe = pdrv_probe,.remove = pdrv_remove,.driver = {.name = "bbbbb", // 驱动名.of_match_table = oftable,},
};
// 一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
应用程序:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "head.h"int main(int argc, char const *argv[])
{int a, b;int fd;while (1){// 从中断获取我们想实现的功能功能printf("请选择要控制的灯:0-LED1,1-LED2,2-LED3\n");scanf("%d", &a);switch (a){case 1:fd = open("/dev/mycdev0", O_RDWR);break;case 2:fd = open("/dev/mycdev1", O_RDWR);break;case 3:fd = open("/dev/mycdev2", O_RDWR);break;}if (fd < 0){printf("打开设备文件失败\n");return -1;}printf("打开设备文件成功\n");printf("请输入想要实现的功能 ");printf("0(关灯)1(开灯)\n");printf("请输入>>>");scanf("%d", &b);switch (b){case 1:ioctl(fd, LED_ON, &b);break;case 0:ioctl(fd, LED_OFF, &b);break;}}close(fd);return 0;
}
这篇关于【驱动开发】注册字符设备使用gpio设备树节点控制led三盏灯的亮灭的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!