SPI总线驱动(数码管显示)

功能1:流水数字

功能2:1234

功能3:5555

 

#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 <signal.h>
#include "myled.h"
#include <unistd.h>
#include <arpa/inet.h>

#define SEND_MSG _IOW('c',0,short)

void delay_ms(int ms)
{
	int i,j;
	for(i = 0; i < ms;i++)
		for (j = 0; j < 1800; j++);
}


int main(int argc, char const *argv[])
{
    char num[10] = {0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f};
    int fd;
    int n;
    char buf[2];
    int index=0;
    fd=open("/dev/m74hc595",O_RDWR);
    if(fd<0)
    {
        printf("打开m74hc595设备失败\n");
        exit(-1);  
    }
    printf("打开m74hc595设备成功\n");

    while(1)
    {
        printf("请选择数码管显示方式\n");
        printf("1,流水灯\n");
        printf("2,1234\n");
        printf("3,5555\n");
        scanf("%d",&n);
        getchar();
        switch (n)
        {
        case 1:
            {
                while(1)
                {
                    for(int i=1;i<9;i=i*2)
                    {                            
                        buf[0]=i;
                        if(index>9)
                        {
                            index=0;
                        }
                        buf[1]=num[index];
                        ioctl(fd,SEND_MSG,buf);
                        sleep(1);
                        buf[1]=0;
                        ioctl(fd,SEND_MSG,buf);
                        index++;
                        sleep(1);
                    }
                }
            }
            break;
        case 2:
            {
                while(1)
                {    
                    buf[0]=1;
                    buf[1]=num[1];
                    ioctl(fd,SEND_MSG,buf);
                    delay_ms(1);
                    buf[0]=2;
                    buf[1]=num[2];
                    ioctl(fd,SEND_MSG,buf);
                    delay_ms(1);
                    buf[0]=4;
                    buf[1]=num[3];
                    ioctl(fd,SEND_MSG,buf);
                    delay_ms(1);
                    buf[0]=8;
                    buf[1]=num[4];
                    ioctl(fd,SEND_MSG,buf);
                    delay_ms(1);          
                }
            }
            break;
        case 3:
            {
                
                buf[0]=0xf;
                buf[1]=num[5];
                ioctl(fd,SEND_MSG,buf);
                
            }
            break;
        
        default:
            break;
        }
    }


    close(fd);
  


    return 0;   
}
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/spi/spi.h>

#define SEND_MSG _IOW('c',0,short)

int major;
struct class *cls;
struct device *dev;

struct spi_device *spi1;
int m74hc595_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
int m74hc595_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
long m74hc595_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
    int ret;
    char buf[2]={};
    switch(cmd)
    {    
        case SEND_MSG:
        {
            ret=copy_from_user((void*)buf,(void*)arg,sizeof(buf));
            if(ret)
            {
                printk("给用户拷贝失败\n");
                return -EINVAL;
            }
            spi_write(spi1,buf,sizeof(buf));
        }
        break;
    }
    return 0;
}
//定义操作方法结构体
struct file_operations fops={
    .open=m74hc595_open,
    .unlocked_ioctl=m74hc595_ioctl,
    .release=m74hc595_close,
};


int m74hc595_probe(struct spi_device *spi)
{
    spi1=spi;
    major=register_chrdev(0,"m74hc595",&fops);
    if(major<0)
    {
        printk("注册字符设备失败\n");
        return major;
    }
    printk("注册字符设备成功\n");

    //自动创建设备节点
    cls=class_create(THIS_MODULE,"si7006");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    dev=device_create(cls,NULL,MKDEV(major,0),NULL,"m74hc595");
    if(IS_ERR(dev))
    {
        printk("向上提交节点信息失败\n");
        return PTR_ERR(dev);
    }  
    printk("向上提交节点信息成功\n");

    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
int m74hc595_remove(struct spi_device *spi)
{
        //注销字符设备驱动
    device_destroy(cls,MKDEV(major,0));
    class_destroy(cls);
    unregister_chrdev(major,"m74hc595");

    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

struct of_device_id oftable[]={
    {.compatible="hqyj,m74hc595"},
    {},
};
MODULE_DEVICE_TABLE(of,oftable);
//分配对象并初始化
struct spi_driver m74hc595 ={
    .probe=m74hc595_probe,
    .remove=m74hc595_remove,
    .driver ={
        .name = "m74hc595_driver",
        .of_match_table = oftable,
    },
};
//声明遵循GPL开源协议
module_spi_driver(m74hc595);
MODULE_LICENSE("GPL");

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林某某..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值