diff --git a/drivers/include/saul/periph.h b/drivers/include/saul/periph.h index 00a148538f..ec6575cfa7 100644 --- a/drivers/include/saul/periph.h +++ b/drivers/include/saul/periph.h @@ -32,13 +32,20 @@ extern "C" { #endif #ifdef MODULE_SAUL_GPIO +typedef enum { + SAUL_GPIO_INVERTED = (1 << 0), /**< pin is used as inverted */ + SAUL_GPIO_INIT_CLEAR = 1 << 1, /**< set pin inactive after initialization */ + SAUL_GPIO_INIT_SET = 1 << 2, /**< set pin active after initialization */ +} saul_gpio_flags_t; + /** * @brief Direct mapped GPIO configuration values */ typedef struct { - const char *name; /**< name of the device connected to this pin */ - gpio_t pin; /**< GPIO pin to initialize and expose */ - gpio_mode_t mode; /**< pin mode to use */ + const char *name; /**< name of the device connected to this pin */ + gpio_t pin; /**< GPIO pin to initialize and expose */ + gpio_mode_t mode; /**< pin mode to use */ + saul_gpio_flags_t flags; /**< Configuration flags */ } saul_gpio_params_t; #endif /* MODULE_SAUL_GPIO */ diff --git a/drivers/saul/gpio_saul.c b/drivers/saul/gpio_saul.c index e8306aa9ba..0b5431c811 100644 --- a/drivers/saul/gpio_saul.c +++ b/drivers/saul/gpio_saul.c @@ -29,7 +29,10 @@ static int read(const void *dev, phydat_t *res) { const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev; - res->val[0] = (gpio_read(p->pin)) ? 1: 0; + int inverted = (p->flags & SAUL_GPIO_INVERTED); + + res->val[0] = (gpio_read(p->pin)) ? !inverted : inverted; + memset(&(res->val[1]), 0, 2 * sizeof(int16_t)); res->unit = UNIT_BOOL; res->scale = 0; @@ -39,7 +42,10 @@ static int read(const void *dev, phydat_t *res) static int write(const void *dev, phydat_t *state) { const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev; - gpio_write(p->pin, state->val[0]); + int inverted = (p->flags & SAUL_GPIO_INVERTED); + int value = (state->val[0] ? !inverted : inverted); + + gpio_write(p->pin, value); return 1; }