完结IIO子系统
BIN
IMX6ULL/doc_pic/14_IIO/04.1_iio_event引入与体验.tif
Normal file
BIN
IMX6ULL/doc_pic/14_IIO/04.2_修改DHT11驱动使用iio_event.tif
Normal file
BIN
IMX6ULL/doc_pic/14_IIO/04.3_使用trigger写iio_event(不实用).tif
Normal file
363
IMX6ULL/doc_pic/14_IIO/04_iio_event的使用.md
Normal file
@@ -0,0 +1,363 @@
|
|||||||
|
# iio_event的使用
|
||||||
|
|
||||||
|
IIO(Industrial I/O)参考资料:
|
||||||
|
|
||||||
|
* 系列文章:https://blog.csdn.net/lickylin/article/details/108177756
|
||||||
|
* https://www.cnblogs.com/yongleili717/p/10758691.html
|
||||||
|
* 内核文档:https://www.kernel.org/doc/html/v5.3/driver-api/iio
|
||||||
|
* 参考内核源码
|
||||||
|
* `drivers\staging\iio\impedance-analyzer\ad5933.c`
|
||||||
|
* `drivers\iio\adc\hi8435.c`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 1. iio_event的引入与体验
|
||||||
|
|
||||||
|
### 1.1 问题引入与解决方法
|
||||||
|
|
||||||
|
以温湿度传感器为例,它的温度值超过极限了,APP如何检测到这个事件?
|
||||||
|
|
||||||
|
* APP读取某个设备文件
|
||||||
|
* 传感器驱动提供数据:
|
||||||
|
* 比如硬件监测到温度超标时,触发硬件中断,在中断里使用iio_push_event把"事件"写入kfifo
|
||||||
|
* APP读取kfifo得到"事件"
|
||||||
|
* iio_trigger提供数据:
|
||||||
|
* 有些硬件没有中断引脚,那么可以使用iio_trigger
|
||||||
|
* iio_trigger直接调用驱动程序提供的的另一个"虚拟中断处理函数"(跟iio_buffer不一样的中断处理函数)
|
||||||
|
* 这个中断处理函数读取硬件数据,使用iio_push_event把"事件"写入kfifo
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 1.2 怎么表示事件
|
||||||
|
|
||||||
|
使用结构体"iio_event_data"表示事件:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
所谓事件就是一个64位的ID,里面不含有具体的数值:比如它表示温度超高了,但是没有表示温度值是多少。
|
||||||
|
|
||||||
|
这个ID如何表示不同的事件呢?如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.3 上机体验
|
||||||
|
|
||||||
|
IMX6ULL的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
STM32MP157的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 1.3.1 IMX6ULL
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device2
|
||||||
|
/root/dht11_test /dev/iio\:device2 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 1.3.2 STM32MP157
|
||||||
|
|
||||||
|
```SHELL
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
/root/dht11_test /dev/iio\:device1 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 2. iio_event数据流程
|
||||||
|
|
||||||
|
### 2.1 驱动程序创建KFIFO
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.2 驱动程序写KFIFO
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.3 APP读取KFIFO
|
||||||
|
|
||||||
|
示例源码如下:
|
||||||
|
|
||||||
|
```c
|
||||||
|
int event_fd;
|
||||||
|
struct iio_event_data event;
|
||||||
|
|
||||||
|
int fd = open("/dev/iio:device2", O_RDWR);
|
||||||
|
|
||||||
|
ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
|
||||||
|
read(event_fd, &event, sizeof(event));
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 3. 修改DHT11驱动使用iio_event
|
||||||
|
|
||||||
|
IMX6ULL的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
STM32MP157的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 3.1 设备表明自己有event能力
|
||||||
|
|
||||||
|
在注册iio设备时,会进行如下判断:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
只有设置了indio_dev->info->event_attrs或某个通道的num_event_specs不为0,才会创建KFIFO。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
示例代码如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
这会生成如下sysfs文件:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
生成sysfs文件的流程为:
|
||||||
|
|
||||||
|
```c
|
||||||
|
iio_device_register
|
||||||
|
iio_device_register_eventset
|
||||||
|
__iio_add_event_config_attrs
|
||||||
|
for (j = 0; j < indio_dev->num_channels; j++) {
|
||||||
|
ret = iio_device_add_event_sysfs(indio_dev,
|
||||||
|
&indio_dev->channels[j]);
|
||||||
|
iio_device_add_event
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
这些sysfs对应的读写函数如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 3.2 设备驱动提供sysfs接口函数
|
||||||
|
|
||||||
|
比如我们想指定温度的报警值,那么就需要实现这些sysfs的接口函数:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 3.3 上机实验
|
||||||
|
|
||||||
|
#### 1.3.1 IMX6ULL
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device2
|
||||||
|
/root/dht11_test /dev/iio\:device2 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 1.3.2 STM32MP157
|
||||||
|
|
||||||
|
```SHELL
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
/root/dht11_test /dev/iio\:device1 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 4. 使用trigger写iio_event(不实用)
|
||||||
|
|
||||||
|
IMX6ULL的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
STM32MP157的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 4.1 框图
|
||||||
|
|
||||||
|
下图中,左侧写kfifo的红色线条,就是使用trigger写iio_event:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 4.2 提供虚拟中断处理函数
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 4.3 注册虚拟中断处理函数
|
||||||
|
|
||||||
|
设置iio_device的trgger,比如如下操作:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
```
|
||||||
|
|
||||||
|
会导致如下函数被调用,它就注册了"记录在pollfunc_event里的"虚拟中断函数:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
注意,对比一下:
|
||||||
|
|
||||||
|
* "记录在pollfunc_event里的"虚拟中断函数:
|
||||||
|
* 在设置iio设备的current_trigger时就注册了中断, 就会使能trigger state
|
||||||
|
* 用来产生iio_event,写KFIFO
|
||||||
|
* "记录在pollfunc里的"虚拟中断函数:
|
||||||
|
* 在设置iio设备的current_trigger后、并且使能了buffer,才注册中断, 就会使能trigger state
|
||||||
|
* 用来读取传感器数据,写iio_buffer
|
||||||
|
|
||||||
|
### 4.4 上机体验
|
||||||
|
|
||||||
|
#### 4.4.1 修改内核支持
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 4.4.2 具体操作
|
||||||
|
|
||||||
|
IMX6ULL:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/industrialio-triggered-event.ko
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device2
|
||||||
|
/root/dht11_test /dev/iio\:device2 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
STM32MP157:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/industrialio-triggered-event.ko
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
/root/dht11_test /dev/iio\:device1 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
BIN
IMX6ULL/doc_pic/14_IIO/05.1_驱动示例_IMX6ULL的ADC驱动.tif
Normal file
BIN
IMX6ULL/doc_pic/14_IIO/05.2_驱动示例_STM32MP157的ADC驱动.tif
Normal file
217
IMX6ULL/doc_pic/14_IIO/05_iio驱动示例.md
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
# iio驱动示例
|
||||||
|
|
||||||
|
IIO(Industrial I/O)参考资料:
|
||||||
|
|
||||||
|
* 系列文章:https://blog.csdn.net/lickylin/article/details/108177756
|
||||||
|
* https://www.cnblogs.com/yongleili717/p/10758691.html
|
||||||
|
* 内核文档:https://www.kernel.org/doc/html/v5.3/driver-api/iio
|
||||||
|
* 参考内核源码
|
||||||
|
* IMX6ULL: `drivers\iio\adc\vf610_adc.c`
|
||||||
|
* STM32MP157: `drivers\iio\adc\stm32-adc-core.c`,`drivers\iio\adc\stm32-adc.c`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 1. IMX6ULL ADC驱动
|
||||||
|
|
||||||
|
### 1.1 设备树
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.2 驱动程序
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.3 驱动程序分析
|
||||||
|
|
||||||
|
```shell
|
||||||
|
static int vf610_read_raw(struct iio_dev *indio_dev,
|
||||||
|
struct iio_chan_spec const *chan,
|
||||||
|
int *val,
|
||||||
|
int *val2,
|
||||||
|
long mask)
|
||||||
|
|
||||||
|
怎么使用val, val2 ?
|
||||||
|
|
||||||
|
IIO_VAL_INT : 只有整数没有小数, 整数保存在val里,val2不使用。
|
||||||
|
IIO_VAL_INT_PLUS_MICRO : val=整数,val2=小数部分*1000000,比如3.14如此保存:*val = 3; *val2 = 140000
|
||||||
|
IIO_VAL_INT_PLUS_NANO : val=整数,val2=小数部分*1000000000,比如3.14如此保存:*val = 3; *val2 = 140000000
|
||||||
|
IIO_VAL_INT_PLUS_MICRO_DB : DB数据,数值的表示方法跟IIO_VAL_INT_PLUS_MICRO一样,添加了单位DB。
|
||||||
|
IIO_VAL_INT_MULTIPLE : 返回多个整数值,主要用于iio_info的read_raw_multi函数。
|
||||||
|
IIO_VAL_FRACTIONAL : 分数,比如n/m,表示为:*val=n, *val2=m
|
||||||
|
IIO_VAL_FRACTIONAL_LOG2 : LOG2值, 比如*val=1,*val2=4,表示的数值为:val>>val2, 即1>>4=0.03125
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.4 驱动程序使用
|
||||||
|
|
||||||
|
#### 1.4.1 各类值的含义
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 1.4.2 示例
|
||||||
|
|
||||||
|
IMX6ULL原理图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
可以通过扩展板引出,原理图如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
测试时接线方法:可以把ADC1_CH3或ADC1_CH4,接到3V3或GND,如下图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
操作命令:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# 单步操作
|
||||||
|
cd /sys/bus/iio/devices/iio:device0
|
||||||
|
cat in_voltage3_raw
|
||||||
|
|
||||||
|
# 使用buffer
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
# 设置trigger周期
|
||||||
|
cd /sys/bus/iio/devices/trigger1
|
||||||
|
echo 1000000000 > sampling_frequency # 单位ns, 1s一次
|
||||||
|
|
||||||
|
cd /sys/bus/iio/devices/iio\:device0/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_voltage3_en
|
||||||
|
echo 1024 > buffer/length
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
hexdump /dev/iio\:device0 # 读取数据
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
注意:使用buffer功能会死机,原因未明。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 2. STM32MP157 ADC驱动
|
||||||
|
|
||||||
|
### 2.1 设备树与驱动
|
||||||
|
|
||||||
|
如何确定设备树与驱动?百问网STM32MP157开发板使用的设备树文件是`stm32mp157c-100ask-512d-lcd-v1.dtb`,可以在Ubuntu使用如下命令反汇编得到dts文件:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
dtc -I dtb -O dts stm32mp157c-100ask-512d-lcd-v1.dtb > 1.dts
|
||||||
|
```
|
||||||
|
|
||||||
|
在1.dts中搜`adc`,可以看到如下节点,根据里面的`compatible`属性即可找到设备树和驱动:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.1.1 设备树文件
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 2.1.2 驱动文件
|
||||||
|
|
||||||
|
`drivers\iio\adc\stm32-adc-core.c`,`drivers\iio\adc\stm32-adc.c`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.2 为何驱动程序无法使用
|
||||||
|
|
||||||
|
#### 2.2.1 pinmux空缺
|
||||||
|
|
||||||
|
查看内核打印信息:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
[ 4.112842] stm32mp157-pinctrl soc:pin-controller@50002000: missing pins property in node pins .
|
||||||
|
[ 4.120360] stm32-adc-core: probe of 48003000.adc failed with error -22
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
根据错误信息"missing pins property in node",找到驱动:pinctrl/stm32/pinctrl-stm32.c
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
在设备树里确实发现ADC使用的pinctrl里没有"pinmux"属性:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
如此修改:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.2.2 提供regulator
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.3 上机实验
|
||||||
|
|
||||||
|
#### 2.3.1 原理图与接线
|
||||||
|
|
||||||
|
STM32MP157原理图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
扩展板原理图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
接线方法:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.3.2 测试命令
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# 单步操作
|
||||||
|
cd /sys/bus/iio/devices/iio:device1
|
||||||
|
cat in_voltage0_raw
|
||||||
|
|
||||||
|
# 使用buffer
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # STM32MMP157无需安装驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
# 设置trigger周期
|
||||||
|
cd /sys/bus/iio/devices/trigger1
|
||||||
|
echo 1000000000 > sampling_frequency # 单位ns, 1s一次
|
||||||
|
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_voltage0_en
|
||||||
|
echo 1024 > buffer/length
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
hexdump /dev/iio\:device1 # 读取数据
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.4 驱动程序分析
|
||||||
|
|
||||||
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125161222685.png
Normal file
|
After Width: | Height: | Size: 108 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125163136804.png
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125163406036.png
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125163508774.png
Normal file
|
After Width: | Height: | Size: 90 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125164423639.png
Normal file
|
After Width: | Height: | Size: 55 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125170901645.png
Normal file
|
After Width: | Height: | Size: 72 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125170929132.png
Normal file
|
After Width: | Height: | Size: 19 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125171144376.png
Normal file
|
After Width: | Height: | Size: 197 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125175114799.png
Normal file
|
After Width: | Height: | Size: 191 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125175311198.png
Normal file
|
After Width: | Height: | Size: 194 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125175658789.png
Normal file
|
After Width: | Height: | Size: 81 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125193456857.png
Normal file
|
After Width: | Height: | Size: 102 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241125193714139.png
Normal file
|
After Width: | Height: | Size: 19 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241126094637080.png
Normal file
|
After Width: | Height: | Size: 6.2 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241126094715687.png
Normal file
|
After Width: | Height: | Size: 5.5 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127102844284.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127102920957.png
Normal file
|
After Width: | Height: | Size: 6.6 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127103153056.png
Normal file
|
After Width: | Height: | Size: 80 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127103523330.png
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127104547343.png
Normal file
|
After Width: | Height: | Size: 39 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127131900125.png
Normal file
|
After Width: | Height: | Size: 55 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127132025400.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127132345881.png
Normal file
|
After Width: | Height: | Size: 60 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127132457129.png
Normal file
|
After Width: | Height: | Size: 96 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127132553738.png
Normal file
|
After Width: | Height: | Size: 138 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127132934371.png
Normal file
|
After Width: | Height: | Size: 949 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127133020707.png
Normal file
|
After Width: | Height: | Size: 953 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241127145118931.png
Normal file
|
After Width: | Height: | Size: 1.1 MiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128095523369.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128095902459.png
Normal file
|
After Width: | Height: | Size: 115 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128105502688.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128105620144.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128131324394.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128132450046.png
Normal file
|
After Width: | Height: | Size: 35 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128133548599.png
Normal file
|
After Width: | Height: | Size: 75 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128133617577.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
BIN
IMX6ULL/doc_pic/14_IIO/pic/image-20241128133856564.png
Normal file
|
After Width: | Height: | Size: 756 KiB |
683
IMX6ULL/source/14_iio/05_dht11_iio_event/dht11.c
Normal file
@@ -0,0 +1,683 @@
|
|||||||
|
/*
|
||||||
|
* DHT11/DHT22 bit banging GPIO driver
|
||||||
|
*
|
||||||
|
* Copyright (c) Harald Geyer <harald@ccbib.org>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/err.h>
|
||||||
|
#include <linux/interrupt.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/printk.h>
|
||||||
|
#include <linux/slab.h>
|
||||||
|
#include <linux/of.h>
|
||||||
|
#include <linux/of_device.h>
|
||||||
|
#include <linux/sysfs.h>
|
||||||
|
#include <linux/io.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/wait.h>
|
||||||
|
#include <linux/bitops.h>
|
||||||
|
#include <linux/completion.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/delay.h>
|
||||||
|
#include <linux/gpio.h>
|
||||||
|
#include <linux/of_gpio.h>
|
||||||
|
#include <linux/timekeeping.h>
|
||||||
|
|
||||||
|
#include <linux/iio/iio.h>
|
||||||
|
#include <linux/iio/buffer.h>
|
||||||
|
#include <linux/iio/kfifo_buf.h>
|
||||||
|
#include <linux/iio/trigger_consumer.h>
|
||||||
|
#include <linux/iio/triggered_buffer.h>
|
||||||
|
#include <linux/iio/events.h>
|
||||||
|
#include <linux/iio/triggered_event.h>
|
||||||
|
|
||||||
|
#define DRIVER_NAME "dht11"
|
||||||
|
|
||||||
|
#define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
|
||||||
|
|
||||||
|
#define DHT11_EDGES_PREAMBLE 2
|
||||||
|
#define DHT11_BITS_PER_READ 40
|
||||||
|
/*
|
||||||
|
* Note that when reading the sensor actually 84 edges are detected, but
|
||||||
|
* since the last edge is not significant, we only store 83:
|
||||||
|
*/
|
||||||
|
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
|
||||||
|
DHT11_EDGES_PREAMBLE + 1)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Data transmission timing:
|
||||||
|
* Data bits are encoded as pulse length (high time) on the data line.
|
||||||
|
* 0-bit: 22-30uS -- typically 26uS (AM2302)
|
||||||
|
* 1-bit: 68-75uS -- typically 70uS (AM2302)
|
||||||
|
* The acutal timings also depend on the properties of the cable, with
|
||||||
|
* longer cables typically making pulses shorter.
|
||||||
|
*
|
||||||
|
* Our decoding depends on the time resolution of the system:
|
||||||
|
* timeres > 34uS ... don't know what a 1-tick pulse is
|
||||||
|
* 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
|
||||||
|
* 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
|
||||||
|
* timeres < 23uS ... no problem
|
||||||
|
*
|
||||||
|
* Luckily clocks in the 33-44kHz range are quite uncommon, so we can
|
||||||
|
* support most systems if the threshold for decoding a pulse as 1-bit
|
||||||
|
* is chosen carefully. If somebody really wants to support clocks around
|
||||||
|
* 40kHz, where this driver is most unreliable, there are two options.
|
||||||
|
* a) select an implementation using busy loop polling on those systems
|
||||||
|
* b) use the checksum to do some probabilistic decoding
|
||||||
|
*/
|
||||||
|
#define DHT11_START_TRANSMISSION_MIN 18000 /* us */
|
||||||
|
#define DHT11_START_TRANSMISSION_MAX 20000 /* us */
|
||||||
|
#define DHT11_MIN_TIMERES 34000 /* ns */
|
||||||
|
#define DHT11_THRESHOLD 49000 /* ns */
|
||||||
|
#define DHT11_AMBIG_LOW 23000 /* ns */
|
||||||
|
#define DHT11_AMBIG_HIGH 30000 /* ns */
|
||||||
|
|
||||||
|
struct dht11 {
|
||||||
|
struct device *dev;
|
||||||
|
|
||||||
|
int gpio;
|
||||||
|
int irq;
|
||||||
|
|
||||||
|
struct completion completion;
|
||||||
|
/* The iio sysfs interface doesn't prevent concurrent reads: */
|
||||||
|
struct mutex lock;
|
||||||
|
|
||||||
|
s64 timestamp;
|
||||||
|
int temperature;
|
||||||
|
int humidity;
|
||||||
|
|
||||||
|
/* num_edges: -1 means "no transmission in progress" */
|
||||||
|
int num_edges;
|
||||||
|
struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
|
||||||
|
struct delayed_work work;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
/*
|
||||||
|
* dht11_edges_print: show the data as actually received by the
|
||||||
|
* driver.
|
||||||
|
*/
|
||||||
|
static void dht11_edges_print(struct dht11 *dht11)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
|
||||||
|
for (i = 1; i < dht11->num_edges; ++i) {
|
||||||
|
dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
|
||||||
|
dht11->edges[i].ts - dht11->edges[i - 1].ts,
|
||||||
|
dht11->edges[i - 1].value ? "high" : "low");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* CONFIG_DYNAMIC_DEBUG */
|
||||||
|
|
||||||
|
static unsigned char dht11_decode_byte(char *bits)
|
||||||
|
{
|
||||||
|
unsigned char ret = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 8; ++i) {
|
||||||
|
ret <<= 1;
|
||||||
|
if (bits[i])
|
||||||
|
++ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_decode(struct dht11 *dht11, int offset)
|
||||||
|
{
|
||||||
|
int i, t;
|
||||||
|
char bits[DHT11_BITS_PER_READ];
|
||||||
|
unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
|
||||||
|
|
||||||
|
for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
|
||||||
|
t = dht11->edges[offset + 2 * i + 2].ts -
|
||||||
|
dht11->edges[offset + 2 * i + 1].ts;
|
||||||
|
if (!dht11->edges[offset + 2 * i + 1].value) {
|
||||||
|
dev_dbg(dht11->dev,
|
||||||
|
"lost synchronisation at edge %d\n",
|
||||||
|
offset + 2 * i + 1);
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
bits[i] = t > DHT11_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
|
hum_int = dht11_decode_byte(bits);
|
||||||
|
hum_dec = dht11_decode_byte(&bits[8]);
|
||||||
|
temp_int = dht11_decode_byte(&bits[16]);
|
||||||
|
temp_dec = dht11_decode_byte(&bits[24]);
|
||||||
|
checksum = dht11_decode_byte(&bits[32]);
|
||||||
|
|
||||||
|
if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
|
||||||
|
dev_dbg(dht11->dev, "invalid checksum\n");
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boot_ns();
|
||||||
|
if (hum_int < 20) { /* DHT22 */
|
||||||
|
dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
|
||||||
|
((temp_int & 0x80) ? -100 : 100);
|
||||||
|
dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
|
||||||
|
} else { /* DHT11 */
|
||||||
|
dht11->temperature = temp_int;;
|
||||||
|
dht11->humidity = hum_int;;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* IRQ handler called on GPIO edges
|
||||||
|
*/
|
||||||
|
static irqreturn_t dht11_handle_irq(int irq, void *data)
|
||||||
|
{
|
||||||
|
struct iio_dev *iio = data;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio);
|
||||||
|
|
||||||
|
/* TODO: Consider making the handler safe for IRQ sharing */
|
||||||
|
if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
|
||||||
|
dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
|
||||||
|
dht11->edges[dht11->num_edges++].value =
|
||||||
|
gpio_get_value(dht11->gpio);
|
||||||
|
|
||||||
|
if (dht11->num_edges >= DHT11_EDGES_PER_READ)
|
||||||
|
complete(&dht11->completion);
|
||||||
|
}
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_raw(struct iio_dev *iio_dev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
int *val, int *val2, long m)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpio_direction_output(dht11->gpio, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpio_direction_input(dht11->gpio);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = IIO_VAL_INT;
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
*val = dht11->temperature;
|
||||||
|
else if (chan->type == IIO_HUMIDITYRELATIVE)
|
||||||
|
*val = dht11->humidity;
|
||||||
|
else
|
||||||
|
ret = -EINVAL;
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct iio_info dht11_iio_info = {
|
||||||
|
.driver_module = THIS_MODULE,
|
||||||
|
.read_raw = dht11_read_raw,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct iio_event_spec dht11_temp_events[] = {
|
||||||
|
{
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_RISING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_FALLING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_EITHER,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct iio_chan_spec dht11_chan_spec[] = {
|
||||||
|
{ .type = IIO_TEMP,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_index = 0,
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
.event_spec = dht11_temp_events,
|
||||||
|
.num_event_specs = ARRAY_SIZE(dht11_temp_events),
|
||||||
|
},
|
||||||
|
{ .type = IIO_HUMIDITYRELATIVE,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_index = 1,
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct of_device_id dht11_dt_ids[] = {
|
||||||
|
{ .compatible = "dht11", },
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
|
||||||
|
|
||||||
|
static int dht11_buf_preenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* start work */
|
||||||
|
schedule_delayed_work(&dht11->work, 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* cancel work */
|
||||||
|
cancel_delayed_work_sync(&dht11->work);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
|
||||||
|
.preenable = dht11_buf_preenable,
|
||||||
|
.postenable = dht11_buf_postenable,
|
||||||
|
.postdisable = dht11_buf_postdisable,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_handle(int irq, void * p)
|
||||||
|
{
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
pf->timestamp = iio_get_time_ns(pf->indio_dev);
|
||||||
|
return IRQ_WAKE_THREAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_thread(int irq, void * p)
|
||||||
|
{
|
||||||
|
/* read data */
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
struct iio_dev *iio_dev = pf->indio_dev;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
|
||||||
|
int ret, timeres, offset;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpio_direction_output(dht11->gpio, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpio_direction_input(dht11->gpio);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
if (vals[0] > 30)
|
||||||
|
{
|
||||||
|
iio_push_event(iio_dev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
|
||||||
|
iio_get_time_ns(iio_dev));
|
||||||
|
}
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
iio_trigger_notify_done(iio_dev->trig);
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static irqreturn_t dht11_event_handler(int irq, void *private)
|
||||||
|
{
|
||||||
|
struct iio_poll_func *pf = private;
|
||||||
|
struct iio_dev *idev = pf->indio_dev;
|
||||||
|
|
||||||
|
iio_push_event(idev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
|
||||||
|
iio_get_time_ns(idev));
|
||||||
|
|
||||||
|
iio_trigger_notify_done(idev->trig);
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buffer_init(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
#if 0
|
||||||
|
struct iio_buffer *buffer;
|
||||||
|
|
||||||
|
buffer = iio_kfifo_allocate();
|
||||||
|
if (!buffer)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
iio_device_attach_buffer(indio_dev, buffer);
|
||||||
|
|
||||||
|
/* Ring buffer functions - here trigger setup related */
|
||||||
|
indio_dev->setup_ops = &dht11_buf_setup_ops;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
|
//int ret = iio_triggered_event_setup(indio_dev, NULL, dht11_event_handler);
|
||||||
|
//if (ret)
|
||||||
|
// return ret;
|
||||||
|
|
||||||
|
return iio_triggered_buffer_setup(indio_dev, dht11_triggered_buf_handle, dht11_triggered_buf_thread, NULL);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dht11_work(struct work_struct *work)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = container_of(work, struct dht11, work.work);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
struct iio_dev *iio_dev = dht11->iio;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpio_direction_output(dht11->gpio, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpio_direction_input(dht11->gpio);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
/* re-start work */
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
/* 有错误 */
|
||||||
|
schedule_delayed_work(&dht11->work, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
schedule_delayed_work(&dht11->work, HZ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const unsigned long dht11_scan_masks[] = {0x3, 0};
|
||||||
|
|
||||||
|
static int dht11_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
|
struct device_node *node = dev->of_node;
|
||||||
|
struct dht11 *dht11;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
iio = devm_iio_device_alloc(dev, sizeof(*dht11));
|
||||||
|
if (!iio) {
|
||||||
|
dev_err(dev, "Failed to allocate IIO device\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11 = iio_priv(iio);
|
||||||
|
dht11->dev = dev;
|
||||||
|
dht11->iio = iio;
|
||||||
|
|
||||||
|
ret = of_get_gpio(node, 0);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
dht11->gpio = ret;
|
||||||
|
ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
dht11->irq = gpio_to_irq(dht11->gpio);
|
||||||
|
if (dht11->irq < 0) {
|
||||||
|
dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
|
||||||
|
platform_set_drvdata(pdev, iio);
|
||||||
|
|
||||||
|
init_completion(&dht11->completion);
|
||||||
|
mutex_init(&dht11->lock);
|
||||||
|
iio->name = pdev->name;
|
||||||
|
iio->dev.parent = &pdev->dev;
|
||||||
|
iio->info = &dht11_iio_info;
|
||||||
|
iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
|
||||||
|
iio->channels = dht11_chan_spec;
|
||||||
|
iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
|
||||||
|
iio->available_scan_masks = dht11_scan_masks;
|
||||||
|
|
||||||
|
dht11_buffer_init(iio);
|
||||||
|
|
||||||
|
/* init work */
|
||||||
|
INIT_DELAYED_WORK(&dht11->work, dht11_work);
|
||||||
|
|
||||||
|
return devm_iio_device_register(dev, iio);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct platform_driver dht11_driver = {
|
||||||
|
.driver = {
|
||||||
|
.name = DRIVER_NAME,
|
||||||
|
.of_match_table = dht11_dt_ids,
|
||||||
|
},
|
||||||
|
.probe = dht11_probe,
|
||||||
|
};
|
||||||
|
|
||||||
|
module_platform_driver(dht11_driver);
|
||||||
|
|
||||||
|
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
|
||||||
|
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
|
||||||
|
MODULE_LICENSE("GPL v2");
|
||||||
4
IMX6ULL/source/14_iio/05_dht11_iio_event/dht11.dts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
humidity_sensor {
|
||||||
|
compatible = "dht11";
|
||||||
|
gpios = <&gpio4 19 GPIO_ACTIVE_HIGH>;
|
||||||
|
};
|
||||||
50
IMX6ULL/source/14_iio/05_dht11_iio_event/dht11_test.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
#include <linux/iio/events.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int ret;
|
||||||
|
int event_fd;
|
||||||
|
struct iio_event_data event;
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s /dev/iio\\:deviceX\n", argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open(argv[1], O_RDWR); // /dev/iio:device2
|
||||||
|
if (fd < 0) {
|
||||||
|
printf("can not open %s\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
|
||||||
|
if (ret < 0) {
|
||||||
|
printf("can not get event fd\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
ret = read(event_fd, &event, sizeof(event));
|
||||||
|
if (ret == sizeof(event))
|
||||||
|
{
|
||||||
|
printf("Get event: 0x%lx\n", event.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
751
IMX6ULL/source/14_iio/06_dht11_iio_event_课堂现场编写/dht11.c
Normal file
@@ -0,0 +1,751 @@
|
|||||||
|
/*
|
||||||
|
* DHT11/DHT22 bit banging GPIO driver
|
||||||
|
*
|
||||||
|
* Copyright (c) Harald Geyer <harald@ccbib.org>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/err.h>
|
||||||
|
#include <linux/interrupt.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/printk.h>
|
||||||
|
#include <linux/slab.h>
|
||||||
|
#include <linux/of.h>
|
||||||
|
#include <linux/of_device.h>
|
||||||
|
#include <linux/sysfs.h>
|
||||||
|
#include <linux/io.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/wait.h>
|
||||||
|
#include <linux/bitops.h>
|
||||||
|
#include <linux/completion.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/delay.h>
|
||||||
|
#include <linux/gpio.h>
|
||||||
|
#include <linux/of_gpio.h>
|
||||||
|
#include <linux/timekeeping.h>
|
||||||
|
|
||||||
|
#include <linux/iio/iio.h>
|
||||||
|
#include <linux/iio/buffer.h>
|
||||||
|
#include <linux/iio/kfifo_buf.h>
|
||||||
|
#include <linux/iio/trigger_consumer.h>
|
||||||
|
#include <linux/iio/triggered_buffer.h>
|
||||||
|
#include <linux/iio/triggered_event.h>
|
||||||
|
#include <linux/iio/events.h>
|
||||||
|
|
||||||
|
#define DRIVER_NAME "dht11"
|
||||||
|
|
||||||
|
#define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
|
||||||
|
|
||||||
|
#define DHT11_EDGES_PREAMBLE 2
|
||||||
|
#define DHT11_BITS_PER_READ 40
|
||||||
|
/*
|
||||||
|
* Note that when reading the sensor actually 84 edges are detected, but
|
||||||
|
* since the last edge is not significant, we only store 83:
|
||||||
|
*/
|
||||||
|
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
|
||||||
|
DHT11_EDGES_PREAMBLE + 1)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Data transmission timing:
|
||||||
|
* Data bits are encoded as pulse length (high time) on the data line.
|
||||||
|
* 0-bit: 22-30uS -- typically 26uS (AM2302)
|
||||||
|
* 1-bit: 68-75uS -- typically 70uS (AM2302)
|
||||||
|
* The acutal timings also depend on the properties of the cable, with
|
||||||
|
* longer cables typically making pulses shorter.
|
||||||
|
*
|
||||||
|
* Our decoding depends on the time resolution of the system:
|
||||||
|
* timeres > 34uS ... don't know what a 1-tick pulse is
|
||||||
|
* 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
|
||||||
|
* 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
|
||||||
|
* timeres < 23uS ... no problem
|
||||||
|
*
|
||||||
|
* Luckily clocks in the 33-44kHz range are quite uncommon, so we can
|
||||||
|
* support most systems if the threshold for decoding a pulse as 1-bit
|
||||||
|
* is chosen carefully. If somebody really wants to support clocks around
|
||||||
|
* 40kHz, where this driver is most unreliable, there are two options.
|
||||||
|
* a) select an implementation using busy loop polling on those systems
|
||||||
|
* b) use the checksum to do some probabilistic decoding
|
||||||
|
*/
|
||||||
|
#define DHT11_START_TRANSMISSION_MIN 18000 /* us */
|
||||||
|
#define DHT11_START_TRANSMISSION_MAX 20000 /* us */
|
||||||
|
#define DHT11_MIN_TIMERES 34000 /* ns */
|
||||||
|
#define DHT11_THRESHOLD 49000 /* ns */
|
||||||
|
#define DHT11_AMBIG_LOW 23000 /* ns */
|
||||||
|
#define DHT11_AMBIG_HIGH 30000 /* ns */
|
||||||
|
|
||||||
|
struct dht11 {
|
||||||
|
struct device *dev;
|
||||||
|
|
||||||
|
int gpio;
|
||||||
|
int irq;
|
||||||
|
|
||||||
|
struct completion completion;
|
||||||
|
/* The iio sysfs interface doesn't prevent concurrent reads: */
|
||||||
|
struct mutex lock;
|
||||||
|
|
||||||
|
s64 timestamp;
|
||||||
|
int temperature;
|
||||||
|
int humidity;
|
||||||
|
|
||||||
|
/* num_edges: -1 means "no transmission in progress" */
|
||||||
|
int num_edges;
|
||||||
|
struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
|
||||||
|
struct delayed_work work;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
int temp_ev_enable;
|
||||||
|
int temp_thres_up;
|
||||||
|
int temp_thres_down;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
/*
|
||||||
|
* dht11_edges_print: show the data as actually received by the
|
||||||
|
* driver.
|
||||||
|
*/
|
||||||
|
static void dht11_edges_print(struct dht11 *dht11)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
|
||||||
|
for (i = 1; i < dht11->num_edges; ++i) {
|
||||||
|
dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
|
||||||
|
dht11->edges[i].ts - dht11->edges[i - 1].ts,
|
||||||
|
dht11->edges[i - 1].value ? "high" : "low");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* CONFIG_DYNAMIC_DEBUG */
|
||||||
|
|
||||||
|
static unsigned char dht11_decode_byte(char *bits)
|
||||||
|
{
|
||||||
|
unsigned char ret = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 8; ++i) {
|
||||||
|
ret <<= 1;
|
||||||
|
if (bits[i])
|
||||||
|
++ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_decode(struct dht11 *dht11, int offset)
|
||||||
|
{
|
||||||
|
int i, t;
|
||||||
|
char bits[DHT11_BITS_PER_READ];
|
||||||
|
unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
|
||||||
|
|
||||||
|
for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
|
||||||
|
t = dht11->edges[offset + 2 * i + 2].ts -
|
||||||
|
dht11->edges[offset + 2 * i + 1].ts;
|
||||||
|
if (!dht11->edges[offset + 2 * i + 1].value) {
|
||||||
|
dev_dbg(dht11->dev,
|
||||||
|
"lost synchronisation at edge %d\n",
|
||||||
|
offset + 2 * i + 1);
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
bits[i] = t > DHT11_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
|
hum_int = dht11_decode_byte(bits);
|
||||||
|
hum_dec = dht11_decode_byte(&bits[8]);
|
||||||
|
temp_int = dht11_decode_byte(&bits[16]);
|
||||||
|
temp_dec = dht11_decode_byte(&bits[24]);
|
||||||
|
checksum = dht11_decode_byte(&bits[32]);
|
||||||
|
|
||||||
|
if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
|
||||||
|
dev_dbg(dht11->dev, "invalid checksum\n");
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boot_ns();
|
||||||
|
if (hum_int < 20) { /* DHT22 */
|
||||||
|
dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
|
||||||
|
((temp_int & 0x80) ? -100 : 100);
|
||||||
|
dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
|
||||||
|
} else { /* DHT11 */
|
||||||
|
dht11->temperature = temp_int;;
|
||||||
|
dht11->humidity = hum_int;;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* IRQ handler called on GPIO edges
|
||||||
|
*/
|
||||||
|
static irqreturn_t dht11_handle_irq(int irq, void *data)
|
||||||
|
{
|
||||||
|
struct iio_dev *iio = data;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio);
|
||||||
|
|
||||||
|
/* TODO: Consider making the handler safe for IRQ sharing */
|
||||||
|
if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
|
||||||
|
dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
|
||||||
|
dht11->edges[dht11->num_edges++].value =
|
||||||
|
gpio_get_value(dht11->gpio);
|
||||||
|
|
||||||
|
if (dht11->num_edges >= DHT11_EDGES_PER_READ)
|
||||||
|
complete(&dht11->completion);
|
||||||
|
}
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_raw(struct iio_dev *iio_dev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
int *val, int *val2, long m)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpio_direction_output(dht11->gpio, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpio_direction_input(dht11->gpio);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = IIO_VAL_INT;
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
*val = dht11->temperature;
|
||||||
|
else if (chan->type == IIO_HUMIDITYRELATIVE)
|
||||||
|
*val = dht11->humidity;
|
||||||
|
else
|
||||||
|
ret = -EINVAL;
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_event_config(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
return !!dht11->temp_ev_enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_write_event_config(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir, int state)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
dht11->temp_ev_enable = state;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_event_value(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir,
|
||||||
|
enum iio_event_info info,
|
||||||
|
int *val, int *val2)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
{
|
||||||
|
if (dir == IIO_EV_DIR_FALLING) {
|
||||||
|
*val = dht11->temp_thres_down;
|
||||||
|
} else {
|
||||||
|
*val = dht11->temp_thres_up;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return IIO_VAL_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_write_event_value(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir,
|
||||||
|
enum iio_event_info info,
|
||||||
|
int val, int val2)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
{
|
||||||
|
if (dir == IIO_EV_DIR_FALLING) {
|
||||||
|
dht11->temp_thres_down = val;
|
||||||
|
} else {
|
||||||
|
dht11->temp_thres_up = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return IIO_VAL_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct iio_info dht11_iio_info = {
|
||||||
|
.driver_module = THIS_MODULE,
|
||||||
|
.read_raw = dht11_read_raw,
|
||||||
|
.write_event_value = dht11_write_event_value,
|
||||||
|
.read_event_value = dht11_read_event_value,
|
||||||
|
.read_event_config = dht11_read_event_config,
|
||||||
|
.write_event_config = dht11_write_event_config,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct iio_event_spec dht11_temp_events[] = {
|
||||||
|
{
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_RISING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_FALLING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_EITHER,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static const struct iio_chan_spec dht11_chan_spec[] = {
|
||||||
|
{ .type = IIO_TEMP,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_index = 0,
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
|
||||||
|
.event_spec = dht11_temp_events,
|
||||||
|
.num_event_specs = ARRAY_SIZE(dht11_temp_events),
|
||||||
|
|
||||||
|
},
|
||||||
|
{ .type = IIO_HUMIDITYRELATIVE,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_index = 1,
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct of_device_id dht11_dt_ids[] = {
|
||||||
|
{ .compatible = "dht11", },
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
|
||||||
|
|
||||||
|
static int dht11_buf_preenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* start work */
|
||||||
|
schedule_delayed_work(&dht11->work, 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* cancel work */
|
||||||
|
cancel_delayed_work_sync(&dht11->work);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
|
||||||
|
.preenable = dht11_buf_preenable,
|
||||||
|
.postenable = dht11_buf_postenable,
|
||||||
|
.postdisable = dht11_buf_postdisable,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_handle(int irq, void * p)
|
||||||
|
{
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
pf->timestamp = iio_get_time_ns(pf->indio_dev);
|
||||||
|
return IRQ_WAKE_THREAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_thread(int irq, void * p)
|
||||||
|
{
|
||||||
|
/* read data */
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
struct iio_dev *iio_dev = pf->indio_dev;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
|
||||||
|
int ret, timeres, offset;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpio_direction_output(dht11->gpio, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpio_direction_input(dht11->gpio);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
if (dht11->temp_ev_enable)
|
||||||
|
{
|
||||||
|
if (vals[0] > dht11->temp_thres_up)
|
||||||
|
{
|
||||||
|
iio_push_event(iio_dev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
|
||||||
|
iio_get_time_ns(iio_dev));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vals[0] < dht11->temp_thres_down)
|
||||||
|
{
|
||||||
|
iio_push_event(iio_dev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
|
||||||
|
iio_get_time_ns(iio_dev));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
iio_trigger_notify_done(iio_dev->trig);
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int dht11_buffer_init(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
#if 0
|
||||||
|
struct iio_buffer *buffer;
|
||||||
|
|
||||||
|
buffer = iio_kfifo_allocate();
|
||||||
|
if (!buffer)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
iio_device_attach_buffer(indio_dev, buffer);
|
||||||
|
|
||||||
|
/* Ring buffer functions - here trigger setup related */
|
||||||
|
indio_dev->setup_ops = &dht11_buf_setup_ops;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
|
return iio_triggered_buffer_setup(indio_dev, dht11_triggered_buf_handle, dht11_triggered_buf_thread, NULL);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dht11_work(struct work_struct *work)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = container_of(work, struct dht11, work.work);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
struct iio_dev *iio_dev = dht11->iio;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpio_direction_output(dht11->gpio, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpio_direction_input(dht11->gpio);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
/* re-start work */
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
/* 有错误 */
|
||||||
|
schedule_delayed_work(&dht11->work, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
schedule_delayed_work(&dht11->work, HZ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const unsigned long dht11_scan_masks[] = {0x3, 0};
|
||||||
|
|
||||||
|
static int dht11_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
|
struct device_node *node = dev->of_node;
|
||||||
|
struct dht11 *dht11;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
iio = devm_iio_device_alloc(dev, sizeof(*dht11));
|
||||||
|
if (!iio) {
|
||||||
|
dev_err(dev, "Failed to allocate IIO device\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11 = iio_priv(iio);
|
||||||
|
dht11->dev = dev;
|
||||||
|
dht11->iio = iio;
|
||||||
|
|
||||||
|
ret = of_get_gpio(node, 0);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
dht11->gpio = ret;
|
||||||
|
ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
dht11->irq = gpio_to_irq(dht11->gpio);
|
||||||
|
if (dht11->irq < 0) {
|
||||||
|
dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
|
||||||
|
platform_set_drvdata(pdev, iio);
|
||||||
|
|
||||||
|
init_completion(&dht11->completion);
|
||||||
|
mutex_init(&dht11->lock);
|
||||||
|
iio->name = pdev->name;
|
||||||
|
iio->dev.parent = &pdev->dev;
|
||||||
|
iio->info = &dht11_iio_info;
|
||||||
|
iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
|
||||||
|
iio->channels = dht11_chan_spec;
|
||||||
|
iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
|
||||||
|
iio->available_scan_masks = dht11_scan_masks;
|
||||||
|
|
||||||
|
dht11_buffer_init(iio);
|
||||||
|
|
||||||
|
/* init work */
|
||||||
|
INIT_DELAYED_WORK(&dht11->work, dht11_work);
|
||||||
|
|
||||||
|
return devm_iio_device_register(dev, iio);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct platform_driver dht11_driver = {
|
||||||
|
.driver = {
|
||||||
|
.name = DRIVER_NAME,
|
||||||
|
.of_match_table = dht11_dt_ids,
|
||||||
|
},
|
||||||
|
.probe = dht11_probe,
|
||||||
|
};
|
||||||
|
|
||||||
|
module_platform_driver(dht11_driver);
|
||||||
|
|
||||||
|
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
|
||||||
|
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
|
||||||
|
MODULE_LICENSE("GPL v2");
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
humidity_sensor {
|
||||||
|
compatible = "dht11";
|
||||||
|
gpios = <&gpio4 19 GPIO_ACTIVE_HIGH>;
|
||||||
|
};
|
||||||
50
IMX6ULL/source/14_iio/06_dht11_iio_event_课堂现场编写/dht11_test.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
#include <linux/iio/events.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int ret;
|
||||||
|
int event_fd;
|
||||||
|
struct iio_event_data event;
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s /dev/iio\\:deviceX\n", argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open(argv[1], O_RDWR); // /dev/iio:device2
|
||||||
|
if (fd < 0) {
|
||||||
|
printf("can not open %s\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
|
||||||
|
if (ret < 0) {
|
||||||
|
printf("can not get event fd\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
ret = read(event_fd, &event, sizeof(event));
|
||||||
|
if (ret == sizeof(event))
|
||||||
|
{
|
||||||
|
printf("Get event: 0x%lx\n", event.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
24
README.md
@@ -880,41 +880,41 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
|
|||||||
[05-4-5]_使用media子系统和subdev的APP
|
[05-4-5]_使用media子系统和subdev的APP
|
||||||
```
|
```
|
||||||
|
|
||||||
* 2024.10.25 发布IIO"子系统"
|
* 2024.10.25 发布"IIO子系统"
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
01.1_IIO子系统简化框架
|
01.1_IIO子系统简化框架
|
||||||
01.2_DHT11操作原理与编程思路
|
01.2_DHT11操作原理与编程思路
|
||||||
```
|
```
|
||||||
|
|
||||||
* 2024.10.31 发布IIO"子系统"
|
* 2024.10.31 发布"IIO子系统"
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
01.3_DHT11驱动程序体验_IMX6ULL
|
01.3_DHT11驱动程序体验_IMX6ULL
|
||||||
01.3_DHT11驱动程序体验_STM32MP157
|
01.3_DHT11驱动程序体验_STM32MP157
|
||||||
```
|
```
|
||||||
|
|
||||||
* 2024.11.05 发布IIO"子系统"
|
* 2024.11.05 发布"IIO子系统"
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
01.4_DHT11驱动程序分析
|
01.4_DHT11驱动程序分析
|
||||||
01.5_通道的sysfs信息修改与体验
|
01.5_通道的sysfs信息修改与体验
|
||||||
```
|
```
|
||||||
|
|
||||||
* 2024.11.07 发布IIO"子系统"
|
* 2024.11.07 发布"IIO子系统"
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
02.1_iio_buffer的核心要素与体验
|
02.1_iio_buffer的核心要素与体验
|
||||||
```
|
```
|
||||||
|
|
||||||
* 2024.11.08 发布IIO"子系统"
|
* 2024.11.08 发布"IIO子系统"
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
02.2_增加iio_buffer并体验sysfs
|
02.2_增加iio_buffer并体验sysfs
|
||||||
02.3_实现iio_buffer的写入
|
02.3_实现iio_buffer的写入
|
||||||
```
|
```
|
||||||
|
|
||||||
* 2024.11.25 发布IIO"子系统"
|
* 2024.11.25 发布"IIO子系统"
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
03.1_iio_trigger的引入与体验
|
03.1_iio_trigger的引入与体验
|
||||||
@@ -923,6 +923,18 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
|
|||||||
03.4_修改DHT11驱动使用iio_trigger
|
03.4_修改DHT11驱动使用iio_trigger
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 2024.11.28 完结"IIO子系统"
|
||||||
|
|
||||||
|
```shell
|
||||||
|
04.1_iio_event的引入与体验
|
||||||
|
04.2_修改DHT11驱动使用iio_event_编程
|
||||||
|
04.3_修改DHT11驱动使用iio_event_上机测试
|
||||||
|
04.4_使用trigger写iio_event(不实用)
|
||||||
|
05.1_驱动示例_IMX6ULL的ADC驱动
|
||||||
|
05.2_驱动示例_STM32MP157的ADC驱动
|
||||||
|
05.3_STM32MP157的ADC驱动分析
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 6. 联系方式
|
## 6. 联系方式
|
||||||
|
|||||||
BIN
STM32MP157/doc_pic/14_IIO/04.1_iio_event引入与体验.tif
Normal file
BIN
STM32MP157/doc_pic/14_IIO/04.2_修改DHT11驱动使用iio_event.tif
Normal file
BIN
STM32MP157/doc_pic/14_IIO/04.3_使用trigger写iio_event(不实用).tif
Normal file
363
STM32MP157/doc_pic/14_IIO/04_iio_event的使用.md
Normal file
@@ -0,0 +1,363 @@
|
|||||||
|
# iio_event的使用
|
||||||
|
|
||||||
|
IIO(Industrial I/O)参考资料:
|
||||||
|
|
||||||
|
* 系列文章:https://blog.csdn.net/lickylin/article/details/108177756
|
||||||
|
* https://www.cnblogs.com/yongleili717/p/10758691.html
|
||||||
|
* 内核文档:https://www.kernel.org/doc/html/v5.3/driver-api/iio
|
||||||
|
* 参考内核源码
|
||||||
|
* `drivers\staging\iio\impedance-analyzer\ad5933.c`
|
||||||
|
* `drivers\iio\adc\hi8435.c`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 1. iio_event的引入与体验
|
||||||
|
|
||||||
|
### 1.1 问题引入与解决方法
|
||||||
|
|
||||||
|
以温湿度传感器为例,它的温度值超过极限了,APP如何检测到这个事件?
|
||||||
|
|
||||||
|
* APP读取某个设备文件
|
||||||
|
* 传感器驱动提供数据:
|
||||||
|
* 比如硬件监测到温度超标时,触发硬件中断,在中断里使用iio_push_event把"事件"写入kfifo
|
||||||
|
* APP读取kfifo得到"事件"
|
||||||
|
* iio_trigger提供数据:
|
||||||
|
* 有些硬件没有中断引脚,那么可以使用iio_trigger
|
||||||
|
* iio_trigger直接调用驱动程序提供的的另一个"虚拟中断处理函数"(跟iio_buffer不一样的中断处理函数)
|
||||||
|
* 这个中断处理函数读取硬件数据,使用iio_push_event把"事件"写入kfifo
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 1.2 怎么表示事件
|
||||||
|
|
||||||
|
使用结构体"iio_event_data"表示事件:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
所谓事件就是一个64位的ID,里面不含有具体的数值:比如它表示温度超高了,但是没有表示温度值是多少。
|
||||||
|
|
||||||
|
这个ID如何表示不同的事件呢?如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.3 上机体验
|
||||||
|
|
||||||
|
IMX6ULL的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
STM32MP157的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 1.3.1 IMX6ULL
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device2
|
||||||
|
/root/dht11_test /dev/iio\:device2 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 1.3.2 STM32MP157
|
||||||
|
|
||||||
|
```SHELL
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
/root/dht11_test /dev/iio\:device1 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 2. iio_event数据流程
|
||||||
|
|
||||||
|
### 2.1 驱动程序创建KFIFO
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.2 驱动程序写KFIFO
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.3 APP读取KFIFO
|
||||||
|
|
||||||
|
示例源码如下:
|
||||||
|
|
||||||
|
```c
|
||||||
|
int event_fd;
|
||||||
|
struct iio_event_data event;
|
||||||
|
|
||||||
|
int fd = open("/dev/iio:device2", O_RDWR);
|
||||||
|
|
||||||
|
ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
|
||||||
|
read(event_fd, &event, sizeof(event));
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 3. 修改DHT11驱动使用iio_event
|
||||||
|
|
||||||
|
IMX6ULL的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
STM32MP157的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 3.1 设备表明自己有event能力
|
||||||
|
|
||||||
|
在注册iio设备时,会进行如下判断:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
只有设置了indio_dev->info->event_attrs或某个通道的num_event_specs不为0,才会创建KFIFO。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
示例代码如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
这会生成如下sysfs文件:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
生成sysfs文件的流程为:
|
||||||
|
|
||||||
|
```c
|
||||||
|
iio_device_register
|
||||||
|
iio_device_register_eventset
|
||||||
|
__iio_add_event_config_attrs
|
||||||
|
for (j = 0; j < indio_dev->num_channels; j++) {
|
||||||
|
ret = iio_device_add_event_sysfs(indio_dev,
|
||||||
|
&indio_dev->channels[j]);
|
||||||
|
iio_device_add_event
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
这些sysfs对应的读写函数如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 3.2 设备驱动提供sysfs接口函数
|
||||||
|
|
||||||
|
比如我们想指定温度的报警值,那么就需要实现这些sysfs的接口函数:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 3.3 上机实验
|
||||||
|
|
||||||
|
#### 1.3.1 IMX6ULL
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device2
|
||||||
|
/root/dht11_test /dev/iio\:device2 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 1.3.2 STM32MP157
|
||||||
|
|
||||||
|
```SHELL
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
/root/dht11_test /dev/iio\:device1 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 4. 使用trigger写iio_event(不实用)
|
||||||
|
|
||||||
|
IMX6ULL的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
STM32MP157的源码:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 4.1 框图
|
||||||
|
|
||||||
|
下图中,左侧写kfifo的红色线条,就是使用trigger写iio_event:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 4.2 提供虚拟中断处理函数
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 4.3 注册虚拟中断处理函数
|
||||||
|
|
||||||
|
设置iio_device的trgger,比如如下操作:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
```
|
||||||
|
|
||||||
|
会导致如下函数被调用,它就注册了"记录在pollfunc_event里的"虚拟中断函数:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
注意,对比一下:
|
||||||
|
|
||||||
|
* "记录在pollfunc_event里的"虚拟中断函数:
|
||||||
|
* 在设置iio设备的current_trigger时就注册了中断, 就会使能trigger state
|
||||||
|
* 用来产生iio_event,写KFIFO
|
||||||
|
* "记录在pollfunc里的"虚拟中断函数:
|
||||||
|
* 在设置iio设备的current_trigger后、并且使能了buffer,才注册中断, 就会使能trigger state
|
||||||
|
* 用来读取传感器数据,写iio_buffer
|
||||||
|
|
||||||
|
### 4.4 上机体验
|
||||||
|
|
||||||
|
#### 4.4.1 修改内核支持
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 4.4.2 具体操作
|
||||||
|
|
||||||
|
IMX6ULL:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/industrialio-triggered-event.ko
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device2/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device2
|
||||||
|
/root/dht11_test /dev/iio\:device2 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
STM32MP157:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
|
||||||
|
insmod /root/industrialio-triggered-event.ko
|
||||||
|
|
||||||
|
insmod /root/dht11.ko
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 30 > events/in_temp_thresh_rising_value # 设置上限报警值
|
||||||
|
echo 10 > events/in_temp_thresh_falling_value # 设置下限报警值
|
||||||
|
echo 1 > events/in_temp_thresh_either_en # 使能event
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_humidityrelative_en
|
||||||
|
echo 1 > scan_elements/in_temp_en
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
/root/dht11_test /dev/iio\:device1 # 读取事件
|
||||||
|
```
|
||||||
|
|
||||||
BIN
STM32MP157/doc_pic/14_IIO/05.1_驱动示例_IMX6ULL的ADC驱动.tif
Normal file
BIN
STM32MP157/doc_pic/14_IIO/05.2_驱动示例_STM32MP157的ADC驱动.tif
Normal file
217
STM32MP157/doc_pic/14_IIO/05_iio驱动示例.md
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
# iio驱动示例
|
||||||
|
|
||||||
|
IIO(Industrial I/O)参考资料:
|
||||||
|
|
||||||
|
* 系列文章:https://blog.csdn.net/lickylin/article/details/108177756
|
||||||
|
* https://www.cnblogs.com/yongleili717/p/10758691.html
|
||||||
|
* 内核文档:https://www.kernel.org/doc/html/v5.3/driver-api/iio
|
||||||
|
* 参考内核源码
|
||||||
|
* IMX6ULL: `drivers\iio\adc\vf610_adc.c`
|
||||||
|
* STM32MP157: `drivers\iio\adc\stm32-adc-core.c`,`drivers\iio\adc\stm32-adc.c`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 1. IMX6ULL ADC驱动
|
||||||
|
|
||||||
|
### 1.1 设备树
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.2 驱动程序
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.3 驱动程序分析
|
||||||
|
|
||||||
|
```shell
|
||||||
|
static int vf610_read_raw(struct iio_dev *indio_dev,
|
||||||
|
struct iio_chan_spec const *chan,
|
||||||
|
int *val,
|
||||||
|
int *val2,
|
||||||
|
long mask)
|
||||||
|
|
||||||
|
怎么使用val, val2 ?
|
||||||
|
|
||||||
|
IIO_VAL_INT : 只有整数没有小数, 整数保存在val里,val2不使用。
|
||||||
|
IIO_VAL_INT_PLUS_MICRO : val=整数,val2=小数部分*1000000,比如3.14如此保存:*val = 3; *val2 = 140000
|
||||||
|
IIO_VAL_INT_PLUS_NANO : val=整数,val2=小数部分*1000000000,比如3.14如此保存:*val = 3; *val2 = 140000000
|
||||||
|
IIO_VAL_INT_PLUS_MICRO_DB : DB数据,数值的表示方法跟IIO_VAL_INT_PLUS_MICRO一样,添加了单位DB。
|
||||||
|
IIO_VAL_INT_MULTIPLE : 返回多个整数值,主要用于iio_info的read_raw_multi函数。
|
||||||
|
IIO_VAL_FRACTIONAL : 分数,比如n/m,表示为:*val=n, *val2=m
|
||||||
|
IIO_VAL_FRACTIONAL_LOG2 : LOG2值, 比如*val=1,*val2=4,表示的数值为:val>>val2, 即1>>4=0.03125
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 1.4 驱动程序使用
|
||||||
|
|
||||||
|
#### 1.4.1 各类值的含义
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 1.4.2 示例
|
||||||
|
|
||||||
|
IMX6ULL原理图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
可以通过扩展板引出,原理图如下:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
测试时接线方法:可以把ADC1_CH3或ADC1_CH4,接到3V3或GND,如下图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
操作命令:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# 单步操作
|
||||||
|
cd /sys/bus/iio/devices/iio:device0
|
||||||
|
cat in_voltage3_raw
|
||||||
|
|
||||||
|
# 使用buffer
|
||||||
|
insmod /root/iio-trig-hrtimer.ko
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
# 设置trigger周期
|
||||||
|
cd /sys/bus/iio/devices/trigger1
|
||||||
|
echo 1000000000 > sampling_frequency # 单位ns, 1s一次
|
||||||
|
|
||||||
|
cd /sys/bus/iio/devices/iio\:device0/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_voltage3_en
|
||||||
|
echo 1024 > buffer/length
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
hexdump /dev/iio\:device0 # 读取数据
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
注意:使用buffer功能会死机,原因未明。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 2. STM32MP157 ADC驱动
|
||||||
|
|
||||||
|
### 2.1 设备树与驱动
|
||||||
|
|
||||||
|
如何确定设备树与驱动?百问网STM32MP157开发板使用的设备树文件是`stm32mp157c-100ask-512d-lcd-v1.dtb`,可以在Ubuntu使用如下命令反汇编得到dts文件:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
dtc -I dtb -O dts stm32mp157c-100ask-512d-lcd-v1.dtb > 1.dts
|
||||||
|
```
|
||||||
|
|
||||||
|
在1.dts中搜`adc`,可以看到如下节点,根据里面的`compatible`属性即可找到设备树和驱动:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.1.1 设备树文件
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
#### 2.1.2 驱动文件
|
||||||
|
|
||||||
|
`drivers\iio\adc\stm32-adc-core.c`,`drivers\iio\adc\stm32-adc.c`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.2 为何驱动程序无法使用
|
||||||
|
|
||||||
|
#### 2.2.1 pinmux空缺
|
||||||
|
|
||||||
|
查看内核打印信息:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
[ 4.112842] stm32mp157-pinctrl soc:pin-controller@50002000: missing pins property in node pins .
|
||||||
|
[ 4.120360] stm32-adc-core: probe of 48003000.adc failed with error -22
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
根据错误信息"missing pins property in node",找到驱动:pinctrl/stm32/pinctrl-stm32.c
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
在设备树里确实发现ADC使用的pinctrl里没有"pinmux"属性:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
如此修改:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.2.2 提供regulator
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.3 上机实验
|
||||||
|
|
||||||
|
#### 2.3.1 原理图与接线
|
||||||
|
|
||||||
|
STM32MP157原理图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
扩展板原理图:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
接线方法:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 2.3.2 测试命令
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# 单步操作
|
||||||
|
cd /sys/bus/iio/devices/iio:device1
|
||||||
|
cat in_voltage0_raw
|
||||||
|
|
||||||
|
# 使用buffer
|
||||||
|
# insmod /root/iio-trig-hrtimer.ko # STM32MMP157无需安装驱动
|
||||||
|
mkdir /sys/kernel/config/iio/triggers/hrtimer/timer_abc # 创建trigger
|
||||||
|
cat /sys/bus/iio/devices/trigger1/name # 可以看到这个trigger
|
||||||
|
# 设置trigger周期
|
||||||
|
cd /sys/bus/iio/devices/trigger1
|
||||||
|
echo 1000000000 > sampling_frequency # 单位ns, 1s一次
|
||||||
|
|
||||||
|
cd /sys/bus/iio/devices/iio\:device1/
|
||||||
|
echo timer_abc > trigger/current_trigger # 在设备上使用trigger
|
||||||
|
|
||||||
|
echo 1 > scan_elements/in_voltage0_en
|
||||||
|
echo 1024 > buffer/length
|
||||||
|
echo 1 > buffer/enable
|
||||||
|
|
||||||
|
# hexdump /dev/iio\:device1
|
||||||
|
hexdump /dev/iio\:device1 # 读取数据
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 2.4 驱动程序分析
|
||||||
|
|
||||||
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125161222685.png
Normal file
|
After Width: | Height: | Size: 108 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125163136804.png
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125163406036.png
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125163508774.png
Normal file
|
After Width: | Height: | Size: 90 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125164423639.png
Normal file
|
After Width: | Height: | Size: 55 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125170901645.png
Normal file
|
After Width: | Height: | Size: 72 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125170929132.png
Normal file
|
After Width: | Height: | Size: 19 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125171144376.png
Normal file
|
After Width: | Height: | Size: 197 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125175114799.png
Normal file
|
After Width: | Height: | Size: 191 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125175311198.png
Normal file
|
After Width: | Height: | Size: 194 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125175658789.png
Normal file
|
After Width: | Height: | Size: 81 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125193456857.png
Normal file
|
After Width: | Height: | Size: 102 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241125193714139.png
Normal file
|
After Width: | Height: | Size: 19 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241126094637080.png
Normal file
|
After Width: | Height: | Size: 6.2 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241126094715687.png
Normal file
|
After Width: | Height: | Size: 5.5 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127102844284.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127102920957.png
Normal file
|
After Width: | Height: | Size: 6.6 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127103153056.png
Normal file
|
After Width: | Height: | Size: 80 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127103523330.png
Normal file
|
After Width: | Height: | Size: 355 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127104547343.png
Normal file
|
After Width: | Height: | Size: 39 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127131900125.png
Normal file
|
After Width: | Height: | Size: 55 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127132025400.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127132345881.png
Normal file
|
After Width: | Height: | Size: 60 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127132457129.png
Normal file
|
After Width: | Height: | Size: 96 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127132553738.png
Normal file
|
After Width: | Height: | Size: 138 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127132934371.png
Normal file
|
After Width: | Height: | Size: 949 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127133020707.png
Normal file
|
After Width: | Height: | Size: 953 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241127145118931.png
Normal file
|
After Width: | Height: | Size: 1.1 MiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128095523369.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128095902459.png
Normal file
|
After Width: | Height: | Size: 115 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128105502688.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128105620144.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128131324394.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128132450046.png
Normal file
|
After Width: | Height: | Size: 35 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128133548599.png
Normal file
|
After Width: | Height: | Size: 75 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128133617577.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
BIN
STM32MP157/doc_pic/14_IIO/pic/image-20241128133856564.png
Normal file
|
After Width: | Height: | Size: 756 KiB |
664
STM32MP157/source/A7/14_iio/05_dht11_iio_event/dht11.c
Normal file
@@ -0,0 +1,664 @@
|
|||||||
|
/*
|
||||||
|
* DHT11/DHT22 bit banging GPIO driver
|
||||||
|
*
|
||||||
|
* Copyright (c) Harald Geyer <harald@ccbib.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/err.h>
|
||||||
|
#include <linux/interrupt.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/printk.h>
|
||||||
|
#include <linux/slab.h>
|
||||||
|
#include <linux/of.h>
|
||||||
|
#include <linux/of_device.h>
|
||||||
|
#include <linux/sysfs.h>
|
||||||
|
#include <linux/io.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/wait.h>
|
||||||
|
#include <linux/bitops.h>
|
||||||
|
#include <linux/completion.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/delay.h>
|
||||||
|
#include <linux/gpio/consumer.h>
|
||||||
|
#include <linux/timekeeping.h>
|
||||||
|
|
||||||
|
#include <linux/iio/iio.h>
|
||||||
|
#include <linux/iio/buffer.h>
|
||||||
|
#include <linux/iio/kfifo_buf.h>
|
||||||
|
#include <linux/iio/trigger_consumer.h>
|
||||||
|
#include <linux/iio/triggered_buffer.h>
|
||||||
|
#include <linux/iio/events.h>
|
||||||
|
#include <linux/iio/triggered_event.h>
|
||||||
|
|
||||||
|
#define DRIVER_NAME "dht11"
|
||||||
|
|
||||||
|
#define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
|
||||||
|
|
||||||
|
#define DHT11_EDGES_PREAMBLE 2
|
||||||
|
#define DHT11_BITS_PER_READ 40
|
||||||
|
/*
|
||||||
|
* Note that when reading the sensor actually 84 edges are detected, but
|
||||||
|
* since the last edge is not significant, we only store 83:
|
||||||
|
*/
|
||||||
|
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
|
||||||
|
DHT11_EDGES_PREAMBLE + 1)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Data transmission timing:
|
||||||
|
* Data bits are encoded as pulse length (high time) on the data line.
|
||||||
|
* 0-bit: 22-30uS -- typically 26uS (AM2302)
|
||||||
|
* 1-bit: 68-75uS -- typically 70uS (AM2302)
|
||||||
|
* The acutal timings also depend on the properties of the cable, with
|
||||||
|
* longer cables typically making pulses shorter.
|
||||||
|
*
|
||||||
|
* Our decoding depends on the time resolution of the system:
|
||||||
|
* timeres > 34uS ... don't know what a 1-tick pulse is
|
||||||
|
* 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
|
||||||
|
* 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
|
||||||
|
* timeres < 23uS ... no problem
|
||||||
|
*
|
||||||
|
* Luckily clocks in the 33-44kHz range are quite uncommon, so we can
|
||||||
|
* support most systems if the threshold for decoding a pulse as 1-bit
|
||||||
|
* is chosen carefully. If somebody really wants to support clocks around
|
||||||
|
* 40kHz, where this driver is most unreliable, there are two options.
|
||||||
|
* a) select an implementation using busy loop polling on those systems
|
||||||
|
* b) use the checksum to do some probabilistic decoding
|
||||||
|
*/
|
||||||
|
#define DHT11_START_TRANSMISSION_MIN 18000 /* us */
|
||||||
|
#define DHT11_START_TRANSMISSION_MAX 20000 /* us */
|
||||||
|
#define DHT11_MIN_TIMERES 34000 /* ns */
|
||||||
|
#define DHT11_THRESHOLD 49000 /* ns */
|
||||||
|
#define DHT11_AMBIG_LOW 23000 /* ns */
|
||||||
|
#define DHT11_AMBIG_HIGH 30000 /* ns */
|
||||||
|
|
||||||
|
struct dht11 {
|
||||||
|
struct device *dev;
|
||||||
|
|
||||||
|
struct gpio_desc *gpiod;
|
||||||
|
int irq;
|
||||||
|
|
||||||
|
struct completion completion;
|
||||||
|
/* The iio sysfs interface doesn't prevent concurrent reads: */
|
||||||
|
struct mutex lock;
|
||||||
|
|
||||||
|
s64 timestamp;
|
||||||
|
int temperature;
|
||||||
|
int humidity;
|
||||||
|
|
||||||
|
/* num_edges: -1 means "no transmission in progress" */
|
||||||
|
int num_edges;
|
||||||
|
struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
|
||||||
|
struct delayed_work work;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
/*
|
||||||
|
* dht11_edges_print: show the data as actually received by the
|
||||||
|
* driver.
|
||||||
|
*/
|
||||||
|
static void dht11_edges_print(struct dht11 *dht11)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
|
||||||
|
for (i = 1; i < dht11->num_edges; ++i) {
|
||||||
|
dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
|
||||||
|
dht11->edges[i].ts - dht11->edges[i - 1].ts,
|
||||||
|
dht11->edges[i - 1].value ? "high" : "low");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* CONFIG_DYNAMIC_DEBUG */
|
||||||
|
|
||||||
|
static unsigned char dht11_decode_byte(char *bits)
|
||||||
|
{
|
||||||
|
unsigned char ret = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 8; ++i) {
|
||||||
|
ret <<= 1;
|
||||||
|
if (bits[i])
|
||||||
|
++ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_decode(struct dht11 *dht11, int offset)
|
||||||
|
{
|
||||||
|
int i, t;
|
||||||
|
char bits[DHT11_BITS_PER_READ];
|
||||||
|
unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
|
||||||
|
|
||||||
|
for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
|
||||||
|
t = dht11->edges[offset + 2 * i + 2].ts -
|
||||||
|
dht11->edges[offset + 2 * i + 1].ts;
|
||||||
|
if (!dht11->edges[offset + 2 * i + 1].value) {
|
||||||
|
dev_dbg(dht11->dev,
|
||||||
|
"lost synchronisation at edge %d\n",
|
||||||
|
offset + 2 * i + 1);
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
bits[i] = t > DHT11_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
|
hum_int = dht11_decode_byte(bits);
|
||||||
|
hum_dec = dht11_decode_byte(&bits[8]);
|
||||||
|
temp_int = dht11_decode_byte(&bits[16]);
|
||||||
|
temp_dec = dht11_decode_byte(&bits[24]);
|
||||||
|
checksum = dht11_decode_byte(&bits[32]);
|
||||||
|
|
||||||
|
if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
|
||||||
|
dev_dbg(dht11->dev, "invalid checksum\n");
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boottime_ns();
|
||||||
|
if (hum_int < 4) { /* DHT22: 100000 = (3*256+232)*100 */
|
||||||
|
dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
|
||||||
|
((temp_int & 0x80) ? -100 : 100);
|
||||||
|
dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
|
||||||
|
} else {
|
||||||
|
dht11->temperature = temp_int;
|
||||||
|
dht11->humidity = hum_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* IRQ handler called on GPIO edges
|
||||||
|
*/
|
||||||
|
static irqreturn_t dht11_handle_irq(int irq, void *data)
|
||||||
|
{
|
||||||
|
struct iio_dev *iio = data;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio);
|
||||||
|
|
||||||
|
/* TODO: Consider making the handler safe for IRQ sharing */
|
||||||
|
if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
|
||||||
|
dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
|
||||||
|
dht11->edges[dht11->num_edges++].value =
|
||||||
|
gpiod_get_value(dht11->gpiod);
|
||||||
|
|
||||||
|
if (dht11->num_edges >= DHT11_EDGES_PER_READ)
|
||||||
|
complete(&dht11->completion);
|
||||||
|
}
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_raw(struct iio_dev *iio_dev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
int *val, int *val2, long m)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpiod_direction_output(dht11->gpiod, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpiod_direction_input(dht11->gpiod);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = IIO_VAL_INT;
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
*val = dht11->temperature;
|
||||||
|
else if (chan->type == IIO_HUMIDITYRELATIVE)
|
||||||
|
*val = dht11->humidity;
|
||||||
|
else
|
||||||
|
ret = -EINVAL;
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct iio_info dht11_iio_info = {
|
||||||
|
.read_raw = dht11_read_raw,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct iio_event_spec dht11_temp_events[] = {
|
||||||
|
{
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_RISING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_FALLING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_EITHER,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct iio_chan_spec dht11_chan_spec[] = {
|
||||||
|
{ .type = IIO_TEMP,
|
||||||
|
.scan_index = 0,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
.event_spec = dht11_temp_events,
|
||||||
|
.num_event_specs = ARRAY_SIZE(dht11_temp_events),
|
||||||
|
},
|
||||||
|
{ .type = IIO_HUMIDITYRELATIVE,
|
||||||
|
.scan_index = 1,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct of_device_id dht11_dt_ids[] = {
|
||||||
|
{ .compatible = "dht11", },
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
|
||||||
|
|
||||||
|
static int dht11_buf_preenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* start work */
|
||||||
|
schedule_delayed_work(&dht11->work, 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* cancel work */
|
||||||
|
cancel_delayed_work_sync(&dht11->work);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
|
||||||
|
.preenable = dht11_buf_preenable,
|
||||||
|
.postenable = dht11_buf_postenable,
|
||||||
|
.postdisable = dht11_buf_postdisable,
|
||||||
|
};
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_handle(int irq, void * p)
|
||||||
|
{
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
pf->timestamp = iio_get_time_ns(pf->indio_dev);
|
||||||
|
return IRQ_WAKE_THREAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_thread(int irq, void * p)
|
||||||
|
{
|
||||||
|
/* read data */
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
struct iio_dev *iio_dev = pf->indio_dev;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
|
||||||
|
int ret, timeres, offset;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpiod_direction_output(dht11->gpiod, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpiod_direction_input(dht11->gpiod);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
if (vals[0] > 30)
|
||||||
|
{
|
||||||
|
iio_push_event(iio_dev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
|
||||||
|
iio_get_time_ns(iio_dev));
|
||||||
|
}
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
iio_trigger_notify_done(iio_dev->trig);
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static irqreturn_t dht11_event_handler(int irq, void *private)
|
||||||
|
{
|
||||||
|
struct iio_poll_func *pf = private;
|
||||||
|
struct iio_dev *idev = pf->indio_dev;
|
||||||
|
|
||||||
|
iio_push_event(idev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
|
||||||
|
iio_get_time_ns(idev));
|
||||||
|
|
||||||
|
iio_trigger_notify_done(idev->trig);
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buffer_init(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
#if 0
|
||||||
|
struct iio_buffer *buffer;
|
||||||
|
|
||||||
|
buffer = iio_kfifo_allocate();
|
||||||
|
if (!buffer)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
iio_device_attach_buffer(indio_dev, buffer);
|
||||||
|
|
||||||
|
/* Ring buffer functions - here trigger setup related */
|
||||||
|
indio_dev->setup_ops = &dht11_buf_setup_ops;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
|
//int ret = iio_triggered_event_setup(indio_dev, NULL, dht11_event_handler);
|
||||||
|
//if (ret)
|
||||||
|
// return ret;
|
||||||
|
|
||||||
|
return iio_triggered_buffer_setup(indio_dev, dht11_triggered_buf_handle, dht11_triggered_buf_thread, NULL);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dht11_work(struct work_struct *work)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = container_of(work, struct dht11, work.work);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
struct iio_dev *iio_dev = dht11->iio;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpiod_direction_output(dht11->gpiod, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpiod_direction_input(dht11->gpiod);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
/* re-start work */
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
/* 有错误 */
|
||||||
|
schedule_delayed_work(&dht11->work, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
schedule_delayed_work(&dht11->work, HZ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const unsigned long dht11_scan_masks[] = {0x3, 0};
|
||||||
|
|
||||||
|
static int dht11_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
|
struct dht11 *dht11;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
|
||||||
|
iio = devm_iio_device_alloc(dev, sizeof(*dht11));
|
||||||
|
if (!iio) {
|
||||||
|
dev_err(dev, "Failed to allocate IIO device\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11 = iio_priv(iio);
|
||||||
|
dht11->dev = dev;
|
||||||
|
dht11->iio = iio;
|
||||||
|
|
||||||
|
dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
|
||||||
|
if (IS_ERR(dht11->gpiod))
|
||||||
|
return PTR_ERR(dht11->gpiod);
|
||||||
|
|
||||||
|
|
||||||
|
dht11->irq = gpiod_to_irq(dht11->gpiod);
|
||||||
|
if (dht11->irq < 0) {
|
||||||
|
dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
|
||||||
|
platform_set_drvdata(pdev, iio);
|
||||||
|
|
||||||
|
init_completion(&dht11->completion);
|
||||||
|
mutex_init(&dht11->lock);
|
||||||
|
iio->name = pdev->name;
|
||||||
|
iio->dev.parent = &pdev->dev;
|
||||||
|
iio->info = &dht11_iio_info;
|
||||||
|
iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
|
||||||
|
iio->channels = dht11_chan_spec;
|
||||||
|
iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
|
||||||
|
iio->available_scan_masks = dht11_scan_masks;
|
||||||
|
|
||||||
|
dht11_buffer_init(iio);
|
||||||
|
|
||||||
|
/* init work */
|
||||||
|
INIT_DELAYED_WORK(&dht11->work, dht11_work);
|
||||||
|
|
||||||
|
return devm_iio_device_register(dev, iio);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct platform_driver dht11_driver = {
|
||||||
|
.driver = {
|
||||||
|
.name = DRIVER_NAME,
|
||||||
|
.of_match_table = dht11_dt_ids,
|
||||||
|
},
|
||||||
|
.probe = dht11_probe,
|
||||||
|
};
|
||||||
|
|
||||||
|
module_platform_driver(dht11_driver);
|
||||||
|
|
||||||
|
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
|
||||||
|
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
|
||||||
|
MODULE_LICENSE("GPL v2");
|
||||||
4
STM32MP157/source/A7/14_iio/05_dht11_iio_event/dht11.dts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
humidity_sensor {
|
||||||
|
compatible = "dht11";
|
||||||
|
gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>;
|
||||||
|
};
|
||||||
50
STM32MP157/source/A7/14_iio/05_dht11_iio_event/dht11_test.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
#include <linux/iio/events.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int ret;
|
||||||
|
int event_fd;
|
||||||
|
struct iio_event_data event;
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s /dev/iio\\:deviceX\n", argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open(argv[1], O_RDWR); // /dev/iio:device2
|
||||||
|
if (fd < 0) {
|
||||||
|
printf("can not open %s\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
|
||||||
|
if (ret < 0) {
|
||||||
|
printf("can not get event fd\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
ret = read(event_fd, &event, sizeof(event));
|
||||||
|
if (ret == sizeof(event))
|
||||||
|
{
|
||||||
|
printf("Get event: 0x%lx\n", event.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
746
STM32MP157/source/A7/14_iio/06_dht11_iio_event_课堂现场编写/dht11.c
Normal file
@@ -0,0 +1,746 @@
|
|||||||
|
/*
|
||||||
|
* DHT11/DHT22 bit banging GPIO driver
|
||||||
|
*
|
||||||
|
* Copyright (c) Harald Geyer <harald@ccbib.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/err.h>
|
||||||
|
#include <linux/interrupt.h>
|
||||||
|
#include <linux/device.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/printk.h>
|
||||||
|
#include <linux/slab.h>
|
||||||
|
#include <linux/of.h>
|
||||||
|
#include <linux/of_device.h>
|
||||||
|
#include <linux/sysfs.h>
|
||||||
|
#include <linux/io.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/wait.h>
|
||||||
|
#include <linux/bitops.h>
|
||||||
|
#include <linux/completion.h>
|
||||||
|
#include <linux/mutex.h>
|
||||||
|
#include <linux/delay.h>
|
||||||
|
#include <linux/gpio/consumer.h>
|
||||||
|
#include <linux/timekeeping.h>
|
||||||
|
|
||||||
|
#include <linux/iio/iio.h>
|
||||||
|
#include <linux/iio/buffer.h>
|
||||||
|
#include <linux/iio/kfifo_buf.h>
|
||||||
|
#include <linux/iio/trigger_consumer.h>
|
||||||
|
#include <linux/iio/triggered_buffer.h>
|
||||||
|
#include <linux/iio/events.h>
|
||||||
|
#include <linux/iio/triggered_event.h>
|
||||||
|
|
||||||
|
#define DRIVER_NAME "dht11"
|
||||||
|
|
||||||
|
#define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
|
||||||
|
|
||||||
|
#define DHT11_EDGES_PREAMBLE 2
|
||||||
|
#define DHT11_BITS_PER_READ 40
|
||||||
|
/*
|
||||||
|
* Note that when reading the sensor actually 84 edges are detected, but
|
||||||
|
* since the last edge is not significant, we only store 83:
|
||||||
|
*/
|
||||||
|
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
|
||||||
|
DHT11_EDGES_PREAMBLE + 1)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Data transmission timing:
|
||||||
|
* Data bits are encoded as pulse length (high time) on the data line.
|
||||||
|
* 0-bit: 22-30uS -- typically 26uS (AM2302)
|
||||||
|
* 1-bit: 68-75uS -- typically 70uS (AM2302)
|
||||||
|
* The acutal timings also depend on the properties of the cable, with
|
||||||
|
* longer cables typically making pulses shorter.
|
||||||
|
*
|
||||||
|
* Our decoding depends on the time resolution of the system:
|
||||||
|
* timeres > 34uS ... don't know what a 1-tick pulse is
|
||||||
|
* 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
|
||||||
|
* 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
|
||||||
|
* timeres < 23uS ... no problem
|
||||||
|
*
|
||||||
|
* Luckily clocks in the 33-44kHz range are quite uncommon, so we can
|
||||||
|
* support most systems if the threshold for decoding a pulse as 1-bit
|
||||||
|
* is chosen carefully. If somebody really wants to support clocks around
|
||||||
|
* 40kHz, where this driver is most unreliable, there are two options.
|
||||||
|
* a) select an implementation using busy loop polling on those systems
|
||||||
|
* b) use the checksum to do some probabilistic decoding
|
||||||
|
*/
|
||||||
|
#define DHT11_START_TRANSMISSION_MIN 18000 /* us */
|
||||||
|
#define DHT11_START_TRANSMISSION_MAX 20000 /* us */
|
||||||
|
#define DHT11_MIN_TIMERES 34000 /* ns */
|
||||||
|
#define DHT11_THRESHOLD 49000 /* ns */
|
||||||
|
#define DHT11_AMBIG_LOW 23000 /* ns */
|
||||||
|
#define DHT11_AMBIG_HIGH 30000 /* ns */
|
||||||
|
|
||||||
|
struct dht11 {
|
||||||
|
struct device *dev;
|
||||||
|
|
||||||
|
struct gpio_desc *gpiod;
|
||||||
|
int irq;
|
||||||
|
|
||||||
|
struct completion completion;
|
||||||
|
/* The iio sysfs interface doesn't prevent concurrent reads: */
|
||||||
|
struct mutex lock;
|
||||||
|
|
||||||
|
s64 timestamp;
|
||||||
|
int temperature;
|
||||||
|
int humidity;
|
||||||
|
|
||||||
|
/* num_edges: -1 means "no transmission in progress" */
|
||||||
|
int num_edges;
|
||||||
|
struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
|
||||||
|
struct delayed_work work;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
int temp_ev_enable;
|
||||||
|
int temp_thres_up;
|
||||||
|
int temp_thres_down;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
/*
|
||||||
|
* dht11_edges_print: show the data as actually received by the
|
||||||
|
* driver.
|
||||||
|
*/
|
||||||
|
static void dht11_edges_print(struct dht11 *dht11)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
|
||||||
|
for (i = 1; i < dht11->num_edges; ++i) {
|
||||||
|
dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
|
||||||
|
dht11->edges[i].ts - dht11->edges[i - 1].ts,
|
||||||
|
dht11->edges[i - 1].value ? "high" : "low");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* CONFIG_DYNAMIC_DEBUG */
|
||||||
|
|
||||||
|
static unsigned char dht11_decode_byte(char *bits)
|
||||||
|
{
|
||||||
|
unsigned char ret = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 8; ++i) {
|
||||||
|
ret <<= 1;
|
||||||
|
if (bits[i])
|
||||||
|
++ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_decode(struct dht11 *dht11, int offset)
|
||||||
|
{
|
||||||
|
int i, t;
|
||||||
|
char bits[DHT11_BITS_PER_READ];
|
||||||
|
unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
|
||||||
|
|
||||||
|
for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
|
||||||
|
t = dht11->edges[offset + 2 * i + 2].ts -
|
||||||
|
dht11->edges[offset + 2 * i + 1].ts;
|
||||||
|
if (!dht11->edges[offset + 2 * i + 1].value) {
|
||||||
|
dev_dbg(dht11->dev,
|
||||||
|
"lost synchronisation at edge %d\n",
|
||||||
|
offset + 2 * i + 1);
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
bits[i] = t > DHT11_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
|
hum_int = dht11_decode_byte(bits);
|
||||||
|
hum_dec = dht11_decode_byte(&bits[8]);
|
||||||
|
temp_int = dht11_decode_byte(&bits[16]);
|
||||||
|
temp_dec = dht11_decode_byte(&bits[24]);
|
||||||
|
checksum = dht11_decode_byte(&bits[32]);
|
||||||
|
|
||||||
|
if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
|
||||||
|
dev_dbg(dht11->dev, "invalid checksum\n");
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boottime_ns();
|
||||||
|
if (hum_int < 4) { /* DHT22: 100000 = (3*256+232)*100 */
|
||||||
|
dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
|
||||||
|
((temp_int & 0x80) ? -100 : 100);
|
||||||
|
dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
|
||||||
|
} else {
|
||||||
|
dht11->temperature = temp_int;
|
||||||
|
dht11->humidity = hum_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* IRQ handler called on GPIO edges
|
||||||
|
*/
|
||||||
|
static irqreturn_t dht11_handle_irq(int irq, void *data)
|
||||||
|
{
|
||||||
|
struct iio_dev *iio = data;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio);
|
||||||
|
|
||||||
|
/* TODO: Consider making the handler safe for IRQ sharing */
|
||||||
|
if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
|
||||||
|
dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
|
||||||
|
dht11->edges[dht11->num_edges++].value =
|
||||||
|
gpiod_get_value(dht11->gpiod);
|
||||||
|
|
||||||
|
if (dht11->num_edges >= DHT11_EDGES_PER_READ)
|
||||||
|
complete(&dht11->completion);
|
||||||
|
}
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_raw(struct iio_dev *iio_dev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
int *val, int *val2, long m)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpiod_direction_output(dht11->gpiod, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpiod_direction_input(dht11->gpiod);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = IIO_VAL_INT;
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
*val = dht11->temperature;
|
||||||
|
else if (chan->type == IIO_HUMIDITYRELATIVE)
|
||||||
|
*val = dht11->humidity;
|
||||||
|
else
|
||||||
|
ret = -EINVAL;
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_event_config(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
return !!dht11->temp_ev_enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_write_event_config(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir, int state)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
dht11->temp_ev_enable = state;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_read_event_value(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir,
|
||||||
|
enum iio_event_info info,
|
||||||
|
int *val, int *val2)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
{
|
||||||
|
if (dir == IIO_EV_DIR_FALLING) {
|
||||||
|
*val = dht11->temp_thres_down;
|
||||||
|
} else {
|
||||||
|
*val = dht11->temp_thres_up;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return IIO_VAL_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_write_event_value(struct iio_dev *idev,
|
||||||
|
const struct iio_chan_spec *chan,
|
||||||
|
enum iio_event_type type,
|
||||||
|
enum iio_event_direction dir,
|
||||||
|
enum iio_event_info info,
|
||||||
|
int val, int val2)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(idev);
|
||||||
|
|
||||||
|
if (chan->type == IIO_TEMP)
|
||||||
|
{
|
||||||
|
if (dir == IIO_EV_DIR_FALLING) {
|
||||||
|
dht11->temp_thres_down = val;
|
||||||
|
} else {
|
||||||
|
dht11->temp_thres_up = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return IIO_VAL_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct iio_info dht11_iio_info = {
|
||||||
|
.read_raw = dht11_read_raw,
|
||||||
|
.write_event_value = dht11_write_event_value,
|
||||||
|
.read_event_value = dht11_read_event_value,
|
||||||
|
.read_event_config = dht11_read_event_config,
|
||||||
|
.write_event_config = dht11_write_event_config,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct iio_event_spec dht11_temp_events[] = {
|
||||||
|
{
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_RISING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_FALLING,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_VALUE),
|
||||||
|
}, {
|
||||||
|
.type = IIO_EV_TYPE_THRESH,
|
||||||
|
.dir = IIO_EV_DIR_EITHER,
|
||||||
|
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct iio_chan_spec dht11_chan_spec[] = {
|
||||||
|
{ .type = IIO_TEMP,
|
||||||
|
.scan_index = 0,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
.event_spec = dht11_temp_events,
|
||||||
|
.num_event_specs = ARRAY_SIZE(dht11_temp_events),
|
||||||
|
},
|
||||||
|
{ .type = IIO_HUMIDITYRELATIVE,
|
||||||
|
.scan_index = 1,
|
||||||
|
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
|
||||||
|
.scan_type = {
|
||||||
|
.sign = 's',
|
||||||
|
.realbits = 8,
|
||||||
|
.storagebits = 8,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct of_device_id dht11_dt_ids[] = {
|
||||||
|
{ .compatible = "dht11", },
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
|
||||||
|
|
||||||
|
static int dht11_buf_preenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postenable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* start work */
|
||||||
|
schedule_delayed_work(&dht11->work, 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = iio_priv(indio_dev);
|
||||||
|
|
||||||
|
/* cancel work */
|
||||||
|
cancel_delayed_work_sync(&dht11->work);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
|
||||||
|
.preenable = dht11_buf_preenable,
|
||||||
|
.postenable = dht11_buf_postenable,
|
||||||
|
.postdisable = dht11_buf_postdisable,
|
||||||
|
};
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_handle(int irq, void * p)
|
||||||
|
{
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
pf->timestamp = iio_get_time_ns(pf->indio_dev);
|
||||||
|
return IRQ_WAKE_THREAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
static irqreturn_t dht11_triggered_buf_thread(int irq, void * p)
|
||||||
|
{
|
||||||
|
/* read data */
|
||||||
|
struct iio_poll_func *pf = p;
|
||||||
|
struct iio_dev *iio_dev = pf->indio_dev;
|
||||||
|
struct dht11 *dht11 = iio_priv(iio_dev);
|
||||||
|
|
||||||
|
int ret, timeres, offset;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpiod_direction_output(dht11->gpiod, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpiod_direction_input(dht11->gpiod);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
if (dht11->temp_ev_enable)
|
||||||
|
{
|
||||||
|
if (vals[0] > dht11->temp_thres_up)
|
||||||
|
{
|
||||||
|
iio_push_event(iio_dev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
|
||||||
|
iio_get_time_ns(iio_dev));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vals[0] < dht11->temp_thres_down)
|
||||||
|
{
|
||||||
|
iio_push_event(iio_dev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
|
||||||
|
iio_get_time_ns(iio_dev));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
iio_trigger_notify_done(iio_dev->trig);
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static irqreturn_t dht11_event_handler(int irq, void *private)
|
||||||
|
{
|
||||||
|
struct iio_poll_func *pf = private;
|
||||||
|
struct iio_dev *idev = pf->indio_dev;
|
||||||
|
|
||||||
|
iio_push_event(idev,
|
||||||
|
IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
|
||||||
|
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
|
||||||
|
iio_get_time_ns(idev));
|
||||||
|
|
||||||
|
iio_trigger_notify_done(idev->trig);
|
||||||
|
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dht11_buffer_init(struct iio_dev *indio_dev)
|
||||||
|
{
|
||||||
|
#if 0
|
||||||
|
struct iio_buffer *buffer;
|
||||||
|
|
||||||
|
buffer = iio_kfifo_allocate();
|
||||||
|
if (!buffer)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
iio_device_attach_buffer(indio_dev, buffer);
|
||||||
|
|
||||||
|
/* Ring buffer functions - here trigger setup related */
|
||||||
|
indio_dev->setup_ops = &dht11_buf_setup_ops;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
|
//int ret = iio_triggered_event_setup(indio_dev, NULL, dht11_event_handler);
|
||||||
|
//if (ret)
|
||||||
|
// return ret;
|
||||||
|
|
||||||
|
return iio_triggered_buffer_setup(indio_dev, dht11_triggered_buf_handle, dht11_triggered_buf_thread, NULL);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dht11_work(struct work_struct *work)
|
||||||
|
{
|
||||||
|
struct dht11 *dht11 = container_of(work, struct dht11, work.work);
|
||||||
|
int ret, timeres, offset;
|
||||||
|
struct iio_dev *iio_dev = dht11->iio;
|
||||||
|
uint8_t vals[2];
|
||||||
|
|
||||||
|
/* read dht11 */
|
||||||
|
mutex_lock(&dht11->lock);
|
||||||
|
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
|
||||||
|
timeres = ktime_get_resolution_ns();
|
||||||
|
dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
|
||||||
|
if (timeres > DHT11_MIN_TIMERES) {
|
||||||
|
dev_err(dht11->dev, "timeresolution %dns too low\n",
|
||||||
|
timeres);
|
||||||
|
/* In theory a better clock could become available
|
||||||
|
* at some point ... and there is no error code
|
||||||
|
* that really fits better.
|
||||||
|
*/
|
||||||
|
ret = -EAGAIN;
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
|
||||||
|
dev_warn(dht11->dev,
|
||||||
|
"timeresolution: %dns - decoding ambiguous\n",
|
||||||
|
timeres);
|
||||||
|
|
||||||
|
reinit_completion(&dht11->completion);
|
||||||
|
|
||||||
|
dht11->num_edges = 0;
|
||||||
|
ret = gpiod_direction_output(dht11->gpiod, 0);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
usleep_range(DHT11_START_TRANSMISSION_MIN,
|
||||||
|
DHT11_START_TRANSMISSION_MAX);
|
||||||
|
ret = gpiod_direction_input(dht11->gpiod);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = request_irq(dht11->irq, dht11_handle_irq,
|
||||||
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
||||||
|
iio_dev->name, iio_dev);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
ret = wait_for_completion_killable_timeout(&dht11->completion,
|
||||||
|
HZ);
|
||||||
|
|
||||||
|
free_irq(dht11->irq, iio_dev);
|
||||||
|
|
||||||
|
#ifdef CONFIG_DYNAMIC_DEBUG
|
||||||
|
dht11_edges_print(dht11);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
|
||||||
|
dev_err(dht11->dev, "Only %d signal edges detected\n",
|
||||||
|
dht11->num_edges);
|
||||||
|
ret = -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
if (ret < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
offset = DHT11_EDGES_PREAMBLE +
|
||||||
|
dht11->num_edges - DHT11_EDGES_PER_READ;
|
||||||
|
for (; offset >= 0; --offset) {
|
||||||
|
ret = dht11_decode(dht11, offset);
|
||||||
|
if (!ret)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
vals[0] = dht11->temperature;
|
||||||
|
vals[1] = dht11->humidity;
|
||||||
|
|
||||||
|
/* put data into iio_buffer */
|
||||||
|
iio_push_to_buffers(iio_dev, vals);
|
||||||
|
|
||||||
|
err:
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
mutex_unlock(&dht11->lock);
|
||||||
|
|
||||||
|
/* re-start work */
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
/* 有错误 */
|
||||||
|
schedule_delayed_work(&dht11->work, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
schedule_delayed_work(&dht11->work, HZ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const unsigned long dht11_scan_masks[] = {0x3, 0};
|
||||||
|
|
||||||
|
static int dht11_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
|
struct dht11 *dht11;
|
||||||
|
struct iio_dev *iio;
|
||||||
|
|
||||||
|
iio = devm_iio_device_alloc(dev, sizeof(*dht11));
|
||||||
|
if (!iio) {
|
||||||
|
dev_err(dev, "Failed to allocate IIO device\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11 = iio_priv(iio);
|
||||||
|
dht11->dev = dev;
|
||||||
|
dht11->iio = iio;
|
||||||
|
|
||||||
|
dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
|
||||||
|
if (IS_ERR(dht11->gpiod))
|
||||||
|
return PTR_ERR(dht11->gpiod);
|
||||||
|
|
||||||
|
|
||||||
|
dht11->irq = gpiod_to_irq(dht11->gpiod);
|
||||||
|
if (dht11->irq < 0) {
|
||||||
|
dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
|
||||||
|
dht11->num_edges = -1;
|
||||||
|
|
||||||
|
platform_set_drvdata(pdev, iio);
|
||||||
|
|
||||||
|
init_completion(&dht11->completion);
|
||||||
|
mutex_init(&dht11->lock);
|
||||||
|
iio->name = pdev->name;
|
||||||
|
iio->dev.parent = &pdev->dev;
|
||||||
|
iio->info = &dht11_iio_info;
|
||||||
|
iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
|
||||||
|
iio->channels = dht11_chan_spec;
|
||||||
|
iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
|
||||||
|
iio->available_scan_masks = dht11_scan_masks;
|
||||||
|
|
||||||
|
dht11_buffer_init(iio);
|
||||||
|
|
||||||
|
/* init work */
|
||||||
|
INIT_DELAYED_WORK(&dht11->work, dht11_work);
|
||||||
|
|
||||||
|
return devm_iio_device_register(dev, iio);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct platform_driver dht11_driver = {
|
||||||
|
.driver = {
|
||||||
|
.name = DRIVER_NAME,
|
||||||
|
.of_match_table = dht11_dt_ids,
|
||||||
|
},
|
||||||
|
.probe = dht11_probe,
|
||||||
|
};
|
||||||
|
|
||||||
|
module_platform_driver(dht11_driver);
|
||||||
|
|
||||||
|
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
|
||||||
|
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
|
||||||
|
MODULE_LICENSE("GPL v2");
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
humidity_sensor {
|
||||||
|
compatible = "dht11";
|
||||||
|
gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>;
|
||||||
|
};
|
||||||