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.
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

View file

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

View file

@ -2,4 +2,4 @@
% 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 <board.h>
#include <device.h>
#include <gpio.h>
#include <misc/util.h>
#include <stdio.h>
#include <misc/printk.h>
/* 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));
}
}

View file

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