Skip to content

Commit 2779dd9

Browse files
Cherish-Gwwcarlescufi
authored andcommitted
drivers: sensor: bmm150: Add trigger support for bmm150 magnetometer sensor
Add bmm150 magetometer sensor data ready trigger support. Signed-off-by: Weiwei Guo <[email protected]>
1 parent d731338 commit 2779dd9

File tree

11 files changed

+278
-1
lines changed

11 files changed

+278
-1
lines changed

drivers/sensor/bmm150/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
zephyr_library()
44
zephyr_library_sources(bmm150.c bmm150_i2c.c bmm150_spi.c)
5+
zephyr_library_sources_ifdef(CONFIG_BMM150_TRIGGER bmm150_trigger.c)

drivers/sensor/bmm150/Kconfig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ config BMM150_PRESET_HIGH_ACCURACY
3333

3434
endchoice
3535

36+
choice BMM150_TRIGGER_MODE
37+
prompt "Trigger mode"
38+
default BMM150_TRIGGER_NONE
39+
help
40+
Specify the type of triggering to be used by the driver.
41+
42+
config BMM150_TRIGGER_NONE
43+
bool "No trigger"
44+
45+
config BMM150_TRIGGER_GLOBAL_THREAD
46+
bool "Use global thread"
47+
select BMM150_TRIGGER
48+
49+
config BMM150_TRIGGER_OWN_THREAD
50+
bool "Use own thread"
51+
select BMM150_TRIGGER
52+
53+
config BMM150_TRIGGER_DIRECT
54+
bool "Use IRQ handler"
55+
select BMM150_TRIGGER
56+
endchoice
57+
58+
config BMM150_TRIGGER
59+
bool
60+
3661
config BMM150_SAMPLING_RATE_RUNTIME
3762
bool "Dynamic sampling rate"
3863
help
@@ -48,4 +73,18 @@ config BMM150_SAMPLING_REP_Z
4873
help
4974
Enable alteration of Z oversampling at runtime.
5075

76+
config BMM150_THREAD_PRIORITY
77+
int "Own thread priority"
78+
depends on BMM150_TRIGGER_OWN_THREAD
79+
default 10
80+
help
81+
Priority of the thread used by the driver to handle interrupts.
82+
83+
config BMM150_THREAD_STACK_SIZE
84+
int "Own thread stack size"
85+
depends on BMM150_TRIGGER_OWN_THREAD
86+
default 1024
87+
help
88+
Stack size of thread used by the driver to handle interrupts.
89+
5190
endif # BMM150

drivers/sensor/bmm150/bmm150.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@ static const struct sensor_driver_api bmm150_api_funcs = {
495495
#endif
496496
.sample_fetch = bmm150_sample_fetch,
497497
.channel_get = bmm150_channel_get,
498+
499+
#ifdef CONFIG_BMM150_TRIGGER
500+
.trigger_set = bmm150_trigger_set,
501+
#endif
498502
};
499503

500504
static int bmm150_full_por(const struct device *dev)
@@ -668,6 +672,13 @@ static int bmm150_init(const struct device *dev)
668672
return -EIO;
669673
}
670674

675+
#ifdef CONFIG_BMM150_TRIGGER
676+
if (bmm150_trigger_mode_init(dev) < 0) {
677+
LOG_ERR("Cannot set up trigger mode.");
678+
return -EINVAL;
679+
}
680+
#endif
681+
671682
return 0;
672683
}
673684

@@ -686,6 +697,13 @@ static int bmm150_init(const struct device *dev)
686697
(BMM150_CONFIG_I2C(inst)), \
687698
(BMM150_CONFIG_SPI(inst)))
688699

700+
#if defined(CONFIG_BMM150_TRIGGER)
701+
#define BMM150_INT_CFG(inst) \
702+
.drdy_int = GPIO_DT_SPEC_INST_GET(inst, drdy_gpios),
703+
#else
704+
#define BMM150_INT_CFG(inst)
705+
#endif
706+
689707
/*
690708
* Main instantiation macro, which selects the correct bus-specific
691709
* instantiation macros for the instance.
@@ -694,6 +712,7 @@ static int bmm150_init(const struct device *dev)
694712
static struct bmm150_data bmm150_data_##inst; \
695713
static const struct bmm150_config bmm150_config_##inst = { \
696714
BMM150_BUS_CFG(inst) \
715+
BMM150_INT_CFG(inst) \
697716
}; \
698717
\
699718
PM_DEVICE_DT_INST_DEFINE(inst, pm_action); \

drivers/sensor/bmm150/bmm150.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,38 @@ struct bmm150_trim_regs {
157157
struct bmm150_config {
158158
union bmm150_bus bus;
159159
const struct bmm150_bus_io *bus_io;
160+
161+
#ifdef CONFIG_BMM150_TRIGGER
162+
struct gpio_dt_spec drdy_int;
163+
#endif
160164
};
161165

162166
struct bmm150_data {
163-
struct k_sem sem;
164167
struct bmm150_trim_regs tregs;
165168
int rep_xy, rep_z, odr, max_odr;
166169
int sample_x, sample_y, sample_z;
170+
171+
#if defined(CONFIG_BMM150_TRIGGER)
172+
struct gpio_callback gpio_cb;
173+
#endif
174+
175+
#ifdef CONFIG_BMM150_TRIGGER_OWN_THREAD
176+
struct k_sem sem;
177+
#endif
178+
179+
#ifdef CONFIG_BMM150_TRIGGER_GLOBAL_THREAD
180+
struct k_work work;
181+
#endif
182+
183+
#if defined(CONFIG_BMM150_TRIGGER_GLOBAL_THREAD) || \
184+
defined(CONFIG_BMM150_TRIGGER_DIRECT)
185+
const struct device *dev;
186+
#endif
187+
188+
#ifdef CONFIG_BMM150_TRIGGER
189+
const struct sensor_trigger *drdy_trigger;
190+
sensor_trigger_handler_t drdy_handler;
191+
#endif /* CONFIG_BMM150_TRIGGER */
167192
};
168193

169194
enum bmm150_axis {
@@ -198,6 +223,12 @@ enum bmm150_presets {
198223
/* Start-Up Time - from suspend to sleep (Max) */
199224
#define BMM150_START_UP_TIME K_MSEC(3)
200225

226+
int bmm150_trigger_mode_init(const struct device *dev);
227+
228+
int bmm150_trigger_set(const struct device *dev,
229+
const struct sensor_trigger *trig,
230+
sensor_trigger_handler_t handler);
231+
201232
int bmm150_reg_update_byte(const struct device *dev, uint8_t reg,
202233
uint8_t mask, uint8_t value);
203234

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/* Bosch BMM150 pressure sensor
2+
*
3+
* Copyright (c) 2020 Facebook, Inc. and its affiliates
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Datasheet:
8+
* https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmm150-ds001.pdf
9+
*/
10+
11+
#include <zephyr/kernel.h>
12+
#include <zephyr/pm/device.h>
13+
#include <zephyr/logging/log.h>
14+
15+
#include "bmm150.h"
16+
17+
LOG_MODULE_DECLARE(BMM150, CONFIG_SENSOR_LOG_LEVEL);
18+
19+
static void bmm150_handle_interrupts(const void *arg)
20+
{
21+
const struct device *dev = (const struct device *)arg;
22+
struct bmm150_data *data = dev->data;
23+
24+
if (data->drdy_handler) {
25+
data->drdy_handler(dev, data->drdy_trigger);
26+
}
27+
}
28+
29+
#ifdef CONFIG_BMM150_TRIGGER_OWN_THREAD
30+
static K_THREAD_STACK_DEFINE(bmm150_thread_stack,
31+
CONFIG_BMM150_THREAD_STACK_SIZE);
32+
static struct k_thread bmm150_thread;
33+
34+
static void bmm150_thread_main(void *arg1, void *unused1, void *unused2)
35+
{
36+
ARG_UNUSED(unused1);
37+
ARG_UNUSED(unused2);
38+
const struct device *dev = (const struct device *)arg1;
39+
struct bmm150_data *data = dev->data;
40+
41+
while (1) {
42+
k_sem_take(&data->sem, K_FOREVER);
43+
bmm150_handle_interrupts(dev);
44+
}
45+
}
46+
#endif
47+
48+
#ifdef CONFIG_BMM150_TRIGGER_GLOBAL_THREAD
49+
static void bmm150_work_handler(struct k_work *work)
50+
{
51+
struct bmm150_data *data = CONTAINER_OF(work,
52+
struct bmm150_data,
53+
work);
54+
55+
bmm150_handle_interrupts(data->dev);
56+
}
57+
#endif
58+
59+
static void bmm150_gpio_callback(const struct device *port,
60+
struct gpio_callback *cb,
61+
uint32_t pin)
62+
{
63+
struct bmm150_data *data = CONTAINER_OF(cb,
64+
struct bmm150_data,
65+
gpio_cb);
66+
67+
ARG_UNUSED(port);
68+
ARG_UNUSED(pin);
69+
70+
#if defined(CONFIG_BMM150_TRIGGER_OWN_THREAD)
71+
k_sem_give(&data->sem);
72+
#elif defined(CONFIG_BMM150_TRIGGER_GLOBAL_THREAD)
73+
k_work_submit(&data->work);
74+
#elif defined(CONFIG_BMM150_TRIGGER_DIRECT)
75+
bmm150_handle_interrupts(data->dev);
76+
#endif
77+
}
78+
79+
int bmm150_trigger_set(
80+
const struct device *dev,
81+
const struct sensor_trigger *trig,
82+
sensor_trigger_handler_t handler)
83+
{
84+
uint16_t values[BMM150_AXIS_XYZR_MAX];
85+
struct bmm150_data *data = dev->data;
86+
const struct bmm150_config *cfg = dev->config;
87+
88+
#ifdef CONFIG_PM_DEVICE
89+
enum pm_device_state state;
90+
91+
(void)pm_device_state_get(dev, &state);
92+
if (state != PM_DEVICE_STATE_ACTIVE) {
93+
return -EBUSY;
94+
}
95+
#endif
96+
97+
if (trig->type != SENSOR_TRIG_DATA_READY) {
98+
return -ENOTSUP;
99+
}
100+
101+
data->drdy_trigger = trig;
102+
data->drdy_handler = handler;
103+
104+
if (bmm150_reg_update_byte(dev,
105+
BMM150_REG_INT_DRDY,
106+
BMM150_MASK_DRDY_EN,
107+
(handler != NULL) << BMM150_SHIFT_DRDY_EN) < 0) {
108+
LOG_ERR("Failed to enable DRDY interrupt");
109+
return -EIO;
110+
}
111+
112+
/* Clean data registers */
113+
if (cfg->bus_io->read(&cfg->bus, BMM150_REG_X_L, (uint8_t *)values, sizeof(values)) < 0) {
114+
LOG_ERR("failed to read sample");
115+
return -EIO;
116+
}
117+
118+
return 0;
119+
}
120+
121+
int bmm150_trigger_mode_init(const struct device *dev)
122+
{
123+
struct bmm150_data *data = dev->data;
124+
const struct bmm150_config *cfg = dev->config;
125+
int ret;
126+
127+
if (!device_is_ready(cfg->drdy_int.port)) {
128+
LOG_ERR("INT device is not ready");
129+
return -ENODEV;
130+
}
131+
132+
#if defined(CONFIG_BMM150_TRIGGER_OWN_THREAD)
133+
k_sem_init(&data->sem, 0, 1);
134+
k_thread_create(
135+
&bmm150_thread,
136+
bmm150_thread_stack,
137+
CONFIG_BMM150_THREAD_STACK_SIZE,
138+
bmm150_thread_main,
139+
(void *)dev,
140+
NULL,
141+
NULL,
142+
K_PRIO_COOP(CONFIG_BMM150_THREAD_PRIORITY),
143+
0,
144+
K_NO_WAIT);
145+
#elif defined(CONFIG_BMM150_TRIGGER_GLOBAL_THREAD)
146+
k_work_init(&data->work, bmm150_work_handler);
147+
#endif
148+
149+
#if defined(CONFIG_BMM150_TRIGGER_GLOBAL_THREAD) || \
150+
defined(CONFIG_BMM150_TRIGGER_DIRECT)
151+
data->dev = dev;
152+
#endif
153+
154+
ret = gpio_pin_configure_dt(&cfg->drdy_int, GPIO_INPUT);
155+
if (ret < 0) {
156+
return ret;
157+
}
158+
159+
gpio_init_callback(&data->gpio_cb,
160+
bmm150_gpio_callback,
161+
BIT(cfg->drdy_int.pin));
162+
163+
ret = gpio_add_callback(cfg->drdy_int.port, &data->gpio_cb);
164+
if (ret < 0) {
165+
return ret;
166+
}
167+
168+
ret = gpio_pin_interrupt_configure_dt(&cfg->drdy_int,
169+
GPIO_INT_EDGE_TO_ACTIVE);
170+
if (ret < 0) {
171+
return ret;
172+
}
173+
174+
return 0;
175+
}

dts/bindings/sensor/bosch,bmm150.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44
# Common fields for BMM150
55

66
include: sensor-device.yaml
7+
8+
properties:
9+
drdy-gpios:
10+
type: phandle-array
11+
description: |
12+
This property specifies the connection for data ready pin.
13+
The polarity default is active high when sensor data is ready.

tests/drivers/build_all/sensor/i2c.dtsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ test_i2c_bmg160: bmg160@c {
8888
test_i2c_bmm150: bmm150@d {
8989
compatible = "bosch,bmm150";
9090
reg = <0xd>;
91+
drdy-gpios = <&test_gpio 0 0>;
9192
};
9293

9394
test_i2c_hmc5883l: hmc5883l@e {

tests/drivers/build_all/sensor/sensors_trigger_global.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONFIG_BMI08X_ACCEL_TRIGGER_GLOBAL_THREAD=y
1010
CONFIG_BMI08X_GYRO_TRIGGER_GLOBAL_THREAD=y
1111
CONFIG_BMI160_TRIGGER_GLOBAL_THREAD=y
1212
CONFIG_BMI270_TRIGGER_GLOBAL_THREAD=y
13+
CONFIG_BMM150_TRIGGER_GLOBAL_THREAD=y
1314
CONFIG_BMP388_TRIGGER_GLOBAL_THREAD=y
1415
CONFIG_BQ274XX_TRIGGER_GLOBAL_THREAD=y
1516
CONFIG_CCS811_TRIGGER_GLOBAL_THREAD=y

tests/drivers/build_all/sensor/sensors_trigger_none.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONFIG_BMI08X_ACCEL_TRIGGER_NONE=y
1010
CONFIG_BMI08X_GYRO_TRIGGER_NONE=y
1111
CONFIG_BMI160_TRIGGER_NONE=y
1212
CONFIG_BMI270_TRIGGER_NONE=y
13+
CONFIG_BMM150_TRIGGER_NONE=y
1314
CONFIG_BMP388_TRIGGER_NONE=y
1415
CONFIG_BQ274XX_TRIGGER_NONE=y
1516
CONFIG_CCS811_TRIGGER_NONE=y

tests/drivers/build_all/sensor/sensors_trigger_own.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CONFIG_BMI08X_ACCEL_TRIGGER_OWN_THREAD=y
99
CONFIG_BMI08X_GYRO_TRIGGER_OWN_THREAD=y
1010
CONFIG_BMI160_TRIGGER_OWN_THREAD=y
1111
CONFIG_BMI270_TRIGGER_OWN_THREAD=y
12+
CONFIG_BMM150_TRIGGER_OWN_THREAD=y
1213
CONFIG_BMP388_TRIGGER_OWN_THREAD=y
1314
CONFIG_BQ274XX_TRIGGER_OWN_THREAD=y
1415
CONFIG_CCS811_TRIGGER_OWN_THREAD=y

0 commit comments

Comments
 (0)