i2c总线驱动,温湿度传感器

检测环境温度,大于25°时led1亮
代码:
test.c

#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 "mysi.h"

int main(int argc, char const *argv[])
{
    int hum,tem,which;
    float hum1,tem1;
    int ret;
    int fd1 = open("/dev/si70060",O_RDWR);
    if(fd1<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    int fd2 = open("/dev/si70061",O_RDWR);
    if(fd2<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }

    while(1)
    {
        ioctl(fd1,GET_TEM,&tem);
        ioctl(fd1,GET_HUM,&hum);
        //字节序的转换
        hum=ntohs(hum);
        tem=ntohs(tem);
        //温湿度数据计算
        hum1=125.0*hum/65536-6;
        tem1=175.72*tem/65536-46.85;
        printf("tem:%f  hum:%f\n",tem1,hum1);
        if (tem1 > 25.0)
        {
            which = 1;
            ioctl(fd2,BEE_ON,&which);
        }
        else
        {
            which =  0;
            ioctl(fd2,BEE_OFF,&which);
        }
        
        sleep(1);
    }
    
    close(fd1);
    close(fd2);

    return 0;
}

mysi.h

#ifndef __SI7006_H__
#define __SI7006_H__

#define GET_TEM _IOR('m',0,int)
#define GET_HUM _IOR('m',1,int)

#define BEE_ON _IOR('b',1,int)
#define BEE_OFF _IOR('b',0,int)

#endif

mysi.c

#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/io.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include "mysi.h"

int major;
struct class *cls;
struct device *dev;
struct i2c_client *tclient;
unsigned int i;

struct device_node *node;
struct gpio_desc *gpiono;

//获取温湿度函数
int i2c_read_hum_tem(unsigned char reg)
{
    int ret;
    //读消息封装
    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获取温湿度数据失败\n");
        return EAGAIN;
    }
    return val;
}

int si7006_open(struct inode *inode, struct file *file)
{
    printk("open\n");
    return 0;
}

long si7006_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int tem, hum,which;
    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 fild\n");
            return EINVAL;
        }
        break;
    case GET_TEM:
        tem = i2c_read_hum_tem(0xe3);
        ret = copy_to_user((void *)arg, (void *)&tem, sizeof(int));
        if (ret)
        {
            printk("copy_to_user fild\n");
            return EINVAL;
        }
        break;
    case BEE_ON:
        ret = copy_from_user(&which,(void *)arg,sizeof(int));
        if (ret)
        {
            printk("copy_from_user fild\n");
            return EINVAL;
        }
        gpiod_set_value(gpiono,which);
        break;
    case BEE_OFF:
        ret = copy_from_user(&which,(void *)arg,sizeof(int));
        if (ret)
        {
            printk("copy_from_user fild\n");
            return EINVAL;
        }
        gpiod_set_value(gpiono,which);
        break;
    }
    return 0;
}

int si7006_close(struct inode *inode, struct file *file)
{
    printk("close\n");
    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;

    //注册字符设备驱动
    major = register_chrdev(0, "si7006", &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");

    for (i = 0; i < 2; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "si7006%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备信息失败\n");
            return PTR_ERR(dev);
        }
    }
    printk("向上提交设备信息成功\n");

    //通过名字获取设备树节点信息
    node = of_find_node_by_name(NULL, "myled");
    if (node == NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析到设备树节点\n");

    //获取并申请gpio编号
    gpiono = gpiod_get_from_of_node(node, "myled1", 0, GPIOD_OUT_LOW, NULL);
    printk("获取gpio编号成功\n");

    return 0;
}

int si7006_remove(struct i2c_client *pdev)
{
    printk("%s:%d\n", __func__, __LINE__);
    return 0;
}

//构建compatible表
struct of_device_id oftable[] =
    {
        {.compatible = "sl,si7006",},
        {}
    };



//给对象初始化
struct i2c_driver si7006 ={
        .probe = si7006_probe,
        .remove = si7006_remove,
        .driver = {
            .name = "hello",
            .of_match_table = oftable, //设备树匹配
        },
};

module_i2c_driver(si7006);
// GPL协议
MODULE_LICENSE("GPL");

实验现象:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值