1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #7586 from cladmi/pr/saul/gpio

saul/gpio: Low footprint handling of active-low signals + state initialization
This commit is contained in:
Martine Lenders 2017-10-10 18:49:02 +02:00 committed by GitHub
commit 72edaa3e47
6 changed files with 33 additions and 17 deletions

View File

@ -37,12 +37,14 @@ static const saul_gpio_params_t saul_gpio_params[] =
{
.name = "LED(orange)",
.pin = LED0_PIN,
.mode = GPIO_OUT
.mode = GPIO_OUT,
.flags = SAUL_GPIO_INVERTED,
},
{
.name = "Button(SW0)",
.pin = BTN0_PIN,
.mode = BTN0_MODE
.mode = BTN0_MODE,
.flags = SAUL_GPIO_INVERTED,
},
};

BIN
drivers/.Makefile.dep.swo Normal file

Binary file not shown.

View File

@ -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 init */
SAUL_GPIO_INIT_SET = (1 << 2), /**< set pin active after init */
} 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 */

View File

@ -23,12 +23,16 @@
#include "saul.h"
#include "phydat.h"
#include "periph/gpio.h"
#include "saul/periph.h"
static int read(const void *dev, phydat_t *res)
{
gpio_t pin = *((const gpio_t *)dev);
res->val[0] = (gpio_read(pin)) ? 1 : 0;
const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev;
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;
@ -37,8 +41,11 @@ static int read(const void *dev, phydat_t *res)
static int write(const void *dev, phydat_t *state)
{
gpio_t pin = *((const gpio_t *)dev);
gpio_write(pin, state->val[0]);
const saul_gpio_params_t *p = (const saul_gpio_params_t *)dev;
int inverted = (p->flags & SAUL_GPIO_INVERTED);
int value = (state->val[0] ? !inverted : inverted);
gpio_write(p->pin, value);
return 1;
}

View File

@ -32,11 +32,6 @@
*/
#define SAUL_GPIO_NUMOF (sizeof(saul_gpio_params)/sizeof(saul_gpio_params[0]))
/**
* @brief Allocate memory for the device descriptors
*/
static gpio_t saul_gpios[SAUL_GPIO_NUMOF];
/**
* @brief Memory for the registry entries
*/
@ -60,8 +55,7 @@ void auto_init_gpio(void)
LOG_DEBUG("[auto_init_saul] initializing GPIO #%u\n", i);
saul_gpios[i] = p->pin;
saul_reg_entries[i].dev = &(saul_gpios[i]);
saul_reg_entries[i].dev = p;
saul_reg_entries[i].name = p->name;
if ((p->mode == GPIO_IN) || (p->mode == GPIO_IN_PD) ||
(p->mode == GPIO_IN_PU)) {
@ -72,6 +66,12 @@ void auto_init_gpio(void)
}
/* initialize the GPIO pin */
gpio_init(p->pin, p->mode);
/* set initial pin state if configured */
if (p->flags & (SAUL_GPIO_INIT_CLEAR | SAUL_GPIO_INIT_SET)) {
phydat_t s;
s.val[0] = (p->flags & SAUL_GPIO_INIT_SET);
saul_reg_entries[i].driver->write(p, &s);
}
/* add to registry */
saul_reg_add(&(saul_reg_entries[i]));
}

View File

@ -38,7 +38,7 @@ extern "C" {
*/
typedef struct saul_reg {
struct saul_reg *next; /**< pointer to the next device */
void *dev; /**< pointer to the device descriptor */
const void *dev; /**< pointer to the device descriptor */
const char *name; /**< string identifier for the device */
saul_driver_t const *driver; /**< the devices read callback */
} saul_reg_t;