AM 3354のDHT 11気温湿度センサ駆動とテスト

4943 ワード

 , , misc device  。
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define DEVICE_NAME "dht11"

/* Convert GPIO signal to GPIO pin number */
#define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))

#define DHT11_GPIO GPIO_TO_PIN(1, 26)


/*  (8 bit)  */
static char DHT11_read_byte ( void )
{
	char DHT11_byte ;
	unsigned char i ;
	unsigned char temp ;
	
	DHT11_byte = 0 ;
	for ( i = 0 ; i < 8 ; i ++ )
	{
		temp = 0 ;
		while ( ! (gpio_get_value(DHT11_GPIO) ) ) 
		{
			temp ++ ;
			if ( temp > 12 )
				return 1 ;
			udelay ( 5 ) ;
		}
		temp = 0 ;
		while ( gpio_get_value(DHT11_GPIO)  )
		{
			temp ++ ;
			if ( temp > 20 )
				return 1 ;
			udelay ( 5 ) ;
		}
		if ( temp > 6 )
		{
			DHT11_byte <<= 1 ;
			DHT11_byte |= 1 ;
		} 
		else
		{
			DHT11_byte <<= 1 ;
			DHT11_byte |= 0 ;
		}
	}
	return DHT11_byte ;
}

/* DHT11   */

static ssize_t DHT11_read ( struct file* filp, char __user* buf, size_t count, loff_t* f_pos )
{
	unsigned char DataTemp;
	unsigned char i;
	unsigned char err;
	char tempBuf[5];
	//loff_t pos = *f_pos ;
	
	err = 0 ;

	/*   */
	gpio_direction_output(DHT11_GPIO, 0);
	msleep ( 18 );
	//mdelay ( 18 );
	gpio_set_value(DHT11_GPIO, 1);

	udelay ( 40 );
	gpio_direction_input(DHT11_GPIO);
	 
	if ( !err )
	{
		DataTemp = 10 ;
		while ( !( gpio_get_value(DHT11_GPIO) ) && DataTemp )
		{
			DataTemp --;
			udelay ( 10 );
		}
		if ( !DataTemp )
		{
			err = 1;
			count = -EFAULT;
		}
	}
	if ( !err )
	{
		DataTemp = 10 ;
		while ( gpio_get_value(DHT11_GPIO) && DataTemp )
		{
			DataTemp --;
			udelay ( 10 );
		}
		if ( !DataTemp )
		{
			err = 1;
			count = -EFAULT;
		}
	}

	if ( !err )
	{
		for ( i = 0; i < 5; i ++ )
		{
			tempBuf[i] = DHT11_read_byte () ;
		}
		DataTemp = 0 ;
		for ( i = 0; i < 4; i ++ )
		{
			DataTemp += tempBuf[i] ;
		}
		if ( DataTemp != tempBuf[4] )
		{
			count = -EFAULT;
		}
		if ( count > 5 )
		{
			count = 5 ;
		}

		//pos += count;
		//if( copy_to_user ( buf , tempBuf + *f_pos , count ) )
		if ( copy_to_user ( buf , tempBuf , count ) )
		{
			count = -EFAULT ;
		}
		//*f_pos = pos;
	}
	
	/*   */
	gpio_direction_output(DHT11_GPIO, 1);
	return count;
}


static struct file_operations dev_fops = {
	.owner = THIS_MODULE,
	.read = DHT11_read,
	};
static struct miscdevice misc = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = DEVICE_NAME,
	.fops = &dev_fops,
	};

static int __init DHT11_init_module ( void )
{
	int ret;
	/* 1、  */
	ret = gpio_request(DHT11_GPIO, "dht11_gpio");
	if (ret)
		goto free_master;

	/* 2、  */
	gpio_direction_output(DHT11_GPIO, 1);

	/* 3、 misc  */
	ret = misc_register(&misc);

	printk (DEVICE_NAME"\tinitialized
"); free_master: return ret; } static void __exit DHT11_exit_module ( void ) { misc_deregister(&misc); } module_init ( DHT11_init_module ); module_exit ( DHT11_exit_module ); MODULE_LICENSE ( "GPL" ) ; MODULE_AUTHOR ( "[email protected]" ) ;

Makefileファイル
KERN_DIR = /home/zuoyi/kernel/3.2.0


all:
        make -C $(KERN_DIR) M=`pwd` modules


clean:
        make -C $(KERN_DIR) M=`pwd` modules clean
        rm -rf modules.order

obj-m   += dht11.o

ただしinsmod dht 11.koがファイルを駆動するとき
/# cd dev//dev # ls -l dht11  crw-------    1 0        0          10,  52 Jun 27 17:25 dht11/dev # 
テストプログラム
#include 
#include 
#include 
//#include 
#include 
#include 
#include 
//#include 
//#include 
#include 

int main ( void )
{
	int fd ;
	int retval ;
	char buf[5] ;
	fd = open ( "/dev/dht11" , O_RDONLY) ;
	if ( fd == -1 )
	{
		perror ( "open dht11 error
" ) ; exit ( -1 ) ; } printf ( "open /dev/dht11 successfully
" ) ; sleep ( 2 ) ; while ( 1 ) { sleep ( 1 ) ; retval = read ( fd , buf , 5 ); if ( retval == -1 ) { perror ( "read dht11 error" ) ; printf ( "read dht11 error" ) ; exit ( -1 ) ; } printf ( "Humidity:%d.%d %%RH Temperature:%d.%d C sum is:%d
", buf[0], buf[1], buf[2], buf[3],buf[4] ) ; } close ( fd ) ; }

テストプログラムの実行
/nfsswap # ./dht11ts  open/dev/dht11 successfully Humidity:33.0 %RH Temperature:27.0 C  sum is:60 Humidity:33.0 %RH Temperature:27.0 C  sum is:60 Humidity:33.0 %RH Temperature:27.0 C  sum is:60