华清远见(上海中心)22071

IIC总线传输温湿度,并且判断温度大于25℃灯点亮

.h文件>>>

#ifndef __SI7006_H__
#define __SI7006_H__

#define GET_TEM _IOR('m',0,int)
#define GET_HUM _IOR('m',1,int)
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
typedef enum 
{
    LED1,
    LED2,
    LED3,
    LED4,
    LED5,
    LED6
}led_t;
#endif

驱动文件>>>

#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include "si7006.h"

int major;
struct class *cls;
struct device *dev;
struct i2c_client *tclient;
struct device_node *node;
struct gpio_desc *gpiono;
int ret;
//获取温湿度数据的函数
int i2c_read_hum_tem(unsigned char reg)
{
    //读消息的封装
    char r_buf[]={reg};
    unsigned short val;
    struct i2c_msg r_msg[]={
        [0]={
            .addr=tclient->addr,
            .flags=0,
            .len=1,
            .buf=r_buf,
        },
        [1]={
            .addr=tclient->addr,
            .flags=1,
            .len=2,
            .buf=(char *)&val,
        },
    };
    //消息传输
    ret=i2c_transfer(tclient->adapter,r_msg,ARRAY_SIZE(r_msg));
    if(ret!=ARRAY_SIZE(r_msg))
    {
        printk("i2c transfer error\n");
        return EAGAIN;
    }
    return val;
}
int si7006_open(struct inode *inode,struct file *file)
{
    printk("%s:%d\n",__func__,__LINE__);
    return 0;
}
long si7006_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
    int tem,hum;
    int ret;
    switch (cmd)
    {
    case GET_HUM:
        hum=i2c_read_hum_tem(0xe5);
        ret=copy_to_user((void *)arg,(void *)&hum,sizeof(int));
        if(ret)
        {
            printk("copy to user error\n");
            return EINVAL;
        }
        break;
    case GET_TEM:
        tem=i2c_read_hum_tem(0xe3);
        gpiod_set_value(gpiono,1);
        ret=copy_to_user((void *)arg,(void *)&tem,sizeof(int));
        if(ret)
        {
            printk("copy to user error\n");
            return EINVAL;
        }
        break;
    case LED_ON:
        gpiod_set_value(gpiono,1);
        break;
    case LED_OFF:
        gpiod_set_value(gpiono,0);
        break;
    default:
        break;
    }
    return 0;
}
int si7006_close(struct inode *inode,struct file *file)
{
    printk("%s:%d\n",__func__,__LINE__);
    return 0;
}
struct file_operations fops={
    .open=si7006_open,
    .unlocked_ioctl=si7006_ioctl,
    .release=si7006_close,
};
//匹配成功后执行probe
int si7006_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
    tclient=client;
    //点灯初始化
    node=of_find_node_by_name(NULL,"myleds");
    if(node==NULL)
    {
        printk("find node error\n");
        return -EFAULT;
    }
    printk("find node success\n");
    gpiono = gpiod_get_from_of_node(node,"myled1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("get gpiono error\n");
        return PTR_ERR(gpiono);
    }
    //注册字符设备驱动
    major=register_chrdev(0,"si7006",&fops);
    if(major<0)
    {
        printk("register chrdev error\n");
        return major;
    }
    //自动创建设备节点
    cls = class_create(THIS_MODULE,"si7006");
    if(IS_ERR(cls))
    {
        printk("class create error\n");
        return PTR_ERR(cls);
    }
    dev = device_create(cls,NULL,MKDEV(major,0),NULL,"si7006");
    if(IS_ERR(dev))
    {
        printk("device create error\n");
        return PTR_ERR(dev);
    }
    return 0;
}
//设备分离后执行remove
int si7006_remove(struct i2c_client *client)
{
    gpiod_set_value(gpiono,0);
    gpiod_put(gpiono);
    class_destroy(cls);
    device_destroy(cls,MKDEV(major,0));
    unregister_chrdev(major,"si7006");
    printk("%s:%d\n",__func__,__LINE__);
    return 0;
}
//定义设备树匹配表
struct of_device_id oftable[]={
    {.compatible="hqyj,si7006",},
    {}
};
//热插拔
MODULE_DEVICE_TABLE(of,oftable);
//定义对象并初始化
struct i2c_driver si7006={
    .probe=si7006_probe,
    .remove=si7006_remove,
    .driver={
        .name="tem_hum_driver",
        .of_match_table=oftable,
    },
};

//一键注册
module_i2c_driver(si7006);
MODULE_LICENSE("GPL");

应用层文件>>>

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include "si7006.h"

int main(int argc, char const *argv[])
{
    int hum,tem;
    float hum1,tem1;
    int ret;
    int fd;
    int whitch;
    fd = open("/dev/si7006",O_RDWR);
    if(fd<0)
    {
        printf("open error\n");
        exit(-1);
    }
    
    whitch=LED1;
    while (1)
    {
        ioctl(fd,GET_TEM,&tem);
        ioctl(fd,GET_HUM,&hum);
        hum=ntohs(hum);
        tem=ntohs(tem);
        hum1=125.0*hum/65536-6;
        tem1=175.72*tem/65536-46.85;
        if(tem1>25)
        {
            ioctl(fd,LED_ON,&whitch);
        }
        else
        {
            ioctl(fd,LED_OFF,&whitch);
        }
        printf("tem:%f   hum:%f\n",tem1,hum1);
        sleep(1);
    }
    
    return 0;
}

实验现象>>>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值