From d5ec78f9171007c198120f3e5f3c82ed5db72e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Fri, 8 Sep 2017 19:18:50 +0200 Subject: [PATCH 1/5] saul_reg: define saul_reg_t dev field as a pointer to const --- sys/include/saul_reg.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/include/saul_reg.h b/sys/include/saul_reg.h index 624e1db1b3..df9fa19eb3 100644 --- a/sys/include/saul_reg.h +++ b/sys/include/saul_reg.h @@ -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; From be3029d890da42d481df22ace7411d7d454725ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Fri, 8 Sep 2017 12:32:37 +0200 Subject: [PATCH 2/5] saul/gpio: use saul_gpio_params_t structure as saul device This provides the whole structure to read and write. --- drivers/saul/gpio_saul.c | 9 +++++---- sys/auto_init/saul/auto_init_gpio.c | 8 +------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/drivers/saul/gpio_saul.c b/drivers/saul/gpio_saul.c index e325115858..e8306aa9ba 100644 --- a/drivers/saul/gpio_saul.c +++ b/drivers/saul/gpio_saul.c @@ -23,12 +23,13 @@ #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; + res->val[0] = (gpio_read(p->pin)) ? 1: 0; memset(&(res->val[1]), 0, 2 * sizeof(int16_t)); res->unit = UNIT_BOOL; res->scale = 0; @@ -37,8 +38,8 @@ 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; + gpio_write(p->pin, state->val[0]); return 1; } diff --git a/sys/auto_init/saul/auto_init_gpio.c b/sys/auto_init/saul/auto_init_gpio.c index 0632502a29..4de96eeced 100644 --- a/sys/auto_init/saul/auto_init_gpio.c +++ b/sys/auto_init/saul/auto_init_gpio.c @@ -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)) { From c75470e6021ce9cbb3437c37c8c4073564dcd46b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Fri, 8 Sep 2017 13:34:49 +0200 Subject: [PATCH 3/5] saul/gpio: add support for inverted mode gpios --- drivers/include/saul/periph.h | 13 ++++++++++--- drivers/saul/gpio_saul.c | 10 ++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) 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; } From 05402dea298e43a0b5c155ff63565eb54dd618aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Fri, 8 Sep 2017 13:35:19 +0200 Subject: [PATCH 4/5] samr21-xpro/gpio: set LED/BUTTON gpios as inverted This makes LED go off when set to 0 and button reads 1 when pressed. --- boards/samr21-xpro/include/gpio_params.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/boards/samr21-xpro/include/gpio_params.h b/boards/samr21-xpro/include/gpio_params.h index 30ba529089..e1a36f35cc 100644 --- a/boards/samr21-xpro/include/gpio_params.h +++ b/boards/samr21-xpro/include/gpio_params.h @@ -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, }, }; From c39f4d831709aa2a276ae1a79f9968e28949ae4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Fri, 8 Sep 2017 13:58:47 +0200 Subject: [PATCH 5/5] saul/gpio: add support to initialized state --- drivers/.Makefile.dep.swo | Bin 0 -> 16384 bytes drivers/include/saul/periph.h | 4 ++-- sys/auto_init/saul/auto_init_gpio.c | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 drivers/.Makefile.dep.swo diff --git a/drivers/.Makefile.dep.swo b/drivers/.Makefile.dep.swo new file mode 100644 index 0000000000000000000000000000000000000000..db7d17170f1a1549c2bbae151e6b5b0624249bf5 GIT binary patch literal 16384 zcmeI(eT)=m0mt#-MJp<8BdDCEaVruzTDY0L-P^@P3-JyENQl5SJ!0L?&dlBp%Ubks$pYE9BhU4~aRCgE^(=t}-hMic9cu^!E z5}0WMV_s!NalX`_Rb>C8OTO?>X2GY1XIhGQzeqqNAQBJ>hy+9eA_0+rNI)d;Ax^-v zXG`Cs{j=MBIMRNc6?mM}e!i{!cr@^O`>A*k35Wzl0wMvCfJi_jAQBJ>hy+9eA_0+r zNZ|i40ab6U{a^Dp0Qmm@=KBBKeUfw*uVOEr!XAuZ05bY87yrChl75HZ;y3sSzK5j6En}1dn4S7U9|_CFx(dgp)Xq19%quQO0U4$3onT`S{x> zXcK;o!?3UkU&k7(Mi1uT(#Iv~Pk0Llu^Zd46{aU3t>ASR&Vt5}TB z;M4fW$0X?--oz_7j6)d5x3Ctg@hEz5;~q)+J1*e&ID_LjfM>BEDmGvUgZM12ew04o z4>*rs;CcK2&*0lwk31g0^^fowT)}C)f*;{|JO>?X@OkuL5iZY_q(9>wT)@xqBF4}_ z9cv+DIePIwHjXahEKcJTUcz2Hg*_O-05bR-dT^Qfcm|X6a`L>%WY!L?**d&=XiI7H z&^L$IZ5|rTJp7f6Z8)Y~Eot_csY@~Maa7%GlY9KSHQti>m%ukZ|4!pv}TX3Qu1UC8|%1dWph}(l1 ze<_N8g^$KuqylxsjgY2gbJ_T*a`Sc}P}*qS(Mp=8D6)Uu30)5-UvBqGH!A9w6^b)Z zVDv1HIv;MyB2;gyMX@8-aR!xZ&1_K)UMi;AG2@w8t}Ug!bamfOEw++S%XF=dPCb-k zxvCWMa%J31*PRb?{??odSJ+(2;O}Bf15vA9ae$^ao8fihQ!UDXtQ z)2G_1BFEpXOxN_#)Km4Q)t}3UTDTH7RV{NJ)Kn|e zaCF1zmHhSp1nbBDvi9`X|2MzizsS1&ulNhzz%l#~`>_vCVm$`&H7v$Gm<0(JSmXZ+ z2e1nsTx>)E3S{_u0#{h)pT%jM!cn|{eb@^X8!&`HJc#S8@!!SU_!<0tfn(T>ZPXZ8y}#m4 zvG$L+XVaD{TIiN7vL)RweJr;&pu1LVHA%OcGukM&%~HLQawB?-|4dq{s#c|>kJ*+H zad)rptgAJ%X~Z|YW2B}puwodVq20-SR=>`!20qx>*sg1@WMadax4q}o&NK84QT z$tvTQpHU47m*G@y_ZthBBD!BmMh6ydr&*Aj&DgZ?Rwm%z6DFy xpO~+koY6Ahy`?LnJr6^%+L~U>=W;VDBrsz2aYwC%cSj}<```4XE4y`Zc@H)=kD~wp literal 0 HcmV?d00001 diff --git a/drivers/include/saul/periph.h b/drivers/include/saul/periph.h index ec6575cfa7..460a045477 100644 --- a/drivers/include/saul/periph.h +++ b/drivers/include/saul/periph.h @@ -34,8 +34,8 @@ extern "C" { #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_INIT_CLEAR = (1 << 1), /**< set pin inactive after init */ + SAUL_GPIO_INIT_SET = (1 << 2), /**< set pin active after init */ } saul_gpio_flags_t; /** diff --git a/sys/auto_init/saul/auto_init_gpio.c b/sys/auto_init/saul/auto_init_gpio.c index 4de96eeced..c2a400a4bb 100644 --- a/sys/auto_init/saul/auto_init_gpio.c +++ b/sys/auto_init/saul/auto_init_gpio.c @@ -66,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])); }