Skip to content

Commit d36334a

Browse files
JordanYateskartben
authored andcommitted
sensor: tmp108: optimize resource usage
Optimize the resource usage of the driver for the case where `CONFIG_TMP108_ALERT_INTERRUPTS=n`. Signed-off-by: Jordan Yates <[email protected]>
1 parent 093f66f commit d36334a

File tree

3 files changed

+77
-72
lines changed

3 files changed

+77
-72
lines changed

drivers/sensor/ti/tmp108/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
zephyr_library()
44

5-
zephyr_library_sources(tmp108.c tmp108_trigger.c)
5+
zephyr_library_sources(tmp108.c)
6+
zephyr_library_sources_ifdef(CONFIG_TMP108_ALERT_INTERRUPTS tmp108_trigger.c)

drivers/sensor/ti/tmp108/tmp108.c

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,17 @@ static int tmp108_attr_set(const struct device *dev,
201201
const struct sensor_value *val)
202202
{
203203
struct tmp108_data *drv_data = dev->data;
204-
uint16_t mode = 0;
205-
uint16_t reg_value = 0;
204+
__maybe_unused uint16_t reg_value;
205+
__maybe_unused int32_t uval;
206+
uint16_t mode;
206207
int result = 0;
207-
int32_t uval;
208208

209209
if (chan != SENSOR_CHAN_AMBIENT_TEMP && chan != SENSOR_CHAN_ALL) {
210210
return -ENOTSUP;
211211
}
212212

213213
switch ((int) attr) {
214+
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
214215
case SENSOR_ATTR_HYSTERESIS:
215216
if (TI_TMP108_HYSTER_0_C(dev) == TI_TMP108_CONF_NA) {
216217
LOG_WRN("AS621x Series lacks Hysterisis setttings");
@@ -260,6 +261,18 @@ static int tmp108_attr_set(const struct device *dev,
260261
reg_value);
261262
break;
262263

264+
case SENSOR_ATTR_TMP108_ALERT_POLARITY:
265+
if (val->val1 == 1) {
266+
mode = TI_TMP108_CONF_POL_HIGH(dev);
267+
} else {
268+
mode = TI_TMP108_CONF_POL_LOW(dev);
269+
}
270+
result = tmp108_write_config(dev,
271+
TI_TMP108_CONF_POL_MASK(dev),
272+
mode);
273+
break;
274+
#endif /* CONFIG_TMP108_ALERT_INTERRUPTS */
275+
263276
case SENSOR_ATTR_SAMPLING_FREQUENCY:
264277
if (val->val1 < 1) {
265278
mode = TI_TMP108_FREQ_4_SECS(dev);
@@ -296,17 +309,6 @@ static int tmp108_attr_set(const struct device *dev,
296309
drv_data->one_shot_mode = true;
297310
break;
298311

299-
case SENSOR_ATTR_TMP108_ALERT_POLARITY:
300-
if (val->val1 == 1) {
301-
mode = TI_TMP108_CONF_POL_HIGH(dev);
302-
} else {
303-
mode = TI_TMP108_CONF_POL_LOW(dev);
304-
}
305-
result = tmp108_write_config(dev,
306-
TI_TMP108_CONF_POL_MASK(dev),
307-
mode);
308-
break;
309-
310312
default:
311313
return -ENOTSUP;
312314
}
@@ -323,7 +325,9 @@ static DEVICE_API(sensor, tmp108_driver_api) = {
323325
.attr_get = tmp108_attr_get,
324326
.sample_fetch = tmp108_sample_fetch,
325327
.channel_get = tmp108_channel_get,
328+
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
326329
.trigger_set = tmp_108_trigger_set,
330+
#endif
327331
};
328332

329333
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
@@ -371,18 +375,19 @@ static int setup_interrupts(const struct device *dev)
371375
static int tmp108_init(const struct device *dev)
372376
{
373377
const struct tmp108_config *cfg = dev->config;
374-
struct tmp108_data *drv_data = dev->data;
375378
int result = 0;
376379

377380
if (!device_is_ready(cfg->i2c_spec.bus)) {
378381
LOG_ERR("I2C dev %s not ready", cfg->i2c_spec.bus->name);
379382
return -ENODEV;
380383
}
381384

385+
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
386+
struct tmp108_data *drv_data = dev->data;
387+
382388
/* save this driver instance for passing to other functions */
383389
drv_data->tmp108_dev = dev;
384390

385-
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
386391
result = setup_interrupts(dev);
387392

388393
if (result < 0) {
@@ -396,22 +401,16 @@ static int tmp108_init(const struct device *dev)
396401
return result;
397402
}
398403

399-
#define TMP108_DEFINE(inst, t) \
400-
static struct tmp108_data tmp108_prv_data_##inst##t; \
401-
static const struct tmp108_config tmp108_config_##inst##t = { \
402-
.i2c_spec = I2C_DT_SPEC_INST_GET(inst), \
403-
.alert_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, \
404-
alert_gpios, { 0 }),\
405-
.reg_def = t##_CONF \
406-
}; \
407-
SENSOR_DEVICE_DT_INST_DEFINE(inst, \
408-
&tmp108_init, \
409-
NULL, \
410-
&tmp108_prv_data_##inst##t, \
411-
&tmp108_config_##inst##t, \
412-
POST_KERNEL, \
413-
CONFIG_SENSOR_INIT_PRIORITY, \
414-
&tmp108_driver_api);
404+
#define TMP108_DEFINE(inst, t) \
405+
static struct tmp108_data tmp108_prv_data_##inst##t; \
406+
static const struct tmp108_config tmp108_config_##inst##t = { \
407+
.i2c_spec = I2C_DT_SPEC_INST_GET(inst), \
408+
IF_ENABLED(CONFIG_TMP108_ALERT_INTERRUPTS, \
409+
(.alert_gpio = GPIO_DT_SPEC_INST_GET(inst, alert_gpios),)) \
410+
.reg_def = t##_CONF}; \
411+
SENSOR_DEVICE_DT_INST_DEFINE(inst, &tmp108_init, NULL, &tmp108_prv_data_##inst##t, \
412+
&tmp108_config_##inst##t, POST_KERNEL, \
413+
CONFIG_SENSOR_INIT_PRIORITY, &tmp108_driver_api);
415414

416415
#define TMP108_INIT(n) TMP108_DEFINE(n, TI_TMP108)
417416
#undef DT_DRV_COMPAT

drivers/sensor/ti/tmp108/tmp108.h

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,29 @@
2020
#define TI_TMP108_REG_LOW_LIMIT 0x02 /** Low alert set register */
2121
#define TI_TMP108_REG_HIGH_LIMIT 0x03 /** High alert set register */
2222

23-
#define AMS_AS6212_CONF {.CONF_HYS1 = TI_TMP108_CONF_NA,\
24-
.CONF_HYS0 = TI_TMP108_CONF_NA,\
25-
.CONF_CR0 = 0x0040, \
26-
.CONF_CR1 = 0x0080, \
27-
.CONF_SLEEP = 0x0100, \
28-
.CONF_M1 = 0x0000, \
29-
.CONF_TM = 0x0200, \
30-
.CONF_POL = 0x0400, \
31-
.CONF_M0 = 0x8000, \
32-
.CONF_RST = 0x0080, \
33-
.TEMP_MULT = 15625, \
34-
.TEMP_DIV = 2 }
35-
36-
#define TI_TMP108_CONF {.CONF_HYS0 = 0x0010, \
37-
.CONF_HYS1 = 0x0020, \
38-
.CONF_POL = 0x0080, \
39-
.CONF_M0 = 0x0100, \
40-
.CONF_M1 = 0x0200, \
41-
.CONF_TM = 0x0400, \
42-
.CONF_CR0 = 0x2000, \
43-
.CONF_CR1 = 0x4000, \
44-
.CONF_RST = 0x0022, \
45-
.TEMP_MULT = 15625, \
46-
.TEMP_DIV = 4 }
23+
#define AMS_AS6212_CONF \
24+
{.CONF_CR0 = 0x0040, \
25+
.CONF_CR1 = 0x0080, \
26+
.CONF_SLEEP = 0x0100, \
27+
.CONF_M1 = 0x0000, \
28+
.CONF_TM = 0x0200, \
29+
.CONF_M0 = 0x8000, \
30+
.CONF_RST = 0x0080, \
31+
.TEMP_MULT = 15625, \
32+
.TEMP_DIV = 2, \
33+
IF_ENABLED(CONFIG_TMP108_ALERT_INTERRUPTS, (.CONF_POL = 0x0400))}
34+
35+
#define TI_TMP108_CONF \
36+
{.CONF_M0 = 0x0100, \
37+
.CONF_M1 = 0x0200, \
38+
.CONF_TM = 0x0400, \
39+
.CONF_CR0 = 0x2000, \
40+
.CONF_CR1 = 0x4000, \
41+
.CONF_RST = 0x0022, \
42+
.TEMP_MULT = 15625, \
43+
.TEMP_DIV = 4, \
44+
IF_ENABLED(CONFIG_TMP108_ALERT_INTERRUPTS, \
45+
(.CONF_HYS0 = 0x0010, .CONF_HYS1 = 0x0020, .CONF_POL = 0x0080))}
4746

4847
#define TI_TMP108_MODE_SHUTDOWN(x) 0
4948
#define TI_TMP108_MODE_ONE_SHOT(x) (TI_TMP108_CONF_M0(x) | TI_TMP108_CONF_SLEEP(x))
@@ -87,39 +86,45 @@
8786
#define TI_TMP108_CONF_NA 0x0000
8887

8988
struct tmp_108_reg_def {
90-
uint16_t CONF_M0; /** Mode 1 configuration bit */
91-
uint16_t CONF_M1; /** Mode 2 configuration bit */
92-
uint16_t CONF_SLEEP; /** Sleep mode configuration bit */
93-
uint16_t CONF_CR0; /** Conversion rate 1 configuration bit */
94-
uint16_t CONF_CR1; /** Conversion rate 2 configuration bit */
95-
uint16_t CONF_POL; /** Alert pin Polarity configuration bit */
96-
uint16_t CONF_TM; /** Thermostat mode setting bit */
97-
uint16_t CONF_HYS1; /** Temperature hysteresis config 1 bit */
98-
uint16_t CONF_HYS0; /** Temperature hysteresis config 2 bit */
99-
int32_t TEMP_MULT; /** Temperature multiplier */
100-
int32_t TEMP_DIV; /** Temperature divisor */
101-
uint16_t CONF_RST; /** default reset values on init */
89+
uint16_t CONF_M0; /** Mode 1 configuration bit */
90+
uint16_t CONF_M1; /** Mode 2 configuration bit */
91+
uint16_t CONF_SLEEP; /** Sleep mode configuration bit */
92+
uint16_t CONF_CR0; /** Conversion rate 1 configuration bit */
93+
uint16_t CONF_CR1; /** Conversion rate 2 configuration bit */
94+
uint16_t CONF_TM; /** Thermostat mode setting bit */
95+
int32_t TEMP_MULT; /** Temperature multiplier */
96+
int32_t TEMP_DIV; /** Temperature divisor */
97+
uint16_t CONF_RST; /** default reset values on init */
98+
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
99+
uint16_t CONF_POL; /** Alert pin Polarity configuration bit */
100+
uint16_t CONF_HYS1; /** Temperature hysteresis config 1 bit */
101+
uint16_t CONF_HYS0; /** Temperature hysteresis config 2 bit */
102+
#endif
102103
};
103104

104105
#define TI_TMP108_GET_CONF(x, cfg) ((struct tmp108_config *)(x->config))->reg_def.cfg
105106

106107
struct tmp108_config {
107108
const struct i2c_dt_spec i2c_spec;
108-
const struct gpio_dt_spec alert_gpio;
109109
struct tmp_108_reg_def reg_def;
110+
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
111+
const struct gpio_dt_spec alert_gpio;
112+
#endif /* CONFIG_TMP108_ALERT_INTERRUPTS */
110113
};
111114

112115
struct tmp108_data {
113-
const struct device *tmp108_dev;
114-
115116
int16_t sample;
116117

117118
bool one_shot_mode;
118119

120+
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
121+
const struct device *tmp108_dev;
122+
119123
const struct sensor_trigger *temp_alert_trigger;
120124
sensor_trigger_handler_t temp_alert_handler;
121125

122126
struct gpio_callback temp_alert_gpio_cb;
127+
#endif /* CONFIG_TMP108_ALERT_INTERRUPTS */
123128
};
124129

125130
int tmp_108_trigger_set(const struct device *dev,

0 commit comments

Comments
 (0)