dht11模块是一个能检测温湿度的传感器,采用单总线的通信方式传输数据。
1. 开发准备
1.1 硬件资源
- 野火iMX.6ULL PRO开发板
- dht11模块
2. 相关代码
2.1 驱动代码(dht11_drv.c)
#include <linux/module.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/miscdevice.h> #include <linux/kernel.h> #include <linux/major.h> #include <linux/mutex.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/stat.h> #include <linux/init.h> #include <linux/device.h> #include <linux/tty.h> #include <linux/kmod.h> #include <linux/gfp.h> #include <asm/mach/map.h> #include <linux/platform_device.h> #include <asm/io.h> #include <linux/mod_devicetable.h> #include <linux/gpio.h> #include <linux/delay.h> #include <linux/timekeeping.h> static struct cdev dht11_cdev; static struct class *dht11_class; struct gpio_desc * dht11_gpio_desc; static int major; static unsigned char g_us[40]; static void dht11_reset(void) { gpiod_direction_output(dht11_gpio_desc, 1); } // 注意!!!!!! static void dht11_start(void) { // 不能使用GPIOD_OUT_HIGH,有问题,字节用0,1 mdelay(30); gpiod_set_value(dht11_gpio_desc, 0); mdelay(20); gpiod_set_value(dht11_gpio_desc, 1); udelay(40); gpiod_direction_input(dht11_gpio_desc); udelay(2); } static int dht11_wait_ready(void) { int timeout_us = 20000; /* 等待低电平 */ timeout_us = 200; while(gpiod_get_value(dht11_gpio_desc) && --timeout_us) { udelay(1); } // 超时 if (!timeout_us) { printk("----debug1-----\n"); return -1; } /* 等待高电平 */ timeout_us = 200; while(!gpiod_get_value(dht11_gpio_desc) && --timeout_us) { udelay(1); } if (!timeout_us) { printk("----debug2-----\n"); return -1; } /* 等待低电平 */ timeout_us = 200; while(gpiod_get_value(dht11_gpio_desc) && --timeout_us) { udelay(1); } // 超时 if (!timeout_us) { printk("----debug3-----\n"); return -1; } return 0; } static int dht11_read_byte(unsigned char *data) { int i = 0; unsigned char buffer; int timeout_us = 400; int high_us = 0; for (i=0;i<8;i++) { /* 等待高电平 */ timeout_us = 400; while(!gpiod_get_value(dht11_gpio_desc) && --timeout_us) { udelay(1); } if (!timeout_us) { return -1; } udelay(40); if (gpiod_get_value(dht11_gpio_desc)) { buffer = (buffer << 1) | 1; /* 等待高电平结束 */ timeout_us = 400; while(gpiod_get_value(dht11_gpio_desc) && --timeout_us) { udelay(1); } if (!timeout_us) { return -1; } } else { buffer = (buffer << 1) | 0; } } *data = buffer; return 0; } /* 写法与协议有关 */ static ssize_t dht11_drv_read(struct file * file, char __user * buf, size_t size, loff_t *offset) { int i = 0; unsigned long flags; unsigned char kernel_buf[5]; u64 pre,last; if (size !=4 ) { return -EINVAL; } local_irq_save(flags); pre = ktime_get_boottime_ns(); //for(i=0;i<1000;i++) // udelay(1); udelay(40); last = ktime_get_boottime_ns(); printk("udelay 1000 times use ns: %lld\n", last-pre); // 1. 发送高脉冲启动DHT11 dht11_reset(); dht11_start(); // 2. 等待DHT11就绪 if (0 != dht11_wait_ready()) { printk("设备未就绪\n"); local_irq_restore(flags); return -1; } // 3. 读取5个字节 for (i=0;i<5;i++) { if (dht11_read_byte(&kernel_buf[i]) != 0) { local_irq_restore(flags); return -1; } } dht11_reset(); local_irq_restore(flags); // 4. 根据检验码验证数据 if (kernel_buf[4] != kernel_buf[0] + kernel_buf[1] + kernel_buf[2] + kernel_buf[3]) { printk("验证错误\n"); local_irq_restore(flags); return -1; } // 5. 返回给用户 size = copy_to_user(buf, kernel_buf, size); return size; } static ssize_t dht11_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset) { return 0; } static int dht11_drv_open(struct inode *node, struct file *file) { return 0; } static int dht11_drv_close(struct inode *node, struct file *file) { return 0; } static struct file_operations dht11_opr = { .open = dht11_drv_open, .release = dht11_drv_close, .read = dht11_drv_read, .write = dht11_drv_write, }; static int dht11_probe(struct platform_device *pdev) { int err; dev_t devid; printk("====%s====\n", __FUNCTION__); // 从设备树获取资源 dht11_gpio_desc = gpiod_get(&pdev->dev, NULL, GPIOD_OUT_HIGH); //dht11_data_pin = gpiod_get(&pdev->dev, NULL, GPIOD_OUT_HIGH); if (IS_ERR(dht11_gpio_desc)) { printk("get gpiod_desc error"); return -1; } // 设置/注册cdev err = alloc_chrdev_region(&devid, 0, 1, "dht11"); major = MAJOR(devid); cdev_init(&dht11_cdev, &dht11_opr); cdev_add(&dht11_cdev, devid, 1); // 建立class dht11_class = class_create(THIS_MODULE, "dht11_class"); // 建立device device_create(dht11_class, NULL, MKDEV(major, 0), NULL, "dht11"); // 创建节点/dev/dht11 return 0; } static int dht11_remove(struct platform_device *pdev) { // 释放class,device printk("======%s=======\n", __FUNCTION__); device_destroy(dht11_class, MKDEV(major, 0)); class_destroy(dht11_class); unregister_chrdev(major, "dht11"); gpiod_put(dht11_gpio_desc); return 0; } static const struct of_device_id dht11_match[] = { { .compatible = "fire,dht11" }, { }, }; struct platform_driver dht11_driver = { .probe = dht11_probe, .remove = dht11_remove, .driver = { .name = "my_dht11_driver", .of_match_table = dht11_match, }, }; // 入口函数 static int dht11_platform_driver_init(void) { int ret = 0; printk("====%s====\n", __FUNCTION__); ret = platform_driver_register(&dht11_driver); // 注册设备信息 return ret; } // 出口函数 static void dht11_platform_driver_exit(void) { printk("====%s====\n", __FUNCTION__); platform_driver_unregister(&dht11_driver); // 注册设备信息 } module_init(dht11_platform_driver_init); module_exit(dht11_platform_driver_exit); MODULE_LICENSE("GPL");
2.2 设备树
根节点下添加如下节点
dht11:fire_dht11 { compatible = "fire,dht11"; gpios = <&gpio4 28 GPIO_ACTIVE_HIGH>; // 填写你使用的引脚 };
2.3 测试程序
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argn, char **argv) { int fd; int ret = 0; unsigned char data[4]; if (argn !=2) { printf("Usage: %s /dev/xxx\n", argv[0]); return -1; } fd = open(argv[1], O_RDONLY); if (fd < 0) { perror("open error"); return -1; } while(1) { ret = read(fd, data, 4); if (ret < 0) { perror("read error"); return -1; } printf("temperature: %d.%d\t", data[0], data[1]); printf("humidity: %d.%d\n", data[2], data[3]); sleep(1); } }
2.4 Makefile
# 指定架构 ARCH=arm # 指定编译工具链 CROSS_COMPILE=arm-linux-gnueabihf- export ARCH CROSS_COMPILE # 指定内核根目录,需要和板子上运行的驱动一致 KERN_DIR = /home/hxd/linux_kernel/kernel_5.4 name=dht11 all: make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o $(name)_test $(name)_test.c clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order rm -f $(name)_test obj-m += $(name)_drv.o
3. 开发经验总结
3.1 gpiod_direction_output和gpiod_set_value是有区别的
前者将引脚配置为输出模式,然后再输出,后者只有一步,直接输出。在这种时序相关的驱动开发时,初始化时使用前者,但在协议上的编程时应该使用后者。
3.2 gpiod_set_value设置值的时候直接使用0或1
测试时发现使用GPIOD_OUT_HIGH代替1,GPIOD_OUT_LOW代替0会出现问题
3.3 有些函数也会耗时,内核延时函数也不是很准确
可以使用如下方式进行软件上的测量:
#include <linux/timekeeping.h> u64 pre,last; pre = ktime_get_boottime_ns(); // 测试代码 for(i=0;i<1000;i++) udelay(40); last = ktime_get_boottime_ns(); printk("udelay 1000 times use ns: %lld\n", last-pre);