201 lines
4.0 KiB
C
201 lines
4.0 KiB
C
|
/*
|
||
|
* dht.c - driver for DHT11 Test
|
||
|
*
|
||
|
* Copyright (C) 2012 Loongson Corporation
|
||
|
*
|
||
|
* 2013-5-29
|
||
|
*/
|
||
|
#include <linux/module.h>
|
||
|
#include <linux/init.h>
|
||
|
#include <linux/moduleparam.h>
|
||
|
#include <linux/miscdevice.h>
|
||
|
#include <linux/types.h>
|
||
|
#include <linux/delay.h>
|
||
|
#include <linux/module.h>
|
||
|
#include <linux/interrupt.h>
|
||
|
#include <linux/ioport.h>
|
||
|
#include <linux/init.h>
|
||
|
#include <linux/err.h>
|
||
|
#include <linux/rcupdate.h>
|
||
|
#include <linux/platform_device.h>
|
||
|
#include <linux/cdev.h>
|
||
|
#include <linux/slab.h>
|
||
|
#include <linux/io.h>
|
||
|
#include <linux/of.h>
|
||
|
#include <linux/completion.h>
|
||
|
#include <asm/io.h>
|
||
|
#include <linux/fs.h>
|
||
|
#include <linux/fcntl.h>
|
||
|
#include <asm/uaccess.h>
|
||
|
#include <linux/gpio.h>
|
||
|
#include <linux/time.h>
|
||
|
|
||
|
#define GPIODHT 5
|
||
|
unsigned char str[5];
|
||
|
unsigned char send[4];
|
||
|
unsigned char temp;
|
||
|
unsigned char data;
|
||
|
unsigned char result;
|
||
|
int flag=10000;
|
||
|
|
||
|
static int dht_sensor_open(struct inode *inode, struct file *file)
|
||
|
{
|
||
|
printk(KERN_ALERT"########DHT11 sensor open succesful#############\n");
|
||
|
gpio_request(GPIODHT,"DHT11");
|
||
|
// gpio_direction_input(GPIOECHO);
|
||
|
gpio_direction_output(GPIODHT,1);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int dht_sensor_close(struct inode *inode, struct file *file)
|
||
|
{
|
||
|
gpio_free(GPIODHT);
|
||
|
printk(KERN_ALERT"########DHT11 sensor close succesful###########\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static ssize_t dht_sensor_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
|
||
|
{
|
||
|
/*
|
||
|
unsigned int count=size;
|
||
|
if(copy_to_user(buf,(void*)(dht),count));
|
||
|
{
|
||
|
return -EFAULT;
|
||
|
}
|
||
|
*/
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static ssize_t dht_sensor_write(struct file *file, const char __user *buf, size_t count, loff_t *ptr)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static char dht_read_8bit(void)
|
||
|
{
|
||
|
char i;
|
||
|
for(i=0;i<8;i++)
|
||
|
{
|
||
|
flag=10000;
|
||
|
while(!(gpio_get_value(GPIODHT))&&(flag--));
|
||
|
udelay(40);
|
||
|
temp=0;
|
||
|
if(gpio_get_value(GPIODHT))
|
||
|
{
|
||
|
temp=1;
|
||
|
}
|
||
|
while(gpio_get_value(GPIODHT)&&(flag--));
|
||
|
if(flag<0)
|
||
|
{
|
||
|
// printk(KERN_ALERT"###### overtime error\n");
|
||
|
break;
|
||
|
}
|
||
|
data<<=1;
|
||
|
data|=temp;
|
||
|
}
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
static void dht_start(void)
|
||
|
{
|
||
|
gpio_direction_output(GPIODHT,0);
|
||
|
mdelay(20);
|
||
|
gpio_direction_output(GPIODHT,1);
|
||
|
udelay(40);
|
||
|
gpio_direction_input(GPIODHT);
|
||
|
}
|
||
|
|
||
|
static int dht(void)
|
||
|
{
|
||
|
unsigned char sum,checkdata;
|
||
|
dht_start();
|
||
|
if(!gpio_get_value(GPIODHT)) //dht respond
|
||
|
{
|
||
|
flag=10000;
|
||
|
while((!gpio_get_value(GPIODHT))&&(flag--));
|
||
|
flag=10000;
|
||
|
while((gpio_get_value(GPIODHT))&&(flag--));
|
||
|
str[0]=dht_read_8bit();
|
||
|
str[1]=dht_read_8bit();
|
||
|
str[2]=dht_read_8bit();
|
||
|
str[3]=dht_read_8bit();
|
||
|
str[4]=dht_read_8bit();
|
||
|
gpio_direction_output(GPIODHT,1);
|
||
|
sum=str[0]+str[1]+str[2]+str[3];
|
||
|
checkdata=str[4];
|
||
|
if(sum==checkdata)
|
||
|
{
|
||
|
send[0]=str[0];
|
||
|
send[1]=str[1];
|
||
|
send[2]=str[2];
|
||
|
send[3]=str[3];
|
||
|
// printk(KERN_ALERT"kernel temperature=%d,humidity=%d",send[2],send[0]);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
static long dht_sensor_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
|
||
|
{
|
||
|
switch(cmd){
|
||
|
case 0:
|
||
|
printk(KERN_ALERT"in ioctl 0!\n");
|
||
|
gpio_direction_output(GPIODHT,1);
|
||
|
break;
|
||
|
case 1:
|
||
|
{
|
||
|
void __user *p=(void __user *)arg;
|
||
|
printk(KERN_ALERT"in ioctl 1!\n");
|
||
|
dht();
|
||
|
copy_to_user(p,send,sizeof(send));
|
||
|
break;
|
||
|
}
|
||
|
case 2:
|
||
|
printk(KERN_ALERT"in ioctl 2!\n");
|
||
|
break;
|
||
|
default:
|
||
|
printk(KERN_ALERT"in ioctl default!\n");
|
||
|
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static const struct file_operations dht_sensor_ops = {
|
||
|
.owner = THIS_MODULE,
|
||
|
.open = dht_sensor_open,
|
||
|
.release = dht_sensor_close,
|
||
|
.read = dht_sensor_read,
|
||
|
.write = dht_sensor_write,
|
||
|
.unlocked_ioctl = dht_sensor_ioctl
|
||
|
};
|
||
|
|
||
|
static struct miscdevice dht_sensor_miscdev = {
|
||
|
MISC_DYNAMIC_MINOR,
|
||
|
"dht",
|
||
|
&dht_sensor_ops,
|
||
|
};
|
||
|
|
||
|
static int __init dht_sensor_init(void)
|
||
|
{
|
||
|
if(misc_register(&dht_sensor_miscdev))
|
||
|
{
|
||
|
printk(KERN_ALERT"###############: Couldn't register device 0, %d.\n", 255);
|
||
|
return -EBUSY;
|
||
|
}
|
||
|
printk(KERN_ALERT"register succesful\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static void __exit dht_sensor_exit(void)
|
||
|
{
|
||
|
misc_deregister(&dht_sensor_miscdev);
|
||
|
}
|
||
|
|
||
|
module_init(dht_sensor_init);
|
||
|
module_exit(dht_sensor_exit);
|
||
|
MODULE_LICENSE("GPL");
|