LED.H
#ifndef __LED_H__
#define __LED_H__
//1:PE10 2:PF10 3:PE8
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
MYCDEV.C文章来源:https://www.toymoban.com/news/detail-506686.html
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/device.h>
#include"led.h"
int major;
char kbuf[128]={0};
// gpio_t *vir_led1;
// gpio_t *vir_led2;
// gpio_t *vir_led3;
//unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
int gpiono1; //led1
int gpiono2; //led2
int gpiono3; //led3
struct device_node *dnode;
int mycdev_open(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)
{
int which;
int ret=copy_from_user(&which,(void *)arg,4);
if(ret)
{
printk("copy_from_user filed\n");
return ret;
}
switch(cmd)
{
case LED_ON://开灯
switch(which)
{
case 1:
gpio_set_value(gpiono1,1);
break;
case 2:
gpio_set_value(gpiono2,1);
break;
case 3:
gpio_set_value(gpiono3,1);
break;
}
break;
case LED_OFF://关灯
switch(which)
{
case 1:
gpio_set_value(gpiono1,0);
break;
case 2:
gpio_set_value(gpiono2,0);
break;
case 3:
gpio_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;
//字符设备驱动注册
major=register_chrdev(0,"mycdev",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n",major);
//向上提交目录
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点信息
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"mycdev");
if(IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点成功\n");
//解析设备树节点
dnode=of_find_node_by_name(NULL,"myleds");
if(dnode==NULL)
{
printk("解析设备树节点失败...\n");
return -ENOMEM;
}
printk("解析设备树节点成功...\n");
//根据设备树节点解析出gpio编号
/***************LED1*******************/
gpiono1=of_get_named_gpio(dnode,"led1",0);
if(gpiono1<0)
{
printk("解析设备号%d失败...\n",gpiono1);
return -EIO;
}
printk("解析设备号%d成功...\n",gpiono1);
//申请gpio编号
ret=gpio_request(gpiono1,NULL);
if(ret)
{
printk("申请GPIO编号%d失败...\n",gpiono1);
return -EIO;
}
printk("申请GPIO编号%d成功...\n",gpiono1);
//设置管脚为输出
gpio_direction_output(gpiono1,0);
/***************LED2*******************/
gpiono2=of_get_named_gpio(dnode,"led2",0);
if(gpiono2<0)
{
printk("解析设备号%d失败...\n",gpiono2);
return -EIO;
}
printk("解析设备号%d成功...\n",gpiono2);
//申请gpio编号
ret=gpio_request(gpiono2,NULL);
if(ret)
{
printk("申请GPIO编号%d失败...\n",gpiono2);
return -EIO;
}
printk("申请GPIO编号%d成功...\n",gpiono2);
//设置管脚为输出
gpio_direction_output(gpiono2,0);
/***************LED3*******************/
gpiono3=of_get_named_gpio(dnode,"led3",0);
if(gpiono3<0)
{
printk("解析设备号%d失败...\n",gpiono3);
return -EIO;
}
printk("解析设备号%d成功...\n",gpiono3);
//申请gpio编号
ret=gpio_request(gpiono3,NULL);
if(ret)
{
printk("申请GPIO编号%d失败...\n",gpiono3);
return -EIO;
}
printk("申请GPIO编号%d成功...\n",gpiono3);
//设置管脚为输出
gpio_direction_output(gpiono3,0);
return 0;
}
static void __exit mycdev_exit(void)
{
//销毁设备节点信息
device_destroy(cls,MKDEV(major,0));
//销毁目录空间
class_destroy(cls);
//注销字符设备驱动
unregister_chrdev(major,"mychrdev");
//释放编号
gpio_free(gpiono1); //led1
gpio_free(gpiono2); //led2
gpio_free(gpiono3); //led3
//关灯
gpio_set_value(gpiono1,0);
gpio_set_value(gpiono2,0);
gpio_set_value(gpiono3,0);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
TEST.C文章来源地址https://www.toymoban.com/news/detail-506686.html
#include"led.h"
#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>
int main(int argc, char const *argv[])
{
int a,b;
char buf[128]={0};
int fd=open("/dev/mycdev",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
printf("请输入:1(开灯) 0(关灯)>");
scanf("%d",&a);
printf("请输入控制的灯:1(LED1) 2(LED2) 3(LED3)>");
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;
}
到了这里,关于驱动开发作业2的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!