samples: button: update to use new GPIO API
Update gpio_pin_configure() to take into account GPIO flags defined by the devicetree. Use gpio_pin_get/gpio_pin_set to verify reading/writing of logical pin values. Use gpio_pin_interrupt_configure() to configure interrupts. Signed-off-by: Piotr Mienkowski <piotr.mienkowski@gmail.com>
This commit is contained in:
parent
ebc5bc62b0
commit
951f91071a
1 changed files with 55 additions and 51 deletions
|
@ -11,79 +11,83 @@
|
||||||
#include <sys/printk.h>
|
#include <sys/printk.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
/* change this to use another GPIO port */
|
#define SLEEP_TIME_MS 1
|
||||||
#ifndef DT_ALIAS_SW0_GPIOS_CONTROLLER
|
|
||||||
#ifdef SW0_GPIO_NAME
|
|
||||||
#define DT_ALIAS_SW0_GPIOS_CONTROLLER SW0_GPIO_NAME
|
|
||||||
#else
|
|
||||||
#error SW0_GPIO_NAME or DT_ALIAS_SW0_GPIOS_CONTROLLER needs to be set in board.h
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#define PORT DT_ALIAS_SW0_GPIOS_CONTROLLER
|
|
||||||
|
|
||||||
/* change this to use another GPIO pin */
|
|
||||||
#ifdef DT_ALIAS_SW0_GPIOS_PIN
|
|
||||||
#define PIN DT_ALIAS_SW0_GPIOS_PIN
|
|
||||||
#else
|
|
||||||
#error DT_ALIAS_SW0_GPIOS_PIN needs to be set in board.h
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* change to use another GPIO pin interrupt config */
|
|
||||||
#ifdef DT_ALIAS_SW0_GPIOS_FLAGS
|
|
||||||
#define EDGE (DT_ALIAS_SW0_GPIOS_FLAGS | GPIO_INT_EDGE)
|
|
||||||
#else
|
|
||||||
/*
|
|
||||||
* If DT_ALIAS_SW0_GPIOS_FLAGS not defined used default EDGE value.
|
|
||||||
* Change this to use a different interrupt trigger
|
|
||||||
*/
|
|
||||||
#define EDGE (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* change this to enable pull-up/pull-down */
|
|
||||||
#ifndef DT_ALIAS_SW0_GPIOS_FLAGS
|
#ifndef DT_ALIAS_SW0_GPIOS_FLAGS
|
||||||
#ifdef DT_ALIAS_SW0_GPIOS_PIN_PUD
|
|
||||||
#define DT_ALIAS_SW0_GPIOS_FLAGS DT_ALIAS_SW0_GPIOS_PIN_PUD
|
|
||||||
#else
|
|
||||||
#define DT_ALIAS_SW0_GPIOS_FLAGS 0
|
#define DT_ALIAS_SW0_GPIOS_FLAGS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef DT_ALIAS_LED0_GPIOS_FLAGS
|
||||||
|
#define DT_ALIAS_LED0_GPIOS_FLAGS 0
|
||||||
#endif
|
#endif
|
||||||
#define PULL_UP DT_ALIAS_SW0_GPIOS_FLAGS
|
|
||||||
|
|
||||||
/* Sleep time */
|
void button_pressed(struct device *dev, struct gpio_callback *cb,
|
||||||
#define SLEEP_TIME 500
|
|
||||||
|
|
||||||
|
|
||||||
void button_pressed(struct device *gpiob, struct gpio_callback *cb,
|
|
||||||
u32_t pins)
|
u32_t pins)
|
||||||
{
|
{
|
||||||
printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32());
|
printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32());
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct gpio_callback gpio_cb;
|
static struct gpio_callback button_cb_data;
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
struct device *gpiob;
|
struct device *dev_button;
|
||||||
|
int ret;
|
||||||
|
|
||||||
printk("Press the user defined button on the board\n");
|
dev_button = device_get_binding(DT_ALIAS_SW0_GPIOS_CONTROLLER);
|
||||||
gpiob = device_get_binding(PORT);
|
if (dev_button == NULL) {
|
||||||
if (!gpiob) {
|
printk("Error: didn't find %s device\n",
|
||||||
printk("error\n");
|
DT_ALIAS_SW0_GPIOS_CONTROLLER);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_pin_configure(gpiob, PIN,
|
ret = gpio_pin_configure(dev_button, DT_ALIAS_SW0_GPIOS_PIN,
|
||||||
GPIO_DIR_IN | GPIO_INT | PULL_UP | EDGE);
|
DT_ALIAS_SW0_GPIOS_FLAGS | GPIO_INPUT);
|
||||||
|
if (ret != 0) {
|
||||||
|
printk("Error %d: failed to configure pin %d '%s'\n",
|
||||||
|
ret, DT_ALIAS_SW0_GPIOS_PIN, DT_ALIAS_SW0_LABEL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
gpio_init_callback(&gpio_cb, button_pressed, BIT(PIN));
|
ret = gpio_pin_interrupt_configure(dev_button, DT_ALIAS_SW0_GPIOS_PIN,
|
||||||
|
GPIO_INT_EDGE_TO_ACTIVE);
|
||||||
|
if (ret != 0) {
|
||||||
|
printk("Error %d: failed to configure interrupt on pin %d '%s'\n",
|
||||||
|
ret, DT_ALIAS_SW0_GPIOS_PIN, DT_ALIAS_SW0_LABEL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
gpio_add_callback(gpiob, &gpio_cb);
|
gpio_init_callback(&button_cb_data, button_pressed,
|
||||||
gpio_pin_enable_callback(gpiob, PIN);
|
BIT(DT_ALIAS_SW0_GPIOS_PIN));
|
||||||
|
gpio_add_callback(dev_button, &button_cb_data);
|
||||||
|
|
||||||
|
#ifdef DT_ALIAS_LED0_GPIOS_CONTROLLER
|
||||||
|
struct device *dev_led;
|
||||||
|
|
||||||
|
dev_led = device_get_binding(DT_ALIAS_LED0_GPIOS_CONTROLLER);
|
||||||
|
if (dev_led == NULL) {
|
||||||
|
printk("Error: didn't find %s device\n",
|
||||||
|
DT_ALIAS_LED0_GPIOS_CONTROLLER);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = gpio_pin_configure(dev_led, DT_ALIAS_LED0_GPIOS_PIN,
|
||||||
|
DT_ALIAS_LED0_GPIOS_FLAGS | GPIO_OUTPUT);
|
||||||
|
if (ret != 0) {
|
||||||
|
printk("Error %d: failed to configure pin %d '%s'\n",
|
||||||
|
ret, DT_ALIAS_LED0_GPIOS_PIN, DT_ALIAS_LED0_LABEL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
printk("Press %s on the board\n", DT_ALIAS_SW0_LABEL);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
u32_t val = 0U;
|
#ifdef DT_ALIAS_LED0_GPIOS_CONTROLLER
|
||||||
|
bool val;
|
||||||
|
|
||||||
gpio_pin_read(gpiob, PIN, &val);
|
val = gpio_pin_get(dev_button, DT_ALIAS_SW0_GPIOS_PIN);
|
||||||
k_sleep(SLEEP_TIME);
|
gpio_pin_set(dev_led, DT_ALIAS_LED0_GPIOS_PIN, val);
|
||||||
|
k_sleep(SLEEP_TIME_MS);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue