Skip to content

Commit

Permalink
samples: up_squared/gpio_counter: update to new GPIO API
Browse files Browse the repository at this point in the history
Update the gpio_counter sample app for the UP Squared board:
() Update configuration calls to use new flags.
() Separate pin configuration into setting it to input, and
   setting the pin for interrupt.
() Use gpio_pin_set() instead of gpio_pin_write().

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
  • Loading branch information
dcpleung authored and carlescufi committed Jan 14, 2020
1 parent 2cf75c6 commit 07fc50a
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions samples/boards/up_squared/gpio_counter/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,9 @@ static volatile u32_t counter;

K_SEM_DEFINE(counter_sem, 0, 1);

#define INTR_PIN_FLAGS \
(GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_HIGH | \
GPIO_INT_DEBOUNCE | GPIO_PUD_PULL_DOWN)

#define NUM_PINS ARRAY_SIZE(counter_pins)
#define MASK (BIT(NUM_PINS) - 1)

#define GPIO_DEV DT_APL_GPIO_LABEL

void button_cb(struct device *gpiodev, struct gpio_callback *cb, u32_t pin)
{
counter++;
Expand Down Expand Up @@ -125,7 +119,7 @@ void main(void)
for (i = 0; i < NUM_PINS; i++) {
ret = gpio_pin_configure(counter_pins[i].gpio_dev,
counter_pins[i].pin,
GPIO_DIR_OUT);
GPIO_OUTPUT_LOW);
if (ret) {
printk("ERROR: cannot set HAT pin %d to OUT (%d)\n",
counter_pins[i].hat_num, ret);
Expand All @@ -135,27 +129,36 @@ void main(void)

/* Setup input pin */
ret = gpio_pin_configure(intr_pin.gpio_dev, intr_pin.pin,
INTR_PIN_FLAGS);
GPIO_INPUT);
if (ret) {
printk("ERROR: cannot set HAT pin %d to OUT (%d)\n",
printk("ERROR: cannot set HAT pin %d to IN (%d)\n",
intr_pin.hat_num, ret);
return;
}


/* Callback uses pin_mask, so need bit shifting */
gpio_init_callback(&gpio_cb, button_cb, (1 << intr_pin.pin));
gpio_add_callback(intr_pin.gpio_dev, &gpio_cb);
gpio_pin_enable_callback(intr_pin.gpio_dev, intr_pin.pin);

/* Setup input pin for interrupt */
ret = gpio_pin_interrupt_configure(intr_pin.gpio_dev, intr_pin.pin,
GPIO_INT_EDGE_RISING);
if (ret) {
printk("ERROR: cannot config interrupt on HAT pin %d (%d)\n",
intr_pin.hat_num, ret);
return;
}

/* main loop */
val = 0U;
while (1) {
printk("counter: 0x%x\n", val);

for (i = 0; i < NUM_PINS; i++) {
ret = gpio_pin_write(counter_pins[i].gpio_dev,
counter_pins[i].pin,
(val & BIT(i)));
ret = gpio_pin_set(counter_pins[i].gpio_dev,
counter_pins[i].pin,
(val & BIT(i)));
if (ret) {
printk("ERROR: cannot set HAT pin %d value (%d)\n",
counter_pins[i].hat_num, ret);
Expand Down

0 comments on commit 07fc50a

Please sign in to comment.