samples: button: modify sample to work on more boards

Change-Id: Ib29dc5ce70618f6c4d0a4d732910ea67f92c37a5
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Anas Nashif 2016-10-01 21:45:08 -04:00 committed by Anas Nashif
commit 43f1c36ed5
5 changed files with 33 additions and 16 deletions

View file

@ -5,11 +5,11 @@ Description:
A simple button demo showcasing the use of GPIO input with interrupts. A simple button demo showcasing the use of GPIO input with interrupts.
The demo assumes that a push button is connected to one of GPIO The demo assumes that a push button is connected to one of GPIO
lines. The sample code is configured to work on Nucleo-64 F103RB lines. The sample code is configured to work on boards with user defined
board with button B1 providing the input. buttons and that have defined the SW0_* variable in board.h
After startup, the program looks up a predefined GPIO device (GPIOC), After startup, the program looks up a predefined GPIO device,
and configures pin 13 in input mode, enabling interrupt generation on and configures the pin in input mode, enabling interrupt generation on
falling edge. During each iteration of the main loop, the state of falling edge. During each iteration of the main loop, the state of
GPIO line is monitored and printed to the serial console. When the GPIO line is monitored and printed to the serial console. When the
input button gets pressed, the interrupt handler will print an input button gets pressed, the interrupt handler will print an

View file

@ -1 +1 @@
CONFIG_STDOUT_CONSOLE=y CONFIG_GPIO=y

View file

@ -2,4 +2,4 @@
% TASK NAME PRIO ENTRY STACK GROUPS % TASK NAME PRIO ENTRY STACK GROUPS
% ================================== % ==================================
TASK TASKA 7 main 2048 [EXE] TASK TASKA 7 main 1024 [EXE]

View file

@ -15,25 +15,37 @@
*/ */
#include <zephyr.h> #include <zephyr.h>
#include <board.h>
#include <device.h> #include <device.h>
#include <gpio.h> #include <gpio.h>
#include <misc/util.h> #include <misc/util.h>
#include <stdio.h> #include <misc/printk.h>
/* change this to use another GPIO port */ /* change this to use another GPIO port */
#define PORT "GPIOC" #ifdef SW0_GPIO_NAME
#define PORT SW0_GPIO_NAME
#else
#error SW0_GPIO_NAME needs to be set in board.h
#endif
/* change this to use another GPIO pin */ /* change this to use another GPIO pin */
#define PIN 13 #ifdef SW0_GPIO_PIN
#define PIN SW0_GPIO_PIN
#else
#error SW0_GPIO_PIN needs to be set in board.h
#endif
/* change this to enable pull-up/pull-down */ /* change this to enable pull-up/pull-down */
#define PULL_UP 0 #define PULL_UP 0
/* change this to use a different interrupt trigger */ /* change this to use a different interrupt trigger */
#define EDGE (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW) #define EDGE (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW)
void button_pressed(struct device *gpiob, void button_pressed(struct device *gpiob, struct gpio_callback *cb,
struct gpio_callback *cb, uint32_t pins) uint32_t pins)
{ {
printf("Button pressed at %d\n", sys_tick_get_32()); printk("Button pressed at %d\n", sys_tick_get_32());
} }
static struct gpio_callback gpio_cb; static struct gpio_callback gpio_cb;
@ -42,12 +54,14 @@ void main(void)
{ {
struct device *gpiob; struct device *gpiob;
printk("Press the user defined button on the board\n");
gpiob = device_get_binding(PORT); gpiob = device_get_binding(PORT);
if (!gpiob) {
printk("error\n");
}
gpio_pin_configure(gpiob, PIN, gpio_pin_configure(gpiob, PIN,
GPIO_DIR_IN | GPIO_INT GPIO_DIR_IN | GPIO_INT | PULL_UP | EDGE);
| EDGE
| PULL_UP);
gpio_init_callback(&gpio_cb, button_pressed, BIT(PIN)); gpio_init_callback(&gpio_cb, button_pressed, BIT(PIN));
@ -58,7 +72,6 @@ void main(void)
int val = 0; int val = 0;
gpio_pin_read(gpiob, PIN, &val); gpio_pin_read(gpiob, PIN, &val);
printf("GPIO val: %d\n", val);
task_sleep(MSEC(500)); task_sleep(MSEC(500));
} }
} }

View file

@ -0,0 +1,4 @@
[test]
build_only = true
tags = samples
platform_whitelist = nucleo_f103rb quark_se_c1000_devboard