Skip to content

Commit

Permalink
samples/button: button input example
Browse files Browse the repository at this point in the history
An example of button driver.

Change-Id: I8af860058166d7ca5653f50ff585bc9784008f78
Origin: Original
Signed-off-by: Maciej Borzecki <maciek.borzecki@gmail.com>
  • Loading branch information
bboozzoo authored and dkalowsk committed Mar 18, 2016
1 parent 624c9e3 commit 47d88b2
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
6 changes: 6 additions & 0 deletions samples/drivers/button/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
MDEF_FILE = prj.mdef
KERNEL_TYPE = micro
BOARD ?= nucleo_f103rb
CONF_FILE = prj.conf

include ${ZEPHYR_BASE}/Makefile.inc
41 changes: 41 additions & 0 deletions samples/drivers/button/README.txt
Original file line number Diff line number Diff line change
@@ -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

--------------------------------------------------------------------------------
1 change: 1 addition & 0 deletions samples/drivers/button/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_STDOUT_CONSOLE=y
5 changes: 5 additions & 0 deletions samples/drivers/button/prj.mdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
% Application : Disco

% TASK NAME PRIO ENTRY STACK GROUPS
% ==================================
TASK TASKA 7 main 2048 [EXE]
1 change: 1 addition & 0 deletions samples/drivers/button/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
obj-y = main.o
64 changes: 64 additions & 0 deletions samples/drivers/button/src/main.c
Original file line number Diff line number Diff line change
@@ -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 <zephyr.h>
#include <device.h>
#include <gpio.h>

#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define PRINT printf
#else
#include <misc/printk.h>
#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));
}
}

0 comments on commit 47d88b2

Please sign in to comment.