| 
    nrfcxx
    0.1.0
    
   C++-17 Framework for Nordic nRF5 Devices 
   | 
 
 
 
 
Go to the documentation of this file.   24 #ifndef NRFCXX_CORE_HPP    25 #define NRFCXX_CORE_HPP    32 #include <pabigot/container.hpp>    34 #include <nrfcxx/version.hpp>    38 extern char __HeapBase;       
    39 extern char __HeapLimit;      
    40 extern char __StackLimit;     
    41 extern char __StackTop;       
    56 #define NRFCXX_FAKED 0    59 #if (NRF51 - 0) || (NRFCXX_DOXYGEN - 0) || (NRFCXX_FAKED - 0)    66 #elif (NRF52832 - 0) || (NRF52840 - 0)    71 #error NRF52810 not currently supported    73 #error Unrecognized NRF5 device   137 #if (NRFCXX_DOXYGEN - 0)   144 #define ADCSeriesVariant_IRQHandler [SA]ADC_IRQHandler   164 template <
typename S>
   197     return reinterpret_cast<Type*>(
BASE);
   266 #ifndef NRFCXX_CROSS_COMPILING   271 #define NRFCXX_CROSS_COMPILING 1   274 #if (NRFCXX_CROSS_COMPILING - 0)   275 #if (51 == NRF_SERIES)   277 #elif (52 == NRF_SERIES)   280 #elif (NRFCXX_FAKED - 0)   283 using IRQn_Type = int;
   291 inline void __attribute__((__gnu_inline__,__always_inline__))
   294 #if (NRFCXX_CROSS_COMPILING - 0)   312 void sleep_ms (
unsigned int dur_ms);
   346 #if (NRFCXX_CROSS_COMPILING - 0)   347     __ASM 
volatile (
"mrs\t%0, primask\n\t"   357 #if (NRFCXX_CROSS_COMPILING - 0)   358     __ASM 
volatile (
"msr\tprimask, %0"   378 #if (NRFCXX_CROSS_COMPILING - 0)   379   NVIC_EnableIRQ(static_cast<IRQn_Type>(irqn));
   387 #if (NRFCXX_CROSS_COMPILING - 0)   388   NVIC_DisableIRQ(static_cast<IRQn_Type>(irqn));
   396 #if (NRFCXX_CROSS_COMPILING - 0)   397   NVIC_GetPendingIRQ(static_cast<IRQn_Type>(irqn));
   405 #if (NRFCXX_CROSS_COMPILING - 0)   406   NVIC_SetPendingIRQ(static_cast<IRQn_Type>(irqn));
   414 #if (NRFCXX_CROSS_COMPILING - 0)   415   NVIC_ClearPendingIRQ(static_cast<IRQn_Type>(irqn));
   423 #if (NRFCXX_CROSS_COMPILING - 0)   424   NVIC_SetPriority(static_cast<IRQn_Type>(irqn), priority);
   432 #if (NRFCXX_CROSS_COMPILING - 0)   433   NVIC_GetPriority(static_cast<IRQn_Type>(irqn));
   458 #if (NRFCXX_CROSS_COMPILING - 0)   459     uint32_t mask = 1U << (0x1F & (
unsigned int)irqn);
   460     in_mask_ = mask & NVIC->ISER[0];
   461     NVIC->ICER[0] = mask;
   465 #if (NRFCXX_CROSS_COMPILING - 0)   475 #if (NRFCXX_CROSS_COMPILING - 0)   476     NVIC->ISER[0] = in_mask_;
   480   nvic_BlockIRQ (
const nvic_BlockIRQ&) = 
delete;
   481   nvic_BlockIRQ& operator= (
const nvic_BlockIRQ&) = 
delete;
   482   nvic_BlockIRQ (nvic_BlockIRQ&& ) = 
delete;
   483   nvic_BlockIRQ& operator= (nvic_BlockIRQ&) = 
delete;
   496 template <IRQn_Type IRQn>
   555   operator bool()
 const   576 #if (NRFCXX_CROSS_COMPILING - 0)   607 #if (NRFCXX_CROSS_COMPILING - 0)   664     events_{
events.fetch_and_clear()}
   671   operator bool()
 const   699     bool rv = (events_ & evt);
   794   return static_cast<FailSafeCode>(static_cast<unsigned int>(lhs) + incr);
   819 inline __attribute__((__always_inline__))
   823 inline __attribute__((__always_inline__))
   856   using om_type = uint8_t;
   918                                              unsigned int om_set = 0);
   942       ++systemState::wfe_count_;
  1230     uint16_t reset_reas_;
  1385                     unsigned int channel_mask = 1,
  1386                     bool run_in_sleep = 
true,
  1387                     bool run_in_debug = 
false) 
const;
  1451   __attribute__((__always_inline__))
  1453               bool bypass_watchdog = false)
  1489                                      uint64_t &radio_utt) 
const;
  1560   static void wdt_irqhandler (
void* sp);
  1576   static void systemOff (
unsigned int preserve,
  1577                          int button_psel = -1);
  1584   static void reset_ (uint32_t pc,
  1586                       bool bypass_watchdog = 
false);
  1588   friend void failsafe (
unsigned int code);
  1595   static void controlledResetPrep_ (
unsigned int preserve);
  1599   static void failsafe_ (uint32_t pc,
  1604   static void controlledReset_ (
unsigned int preserve,
  1605                                 bool bypass_watchdog);
  1620   const uint32_t magic_;
  1621   static unsigned int wfe_count_;
  1654   using chain_type = pabigot::container::forward_chain<watchdog_extended_channel, ref_next>;
  1679   bool check (
unsigned int now)
 const  1681     return interval_utt > (now - last_fed_);
  1684   static chain_type chain_;
  1685   static bool failed_;
  1687   chain_type::pointer_type next_ = chain_type::unlinked_ptr();
  1688   unsigned int last_fed_;
  1689   unsigned int const interval_utt;
  1719 inline int __attribute__((__gnu_inline__,__always_inline__))
  1722   return (int32_t)(__get_MSP() - static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&__StackLimit)));
  1730 inline void __attribute__((__gnu_inline__,__always_inline__))
  
uint64_t om_total[NUM_OPERATIONAL_MODES]
Cumulative time spent in each operational mode since restart.
Definition: core.hpp:1022
 
event_type fetch_and_clear()
Atomically return the current event set and clear the event record.
Definition: core.hpp:584
 
void sleep_ms(unsigned int dur_ms)
Sleep for the specified duration.
 
static constexpr int8_t NO_IRQ
Flag value for IRQn indicating that the peripheral does not have an assign interrupt vector entry.
Definition: core.hpp:172
 
Application attempted to allocate more resources than available.
 
void set(event_type events)
Add a new event to the set.
Definition: core.hpp:689
 
const uintptr_t BASE
The address of the instance.
Definition: core.hpp:208
 
reset_reas_type reset_reas
Captured data from NRF_POWER->RESETREAS.
Definition: core.hpp:1221
 
unsigned int heap_used() const
Return the current total heap allocation.
 
void failsafe(FailSafeCode code)
Record a critical system failure and reset the system.
Definition: core.hpp:1692
 
bool watchdogCheckExtended()
Check that the extended watchdog channels are current.
 
static const char *const operationalModeText[NUM_OPERATIONAL_MODES]
Text representations of each operational mode.
Definition: core.hpp:921
 
watchdog_extended_channel(unsigned int interval_utt)
Construct and register an extended watchdog with the specified interval.
 
Board setup failed in some critical way.
 
static void WFE()
Issue a __WFE instruction that records time spent sleeping.
 
reset_reas_enum
Defined bits in reset_reas.
Definition: core.hpp:1117
 
NRF_PPI_Type Type
The structure describing the instance content, such as NRF_GPIO_Type or NRF_UART_Type.
Definition: core.hpp:168
 
notifier_type make_setter(event_type events)
Return an invokable object that records events in this set.
Definition: core.hpp:637
 
Operational mode flag indicating that high-frequency clock is running.
Definition: core.hpp:881
 
void(*)(const state_type &ss, bool is_reset, bool was_retained) app_handler_type
Type of a function allowing application state maintenance.
Definition: core.hpp:1302
 
API specific to the nRF51 series supporting <nrfcxx/core.hpp>.
 
static unsigned int updateOperationalMode(unsigned int om_clear=0, unsigned int om_set=0)
Update the operational mode.
 
event_set_copy(event_set &events)
Capture events and reset the original set.
Definition: core.hpp:663
 
static void nvic_SetPendingIRQ(int irqn)
Wrapper around NVIC_SetPending to work around issues with peripheral::IRQn.
Definition: core.hpp:403
 
Application attempted to retrieve a non-existent peripheral instance.
 
unsigned int stack_fill_unused(unsigned int marker)
Fill all space between the top of the heap and the current stack pointer with the provided value.
 
void reset(unsigned int code, bool bypass_watchdog=false)
Record some information and reset the system.
Definition: core.hpp:1452
 
Bit mask for reset_reas to indicate a captured watchdog reset.
Definition: core.hpp:1199
 
RAII class to block a peripheral interrupt.
Definition: core.hpp:452
 
static constexpr uint8_t WATCHDOG_CHANNEL_COMMON
The underlying WDT channel used to support extended watchdog channels.
Definition: core.hpp:1659
 
const state_type & state() const
Access a read-only instance of the state referenced by this object.
Definition: core.hpp:1330
 
void set(event_type events)
Atomically record one or more events.
Definition: core.hpp:603
 
nvic_BlockIRQ as a template type.
Definition: core.hpp:497
 
Bit mask for reset_reas to indicate wakeup from system OFF mode due to ANADETECT from the LPCOMP peri...
Definition: core.hpp:1144
 
Application tried something that isn't allowed.
 
unsigned int sdfault_id
Data recording the id of a soft-device fault.
Definition: core.hpp:1073
 
static void systemOff(unsigned int preserve, int button_psel=-1)
Enter system off mode.
 
Bit mask for reset_reas to indicate wakeup from system OFF mode due to NFC field detection.
Definition: core.hpp:1155
 
Unspecified internal error.
 
Bit mask for reset_reas to indicate wakeup from system OFF mode due to VBUS becoming valid.
Definition: core.hpp:1162
 
uint32_t application_crc32(bool exclude_data=false)
Calculate a checksum of the application content.
 
static void nvic_GetPendingIRQ(int irqn)
Wrapper around NVIC_GetPending to work around issues with peripheral::IRQn.
Definition: core.hpp:394
 
bool watchdogActive() const
Check whether the watchdog infrastructure is active.
 
Capture information about an nRF5 peripheral instance.
Definition: core.hpp:165
 
static uint32_t current_pc()
Return the address of the instruction after the call.
 
Type * instance() const
Get a type-correct pointer to the peripheral structure.
Definition: core.hpp:195
 
bool test_and_clear(event_type evt)
Return true iff evt was set.
Definition: core.hpp:697
 
unsigned int code
Data recording the code passed to reset() in the previous session.
Definition: core.hpp:1064
 
uint32_t last_pc
The program counter at which the reset was initiated.
Definition: core.hpp:1048
 
Bit mask for reset_reas to indicate a soft-device fault.
Definition: core.hpp:1206
 
unsigned int heap_reserved() const
Return the size of the reserved heap region.
 
const uint8_t AUX
Auxiliary information relevant to the specific peripheral and type.
Definition: core.hpp:245
 
static void nvic_ClearPendingIRQ(int irqn)
Wrapper around NVIC_ClearPending to work around issues with peripheral::IRQn.
Definition: core.hpp:412
 
Base for system-assigned fail-safe codes.
 
Bit mask for reset_reas to indicate wakeup from system OFF mode due to debug interface mode.
Definition: core.hpp:1148
 
int initialize(bool enable_hfxt=false)
Perform board-specific initialization.
 
Default code for violation of a persisted memory region.
 
A helper class for processing snapshot nrfcxx::event_set values.
Definition: core.hpp:654
 
static void nvic_EnableIRQ(int irqn)
Wrapper around NVIC_EnableIRQ to work around issues with peripheral::IRQn.
Definition: core.hpp:376
 
static void nvic_GetPriority(int irqn)
Wrapper around NVIC_GetPriority to work around issues with peripheral::IRQn.
Definition: core.hpp:430
 
unsigned int om_updated
Time of last change to operational mode.
Definition: core.hpp:1018
 
const bool track_om
true iff time-in-mode is being tracked by this instance.
Definition: core.hpp:949
 
static constexpr unsigned int DELAY_US_OVERHEAD_cyc
Overhead setting up the call to delay_cycles() in the standard delay_us() implementation.
Definition: core.hpp:65
 
static unsigned int wfe_count()
Return the number of times WFE() has returned.
Definition: core.hpp:1541
 
static constexpr unsigned int WATCHDOG_Hz
Frequency at which the watchdog clock runs.
Definition: core.hpp:1336
 
Bit mask for reset_reas to indicate wakeup from system OFF mode due to DETECT from the GPIO periphera...
Definition: core.hpp:1140
 
A traits type identifying GPIO peripheral instances.
Definition: core.hpp:260
 
RAII class that performs no mutex operations.
Definition: core.hpp:319
 
Bit mask for reset_reas to indicate reset due to AIRCR.SYSRESETRQ.
Definition: core.hpp:1132
 
void delay_cycles(unsigned int cycles)
Loop to delay for a requested number of cycles.
 
Operational mode flag indicating that CPU is turned off.
Definition: core.hpp:867
 
Bit mask for reset_reas to indicate reset due to watchdog.
Definition: core.hpp:1128
 
The raw data supporting cross-reset state transfer.
Definition: core.hpp:993
 
Bit mask for reset_reas to indicate that the reset was controlled.
Definition: core.hpp:1215
 
API specific to the faked host-based unit testing supporting <nrfcxx/core.hpp>.
 
static void cev()
Safely clear the MCU event flag.
Definition: core.hpp:574
 
Exceeded allocated space of a memory pool.
 
Bit mask for reset_reas to indicate reset due to the reset pin.
Definition: core.hpp:1124
 
constexpr event_set(event_type events=0U)
Construct with an optional initial set of events.
Definition: core.hpp:550
 
static constexpr auto WATCHDOG_MASK_EXTENDED
Pseudo-channel used to support extended watchdog channels.
Definition: core.hpp:1342
 
void watchdogFeedMulti(unsigned int channel_mask) const
As with watchdogFeed() but feeds multiple channels.
 
static state_type * statep_
Pointer to the #state_ field of the first systemState instance to be constructed.
Definition: core.hpp:1612
 
Operational mode flag indicating that 2.4 GHz radio is running.
Definition: core.hpp:890
 
Bit mask for reset_reas to indicate a fatal system error from which a reset will not recover.
Definition: core.hpp:1192
 
event_set_copy copy_and_clear()
Create a non-mutex copy then clear the current event set.
Definition: core.hpp:710
 
unsigned int stack_infer_highwater(unsigned int marker)
Check the space between the top of the heap and the current stack pointer for matches to the provided...
 
RAII instance that configures the operational mode to enter OM_SLEEP when constructed,...
Definition: core.hpp:932
 
Support for extended watchdog channels.
Definition: core.hpp:1644
 
void reset(event_type events=0U)
Reset the event set to a specific state.
Definition: core.hpp:613
 
event_type fetch() const
Return the current event set.
Definition: core.hpp:561
 
Allocated too much from system heap.
 
void validate_stack_pointer()
Force a system failure if the stack is beyond its configured maximum depth.
Definition: core.hpp:1731
 
event_set::event_type event_type
The type used to represent a (set of) event(s).
Definition: core.hpp:660
 
uint64_t total_uptime
Total of last_uptime values since the state was reset.
Definition: core.hpp:1040
 
void watchdogFeed(unsigned int channel) const
Reload a specific channel in the watchdog.
 
Application failed to perform all steps required to run.
 
Bit mask for reset_reas to indicate framework-requested reset.
Definition: core.hpp:1176
 
systemState(state_type &state, uint32_t magic, app_handler_type app_handler=nullptr)
Set up a wrapper around a cross-reset state object.
 
uint8_t om_value
Bits identifying the current operational mode.
Definition: core.hpp:1263
 
uint64_t last_uptime
Duration of the last system session, in uptime clock ticks.
Definition: core.hpp:1033
 
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
 
uint8_t wdt_status
Bits identifying watchdog channels that caused a system reset.
Definition: core.hpp:1252
 
int stack_space_remaining()
Determine how much of the reserved stack space remains.
Definition: core.hpp:1720
 
A class supporting watchdog configuration and cross-reset retention of state.
Definition: core.hpp:853
 
void feed()
Feed the extended watchdog.
 
uint64_t operationalModeBreakdown(uint64_t &sleep_utt, uint64_t &radio_utt) const
Provide information on the total time spent in various operations states since reset.
 
om_enum
Bits used to build up an operational mode.
Definition: core.hpp:860
 
int watchdogInit(unsigned int delay_32KiHz, unsigned int channel_mask=1, bool run_in_sleep=true, bool run_in_debug=false) const
Configure the watchdog infrastructure.
 
uint32_t magic
A magic number that is used to determine the state is valid.
Definition: core.hpp:1012
 
Base for application-assigned fail-safe codes.
 
static scoped_sleeper make_scoped_sleeper() noexcept
Construct an RAII object to track sleep periods.
Definition: core.hpp:972
 
Bit mask isolating the bits of reset_reas that are set from hardware.
Definition: core.hpp:1169
 
unsigned int stack_used() const
Return the size of stack region that has been used since startup.
 
static constexpr uint8_t NO_INSTANCE
Flag value for INSTANCE indicating that the peripheral does not have enumerated instances (e....
Definition: core.hpp:177
 
Detected stack pointer into heap memory.
 
static app_handler_type app_handler_
Pointer to the application state handler for the first systemState instance to be constructed.
Definition: core.hpp:1616
 
static void sd_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
A function suitable for use as the fault_handler argument to sd_softdevice_enable().
 
Bit mask for reset_reas to indicate reset due to CPU lock-up.
Definition: core.hpp:1136
 
API specific to the nRF52 series supporting <nrfcxx/core.hpp>.
 
unsigned int event_type
The type used to represent a (set of) event(s).
Definition: core.hpp:547
 
uint16_t reset_reas_type
Base type underlying bits stored in reset_reas.
Definition: core.hpp:1091
 
unsigned int stack_reserved() const
Return the size of the reserved stack region.
 
The number of distinct operational modes supported.
Definition: core.hpp:895
 
static uint64_t total_now()
Return the aggregate time since the retained state was reset.
 
static constexpr unsigned int CLOCK_MHz
CPU clock speed in MHz.
Definition: core.hpp:50
 
event_set_copy()=default
Create an empty event set.
 
FailSafeCode
Enumerated constants used in failsafe() calls.
Definition: core.hpp:719
 
uint16_t reset_count
The number of times the system has been restarted with cross-reset state successfully transferred.
Definition: core.hpp:1088
 
const int8_t IRQn
The interrupt number associated with the instance, or negative if the instance has no dedicated inter...
Definition: core.hpp:221
 
Type * operator->() const
Allow dereferencing the instance as though it were a pointer to the peripheral structure.
Definition: core.hpp:202
 
constexpr peripheral(uintptr_t base, int8_t irqn=NO_IRQ, uint8_t instance=NO_INSTANCE, uint8_t aux=0)
Create an object referencing a peripheral instance.
Definition: core.hpp:183
 
static void nvic_DisableIRQ(int irqn)
Wrapper around NVIC_DisableIRQ to work around issues with peripheral::IRQn.
Definition: core.hpp:385
 
RAII class to block exceptions.
Definition: core.hpp:340
 
static void nvic_SetPriority(int irqn, uint32_t priority)
Wrapper around NVIC_SetPriority to work around issues with peripheral::IRQn.
Definition: core.hpp:421
 
void delay_us(unsigned int number_of_us)
Delay for exactly the specified duration.
Definition: core.hpp:292
 
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
 
static constexpr uint32_t DECL_MAGIC
Constant encoding a revision number marking the last change to the layout or interpretation of this s...
Definition: core.hpp:1000
 
const uint8_t INSTANCE
The peripheral instance, for peripherals like TIMER that have multiple instances.
Definition: core.hpp:228
 
nvic_BlockIRQ(int irqn)
Variant required to work around lack of IRQn_Type when declaring nrfcxx::peripheral.
Definition: core.hpp:468
 
bool empty() const
Return true iff no events remain.
Definition: core.hpp:677
 
A record of events that occur asynchonously.
Definition: core.hpp:541
 
event_type events() const
Return the bitmask of remaining events.
Definition: core.hpp:683
 
Bit mask for reset_reas to indicate an application-requested reset.
Definition: core.hpp:1183
 
Application left the main loop.