Skip to content

Commit d2fdf52

Browse files
XenuIsWatchingdanieldegrasse
authored andcommitted
drivers: sensor: bmm350: fix handling interrupt race condition
There is a race condition where an interrupt can fire before the drdy_handler is registered. The drdy_handler will tradionaly callback the sample get clearing the interrupt... but if it's not configured and NULL, the interrupt will stay forever latched. Read the interrupt status to clear the interrupt flag allowing it to trigger again. Also, move the serial api function helpers in to the header allowing them to be used from other c files. Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
1 parent c7e1f0b commit d2fdf52

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

drivers/sensor/bosch/bmm350/bmm350.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,6 @@
1414

1515
LOG_MODULE_REGISTER(BMM350, CONFIG_SENSOR_LOG_LEVEL);
1616

17-
static inline int bmm350_bus_check(const struct device *dev)
18-
{
19-
const struct bmm350_config *cfg = dev->config;
20-
21-
return cfg->bus_io->check(&cfg->bus);
22-
}
23-
24-
static inline int bmm350_reg_read(const struct device *dev, uint8_t start, uint8_t *buf, int size)
25-
{
26-
const struct bmm350_config *cfg = dev->config;
27-
28-
return cfg->bus_io->read(&cfg->bus, start, buf, size);
29-
}
30-
31-
int bmm350_reg_write(const struct device *dev, uint8_t reg, uint8_t val)
32-
{
33-
const struct bmm350_config *cfg = dev->config;
34-
35-
return cfg->bus_io->write(&cfg->bus, reg, val);
36-
}
37-
3817
static int8_t bmm350_read_otp_word(const struct device *dev, uint8_t addr, uint16_t *lsb_msb)
3918
{
4019
int8_t ret = 0;

drivers/sensor/bosch/bmm350/bmm350.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,5 +495,27 @@ int bmm350_trigger_mode_init(const struct device *dev);
495495

496496
int bmm350_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
497497
sensor_trigger_handler_t handler);
498-
int bmm350_reg_write(const struct device *dev, uint8_t reg, uint8_t val);
498+
499+
/* inline helper functions */
500+
static inline int bmm350_bus_check(const struct device *dev)
501+
{
502+
const struct bmm350_config *cfg = dev->config;
503+
504+
return cfg->bus_io->check(&cfg->bus);
505+
}
506+
507+
static inline int bmm350_reg_read(const struct device *dev, uint8_t start, uint8_t *buf, int size)
508+
{
509+
const struct bmm350_config *cfg = dev->config;
510+
511+
return cfg->bus_io->read(&cfg->bus, start, buf, size);
512+
}
513+
514+
static inline int bmm350_reg_write(const struct device *dev, uint8_t reg, uint8_t val)
515+
{
516+
const struct bmm350_config *cfg = dev->config;
517+
518+
return cfg->bus_io->write(&cfg->bus, reg, val);
519+
}
520+
499521
#endif /* __SENSOR_BMM350_H__ */

drivers/sensor/bosch/bmm350/bmm350_trigger.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,25 @@
1515

1616
LOG_MODULE_DECLARE(BMM350, CONFIG_SENSOR_LOG_LEVEL);
1717

18-
static void bmm350_handle_interrupts(const void *arg)
18+
static int bmm350_handle_interrupts(const void *arg)
1919
{
2020
const struct device *dev = (const struct device *)arg;
2121
struct bmm350_data *data = dev->data;
22+
uint8_t intr_status;
23+
int ret;
24+
25+
/* Read interrupt status to clear interrupt */
26+
ret = bmm350_reg_read(dev, BMM350_REG_INT_STATUS, &intr_status, sizeof(intr_status));
27+
if (ret < 0) {
28+
LOG_ERR("%s: failed to read interrupt status", dev->name);
29+
return ret;
30+
}
2231

2332
if (data->drdy_handler) {
2433
data->drdy_handler(dev, data->drdy_trigger);
2534
}
35+
36+
return 0;
2637
}
2738

2839
#ifdef CONFIG_BMM350_TRIGGER_OWN_THREAD
@@ -38,7 +49,7 @@ static void bmm350_thread_main(void *arg1, void *unused1, void *unused2)
3849

3950
while (1) {
4051
k_sem_take(&data->sem, K_FOREVER);
41-
bmm350_handle_interrupts(dev);
52+
(void)bmm350_handle_interrupts(dev);
4253
}
4354
}
4455
#endif
@@ -48,7 +59,7 @@ static void bmm350_work_handler(struct k_work *work)
4859
{
4960
struct bmm350_data *data = CONTAINER_OF(work, struct bmm350_data, work);
5061

51-
bmm350_handle_interrupts(data->dev);
62+
(void)bmm350_handle_interrupts(data->dev);
5263
}
5364
#endif
5465

0 commit comments

Comments
 (0)