Skip to content

Commit 9e72854

Browse files
Kamil GaworKamil Gawor
authored andcommitted
samples: driver: Add LED GPIO sample
This commit provides a new sample which demonstrate how to use the GPIO LED drivers. Signed-off-by: Kamil Gawor <Kamil.Gawor@nordicsemi.no>
1 parent 8c29e95 commit 9e72854

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
5+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
6+
project(led_gpio)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.. _gpio-led-sample:
2+
3+
LED GPIO Sample
4+
###############
5+
6+
Overview
7+
********
8+
9+
The sample assumes that the LEDs are connected to GPIO lines.
10+
It is configured to work on boards that have defined the led0 and led1
11+
aliases and id properties for them in their board devicetree description file.
12+
The LED id properties are required for the LED GPIO driver.
13+
The LEDs are controlled, using the
14+
following pattern:
15+
16+
1. Turn on LEDs
17+
#. Turn off LEDs
18+
#. Blink the LEDs
19+
#. Turn off LEDs
20+
21+
Building and Running
22+
********************
23+
24+
This sample can be built and flashed to a board as follows:
25+
26+
.. zephyr-app-commands::
27+
:zephyr-app: samples/driver/led_gpio
28+
:board: nrf52840_pca10056
29+
:goals: build flash
30+
:compact:
31+
32+
After flashing the image to the board, the user LEDs on the board should act according to the given pattern.

samples/drivers/led_gpio/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_GPIO=y
2+
CONFIG_LED=y
3+
CONFIG_LED_GPIO=y
4+
CONFIG_LED_SOFTWARE_BLINK=y
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sample:
2+
name: LED GPIO driver sample
3+
description: Demonstration of the LED GPIO driver
4+
tests:
5+
sample.drivers.led.gpio:
6+
tags: LED gpio
7+
depends_on: gpio
8+
harness: led
9+
platform_whitelist: nrf52840_pca10056
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <device.h>
8+
#include <errno.h>
9+
#include <drivers/led.h>
10+
#include <sys/util.h>
11+
#include <sys/printk.h>
12+
#include <zephyr.h>
13+
14+
#define LED_DEV_NAME "LED_GPIO_0"
15+
#define NUM_LEDS 2
16+
#define BLINK_DELAY_ON 500
17+
#define BLINK_DELAY_OFF 500
18+
#define DELAY_TIME 1000
19+
20+
static uint8_t led_id[NUM_LEDS] = {DT_PROP(DT_NODELABEL(led0), id),
21+
DT_PROP(DT_NODELABEL(led1), id)};
22+
23+
void main(void)
24+
{
25+
struct device *led_dev;
26+
int i, ret;
27+
28+
led_dev = device_get_binding(LED_DEV_NAME);
29+
if (led_dev) {
30+
printk("Found LED device %s\n", LED_DEV_NAME);
31+
} else {
32+
printk("LED device %s not found\n", LED_DEV_NAME);
33+
return;
34+
}
35+
36+
printk("Testing leds");
37+
38+
while (1) {
39+
/* Turn on LEDs one by one */
40+
for (i = 0; i < NUM_LEDS; i++) {
41+
ret = led_on(led_dev, led_id[i]);
42+
if (ret < 0) {
43+
return;
44+
}
45+
46+
k_msleep(DELAY_TIME);
47+
}
48+
49+
/* Turn off LEDs one by one */
50+
for (i = 0; i < NUM_LEDS; i++) {
51+
ret = led_off(led_dev, led_id[i]);
52+
if (ret < 0) {
53+
return;
54+
}
55+
56+
k_msleep(DELAY_TIME);
57+
}
58+
59+
/* Test the blinking of LEDs one by one */
60+
for (i = 0; i < NUM_LEDS; i++) {
61+
ret = led_blink(led_dev, led_id[i], BLINK_DELAY_ON,
62+
BLINK_DELAY_OFF);
63+
if (ret < 0) {
64+
return;
65+
}
66+
67+
k_msleep(DELAY_TIME);
68+
}
69+
70+
/* Wait a few blinking before turning off the LEDs */
71+
k_msleep(DELAY_TIME * 10);
72+
73+
/* Turn off LEDs one by one */
74+
for (i = 0; i < NUM_LEDS; i++) {
75+
ret = led_off(led_dev, led_id[i]);
76+
if (ret < 0) {
77+
return;
78+
}
79+
80+
k_msleep(DELAY_TIME);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)