驱动程序:
#include
#include
#include
int major;
char kbuf[128]={0};
struct class *cls; //句柄
struct device *dev;
struct spi_device *spi_wr;//spi结构体指针,用于向芯片写入数据
int mycdev_open(struct inode *inode, struct file *file)//对应应用层open
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)//对应应用层write
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
spi_write(spi_wr,kbuf,2); //写入芯片
return size;
}
int mycdev_close(struct inode *inode, struct file *file)//对应应用层close
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//操作方法结构体的初始化
struct file_operations fops={
.open=mycdev_open,
.write=mycdev_write,
.release=mycdev_close,
};
int m74hc595_probe(struct spi_device *spi)
{
//动态注册字符设备驱动
major = register_chrdev(0,"myspi",&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
//向上提交节点目录
cls = class_create(THIS_MODULE,"MYSPI");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//创建设备节点
dev = device_create(cls,NULL,MKDEV(major,0),NULL,"myspi");
if(IS_ERR(dev))
{
printk("创建节点失败\n");
return PTR_ERR(dev);
}
printk("创建节点成功\n");
//将连接成功得到的spi给spi_write
spi_wr = spi;
return 0;
}
int m74hc595_remove(struct spi_device *spi)
{
//关闭所有数码管
kbuf[0]=0;
spi_write(spi_wr,kbuf,2); //写入芯片
device_destroy(cls,MKDEV(major,0)); //销毁节点
class_destroy(cls); //销毁目录
unregister_chrdev(major,"myspi"); //注销字符设备驱动
printk("模块卸载成功\n");
return 0;
}
//设备树匹配表
struct of_device_id of_table[]={
{.compatible="hqyj,m74hc595"},
{},
};
MODULE_DEVICE_TABLE(of,of_table);
//定义SPI对象并且初始化
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
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
//led显示数字0-9
char ledmu[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
int fd=open("/dev/myspi",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("设备文件打开成功\n");
char nss;//选择使用那些数码管
char buf[10]; //发送数据到内核层
int i=0;
/*
//循环打印数值,数码管0显示1秒0,然后数码管1显示1秒1,然后数码管2显示1秒2,然后数码管3显示1秒3,然后数码管0显示1秒4
int j=0;
while(1)
{
nss = 1;
nss <<= i++;
sprintf(buf, "%c%c",nss, ledmu[j++]);
write(fd,buf,2);
if(i==4)
i=0;
if(j==10)
j=0;
sleep(1);
}
*/
//自由控制哪个数码管显示及显示的数字
while(1)
{
nss = 0;
bzero(buf,sizeof(buf));
printf("请选择要显示数字的数码, 显示:1, 不显示:0 例2,3显示:(0110) 输入非0,1退出\n");
fgets(buf,sizeof(buf),stdin);
if(buf[0]!='0' && buf[0]!='1')
{
break;
}
for(i=0; i<4; i++)
{
nss <<= 1;
if(buf[i]=='1')
{
nss |= 1;
}
}
printf("请输入要显示的数字:");
scanf("%d",&i);
while(getchar()!='\n');//吸收垃圾字符
sprintf(buf, "%c%c",nss, ledmu[i]);
write(fd, buf, 2);
}
close(fd);
return 0;
}
测试结果: