Skip to content

Commit

Permalink
drivers: sensor: adxl372: add missing error handling
Browse files Browse the repository at this point in the history
Error handling was missing in numerous places, mostly for GPIO related
callbacks. An assertion has been used in the context of thread callback.

Fixes zephyrproject-rtos#38132

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
  • Loading branch information
gmarull committed Aug 30, 2021
1 parent 4b5bab2 commit c84c41c
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions drivers/sensor/adxl372/adxl372_trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static void adxl372_thread_cb(const struct device *dev)
const struct adxl372_dev_config *cfg = dev->config;
struct adxl372_data *drv_data = dev->data;
uint8_t status1, status2;
int ret;

/* Clear the status */
if (adxl372_get_status(dev, &status1, &status2, NULL) < 0) {
Expand All @@ -46,7 +47,9 @@ static void adxl372_thread_cb(const struct device *dev)
drv_data->drdy_handler(dev, &drv_data->drdy_trigger);
}

gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_EDGE_TO_ACTIVE);
ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
GPIO_INT_EDGE_TO_ACTIVE);
__ASSERT(ret == 0, "Interrupt configuration failed");
}

static void adxl372_gpio_callback(const struct device *dev,
Expand Down Expand Up @@ -93,7 +96,11 @@ int adxl372_trigger_set(const struct device *dev,
uint8_t int_mask, int_en, status1, status2;
int ret;

gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_DISABLE);
ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
GPIO_INT_DISABLE);
if (ret < 0) {
return ret;
}

switch (trig->type) {
case SENSOR_TRIG_THRESHOLD:
Expand All @@ -109,8 +116,7 @@ int adxl372_trigger_set(const struct device *dev,
break;
default:
LOG_ERR("Unsupported sensor trigger");
ret = -ENOTSUP;
goto out;
return -ENOTSUP;
}

if (handler) {
Expand All @@ -120,34 +126,50 @@ int adxl372_trigger_set(const struct device *dev,
}

ret = adxl372_reg_write_mask(dev, ADXL372_INT1_MAP, int_mask, int_en);
if (ret < 0) {
return ret;
}

adxl372_get_status(dev, &status1, &status2, NULL); /* Clear status */
out:
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_EDGE_TO_ACTIVE);
ret = adxl372_get_status(dev, &status1, &status2, NULL); /* Clear status */
if (ret < 0) {
return ret;
}

return ret;
ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
GPIO_INT_EDGE_TO_ACTIVE);
if (ret < 0) {
return ret;
}

return 0;
}

int adxl372_init_interrupt(const struct device *dev)
{
const struct adxl372_dev_config *cfg = dev->config;
struct adxl372_data *drv_data = dev->data;
int ret;

if (!device_is_ready(cfg->interrupt.port)) {
LOG_ERR("GPIO port %s not ready", cfg->interrupt.port->name);
return -EINVAL;
}

gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
ret = gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
if (ret < 0) {
return ret;
}

gpio_init_callback(&drv_data->gpio_cb,
adxl372_gpio_callback,
BIT(cfg->interrupt.pin));

if (gpio_add_callback(cfg->interrupt.port, &drv_data->gpio_cb) < 0) {
ret = gpio_add_callback(cfg->interrupt.port, &drv_data->gpio_cb);
if (ret < 0) {
LOG_ERR("Failed to set gpio callback!");
return -EIO;
return ret;
}

drv_data->dev = dev;

#if defined(CONFIG_ADXL372_TRIGGER_OWN_THREAD)
Expand Down

0 comments on commit c84c41c

Please sign in to comment.