forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples/button: button input example
An example of button driver. Change-Id: I8af860058166d7ca5653f50ff585bc9784008f78 Origin: Original Signed-off-by: Maciej Borzecki <maciek.borzecki@gmail.com>
- Loading branch information
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
-------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CONFIG_STDOUT_CONSOLE=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
obj-y = main.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |