Skip to content

Commit 3c70a38

Browse files
Alexander WachterMaureenHelm
authored andcommitted
drivers: sensor: ams_iAQcore: Implemented ASM Indoor Air Quality Sensor
Implementation of AMS (Austria Micro Systems) Indoor Air Quality Sensor Signed-off-by: Alexander Wachter <[email protected]>
1 parent 4f23741 commit 3c70a38

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_subdirectory_ifdef(CONFIG_ADXL362 adxl362)
55
add_subdirectory_ifdef(CONFIG_ADXL372 adxl372)
66
add_subdirectory_ifdef(CONFIG_AK8975 ak8975)
77
add_subdirectory_ifdef(CONFIG_AMG88XX amg88xx)
8+
add_subdirectory_ifdef(CONFIG_AMS_IAQ_CORE ams_iAQcore)
89
add_subdirectory_ifdef(CONFIG_APDS9960 apds9960)
910
add_subdirectory_ifdef(CONFIG_BMA280 bma280)
1011
add_subdirectory_ifdef(CONFIG_BMC150_MAGN bmc150_magn)

drivers/sensor/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ source "drivers/sensor/ak8975/Kconfig"
3535

3636
source "drivers/sensor/amg88xx/Kconfig"
3737

38+
source "drivers/sensor/ams_iAQcore/Kconfig"
39+
3840
source "drivers/sensor/apds9960/Kconfig"
3941

4042
source "drivers/sensor/bma280/Kconfig"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
5+
zephyr_library_sources_ifdef(CONFIG_AMS_IAQ_CORE iAQcore.c)

drivers/sensor/ams_iAQcore/Kconfig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Kconfig - iAQ-core Digital VOC sensor configuration options
2+
3+
#
4+
# Copyright (c) 2018 Alexander Wachter
5+
#
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
9+
menuconfig AMS_IAQ_CORE
10+
bool "iAQ-core Digital VOC sensor"
11+
depends on I2C && HAS_DTS_I2C
12+
help
13+
Enable driver for iAQ-core Digital VOC sensor.
14+
15+
if AMS_IAQ_CORE
16+
17+
config IAQ_CORE_MAX_READ_RETRIES
18+
int "Number of read retries"
19+
default 4
20+
help
21+
Number of retries when reading failed or device not ready.
22+
23+
endif # AMS_IAQ_CORE
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2018 Alexander Wachter.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <device.h>
8+
#include <i2c.h>
9+
#include <kernel.h>
10+
#include <misc/byteorder.h>
11+
#include <misc/util.h>
12+
#include <sensor.h>
13+
#include <misc/__assert.h>
14+
#include <logging/log.h>
15+
16+
#include "iAQcore.h"
17+
18+
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
19+
LOG_MODULE_REGISTER(IAQ_CORE);
20+
21+
static int iaqcore_sample_fetch(struct device *dev, enum sensor_channel chan)
22+
{
23+
struct iaq_core_data *drv_data = dev->driver_data;
24+
struct iaq_registers buf;
25+
struct i2c_msg msg;
26+
int ret, tries;
27+
28+
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
29+
30+
msg.buf = (u8_t *)&buf;
31+
msg.len = sizeof(struct iaq_registers);
32+
msg.flags = I2C_MSG_READ | I2C_MSG_STOP;
33+
34+
for (tries = 0; tries < CONFIG_IAQ_CORE_MAX_READ_RETRIES; tries++) {
35+
36+
ret = i2c_transfer(drv_data->i2c, &msg, 1,
37+
DT_AMS_IAQCORE_0_BASE_ADDRESS);
38+
if (ret < 0) {
39+
LOG_ERR("Failed to read registers data [%d].", ret);
40+
return -EIO;
41+
}
42+
43+
drv_data->status = buf.status;
44+
45+
if (buf.status == 0x00) {
46+
drv_data->co2 = sys_be16_to_cpu(buf.co2_pred);
47+
drv_data->voc = sys_be16_to_cpu(buf.voc);
48+
drv_data->status = buf.status;
49+
drv_data->resistance = sys_be32_to_cpu(buf.resistance);
50+
51+
return 0;
52+
}
53+
54+
k_sleep(100);
55+
}
56+
57+
if (drv_data->status == 0x01) {
58+
LOG_INF("Sensor data not available");
59+
}
60+
61+
if (drv_data->status == 0x80) {
62+
LOG_ERR("Sensor Error");
63+
}
64+
65+
return -EIO;
66+
}
67+
68+
static int iaqcore_channel_get(struct device *dev,
69+
enum sensor_channel chan,
70+
struct sensor_value *val)
71+
{
72+
struct iaq_core_data *drv_data = dev->driver_data;
73+
74+
switch (chan) {
75+
case SENSOR_CHAN_CO2:
76+
val->val1 = drv_data->co2;
77+
val->val2 = 0;
78+
break;
79+
case SENSOR_CHAN_VOC:
80+
val->val1 = drv_data->voc;
81+
val->val2 = 0;
82+
break;
83+
case SENSOR_CHAN_RESISTANCE:
84+
val->val1 = drv_data->resistance;
85+
val->val2 = 0;
86+
break;
87+
default:
88+
return -ENOTSUP;
89+
}
90+
91+
return 0;
92+
}
93+
94+
static const struct sensor_driver_api iaq_core_driver_api = {
95+
.sample_fetch = iaqcore_sample_fetch,
96+
.channel_get = iaqcore_channel_get,
97+
};
98+
99+
static int iaq_core_init(struct device *dev)
100+
{
101+
struct iaq_core_data *drv_data = dev->driver_data;
102+
103+
drv_data->i2c = device_get_binding(DT_AMS_IAQCORE_0_BUS_NAME);
104+
if (drv_data->i2c == NULL) {
105+
LOG_ERR("Failed to get pointer to %s device!",
106+
DT_AMS_IAQCORE_0_BUS_NAME);
107+
return -EINVAL;
108+
}
109+
110+
return 0;
111+
}
112+
113+
static struct iaq_core_data iaq_core_driver;
114+
115+
DEVICE_AND_API_INIT(iaq_core, DT_AMS_IAQCORE_0_LABEL, iaq_core_init,
116+
&iaq_core_driver, NULL, POST_KERNEL,
117+
CONFIG_SENSOR_INIT_PRIORITY, &iaq_core_driver_api);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2018 Alexander Wachter
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_DRIVERS_SENSOR_AMS_IAQCORE_IAQCORE_H_
8+
#define ZEPHYR_DRIVERS_SENSOR_AMS_IAQCORE_IAQCORE_H_
9+
10+
#include <device.h>
11+
#include <misc/util.h>
12+
13+
struct iaq_registers {
14+
u16_t co2_pred;
15+
u8_t status;
16+
s32_t resistance;
17+
u16_t voc;
18+
} __packed;
19+
20+
struct iaq_core_data {
21+
struct device *i2c;
22+
u16_t co2;
23+
u16_t voc;
24+
u8_t status;
25+
s32_t resistance;
26+
};
27+
28+
#endif /* ZEPHYR_DRIVERS_SENSOR_AMS_IAQCORE_IAQCORE_H_ */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2018, Alexander Wachter
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
---
7+
title: AMS (Austria Mikro Systeme) Indoor Air Quality Sensor iAQ-core
8+
version: 0.1
9+
10+
description: >
11+
This binding gives a base representation of iAQ-core indoor air quality
12+
sensor
13+
14+
inherits:
15+
!include i2c-device.yaml
16+
17+
properties:
18+
compatible:
19+
constraint: "ams,iaqcore"
20+
21+
...

include/sensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ enum sensor_channel {
119119
SENSOR_CHAN_VOLTAGE,
120120
/** Current, in amps **/
121121
SENSOR_CHAN_CURRENT,
122+
/** Resistance , in Ohm **/
123+
SENSOR_CHAN_RESISTANCE,
122124

123125
/** Angular rotation, in degrees */
124126
SENSOR_CHAN_ROTATION,

0 commit comments

Comments
 (0)