nrfcxx
0.1.0
C++-17 Framework for Nordic nRF5 Devices
|
Class supporting GPIO task and event operations. More...
#include <nrfcxx/periph.hpp>
Data Structures | |
class | event_listener |
Object used to manage event callbacks. More... | |
class | sense_listener |
Object used to manage sense callbacks. More... | |
struct | sense_status_type |
Structure used to convey information about pin levels to sense_listener callbacks. More... | |
Public Types | |
using | event_reference_type = __O uint32_t & |
Type for a reference to a peripheral event. | |
using | task_reference_type = __IO uint32_t & |
Type for a reference to a peripheral task. | |
using | event_callback_bi = std::function< void(GPIOTE &instance)> |
Signature for a event callback. More... | |
using | sense_callback_bi = std::function< void(const sense_status_type *sp)> |
Signature for a sense callback. More... | |
using | mutex_type = mutex_irq< GPIOTE_IRQn > |
An RAII type for mutex access to state that must be protected from GPIOTE interrupts. | |
Public Member Functions | |
GPIOTE (const GPIOTE &)=delete | |
GPIOTE & | operator= (const GPIOTE &)=delete |
GPIOTE (const GPIOTE &&)=delete | |
GPIOTE & | operator= (GPIOTE &&)=delete |
void | release () |
Release the instance. More... | |
void | config_event (unsigned int psel, unsigned int polarity=GPIOTE_CONFIG_POLARITY_Toggle) |
Configure the instance to monitor edge events. More... | |
void | config_task (unsigned int psel, unsigned int polarity=GPIOTE_CONFIG_POLARITY_Toggle) |
Configure the instance to change pin level. More... | |
void | config_disabled () |
Deconfigure the instance. More... | |
void | enable_event () |
Enable event callbacks for this instance. More... | |
void | disable_event () |
Disable event callbacks for this instance. More... | |
event_reference_type | EVENTS_IN () |
Reference the channel-specific EVENTS_IN register. | |
task_reference_type | TASKS_OUT () |
Reference the channel-specific TASKS_OUT register. | |
task_reference_type | TASKS_SET () |
Reference the channel-specific TASKS_SET register. More... | |
task_reference_type | TASKS_CLR () |
Reference the channel-specific TASKS_CLR register. More... | |
Static Public Member Functions | |
static void | synchronize_sense () |
Process as though a PORT event had been received. More... | |
static GPIOTE * | allocate () |
Allocate a GPIOTE instance. More... | |
static void | enable_sense () |
Enable sense callbacks. More... | |
static void | disable_sense () |
Stop monitoring sense events. | |
static void | irq_handler (void) |
Implementation for GPIOTE_IRQHandler required by this module. More... | |
Data Fields | |
const uint8_t | channel |
The channel index for this GPIOTE instance. | |
Static Public Attributes | |
constexpr static size_t | CHANNEL_COUNT = nrf5::GPIOTE.AUX |
The number of GPIOTE channels available. More... | |
Class supporting GPIO task and event operations.
The nRF51 provides a set of four channels that can detect or emit GPIO pin state changes. It also provides a general facility to detect pin level.
References to GPIOTE instances are obtained through allocate(), and released through release(). Each instance can be in one of the following modes:
Each instance starts disabled when allocated and is automatically disabled when released.
In addition to these instances the module provides an ability to invoke callbacks when a GPIO pin transitions to a specific state.
using nrfcxx::periph::GPIOTE::event_callback_bi = std::function<void(GPIOTE& instance)> |
Signature for a event callback.
instance | the GPIOTE instance on which the event was triggered. |
using nrfcxx::periph::GPIOTE::sense_callback_bi = std::function<void(const sense_status_type* sp)> |
Signature for a sense callback.
The callback should do something like this:
while ((0 <= sp->psel) && (psel < sp->psel)) { ++sp; } if (psel == sp->psel) { // process sp->counter_state } else { // apparently psel isn't configured for sense detection }
ssp | pointer to a sequence of records indicating the status of all GPIO pins for which sense detection is enabled. The pins will be in increasing order of psel; the sequence terminates with a negative psel. See sense_status_type::counter_state for the information returned. |
|
static |
Allocate a GPIOTE instance.
|
inline |
Deconfigure the instance.
This function atomically configures the GPIOTE instance to disable both event monitoring and automatic state changes. It has no effect on event handling.
|
inline |
Configure the instance to monitor edge events.
This function only configures the GPIOTE instance to detect edge events. The event must also be enabled through enable_event() and a event listener must be provided.
See enable_event() for an example of using this method in context.
psel | the GPIO pin on which edge events will be detected. |
polarity | optional configuration for the type of edge event. By default both rising and falling edges are detected. |
|
inline |
Configure the instance to change pin level.
This function only configures the GPIOTE instance to trigger state changes when events are detected. It has no effect on event handling nor does it provide a mechanism to trigger events.
psel | the GPIO pin that will change state. |
polarity | the state change to be executed when an event is detected. |
|
inline |
Disable event callbacks for this instance.
|
inline |
Enable event callbacks for this instance.
This only enables the instance within the peripheral module instance. The IRQ must separately be configured and enabled, and an event listener must be enabled.
Example:
using namespace nrfcxx::gpio; // Configure the GPIO as an input with pull. SENSE is not needed. GPIO->PIN_CNF[NRFCXX_BOARD_PSEL_BUTTON0] = PIN_CNF_ACTIVE_LOW & ~GPIO_PIN_CNF_SENSE_Msk; // Allocate and configure a channel to generate events from the configured GPIO auto b0ch = GPIOTE::allocate(); b0ch->config_event(NRFCXX_BOARD_PSEL_BUTTON0); // Create and enable an event listener tied to the GPIOTE channel GPIOTE::event_listener button0{[](auto& r, void*) { events.set(EVT_EDGE_BUTTON0); }}; button0.enable(b0ch); // Start listening to events on the GPIOTE channel b0ch->enable_event();
|
static |
Enable sense callbacks.
This facility allows detection of a generic event that is triggered whenever a GPIO pin configured for input changes state to a level specified in its SENSE field. When detected any registered sense listeners will be invoked.
Example:
using namespace nrfcxx::gpio; // Configure the GPIO as an input with pull and sense. GPIO->PIN_CNF[NRFCXX_BOARD_PSEL_BUTTON1] = PIN_CNF_ACTIVE_LOW; // Create and enable a sense listener volatile unsigned int b1_state; GPIOTE::sense_listener button1{[&](auto sp) { while ((0 <= sp->psel) && (sp->psel < NRFCXX_BOARD_PSEL_BUTTON1)) { ++sp; } if (NRFCXX_BOARD_PSEL_BUTTON1 == sp->psel) { b1_state = sp->aggregate_state(b1_state); if (1 < b1_state->counter_state) { events.set(EVT_LEVEL_BUTTON1); } } }}; button1.enable(); // Start listening to sense events (only need this once) GPIOTE::enable_sense(); GPIOTE::synchronize_sense();
|
static |
Implementation for GPIOTE_IRQHandler
required by this module.
This module requires that the GPIOTE_IRQHandler
process events to fulfill its obligations. To reduce code space this handler is not normally installed. Applications that require must install it: extern "C" { void GPIOTE_IRQHandler (void) { nrfcxx::periph::GPIOTE::irq_handler(); } } When invoked this function will process and clear all GPIOTE events, regardless of whether instances in this module are configured for those events. You will also need the following in your application main function: NVIC_EnableIRQ(GPIOTE_IRQn);
void nrfcxx::periph::GPIOTE::release | ( | ) |
Release the instance.
This should be invoked at the point where the instance is no longer needed. It provides an equivalent to this sequence:
gpiote.config_disabled() gpiote.disable_event() gpiote_listener.disable()
as well as making the resource available for subsequent invocations of allocate().
|
static |
Process as though a PORT event had been received.
This should be invoked after configuring a GPIO for sense detection, to ensure that the configured sense detects a change from the current level. All enabled sense listeners will invoked during the call.
task_reference_type nrfcxx::periph::GPIOTE::TASKS_CLR | ( | ) |
Reference the channel-specific TASKS_CLR register.
task_reference_type nrfcxx::periph::GPIOTE::TASKS_SET | ( | ) |
Reference the channel-specific TASKS_SET register.
|
staticconstexpr |
The number of GPIOTE channels available.
The nRF51 has four; nrf52 has eight.