底层驱动实现数码管显示温湿度数值功能

开发板:STM32MP157A

温湿度传感器:si7006

显示器(数码管):m74hc595

遇到的问题:循环采集温湿度传感器数值,并将数值发送给数码管的时候两者存在竞态关系,导致数码管显示亮度很暗

解决办法:采用多线程或者多进程解决内核竞态问题

驱动代码

#include 
#include 
#include 
#include 
#include 
#include "head.h"

unsigned int major;
struct class *cls;
struct device *dev;
struct i2c_client *client1;

//读取温湿度的函数
int i2c_read_hum_tem(char reg)
{
    short value;
    char r_buf[]={reg};
    int ret;
    //封装消息
    struct i2c_msg r_msg[]={
        [0]={
            .addr=client1->addr,
            .flags=0,//先写
            .len=sizeof(r_buf),
            .buf=r_buf,
        },
        [1]={
            .addr=client1->addr,
            .flags=1,
            .len=2,
            .buf=(char *)&value,
        },
    };
    //将消息传送
    ret=i2c_transfer(client1->adapter,r_msg,2);
    if(ret!=2)
    {
        printk("消息传输失败\n");
        return -EIO;
    }
    return value;//将读取到的温湿度返回
}

int si7006_open(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long si7006_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
    int tem,hum,ret;
    switch(cmd)
    {
        case GET_HUM://读取湿度
            //读取湿度的逻辑
            hum=i2c_read_hum_tem(0xE5);
            ret=copy_to_user((void *)arg,&hum,4);//int类型4个字节
            if(ret)
            {
                printk("copy_to_user error\n");
                return ret;
            }
            break;
        case GET_TEM://读取温度
            //读取温度的逻辑
            tem=i2c_read_hum_tem(0xE3);
            ret=copy_to_user((void *)arg,&tem,4);
            if(ret)
            {
                printk("copy_to_user error\n");
                return ret;
            }
            break;
    }
    return 0;
}

int si7006_close(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);    
    return 0;
}
 
//操作方法结构体
struct file_operations fops={
    .open=si7006_open,
    .unlocked_ioctl=si7006_ioctl,
    .release=si7006_close,
};

//给对象分配空间并且初始化
int i2c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
    client1=client;
    //字符设备驱动的注册
    major=register_chrdev(0,"si7006",&fops);
    if(major<0)
    {
        printk("register_chrdev failed\n"); 
        return major;
    }
    printk("register_chrdev success\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,"si7006");
    if(IS_ERR(dev))
    {
        printk("向上提交设备节点失败\n");
        return -PTR_ERR(dev);   
    }
    printk("向上提交设备节点成功\n");
    return 0;
}

int i2c_remove(struct i2c_client *client)
{
    //销毁节点
    device_destroy(cls,MKDEV(major,0));
    //销毁目录
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major,"si7006");

    return 0;
}
//定义设备树匹配的表
struct of_device_id oftable[]={
    {.compatible="hqyj,si7006",},
    {},
};

//分配IIC驱动信息对象
struct i2c_driver i2c_drv={
    .probe=i2c_probe,
    .remove=i2c_remove,
    .driver={
        .name="si7006",
        .of_match_table=oftable,
    },
};

module_i2c_driver(i2c_drv);
MODULE_LICENSE("GPL");

应用层代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
#include 

int number=1000;
int *p=&number;
int flag = 0;


void *shuma_callback(void *arg)
{

    while(1)
    {

        //将温湿度传给数码管ioctl显示,发送数据
        ioctl(*(int *)arg,GET_SHUMA,p);
 
    }   
    pthread_exit(NULL);
}

int main(int argc,char const *argv[])
{
    int tem,hum;
    float tem1,hum1;


    int fd_i2c=open("/dev/si7006",O_RDWR);
    if(fd_i2c<0)
    {
        printf("设备文件打开失败\n");
        exit(-1);
    }

    //温湿度值采用数码管显示
    int fd_spi=open("/dev/m74hc595",O_RDWR);
    if(fd_spi<0)
    {
        printf("设备文件打开失败\n");
        exit(-1);
    }
 
    pthread_t tid_1;
    if(pthread_create(&tid_1,NULL,shuma_callback,(void *)&fd_spi)!=0)
    {
        fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
        return -1;
    }
    pthread_detach(tid_1);//分离线程

    while(1)
    {

        //获取数据
        ioctl(fd_i2c,GET_HUM,&hum);
        ioctl(fd_i2c,GET_TEM,&tem);
        printf("hum=%d,tem=%d\n",hum,tem);
        
        //大小端转换
        hum=ntohs(hum);
        tem=ntohs(tem);

        //计算数据
        hum1=125.0*hum/65536-6;
        tem1=175.72*tem/65536-46.85;

        printf("hum1=%d,tem1=%d\n",hum1,tem1);
        number=(int)hum1 * 100 + (int)tem1;

        sleep(1);

        
    }

    pthread_join(tid_1,NULL);//阻塞等待tid_1线程退出

    return 0;
}

头文件

#ifndef __HEAD_H__
#define __HEAD_H__

#define GET_SHUMA _IOR('m',2,int)//获取数码管的功能码
#define GET_HUM _IOR('m',1,int)//获取湿度的功能码
#define GET_TEM _IOR('m',0,int)//获取温度的功能码


#endif

你可能感兴趣的:(stm32,嵌入式硬件,单片机)