我是靠谱客的博主 炙热蜡烛,最近开发中收集的这篇文章主要介绍华清远见(上海中心)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 errorn");
        return EAGAIN;
    }
    return val;
}
int si7006_open(struct inode *inode,struct file *file)
{
    printk("%s:%dn",__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 errorn");
            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 errorn");
            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:%dn",__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 errorn");
        return -EFAULT;
    }
    printk("find node successn");
    gpiono = gpiod_get_from_of_node(node,"myled1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("get gpiono errorn");
        return PTR_ERR(gpiono);
    }
    //注册字符设备驱动
    major=register_chrdev(0,"si7006",&fops);
    if(major<0)
    {
        printk("register chrdev errorn");
        return major;
    }
    //自动创建设备节点
    cls = class_create(THIS_MODULE,"si7006");
    if(IS_ERR(cls))
    {
        printk("class create errorn");
        return PTR_ERR(cls);
    }
    dev = device_create(cls,NULL,MKDEV(major,0),NULL,"si7006");
    if(IS_ERR(dev))
    {
        printk("device create errorn");
        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:%dn",__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 errorn");
        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:%fn",tem1,hum1);
        sleep(1);
    }
    
    return 0;
}

实验现象>>>

 

 

最后

以上就是炙热蜡烛为你收集整理的华清远见(上海中心)22071的全部内容,希望文章能够帮你解决华清远见(上海中心)22071所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部