任务目标
应用层程序控制数码管显示数字
大致思路
1、定义初始化spi对象并注册
2、匹配设备树节点
3、匹配成功后,在probe函数中注册字符设备驱动
4、通过字符设备驱动对内核写入需要变换的数码管数组
5、在内核层实现write函数获取到应用层内容时对数码管进行控制
6、应用层循环发送0-9数据 依次给4个数码管,实现类似流水灯控制
代码实现
驱动代码
#include <linux/init.h>
#include<linux/spi/spi.h>
#include <linux/module.h>
struct cdev *cdev;
struct class *cls;
struct device *dev;
char kbuf[]={0x1,0x3f};
struct spi_device *spi1=NULL;
int minor=0;
#if 1
unsigned int major = 0;
#else
unsigned int major = 500;
#endif
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
spi_write(spi1,kbuf,sizeof(kbuf));
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
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,
};
int m74hc595_probe(struct spi_device *spi)
{
int ret,i;
dev_t devno;
printk("%s:%d\n",__FILE__,__LINE__);
spi_write(spi,kbuf,sizeof(kbuf));
spi1=spi;
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("cdev alloc memory err\n");
ret = -ENOMEM;
goto ERR1;
}
printk("对象分配成功\n");
cdev_init(cdev,&fops);
if(major==0)
{
ret=alloc_chrdev_region(&devno,minor,1,"keyirq");
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
printk("动态申请设备号成功\n");
}
else
{
ret=register_chrdev_region(MKDEV(major,minor),1,"keyirq");
if(ret)
{
printk("静态申请设备号失败\n");
goto ERR2;
}
printk("静态申请设备号成功\n");
}
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
printk("注册字符设备驱动成功\n");
cls = class_create(THIS_MODULE,"keyirq1");
if(IS_ERR(cls))
{
printk("创建逻辑节点目录失败\n");
ret=PTR_ERR(cls);
goto ERR4;
}
printk("创建逻辑节点目录成功\n");
dev = device_create(cls,NULL,MKDEV(major,0),NULL,"keyirq");
if(IS_ERR(dev))
{
printk("创建逻辑节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
printk("创建逻辑节点成功\n");
return 0;
ERR5:
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),0);
ERR2:
kfree(cdev);
ERR1:
return ret;
return 0;
}
int m74hc595_remove(struct spi_device *spi)
{
printk("%s:%d\n",__FILE__,__LINE__);
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major,minor),0);
kfree(cdev);
return 0;
}
struct of_device_id of_table[]={
{.compatible="hqyj,m74hc595"},
{},
};
MODULE_DEVICE_TABLE(of,of_table);
struct spi_driver m74hc595 ={
.probe=m74hc595_probe,
.remove=m74hc595_remove,
.driver={
.name="m74hc595",
.of_match_table=of_table,
},
};
module_spi_driver(m74hc595);
MODULE_LICENSE("GPL");
测试代码
#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>
int main(int argc, char const *argv[])
{
int fd=open("/dev/keyirq",O_RDWR);
char ubuf[]={0x1,0x3f};
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("设备文件打开成功\n");
while(1)
{
if(ubuf[0]==0x1)
{
ubuf[0]=0x2;
}
else if(ubuf[0]==0x2)
{
ubuf[0]=0x4;
}
else if(ubuf[0]==0x4)
{
ubuf[0]=0x8;
}
else if(ubuf[0]==0x8)
{
ubuf[0]=0x1;
}
switch(ubuf[1])
{
case 0x3f:
ubuf[1]=0x06;
break;
case 0x06:
ubuf[1]=0x5b;
break;
case 0x5b:
ubuf[1]=0x4f;
break;
case 0x4f:
ubuf[1]=0x66;
break;
case 0x66:
ubuf[1]=0x6d;
break;
case 0x6d:
ubuf[1]=0x7d;
break;
case 0x7d:
ubuf[1]=0x07;
break;
case 0x07:
ubuf[1]=0x7f;
break;
case 0x7f:
ubuf[1]=0x6f;
break;
case 0x6f:
ubuf[1]=0x3f;
break;
}
write(fd,&ubuf,sizeof(ubuf));
for(int i=0;i<10000000;i++)
{}
}
close(fd);
return 0;
}