Skip to content

Commit fe6a736

Browse files
aviscontiMaureenHelm
authored andcommitted
drivers: sensor: hts221: Add checks for drdy_gpios macros
To avoid build errors enable the trigger only if DRDY gpio is defined in DTS. Fix #28443 Signed-off-by: Armando Visconti <[email protected]>
1 parent f0f95b1 commit fe6a736

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

drivers/sensor/hts221/hts221.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static int hts221_read_conversion_data(const struct device *dev)
106106
}
107107

108108
static const struct sensor_driver_api hts221_driver_api = {
109-
#if CONFIG_HTS221_TRIGGER
109+
#if HTS221_TRIGGER_ENABLED
110110
.trigger_set = hts221_trigger_set,
111111
#endif
112112
.sample_fetch = hts221_sample_fetch,
@@ -168,11 +168,13 @@ int hts221_init(const struct device *dev)
168168
return -EINVAL;
169169
}
170170

171-
#ifdef CONFIG_HTS221_TRIGGER
171+
#if HTS221_TRIGGER_ENABLED
172172
if (hts221_init_interrupt(dev) < 0) {
173173
LOG_ERR("Failed to initialize interrupt.");
174174
return -EIO;
175175
}
176+
#else
177+
LOG_INF("Cannot enable trigger without drdy-gpios");
176178
#endif
177179

178180
return 0;
@@ -182,11 +184,11 @@ static struct hts221_data hts221_driver;
182184
static const struct hts221_config hts221_cfg = {
183185
.i2c_bus = DT_INST_BUS_LABEL(0),
184186
.i2c_addr = DT_INST_REG_ADDR(0),
185-
#ifdef CONFIG_HTS221_TRIGGER
187+
#if HTS221_TRIGGER_ENABLED
186188
.drdy_pin = DT_INST_GPIO_PIN(0, drdy_gpios),
187189
.drdy_flags = DT_INST_GPIO_FLAGS(0, drdy_gpios),
188190
.drdy_controller = DT_INST_GPIO_LABEL(0, drdy_gpios),
189-
#endif /* CONFIG_HTS221_TRIGGER */
191+
#endif /* HTS221_TRIGGER_ENABLED */
190192
};
191193

192194
DEVICE_AND_API_INIT(hts221, DT_INST_LABEL(0), hts221_init,

drivers/sensor/hts221/hts221.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <zephyr/types.h>
1313
#include <drivers/gpio.h>
1414

15+
#define HTS221_TRIGGER_ENABLED (DT_INST_NODE_HAS_PROP(0, drdy_gpios) && \
16+
IS_ENABLED(CONFIG_HTS221_TRIGGER))
17+
1518
#define HTS221_AUTOINCREMENT_ADDR BIT(7)
1619

1720
#define HTS221_REG_WHO_AM_I 0x0F
@@ -42,7 +45,7 @@ struct hts221_data {
4245
int16_t t0_out;
4346
int16_t t1_out;
4447

45-
#ifdef CONFIG_HTS221_TRIGGER
48+
#if HTS221_TRIGGER_ENABLED
4649
const struct device *dev;
4750
const struct device *drdy_dev;
4851
struct gpio_callback drdy_cb;
@@ -58,20 +61,20 @@ struct hts221_data {
5861
struct k_work work;
5962
#endif
6063

61-
#endif /* CONFIG_HTS221_TRIGGER */
64+
#endif /* HTS221_TRIGGER_ENABLED */
6265
};
6366

6467
struct hts221_config {
6568
const char *i2c_bus;
6669
uint16_t i2c_addr;
67-
#ifdef CONFIG_HTS221_TRIGGER
70+
#if HTS221_TRIGGER_ENABLED
6871
gpio_pin_t drdy_pin;
6972
gpio_flags_t drdy_flags;
7073
const char *drdy_controller;
71-
#endif /* CONFIG_HTS221_TRIGGER */
74+
#endif /* HTS221_TRIGGER_ENABLED */
7275
};
7376

74-
#ifdef CONFIG_HTS221_TRIGGER
77+
#if HTS221_TRIGGER_ENABLED
7578
int hts221_trigger_set(const struct device *dev,
7679
const struct sensor_trigger *trig,
7780
sensor_trigger_handler_t handler);

drivers/sensor/hts221/hts221_trigger.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#define DT_DRV_COMPAT st_hts221
8+
79
#include <device.h>
810
#include <drivers/i2c.h>
911
#include <sys/__assert.h>
@@ -13,6 +15,7 @@
1315
#include <logging/log.h>
1416
#include "hts221.h"
1517

18+
#if HTS221_TRIGGER_ENABLED
1619
LOG_MODULE_DECLARE(HTS221, CONFIG_SENSOR_LOG_LEVEL);
1720

1821
static inline void setup_drdy(const struct device *dev,
@@ -163,3 +166,4 @@ int hts221_init_interrupt(const struct device *dev)
163166

164167
return 0;
165168
}
169+
#endif /* HTS221_TRIGGER_ENABLED */

0 commit comments

Comments
 (0)