samples: boards: nrf91: add nrfx sample
Adds a sample that shows how to use nrfx drivers in Zephyr. Signed-off-by: Marcin Szymczyk <marcin.szymczyk@nordicsemi.no>
This commit is contained in:
parent
8024c7a2c4
commit
d9611be72d
5 changed files with 174 additions and 0 deletions
8
samples/boards/nrf91/nrfx/CMakeLists.txt
Normal file
8
samples/boards/nrf91/nrfx/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.13.1)
|
||||
|
||||
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
|
||||
project(nrfx_sample)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
48
samples/boards/nrf91/nrfx/README.rst
Normal file
48
samples/boards/nrf91/nrfx/README.rst
Normal file
|
@ -0,0 +1,48 @@
|
|||
.. _nrf91_nrfx:
|
||||
|
||||
nRF91 nrfx example
|
||||
##################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
This sample demonstrates the usage of nrfx library in Zephyr.
|
||||
GPIOTE and DPPI are used as examples of nrfx drivers.
|
||||
|
||||
The code shows how to initialize the GPIOTE interrupt in Zephyr
|
||||
so that the nrfx_gpiote driver can use it. Additionally, it shows
|
||||
how the DPPI subsystem can be used to connect tasks and events of
|
||||
nRF peripherals, enabling those peripherals to interact with each
|
||||
other without any intervention from the CPU.
|
||||
|
||||
Zephyr GPIO driver is disabled to prevent it from registering its own handler
|
||||
for the GPIOTE interrupt. Button 1 is configured to create an event when pushed.
|
||||
This calls the ``button_handler()`` callback and triggers the LED 1 pin OUT task.
|
||||
LED is then toggled.
|
||||
|
||||
Requirements
|
||||
************
|
||||
|
||||
This sample has been tested on the NordicSemiconductor nRF9160 DK (nrf9160_pca10090) board.
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
The code can be found in :zephyr_file:`samples/boards/nrf91/nrfx`.
|
||||
|
||||
To build and flash the application:
|
||||
|
||||
.. zephyr-app-commands::
|
||||
:zephyr-app: samples/boards/nrf91/nrfx
|
||||
:board: nrf9160_pca10090
|
||||
:goals: build flash
|
||||
:compact:
|
||||
|
||||
Push Button 1 to toggle the LED 1.
|
||||
|
||||
Connect to the serial port - notice that each time the button is pressed,
|
||||
the ``button_handler()`` function is called.
|
||||
|
||||
See nrfx_repository_ for more information about nrfx.
|
||||
|
||||
.. _nrfx_repository: https://github.com/NordicSemiconductor/nrfx
|
5
samples/boards/nrf91/nrfx/prj.conf
Normal file
5
samples/boards/nrf91/nrfx/prj.conf
Normal file
|
@ -0,0 +1,5 @@
|
|||
CONFIG_GPIO=n
|
||||
CONFIG_NRFX_GPIOTE=y
|
||||
CONFIG_NRFX_DPPI=y
|
||||
CONFIG_LOG=y
|
||||
CONFIG_LOG_PROCESS_THREAD_SLEEP_MS=100
|
6
samples/boards/nrf91/nrfx/sample.yaml
Normal file
6
samples/boards/nrf91/nrfx/sample.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
sample:
|
||||
name: nrfx usage sample
|
||||
tests:
|
||||
sample.board.nrf91.nrfx:
|
||||
platform_whitelist: nrf9160_pca10090
|
||||
tags: board
|
107
samples/boards/nrf91/nrfx/src/main.c
Normal file
107
samples/boards/nrf91/nrfx/src/main.c
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <nrfx_gpiote.h>
|
||||
#include <nrfx_dppi.h>
|
||||
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(nrfx_sample, LOG_LEVEL_INF);
|
||||
|
||||
#define INPUT_PIN DT_ALIAS_SW0_GPIOS_PIN
|
||||
#define OUTPUT_PIN DT_ALIAS_LED0_GPIOS_PIN
|
||||
|
||||
static void button_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
|
||||
{
|
||||
LOG_INF("GPIO input event callback");
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
LOG_INF("nrfx_gpiote sample on %s", CONFIG_BOARD);
|
||||
|
||||
nrfx_err_t err;
|
||||
|
||||
/* Connect GPIOTE_0 IRQ to nrfx_gpiote_irq_handler */
|
||||
IRQ_CONNECT(DT_NORDIC_NRF_GPIOTE_GPIOTE_0_IRQ_0,
|
||||
DT_NORDIC_NRF_GPIOTE_GPIOTE_0_IRQ_0_PRIORITY,
|
||||
nrfx_isr, nrfx_gpiote_irq_handler, 0);
|
||||
|
||||
/* Initialize GPIOTE */
|
||||
err = nrfx_gpiote_init();
|
||||
if (err != NRFX_SUCCESS) {
|
||||
LOG_ERR("nrfx_gpiote_init error: %08x", err);
|
||||
return;
|
||||
}
|
||||
|
||||
nrfx_gpiote_in_config_t const in_config = {
|
||||
.sense = NRF_GPIOTE_POLARITY_HITOLO,
|
||||
.pull = NRF_GPIO_PIN_PULLUP,
|
||||
.is_watcher = false,
|
||||
.hi_accuracy = true,
|
||||
.skip_gpio_setup = false,
|
||||
};
|
||||
|
||||
/* Initialize input pin to generate event on high to low transition
|
||||
* (falling edge) and call button_handler()
|
||||
*/
|
||||
err = nrfx_gpiote_in_init(INPUT_PIN, &in_config, button_handler);
|
||||
if (err != NRFX_SUCCESS) {
|
||||
LOG_ERR("nrfx_gpiote_in_init error: %08x", err);
|
||||
return;
|
||||
}
|
||||
|
||||
nrfx_gpiote_out_config_t const out_config = {
|
||||
.action = NRF_GPIOTE_POLARITY_TOGGLE,
|
||||
.init_state = 1,
|
||||
.task_pin = true,
|
||||
};
|
||||
|
||||
/* Initialize output pin. SET task will turn the LED on,
|
||||
* CLR will turn it off and OUT will toggle it.
|
||||
*/
|
||||
err = nrfx_gpiote_out_init(OUTPUT_PIN, &out_config);
|
||||
if (err != NRFX_SUCCESS) {
|
||||
LOG_ERR("nrfx_gpiote_out_init error: %08x", err);
|
||||
return;
|
||||
}
|
||||
|
||||
nrfx_gpiote_in_event_enable(INPUT_PIN, true);
|
||||
nrfx_gpiote_out_task_enable(OUTPUT_PIN);
|
||||
|
||||
LOG_INF("nrfx_gpiote initialized");
|
||||
|
||||
/* Initialize DPPI channel */
|
||||
u8_t channel;
|
||||
|
||||
err = nrfx_dppi_channel_alloc(&channel);
|
||||
if (err != NRFX_SUCCESS) {
|
||||
LOG_ERR("nrfx_dppi_channel_alloc error: %08x", err);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Configure input pin to publish to the DPPI channel and output pin to
|
||||
* receive events from the DPPI channel. Note that output pin is
|
||||
* subscribed using the OUT task. This means that each time the button
|
||||
* is pressed, the LED pin will be toggled.
|
||||
*/
|
||||
nrf_gpiote_publish_set(
|
||||
nrfx_gpiote_in_event_get(INPUT_PIN),
|
||||
channel);
|
||||
nrf_gpiote_subscribe_set(
|
||||
nrfx_gpiote_out_task_get(OUTPUT_PIN),
|
||||
channel);
|
||||
|
||||
/* Enable DPPI channel */
|
||||
err = nrfx_dppi_channel_enable(channel);
|
||||
if (err != NRFX_SUCCESS) {
|
||||
LOG_ERR("nrfx_dppi_channel_enable error: %08x", err);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INF("DPPI configured, leaving main()");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue