驱动程序:
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
struct class *cls;
struct device *dev;
struct cdev *cdev;
dev_t devno;
int minor=0;
unsigned int major=0;
struct device_node *node;
char kbuf[128]={0};
int gpiono1;
int gpiono2;
int gpiono3;
int mycdev_open(struct inode *inode, struct file *file)
{
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *off)
{
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *off)
{
int ret;
ret=copy_from_user(kbuf,ubuf,sizeof(kbuf));
if(ret){
printk("copy_from_user error");
return -EIO;
}
if(kbuf[0]=='1'){
gpio_set_value(gpiono1,!gpio_get_value(gpiono1));
}else if(kbuf[0]=='2'){
gpio_set_value(gpiono2,!gpio_get_value(gpiono2));
}else if(kbuf[0]=='3'){
gpio_set_value(gpiono3,!gpio_get_value(gpiono3));
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
return 0;
}
//定义操作方法结构体变量并初始化
struct file_operations fops=
{
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
//入口函数
static int __init mycdev_init(void)
{
//**************注册设备驱动****************
int ret;
//给字符设备驱动对象申请空间
cdev=cdev_alloc();
if(cdev==NULL){
printk("字符设备申请空间失败\n");
ret=-ENOMEM;
goto ERR1;
}
//初始化字符设备对象
cdev_init(cdev,&fops);
//申请设备号
ret=alloc_chrdev_region(&devno,minor,1,"mycdev");
if(ret){
printk("申请设备号失败\n");
goto ERR2;
}
major=MAJOR(devno);
minor=MINOR(devno);
//将设备注册到内核
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret){
printk("注册到内核失败\n");
goto ERR3;
}
//****************自动创建设备节点*******************
//向上提交目录
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls)){
printk("向上提交目录失败\n");
ret=PTR_ERR(cls);
goto ERR4;
}
printk("向上提交目录成功\n");
//向上提交设备节点
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"mycdev0");
if(IS_ERR(cls)){
printk("向上提交设备节点失败\n");
ret=PTR_ERR(dev);
goto ERR5;
}
printk("向上提交设备节点成功\n");
//*******************使用GPIO子系统点灯************************************
node=of_find_node_by_path("/myleds");
if(node==NULL){
printk("获取失败\n");
return -ENOENT;
}
gpiono1=of_get_named_gpio(node,"led1",0);
gpiono2=of_get_named_gpio(node,"led2",0);
gpiono3=of_get_named_gpio(node,"led3",0);
gpio_request(gpiono1,NULL);
gpio_request(gpiono2,NULL);
gpio_request(gpiono3,NULL);
gpio_direction_output(gpiono1,0);
gpio_direction_output(gpiono2,0);
gpio_direction_output(gpiono3,0);
return 0;
//*******************失败情况************************
ERR5:
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),1);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
//出口函数
static void __exit mycdev_exit(void)
{
gpio_set_value(gpiono1,0);
gpio_set_value(gpiono2,0);
gpio_set_value(gpiono3,0);
gpio_free(gpiono1);
gpio_free(gpiono2);
gpio_free(gpiono3);
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev(major,"mycdev");
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层程序:
#include <stdio.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include<string.h>
#include <sys/ioctl.h>
void led(){
int fd=open("/dev/mycdev0",O_RDWR);
char buf[128]="";
while(1){
printf("请输入操作的LED(1/2/3)\n");
scanf("%s",buf);
write(fd,buf,sizeof(buf));
}
close(fd);
}
int main(){
led();
return 0;
}