nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Data Fields | Static Public Attributes
nrfcxx::periph::GPIOTE Class Reference

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
 
GPIOTEoperator= (const GPIOTE &)=delete
 
 GPIOTE (const GPIOTE &&)=delete
 
GPIOTEoperator= (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 GPIOTEallocate ()
 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...
 

Detailed Description

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.

Member Typedef Documentation

◆ event_callback_bi

using nrfcxx::periph::GPIOTE::event_callback_bi = std::function<void(GPIOTE& instance)>

Signature for a event callback.

Note
Functions with this signature are expected to be invoked when the GPIOTE IRQ is blocked.
Parameters
instancethe GPIOTE instance on which the event was triggered.

◆ sense_callback_bi

using nrfcxx::periph::GPIOTE::sense_callback_bi = std::function<void(const sense_status_type* sp)>

Signature for a sense callback.

Note
Functions with this signature are expected to be invoked when the GPIOTE IRQ is blocked.
All sense callbacks are invoked any time a level change is detected on any GPIO that has the SENSE feature enabled. The callbacks must determine whether pins they're interested in have changed.

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
}
Parameters
ssppointer 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.

Member Function Documentation

◆ allocate()

static GPIOTE* nrfcxx::periph::GPIOTE::allocate ( )
static

Allocate a GPIOTE instance.

Returns
a pointer to an available instance, or a null pointer if all instances are currently in use within the application.

◆ config_disabled()

void nrfcxx::periph::GPIOTE::config_disabled ( )
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.

◆ config_event()

void nrfcxx::periph::GPIOTE::config_event ( unsigned int  psel,
unsigned int  polarity = GPIOTE_CONFIG_POLARITY_Toggle 
)
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.

Note
It is safe to re-invoke this operation with different arguments while events are enabled, if there is a need to change the source of the event.
Parameters
pselthe GPIO pin on which edge events will be detected.
polarityoptional configuration for the type of edge event. By default both rising and falling edges are detected.

◆ config_task()

void nrfcxx::periph::GPIOTE::config_task ( unsigned int  psel,
unsigned int  polarity = GPIOTE_CONFIG_POLARITY_Toggle 
)
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.

Parameters
pselthe GPIO pin that will change state.
polaritythe state change to be executed when an event is detected.

◆ disable_event()

void nrfcxx::periph::GPIOTE::disable_event ( )
inline

Disable event callbacks for this instance.

Returns
0 on success, or a negative error code (e.g. when the instance is not configured for events).
See also
enable_event()

◆ enable_event()

void nrfcxx::periph::GPIOTE::enable_event ( )
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();
Note
The requirements of irq_handler must also be satisfied.
Returns
0 on success, or a negative error code (e.g. when the instance is already enabled or ).
See also
disable_event()

◆ enable_sense()

static void nrfcxx::periph::GPIOTE::enable_sense ( )
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();
Note
The requirements of irq_handler must also be satisfied.
See also
sense_listener
gpio::PIN_CNF_ACTIVE_LOW
gpio::PIN_CNF_ACTIVE_HIGH

◆ irq_handler()

static void nrfcxx::periph::GPIOTE::irq_handler ( void  )
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);

◆ release()

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().

◆ synchronize_sense()

static void nrfcxx::periph::GPIOTE::synchronize_sense ( )
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.

◆ TASKS_CLR()

task_reference_type nrfcxx::periph::GPIOTE::TASKS_CLR ( )

Reference the channel-specific TASKS_CLR register.

Note
Only available on nRF52 MCUs.

◆ TASKS_SET()

task_reference_type nrfcxx::periph::GPIOTE::TASKS_SET ( )

Reference the channel-specific TASKS_SET register.

Note
Only available on nRF52 MCUs.

Field Documentation

◆ CHANNEL_COUNT

constexpr static size_t nrfcxx::periph::GPIOTE::CHANNEL_COUNT = nrf5::GPIOTE.AUX
staticconstexpr

The number of GPIOTE channels available.

The nRF51 has four; nrf52 has eight.


The documentation for this class was generated from the following file: