Skip to content

Commit

Permalink
drivers: sensor: nrf5: temp_nrf5: Add mutex to temp_nrf5_sample_fetch
Browse files Browse the repository at this point in the history
Added mutex to protect against access from various contexts.

Signed-off-by: Krzysztof Chruscinski <[email protected]>
  • Loading branch information
nordic-krch authored and nashif committed Jul 7, 2020
1 parent 2d8e71b commit 157f6f6
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions drivers/sensor/nrf5/temp_nrf5.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 157f6f6

Please sign in to comment.