linux驱动——dht11温湿度传感器驱动(5.4版本内核)

简介: linux驱动——dht11温湿度传感器驱动(5.4版本内核)

dht11模块是一个能检测温湿度的传感器,采用单总线的通信方式传输数据。

1. 开发准备

1.1 硬件资源

  • 野火iMX.6ULL PRO开发板
  • dht11模块
  • b927f254b4db44e1a8bac1a7d5ebb304.png

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);


目录
相关文章
|
8天前
|
Linux C语言
Linux内核队列queue.h
Linux内核队列queue.h
|
27天前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】⭐Linux 卸载已加载的内核模块rmmod命令 使用指南
【Shell 命令集合 系统设置 】⭐Linux 卸载已加载的内核模块rmmod命令 使用指南
29 1
|
3天前
|
Linux Go
Linux命令Top 100驱动人生! 面试必备
探索Linux命令不再迷茫!本文分10部分详解20个基础命令,带你由浅入深掌握文件、目录管理和文本处理。 [1]: <https://cloud.tencent.com/developer/article/2396114> [2]: <https://pan.quark.cn/s/865a0bbd5720> [3]: <https://yv4kfv1n3j.feishu.cn/docx/MRyxdaqz8ow5RjxyL1ucrvOYnnH>
46 0
|
7天前
|
算法 Linux 调度
深度解析:Linux内核的进程调度机制
【4月更文挑战第12天】 在多任务操作系统如Linux中,进程调度机制是系统的核心组成部分之一,它决定了处理器资源如何分配给多个竞争的进程。本文深入探讨了Linux内核中的进程调度策略和相关算法,包括其设计哲学、实现原理及对系统性能的影响。通过分析进程调度器的工作原理,我们能够理解操作系统如何平衡效率、公平性和响应性,进而优化系统表现和用户体验。
16 3
|
9天前
|
安全 Unix Linux
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
14 0
|
14天前
|
负载均衡 算法 Linux
深度解析:Linux内核调度器的演变与优化策略
【4月更文挑战第5天】 在本文中,我们将深入探讨Linux操作系统的核心组成部分——内核调度器。文章将首先回顾Linux内核调度器的发展历程,从早期的简单轮转调度(Round Robin)到现代的完全公平调度器(Completely Fair Scheduler, CFS)。接着,分析当前CFS面临的挑战以及社区提出的各种优化方案,最后提出未来可能的发展趋势和研究方向。通过本文,读者将对Linux调度器的原理、实现及其优化有一个全面的认识。
|
16天前
|
Linux 内存技术
Linux内核读取spi-nor flash sn
Linux内核读取spi-nor flash sn
13 1
|
16天前
|
Linux
Linux驱动运行灯 Heartbeat
Linux驱动运行灯 Heartbeat
8 0
|
23天前
|
存储 网络协议 Linux
【Linux 解惑 】谈谈你对linux内核的理解
【Linux 解惑 】谈谈你对linux内核的理解
22 0
|
27天前
|
存储 Linux Shell
【Shell 命令集合 系统设置 】Linux 显示Linux内核模块的详细信息 modinfo命令 使用指南
【Shell 命令集合 系统设置 】Linux 显示Linux内核模块的详细信息 modinfo命令 使用指南
24 0