From 43f1c36ed59636a535fa72bf4b4981badaed20ae Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Sat, 1 Oct 2016 21:45:08 -0400 Subject: [PATCH] samples: button: modify sample to work on more boards Change-Id: Ib29dc5ce70618f6c4d0a4d732910ea67f92c37a5 Signed-off-by: Anas Nashif --- samples/drivers/button/README.txt | 8 +++---- samples/drivers/button/prj.conf | 2 +- samples/drivers/button/prj.mdef | 2 +- samples/drivers/button/src/main.c | 33 ++++++++++++++++++++--------- samples/drivers/button/testcase.ini | 4 ++++ 5 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 samples/drivers/button/testcase.ini diff --git a/samples/drivers/button/README.txt b/samples/drivers/button/README.txt index 9e1cae60966..e2e9e748256 100644 --- a/samples/drivers/button/README.txt +++ b/samples/drivers/button/README.txt @@ -5,11 +5,11 @@ Description: 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 -lines. The sample code is configured to work on Nucleo-64 F103RB -board with button B1 providing the input. +lines. The sample code is configured to work on boards with user defined +buttons and that have defined the SW0_* variable in board.h -After startup, the program looks up a predefined GPIO device (GPIOC), -and configures pin 13 in input mode, enabling interrupt generation on +After startup, the program looks up a predefined GPIO device, +and configures the pin in input mode, enabling interrupt generation on falling edge. During each iteration of the main loop, the state of GPIO line is monitored and printed to the serial console. When the input button gets pressed, the interrupt handler will print an diff --git a/samples/drivers/button/prj.conf b/samples/drivers/button/prj.conf index 8b042db5406..91c3c15b37d 100644 --- a/samples/drivers/button/prj.conf +++ b/samples/drivers/button/prj.conf @@ -1 +1 @@ -CONFIG_STDOUT_CONSOLE=y +CONFIG_GPIO=y diff --git a/samples/drivers/button/prj.mdef b/samples/drivers/button/prj.mdef index 7bb5168085a..d52174ca9fb 100644 --- a/samples/drivers/button/prj.mdef +++ b/samples/drivers/button/prj.mdef @@ -2,4 +2,4 @@ % TASK NAME PRIO ENTRY STACK GROUPS % ================================== - TASK TASKA 7 main 2048 [EXE] + TASK TASKA 7 main 1024 [EXE] diff --git a/samples/drivers/button/src/main.c b/samples/drivers/button/src/main.c index d54f0cb7d2d..0d7504c2c73 100644 --- a/samples/drivers/button/src/main.c +++ b/samples/drivers/button/src/main.c @@ -15,25 +15,37 @@ */ #include +#include #include #include #include -#include +#include /* 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 */ -#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 */ #define PULL_UP 0 + /* change this to use a different interrupt trigger */ #define EDGE (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW) -void button_pressed(struct device *gpiob, - struct gpio_callback *cb, uint32_t pins) +void button_pressed(struct device *gpiob, struct gpio_callback *cb, + 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; @@ -42,12 +54,14 @@ void main(void) { struct device *gpiob; + printk("Press the user defined button on the board\n"); gpiob = device_get_binding(PORT); + if (!gpiob) { + printk("error\n"); + } gpio_pin_configure(gpiob, PIN, - GPIO_DIR_IN | GPIO_INT - | EDGE - | PULL_UP); + GPIO_DIR_IN | GPIO_INT | PULL_UP | EDGE); gpio_init_callback(&gpio_cb, button_pressed, BIT(PIN)); @@ -58,7 +72,6 @@ void main(void) int val = 0; gpio_pin_read(gpiob, PIN, &val); - printf("GPIO val: %d\n", val); task_sleep(MSEC(500)); } } diff --git a/samples/drivers/button/testcase.ini b/samples/drivers/button/testcase.ini new file mode 100644 index 00000000000..d530e7b82f1 --- /dev/null +++ b/samples/drivers/button/testcase.ini @@ -0,0 +1,4 @@ +[test] +build_only = true +tags = samples +platform_whitelist = nucleo_f103rb quark_se_c1000_devboard