#应用层
#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>
#include"myspi.h"
/*
1 0x06
2 0x5b
3 0x4f
4 0x66
5 0x6d
6 0x7d
7 0x07
8 0x7f
9 0x6f
0 0x3f
*/
void delay()
{
int i,j;
for(i=0;i<1000;i++)
{
for(j=0;j<2000;j++);
}
}
int main(int argc, char const *argv[])
{
char buf[128]={};
int which;
int fd=open("/dev/my_spi",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("设备文件打开成功\n");
//在终端输入
while(1)
{
printf("请输入控制命令1:开始 0:关闭\n");
fgets(buf,sizeof(buf),stdin);
//吃掉换行符
buf[strlen(buf)-1]='\0';
if(buf[0]=='1')
{
printf("打开数码管\n");
while(1)
{
for(which=0;which<10;which++)
{
ioctl(fd,SPI_ON,&which);
sleep(1);
}
}
}
else
{
ioctl(fd,SPI_OFF);
printf("关闭数码管\n");
}
}
close(fd);
return 0;
}
驱动层
#include <linux/init.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/timer.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include<linux/of.h>
#include "myspi.h"
//主设备号设置
#if 0
unsigned int major =0;
#else
unsigned int major=500;
#endif
//次设备号
unsigned int minor=0;
//申请的设备节点数
unsigned int count=1;
//设备结构体
struct cdev *cdev;
//设备目录(句柄)
struct class *cls;
//设备节点
struct device *dev;
//发送的数码管数据
char buf[2]={};
char site[4]={0x01,0x02,0x04,0x8};
char num[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
struct spi_device myspi;
//open
int m74hc595_open(struct inode *node, struct file *file)
{
printk("%s:%s-%d",__FILE__,__func__,__LINE__);
return 0;
}
//close
int m74hc595_close(struct inode *node, struct file *file)
{
printk("%s:%s-%d",__FILE__,__func__,__LINE__);
return 0;
}
//ioctrl
long m74hc595_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
static int i=0;
int which,ret;
switch(cmd)
{
case SPI_ON:
ret=copy_from_user(&which,(void *)arg,sizeof(int));
if(ret)
{
printk("用户向内核拷贝数据失败\n");
return -EIO;
}
buf[0]=site[i%4];
buf[1]=num[which];
spi_write(&myspi,buf,sizeof(buf));
break;
case SPI_OFF:
break;
default:
printk("功能码错误\n");
break;
}
i++;
return 0;
}
struct file_operations fops={
.open=m74hc595_open,
.release=m74hc595_close,
.unlocked_ioctl=m74hc595_ioctl,
};
int m74hc595_probe(struct spi_device *spi)
{
dev_t devno;
int i,ret;
//分配对象
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,count,"my_led");
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
}
else
{
ret=register_chrdev_region(MKDEV(major,minor),count,"my_led");
if(ret)
{
printk("静态申请设备号失败\n");
goto ERR2;
}
printk("静态申请设备号成功\n");
}
//注册字符设备驱动
ret=cdev_add(cdev,MKDEV(major,minor),count);
if(ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
printk("注册字符设备驱动成功\n");
//自动创建设备节点
cls = class_create(THIS_MODULE,"led");
if(IS_ERR(cls))
{
printk("创建逻辑节点目录失败\n");
ret=PTR_ERR(cls);
goto ERR4;
}
printk("创建逻辑节点目录成功\n");
//向上提交节点信息
for(i=0;i<count;i++)
{
dev = device_create(cls,NULL,MKDEV(major,i),NULL,"my_spi");
if(IS_ERR(dev))
{
printk("创建逻辑节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
printk("创建逻辑节点成功\n");
myspi=*spi;
printk("%s:%s-%d",__FILE__,__func__,__LINE__);
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),count);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
int m74hc595_remove(struct spi_device *spi)
{
int i;
printk("%s:%s-%d",__FILE__,__func__,__LINE__);
//1.销毁设备节点
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
//2.注销字符设备驱动
cdev_del(cdev);
//3.释放设备号
unregister_chrdev_region(MKDEV(major,minor),count);
//4.释放动态申请的空间
kfree(cdev);
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");
.h文件
#ifndef __MY_SPI__
#define __MY_SPI__
#define SPI_ON _IOW('a',1,int[2])
#define SPI_OFF _IO('a',0)
#endif