drivers/sensor/fxas21002: Fix race condition during initialization

A race condition was identified during the initialization of the
driver when the trigger function is enabled. This exposed two issues:

1 - The fxas21002_handle_int is trying to acquire the semaphore before
    it gets initialized. To solve this we need to initialize the
    semaphore before calling the fxas21002_trigger_init function.

2 - During the fxas21002_trigger initialization the i2c bus is used
    (from fxas21002_handle_int function) at the same time as the
    fxas21002_set_power is being called. To fix this we need to
    initialize the semaphore with 0 and just release it after calling
    the fxas21002_set_power function in the fxas21002_int

Signed-off-by: Diego Sueiro <diego.sueiro@gmail.com>
This commit is contained in:
Diego Sueiro 2018-08-09 17:28:44 +01:00 committed by Maureen Helm
commit 72cad66c5b

View file

@ -245,6 +245,8 @@ static int fxas21002_init(struct device *dev)
return -EIO;
}
k_sem_init(&data->sem, 0, UINT_MAX);
#if CONFIG_FXAS21002_TRIGGER
if (fxas21002_trigger_init(dev)) {
SYS_LOG_ERR("Could not initialize interrupts");
@ -263,9 +265,7 @@ static int fxas21002_init(struct device *dev)
FXAS21002_POWER_ACTIVE,
config->dr);
k_busy_wait(transition_time);
k_sem_init(&data->sem, 1, UINT_MAX);
k_sem_give(&data->sem);
SYS_LOG_DBG("Init complete");