概述
#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"si7006.h"
int major;
struct class *cls;
struct device *dev;
struct i2c_client *tclient;
//获取温湿度数据的函数
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;
}
//匹配成功后执行probe
int si7006_open(struct inode *inode, struct file *file)
{
printk("openn");
return 0;
}
ssize_t si7006_read(struct file *file, char *ubuf, size_t size, loff_t *off)
{
printk("readn");
return 0;
}
ssize_t si7006_write(struct file *file, const char *ubuf, size_t size, loff_t *off)
{
printk("writen");
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 fildn");
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 fildn");
return EINVAL;
}
break;
}
return 0;
}
int si7006_close(struct inode *inode, struct file *file)
{
printk("closen");
return 0;
}
struct file_operations fops={
.open=si7006_open,
.read=si7006_read,
.write=si7006_write,
.unlocked_ioctl=si7006_ioctl,
.release=si7006_close,
};
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");
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"si7006");
if(IS_ERR(dev))
{
printk("向上提交设备信息失败n");
return PTR_ERR(dev);
}
printk("向上提交设备信息成功n");
return 0;
}
//设备分离后执行remove
int si7006_remove(struct i2c_client *client)
{
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");
#ifndef __SI7006_H__
#define __SI7006_H__
#define GET_TEM _IOR('m',0,int)
#define GET_HUM _IOR('m',1,int)
#endif
#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"iic.h"
int main(int argc, char const *argv[])
{
int which;
int hum,tem;
int ret;
float hum1,tem1;
int fd=open("/dev/si7006",O_RDWR);
if(fd<0)
{
printf("open is errorn");
return -1;
}
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;
printf("tem:%f hum:%fn",tem1,hum1);
if(tem1<25)
{
which=1;
ioctl(fd,LED_ON,&which);
}
else
{
which=0;
ioctl(fd,LED_OFF,&which);
}
sleep(1);
}
return 0;
}
最后
以上就是还单身指甲油为你收集整理的22071华清远见(上海中心)的全部内容,希望文章能够帮你解决22071华清远见(上海中心)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复