drivers: sensor: nrf5: temp_nrf5: Add mutex to temp_nrf5_sample_fetch

Added mutex to protect against access from various contexts.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2020-07-07 16:20:03 +02:00 committed by Anas Nashif
commit 157f6f65d9

View file

@ -24,6 +24,7 @@ LOG_MODULE_REGISTER(temp_nrf5, CONFIG_SENSOR_LOG_LEVEL);
struct temp_nrf5_data {
struct k_sem device_sync_sem;
struct k_mutex mutex;
int32_t sample;
struct device *clk_dev;
};
@ -52,6 +53,8 @@ static int temp_nrf5_sample_fetch(struct device *dev, enum sensor_channel chan)
return -ENOTSUP;
}
k_mutex_lock(&data->mutex, K_FOREVER);
r = clock_control_async_on(data->clk_dev, CLOCK_CONTROL_NRF_SUBSYS_HF,
&clk_data);
__ASSERT_NO_MSG(!r);
@ -65,6 +68,8 @@ static int temp_nrf5_sample_fetch(struct device *dev, enum sensor_channel chan)
LOG_DBG("sample: %d", data->sample);
nrf_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_STOP);
k_mutex_unlock(&data->mutex);
return 0;
}
@ -117,6 +122,8 @@ static int temp_nrf5_init(struct device *dev)
__ASSERT_NO_MSG(data->clk_dev);
k_sem_init(&data->device_sync_sem, 0, UINT_MAX);
k_mutex_init(&data->mutex);
IRQ_CONNECT(
DT_INST_IRQN(0),
DT_INST_IRQ(0, priority),