Skip to content

Commit 67767df

Browse files
GiulianoFranchettonashif
authored andcommitted
drivers: iis2dlpc: adding activity interrupt
This commit adds the activity/inactivity recognition as well as the stationary/motion detection as defined in the IIS2DLPC application note. For now, there is no possibility to configure this interrupt using device tree binding, as I would like to keep the configuration updatable and not set at boot time. This behaviour is fine for prototypes and samples, but is too restrictive on products that may want to change the interrupt configuration at run-time. The interrupt is configured using the attributes SENSOR_ATTR_SLOPE_TH and SENSOR_ATTR_SLOPE_DUR. Signed-off-by: Giuliano Franchetto <[email protected]>
1 parent 5af7ff4 commit 67767df

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

drivers/sensor/iis2dlpc/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ config IIS2DLPC_TAP
6060
help
6161
Enable tap (single/double) detection
6262

63+
config IIS2DLPC_ACTIVITY
64+
bool "Activity detection"
65+
help
66+
Enable activity/inactivity detection
67+
6368
endif # IIS2DLPC_TRIGGER
6469

6570
endif # IIS2DLPC

drivers/sensor/iis2dlpc/iis2dlpc.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static int iis2dlpc_set_odr(const struct device *dev, uint16_t odr)
6969
return iis2dlpc_data_rate_set(ctx, IIS2DLPC_XL_ODR_OFF);
7070
}
7171

72-
val = IIS2DLPC_ODR_TO_REG(odr);
72+
val = IIS2DLPC_ODR_TO_REG(odr);
7373
if (val > IIS2DLPC_XL_ODR_1k6Hz) {
7474
LOG_ERR("ODR too high");
7575
return -ENOTSUP;
@@ -138,6 +138,46 @@ static int iis2dlpc_channel_get(const struct device *dev,
138138
return -ENOTSUP;
139139
}
140140

141+
#ifdef CONFIG_IIS2DLPC_ACTIVITY
142+
static int ii2sdlpc_set_slope_th(const struct device *dev, uint16_t th)
143+
{
144+
int err;
145+
const struct iis2dlpc_config *cfg = dev->config;
146+
stmdev_ctx_t *ctx = (stmdev_ctx_t *) &cfg->ctx;
147+
148+
err = iis2dlpc_wkup_threshold_set(ctx, th & 0x3F);
149+
if (err) {
150+
LOG_ERR("Could not set WK_THS to 0x%02X, error %d",
151+
th & 0x03, err);
152+
return err;
153+
}
154+
return 0;
155+
}
156+
static int ii2sdlpc_set_slope_dur(const struct device *dev, uint16_t dur)
157+
{
158+
int err;
159+
const struct iis2dlpc_config *cfg = dev->config;
160+
stmdev_ctx_t *ctx = (stmdev_ctx_t *) &cfg->ctx;
161+
uint8_t val;
162+
163+
val = (dur & 0x0F);
164+
err = iis2dlpc_act_sleep_dur_set(ctx, val);
165+
if (err) {
166+
LOG_ERR("Could not set SLEEP_DUR to 0x%02X, error %d",
167+
val, err);
168+
return err;
169+
}
170+
val = ((dur >> 5) & 0x03);
171+
err = iis2dlpc_wkup_dur_set(ctx, val);
172+
if (err) {
173+
LOG_ERR("Could not set WAKE_DUR to 0x%02X, error %d",
174+
val, err);
175+
return err;
176+
}
177+
return 0;
178+
}
179+
#endif /* CONFIG_IIS2DLPC_ACTIVITY */
180+
141181
static int iis2dlpc_dev_config(const struct device *dev,
142182
enum sensor_channel chan,
143183
enum sensor_attribute attr,
@@ -149,6 +189,12 @@ static int iis2dlpc_dev_config(const struct device *dev,
149189
IIS2DLPC_FS_TO_REG(sensor_ms2_to_g(val)));
150190
case SENSOR_ATTR_SAMPLING_FREQUENCY:
151191
return iis2dlpc_set_odr(dev, val->val1);
192+
#ifdef CONFIG_IIS2DLPC_ACTIVITY
193+
case SENSOR_ATTR_SLOPE_TH:
194+
return ii2sdlpc_set_slope_th(dev, val->val1);
195+
case SENSOR_ATTR_SLOPE_DUR:
196+
return ii2sdlpc_set_slope_dur(dev, val->val1);
197+
#endif /* CONFIG_IIS2DLPC_ACTIVITY */
152198
default:
153199
LOG_DBG("Acc attribute not supported");
154200
break;

drivers/sensor/iis2dlpc/iis2dlpc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ struct iis2dlpc_data {
9999
sensor_trigger_handler_t tap_handler;
100100
sensor_trigger_handler_t double_tap_handler;
101101
#endif /* CONFIG_IIS2DLPC_TAP */
102+
#ifdef CONFIG_IIS2DLPC_ACTIVITY
103+
sensor_trigger_handler_t activity_handler;
104+
#endif /* CONFIG_IIS2DLPC_ACTIVITY */
102105
#if defined(CONFIG_IIS2DLPC_TRIGGER_OWN_THREAD)
103106
K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_IIS2DLPC_THREAD_STACK_SIZE);
104107
struct k_thread thread;

drivers/sensor/iis2dlpc/iis2dlpc_trigger.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ static int iis2dlpc_enable_int(const struct device *dev,
6767
return iis2dlpc_pin_int1_route_set(ctx,
6868
&int_route.ctrl4_int1_pad_ctrl);
6969
#endif /* CONFIG_IIS2DLPC_TAP */
70+
#ifdef CONFIG_IIS2DLPC_ACTIVITY
71+
case SENSOR_TRIG_DELTA:
72+
/* set interrupt for pin INT1 */
73+
iis2dlpc_pin_int1_route_get(ctx,
74+
&int_route.ctrl4_int1_pad_ctrl);
75+
int_route.ctrl4_int1_pad_ctrl.int1_wu = enable;
76+
77+
iis2dlpc_act_mode_set(ctx, enable ? IIS2DLPC_DETECT_ACT_INACT
78+
: IIS2DLPC_NO_DETECTION);
79+
80+
return iis2dlpc_pin_int1_route_set(ctx,
81+
&int_route.ctrl4_int1_pad_ctrl);
82+
#endif /* CONFIG_IIS2DLPC_ACTIVITY */
7083
default:
7184
LOG_ERR("Unsupported trigger interrupt route %d", type);
7285
return -ENOTSUP;
@@ -102,6 +115,11 @@ int iis2dlpc_trigger_set(const struct device *dev,
102115
iis2dlpc->double_tap_handler = handler;
103116
return iis2dlpc_enable_int(dev, SENSOR_TRIG_DOUBLE_TAP, state);
104117
#endif /* CONFIG_IIS2DLPC_TAP */
118+
#ifdef CONFIG_IIS2DLPC_ACTIVITY
119+
case SENSOR_TRIG_DELTA:
120+
iis2dlpc->activity_handler = handler;
121+
return iis2dlpc_enable_int(dev, SENSOR_TRIG_DELTA, state);
122+
#endif /* CONFIG_IIS2DLPC_ACTIVITY */
105123
default:
106124
LOG_ERR("Unsupported sensor trigger");
107125
return -ENOTSUP;
@@ -124,6 +142,25 @@ static int iis2dlpc_handle_drdy_int(const struct device *dev)
124142
return 0;
125143
}
126144

145+
#ifdef CONFIG_IIS2DLPC_ACTIVITY
146+
static int iis2dlpc_handle_activity_int(const struct device *dev)
147+
{
148+
struct iis2dlpc_data *data = dev->data;
149+
sensor_trigger_handler_t handler = data->activity_handler;
150+
151+
struct sensor_trigger tap_trig = {
152+
.type = SENSOR_TRIG_DELTA,
153+
.chan = SENSOR_CHAN_ALL,
154+
};
155+
156+
if (handler) {
157+
handler(dev, &tap_trig);
158+
}
159+
160+
return 0;
161+
}
162+
#endif /* CONFIG_IIS2DLPC_ACTIVITY */
163+
127164
#ifdef CONFIG_IIS2DLPC_TAP
128165
static int iis2dlpc_handle_single_tap_int(const struct device *dev)
129166
{
@@ -183,6 +220,11 @@ static void iis2dlpc_handle_interrupt(const struct device *dev)
183220
iis2dlpc_handle_double_tap_int(dev);
184221
}
185222
#endif /* CONFIG_IIS2DLPC_TAP */
223+
#ifdef CONFIG_IIS2DLPC_ACTIVITY
224+
if (sources.all_int_src.wu_ia) {
225+
iis2dlpc_handle_activity_int(dev);
226+
}
227+
#endif /* CONFIG_IIS2DLPC_ACTIVITY */
186228

187229
gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy,
188230
GPIO_INT_EDGE_TO_ACTIVE);

0 commit comments

Comments
 (0)