diff --git a/samples/drivers/button/Makefile b/samples/drivers/button/Makefile new file mode 100644 index 00000000000000..6c0c92869b0e87 --- /dev/null +++ b/samples/drivers/button/Makefile @@ -0,0 +1,6 @@ +MDEF_FILE = prj.mdef +KERNEL_TYPE = micro +BOARD ?= nucleo_f103rb +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/samples/drivers/button/README.txt b/samples/drivers/button/README.txt new file mode 100644 index 00000000000000..9e1cae6096621d --- /dev/null +++ b/samples/drivers/button/README.txt @@ -0,0 +1,41 @@ +Title: Button demo + +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. + +After startup, the program looks up a predefined GPIO device (GPIOC), +and configures pin 13 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 +information about this event along with its timestamp. + +-------------------------------------------------------------------------------- + +Building and Running Project: + +It can be built for a nucleo_f103rb board as follows: + + make + +The code may need adaption before running the code on another board. + +-------------------------------------------------------------------------------- + +Troubleshooting: + +Problems caused by out-dated project information can be addressed by +issuing one of the following commands then rebuilding the project: + + make clean # discard results of previous builds + # but keep existing configuration info +or + make pristine # discard results of previous builds + # and restore pre-defined configuration info + +-------------------------------------------------------------------------------- diff --git a/samples/drivers/button/prj.conf b/samples/drivers/button/prj.conf new file mode 100644 index 00000000000000..8b042db5406866 --- /dev/null +++ b/samples/drivers/button/prj.conf @@ -0,0 +1 @@ +CONFIG_STDOUT_CONSOLE=y diff --git a/samples/drivers/button/prj.mdef b/samples/drivers/button/prj.mdef new file mode 100644 index 00000000000000..7bb5168085ad46 --- /dev/null +++ b/samples/drivers/button/prj.mdef @@ -0,0 +1,5 @@ +% Application : Disco + +% TASK NAME PRIO ENTRY STACK GROUPS +% ================================== + TASK TASKA 7 main 2048 [EXE] diff --git a/samples/drivers/button/src/Makefile b/samples/drivers/button/src/Makefile new file mode 100644 index 00000000000000..00066e156780c2 --- /dev/null +++ b/samples/drivers/button/src/Makefile @@ -0,0 +1 @@ +obj-y = main.o diff --git a/samples/drivers/button/src/main.c b/samples/drivers/button/src/main.c new file mode 100644 index 00000000000000..3311318552930b --- /dev/null +++ b/samples/drivers/button/src/main.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2016 Open-RnD Sp. z o.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#if defined(CONFIG_STDOUT_CONSOLE) +#include +#define PRINT printf +#else +#include +#define PRINT printk +#endif + +/* change this to use another GPIO port */ +#define PORT "GPIOC" +/* change this to use another GPIO pin */ +#define PIN 13 +/* 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, uint32_t pin) +{ + PRINT("Button pressed at %d\n", sys_tick_get_32()); +} + +void main(void) +{ + struct device *gpiob; + + gpiob = device_get_binding(PORT); + + gpio_pin_configure(gpiob, PIN, + GPIO_DIR_IN | GPIO_INT + | EDGE + | PULL_UP); + gpio_set_callback(gpiob, button_pressed); + gpio_pin_enable_callback(gpiob, PIN); + + while (1) { + int val = 0; + + gpio_pin_read(gpiob, PIN, &val); + PRINT("GPIO val: %d\n", val); + task_sleep(MSEC(500)); + } +}