nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
core.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2015-2019 Peter A. Bigot */
3 
24 #ifndef NRFCXX_CORE_HPP
25 #define NRFCXX_CORE_HPP
26 #pragma once
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 
32 #include <pabigot/container.hpp>
33 
34 #include <nrfcxx/version.hpp>
35 
37 extern "C" {
38 extern char __HeapBase;
39 extern char __HeapLimit;
40 extern char __StackLimit;
41 extern char __StackTop;
42 }
45 #ifndef NRFCXX_FAKED
46 
56 #define NRFCXX_FAKED 0
57 #endif /* NRFCXX_FAKED */
58 
59 #if (NRF51 - 0) || (NRFCXX_DOXYGEN - 0) || (NRFCXX_FAKED - 0)
60 
65 #define NRF_SERIES 51
66 #elif (NRF52832 - 0) || (NRF52840 - 0)
67 #define NRF_SERIES 52
68 #elif (NRF52810 - 0)
69 /* NRF52810 has a different set of peripherals. Add that when
70  * we have hardware that needs it. */
71 #error NRF52810 not currently supported
72 #else /* NRF device */
73 #error Unrecognized NRF5 device
74 #endif /* NRF device */
75 
77 namespace nrfcxx {
78 
83 namespace board {
84 
97 int initialize (bool enable_hfxt = false); // weak implemented in src/core.cc
98 
99 } // ns board
100 
131 namespace nrf5 {
132 
135 namespace series {
136 
137 #if (NRFCXX_DOXYGEN - 0)
138 
144 #define ADCSeriesVariant_IRQHandler [SA]ADC_IRQHandler
145 #endif /* NRFCXX_DOXYGEN */
146 
147 } // ns series
148 
164 template <typename S>
165 struct peripheral {
168  using Type = S;
169 
172  static constexpr int8_t NO_IRQ = -128;
173 
177  static constexpr uint8_t NO_INSTANCE = 255;
178 
183  constexpr explicit peripheral (uintptr_t base,
184  int8_t irqn = NO_IRQ,
185  uint8_t instance = NO_INSTANCE,
186  uint8_t aux = 0) :
187  BASE{base},
188  IRQn{irqn},
190  AUX{aux}
191  { }
192 
195  Type* instance() const
196  {
197  return reinterpret_cast<Type*>(BASE);
198  }
199 
202  Type* operator->() const
203  {
204  return instance();
205  }
206 
208  const uintptr_t BASE;
209 
221  const int8_t IRQn;
222 
228  const uint8_t INSTANCE;
229 
245  const uint8_t AUX;
246 };
247 
259 template <int I>
261 { };
262 
263 } // nrf5
264 } // nrfcxx
265 
266 #ifndef NRFCXX_CROSS_COMPILING
267 
271 #define NRFCXX_CROSS_COMPILING 1
272 #endif /* NRFCXX_CROSS_COMPILING */
273 
274 #if (NRFCXX_CROSS_COMPILING - 0)
275 #if (51 == NRF_SERIES)
276 #include <nrfcxx/nrf51/core.hpp>
277 #elif (52 == NRF_SERIES)
278 #include <nrfcxx/nrf52/core.hpp>
279 #endif // NRF_SERIES
280 #elif (NRFCXX_FAKED - 0)
281 #include <nrfcxx/faked/core.hpp>
282 #else
283 using IRQn_Type = int;
284 #endif /* NRFCXX_CROSS_COMPILING */
285 
286 namespace nrfcxx {
287 
291 inline void __attribute__((__gnu_inline__,__always_inline__))
292 delay_us (unsigned int number_of_us)
293 {
294 #if (NRFCXX_CROSS_COMPILING - 0)
295  if (number_of_us) {
297  }
298 #endif /* NRFCXX_CROSS_COMPILING */
299 }
300 
312 void sleep_ms (unsigned int dur_ms);
313 
320 {
321 public:
322  null_mutex ()
323  { }
324 
325  null_mutex (const null_mutex&) = delete;
326  null_mutex& operator= (const null_mutex&) = delete;
327  null_mutex (null_mutex&& ) = delete;
328  null_mutex& operator= (null_mutex&) = delete;
329 };
330 
340 class primask
341 {
342 public:
343  primask () :
344  in_mask_{}
345  {
346 #if (NRFCXX_CROSS_COMPILING - 0)
347  __ASM volatile ("mrs\t%0, primask\n\t"
348  "cpsid\ti"
349  : "=r" (in_mask_)
350  :
351  : "memory");
352 #endif /* NRFCXX_CROSS_COMPILING */
353  }
354 
355  ~primask ()
356  {
357 #if (NRFCXX_CROSS_COMPILING - 0)
358  __ASM volatile ("msr\tprimask, %0"
359  :
360  : "r" (in_mask_)
361  : "memory");
362 #endif /* NRFCXX_CROSS_COMPILING */
363  }
364 
365  primask (const primask&) = delete;
366  primask& operator= (const primask&) = delete;
367  primask (primask&& ) = delete;
368  primask& operator= (primask&) = delete;
369 
370 private:
371  uint32_t in_mask_;
372 };
373 
376 static inline void nvic_EnableIRQ (int irqn)
377 {
378 #if (NRFCXX_CROSS_COMPILING - 0)
379  NVIC_EnableIRQ(static_cast<IRQn_Type>(irqn));
380 #endif /* NRFCXX_CROSS_COMPILING */
381 }
382 
385 static inline void nvic_DisableIRQ (int irqn)
386 {
387 #if (NRFCXX_CROSS_COMPILING - 0)
388  NVIC_DisableIRQ(static_cast<IRQn_Type>(irqn));
389 #endif /* NRFCXX_CROSS_COMPILING */
390 }
391 
394 static inline void nvic_GetPendingIRQ (int irqn)
395 {
396 #if (NRFCXX_CROSS_COMPILING - 0)
397  NVIC_GetPendingIRQ(static_cast<IRQn_Type>(irqn));
398 #endif /* NRFCXX_CROSS_COMPILING */
399 }
400 
403 static inline void nvic_SetPendingIRQ (int irqn)
404 {
405 #if (NRFCXX_CROSS_COMPILING - 0)
406  NVIC_SetPendingIRQ(static_cast<IRQn_Type>(irqn));
407 #endif /* NRFCXX_CROSS_COMPILING */
408 }
409 
412 static inline void nvic_ClearPendingIRQ (int irqn)
413 {
414 #if (NRFCXX_CROSS_COMPILING - 0)
415  NVIC_ClearPendingIRQ(static_cast<IRQn_Type>(irqn));
416 #endif /* NRFCXX_CROSS_COMPILING */
417 }
418 
421 static inline void nvic_SetPriority (int irqn, uint32_t priority)
422 {
423 #if (NRFCXX_CROSS_COMPILING - 0)
424  NVIC_SetPriority(static_cast<IRQn_Type>(irqn), priority);
425 #endif /* NRFCXX_CROSS_COMPILING */
426 }
427 
430 static inline void nvic_GetPriority (int irqn)
431 {
432 #if (NRFCXX_CROSS_COMPILING - 0)
433  NVIC_GetPriority(static_cast<IRQn_Type>(irqn));
434 #endif /* NRFCXX_CROSS_COMPILING */
435 }
436 
453 {
454 public:
455  explicit nvic_BlockIRQ (IRQn_Type irqn) :
456  in_mask_{}
457  {
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;
462 #endif /* NRFCXX_CROSS_COMPILING */
463  }
464 
465 #if (NRFCXX_CROSS_COMPILING - 0)
466 
468  explicit nvic_BlockIRQ (int irqn) :
469  nvic_BlockIRQ{static_cast<IRQn_Type>(irqn)}
470  { }
471 #endif /* NRFCXX_CROSS_COMPILING */
472 
473  ~nvic_BlockIRQ ()
474  {
475 #if (NRFCXX_CROSS_COMPILING - 0)
476  NVIC->ISER[0] = in_mask_;
477 #endif /* NRFCXX_CROSS_COMPILING */
478  }
479 
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;
484 
485 private:
486  uint32_t in_mask_;
487 };
488 
496 template <IRQn_Type IRQn>
497 class mutex_irq : public nvic_BlockIRQ
498 {
499 public:
500  mutex_irq () :
501  nvic_BlockIRQ(IRQn)
502  { }
503 };
504 
514 using notifier_type = std::function<void()>;
515 
516 /* Forward declaration */
517 class event_set_copy;
518 
542 {
543 public:
547  using event_type = unsigned int;
548 
550  constexpr explicit event_set (event_type events = 0U) :
551  events_{events}
552  { }
553 
555  operator bool() const
556  {
557  return events_;
558  }
559 
561  event_type fetch () const
562  {
563  return events_;
564  }
565 
574  static void cev ()
575  {
576 #if (NRFCXX_CROSS_COMPILING - 0)
577  __SEV();
578  __WFE();
579 #endif /* NRFCXX_CROSS_COMPILING */
580  }
581 
585  {
586  primask mutex;
587  event_type rv = events_;
588  events_ = {};
589  return rv;
590  }
591 
597 
603  void set (event_type events)
604  {
605  primask mutex;
606  events_ |= events;
607 #if (NRFCXX_CROSS_COMPILING - 0)
608  __SEV();
609 #endif /* NRFCXX_CROSS_COMPILING */
610  }
611 
613  void reset (event_type events = 0U)
614  {
615  primask mutex;
616  events_ = events;
617  }
618 
638  {
639  return [this,events]
640  {
641  this->set(events);
642  };
643  }
644 
645 private:
646  volatile event_type events_;
647 };
648 
655 {
656 public:
661 
664  events_{events.fetch_and_clear()}
665  { }
666 
668  event_set_copy () = default;
669 
671  operator bool() const
672  {
673  return events_;
674  }
675 
677  bool empty () const
678  {
679  return !events_;
680  }
681 
684  {
685  return events_;
686  }
687 
690  {
691  events_ |= events;
692  }
693 
698  {
699  bool rv = (events_ & evt);
700  events_ &= ~evt;
701  return rv;
702  }
703 
704 private:
705  event_type events_ = {};
706 };
707 
708 inline
709 event_set_copy
711 {
712  return event_set_copy(*this);
713 }
714 
719 enum class FailSafeCode : unsigned int
720 {
722  SYSTEM_BASE = 0xbad00000,
723 
727 
730  MEMORY_POOL = SYSTEM_BASE + 2,
731 
735 
739 
746 
749 
752 
759 
762 
773 
780 
788  APPLICATION_BASE = 0xbad10000,
789 };
790 static inline
791 FailSafeCode operator+ (const FailSafeCode& lhs,
792  unsigned int incr)
793 {
794  return static_cast<FailSafeCode>(static_cast<unsigned int>(lhs) + incr);
795 }
796 
818 [[noreturn]]
819 inline __attribute__((__always_inline__))
820 void failsafe (FailSafeCode code);
821 
822 [[noreturn]]
823 inline __attribute__((__always_inline__))
824 void failsafe (unsigned int code);
825 
854 {
856  using om_type = uint8_t;
857 
858 public:
860  enum om_enum : om_type {
867  OM_SLEEP = 0x01,
868 
881  OM_HFCLK = 0x02,
882 
890  OM_RADIO = 0x04,
891 
896  };
897 
917  static unsigned int updateOperationalMode (unsigned int om_clear = 0,
918  unsigned int om_set = 0);
919 
921  static const char* const operationalModeText[NUM_OPERATIONAL_MODES];
922 
933  {
934  scoped_sleeper (const scoped_sleeper&) = delete;
935  scoped_sleeper& operator= (const scoped_sleeper&) = delete;
936  scoped_sleeper (scoped_sleeper&&) = delete;
937  scoped_sleeper& operator= (scoped_sleeper&&) = delete;
938 
939  public:
940  ~scoped_sleeper () noexcept
941  {
942  ++systemState::wfe_count_;
943  if (track_om) {
945  }
946  }
947 
949  const bool track_om;
950 
951  private:
952  friend systemState;
953 
954  scoped_sleeper (bool track_om) :
956  {
957  if (track_om) {
959  }
960  }
961  };
962 
973  {
974  return {statep_};
975  }
976 
983  static void WFE ();
984 
993  struct state_type {
1000  static constexpr uint32_t DECL_MAGIC = 0x20181013U;
1001 
1012  uint32_t magic;
1013 
1018  unsigned int om_updated;
1019 
1023 
1033  uint64_t last_uptime;
1034 
1040  uint64_t total_uptime;
1041 
1048  uint32_t last_pc;
1049 
1064  unsigned int code;
1065 
1073  unsigned int sdfault_id;
1074 
1076  /* Cache for data to be copied to code during initialization when
1077  * #RESET_REAS_PROGRAMMATIC, #RESET_REAS_FAILSAFE, or
1078  * #RESET_REAS_SDFAULT is set in #reset_reas_.
1079  *
1080  * This is cleared after initialization completes. */
1081  unsigned int code_;
1088  uint16_t reset_count;
1089 
1091  using reset_reas_type = uint16_t;
1092 
1118  {
1125 
1128  RESET_REAS_DOG = 0x0002,
1129 
1133 
1137 
1140  RESET_REAS_OFF = 0x0010,
1141 
1145 
1148  RESET_REAS_DIF = 0x0040,
1149 
1155  RESET_REAS_NFC = 0x0080,
1156 
1163 
1170 
1171  /* Bits below are synthesized; bits above are from nRF5 MCUs.
1172  * The following bits are available: 9 @ 0x0200. */
1173 
1177 
1184 
1193 
1200 
1207 
1216  };
1217 
1222 
1230  uint16_t reset_reas_;
1252  uint8_t wdt_status;
1253 
1263  uint8_t om_value;
1264  };
1265 
1300  using app_handler_type = void (*) (const state_type& ss,
1301  bool is_reset,
1302  bool was_retained);
1303 
1325  uint32_t magic,
1326  app_handler_type app_handler = nullptr);
1327 
1330  const state_type& state () const
1331  {
1332  return state_;
1333  }
1334 
1336  static constexpr unsigned int WATCHDOG_Hz = 32768U;
1337 
1342  static constexpr auto WATCHDOG_MASK_EXTENDED = (1U << 8);
1343 
1384  int watchdogInit (unsigned int delay_32KiHz,
1385  unsigned int channel_mask = 1,
1386  bool run_in_sleep = true,
1387  bool run_in_debug = false) const;
1388 
1394  bool watchdogActive () const;
1395 
1409  void watchdogFeed (unsigned int channel) const;
1410 
1415  void watchdogFeedMulti (unsigned int channel_mask) const;
1416 
1426  bool watchdogCheckExtended ();
1427 
1450  [[noreturn]]
1451  __attribute__((__always_inline__))
1452  void reset (unsigned int code,
1453  bool bypass_watchdog = false)
1454  {
1455  reset_(current_pc(), code, bypass_watchdog);
1456  }
1457 
1459  unsigned int stack_reserved () const;
1460 
1466  unsigned int stack_used () const;
1467 
1469  unsigned int heap_reserved() const;
1470 
1476  unsigned int heap_used () const;
1477 
1488  uint64_t operationalModeBreakdown (uint64_t &sleep_utt,
1489  uint64_t &radio_utt) const;
1490 
1510  static void sd_fault_handler (uint32_t id,
1511  uint32_t pc,
1512  uint32_t info);
1513 
1518  static uint32_t current_pc ();
1519 
1525  static uint64_t total_now ();
1526 
1541  static unsigned int wfe_count ()
1542  {
1543  return wfe_count_;
1544  }
1545 
1560  static void wdt_irqhandler (void* sp);
1575  [[noreturn]]
1576  static void systemOff (unsigned int preserve,
1577  int button_psel = -1);
1578 
1579 protected:
1580 
1582  /* Implement the reset operation. */
1583  [[noreturn]]
1584  static void reset_ (uint32_t pc,
1585  unsigned int code,
1586  bool bypass_watchdog = false);
1587 
1588  friend void failsafe (unsigned int code);
1589  friend void failsafe (FailSafeCode code);
1590 
1595  static void controlledResetPrep_ (unsigned int preserve);
1596 
1597  /* Implement the failsafe operation. */
1598  [[noreturn]]
1599  static void failsafe_ (uint32_t pc,
1600  unsigned int code);
1601 
1602  /* Force or wait out the reset. */
1603  [[noreturn]]
1604  static void controlledReset_ (unsigned int preserve,
1605  bool bypass_watchdog);
1613 
1617 
1618 private:
1619  state_type& state_;
1620  const uint32_t magic_;
1621  static unsigned int wfe_count_;
1622 };
1623 
1645 {
1646  struct ref_next {
1647  using pointer_type = watchdog_extended_channel *;
1648  pointer_type& operator() (watchdog_extended_channel& wec) noexcept
1649  {
1650  return wec.next_;
1651  }
1652  };
1653 
1654  using chain_type = pabigot::container::forward_chain<watchdog_extended_channel, ref_next>;
1655 
1656 public:
1659  static constexpr uint8_t WATCHDOG_CHANNEL_COMMON = 7;
1660 
1663  watchdog_extended_channel (unsigned int interval_utt);
1664 
1665  // On destruction the channel will be removed from the chain.
1667 
1669  void feed ();
1670 
1672  watchdog_extended_channel& operator= (const watchdog_extended_channel&) = delete;
1675 
1676 private:
1677  friend class systemState;
1678 
1679  bool check (unsigned int now) const
1680  {
1681  return interval_utt > (now - last_fed_);
1682  }
1683 
1684  static chain_type chain_;
1685  static bool failed_;
1686 
1687  chain_type::pointer_type next_ = chain_type::unlinked_ptr();
1688  unsigned int last_fed_;
1689  unsigned int const interval_utt;
1690 };
1691 
1693 {
1694  systemState::failsafe_(systemState::current_pc(), static_cast<unsigned int>(code));
1695 }
1696 
1697 void failsafe (unsigned int code)
1698 {
1699  systemState::failsafe_(systemState::current_pc(), code);
1700 }
1701 
1719 inline int __attribute__((__gnu_inline__,__always_inline__))
1721 {
1722  return (int32_t)(__get_MSP() - static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&__StackLimit)));
1723 }
1724 
1730 inline void __attribute__((__gnu_inline__,__always_inline__))
1732 {
1733  if (0 > stack_space_remaining()) {
1735  }
1736 }
1737 
1743 unsigned int
1744 stack_fill_unused (unsigned int marker);
1745 
1751 unsigned int
1752 stack_infer_highwater (unsigned int marker);
1753 
1774 uint32_t application_crc32 (bool exclude_data = false);
1775 
1777 namespace sensor {
1778 } // ns sensor
1779 
1781 namespace misc {
1782 } // ns misc
1783 
1784 } // ns nrfcxx
1785 
1786 #endif /* NRFCXX_CORE_HPP */
nrfcxx::systemState::state_type::om_total
uint64_t om_total[NUM_OPERATIONAL_MODES]
Cumulative time spent in each operational mode since restart.
Definition: core.hpp:1022
nrfcxx::event_set::fetch_and_clear
event_type fetch_and_clear()
Atomically return the current event set and clear the event record.
Definition: core.hpp:584
nrfcxx::sleep_ms
void sleep_ms(unsigned int dur_ms)
Sleep for the specified duration.
nrfcxx::nrf5::peripheral::NO_IRQ
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
nrfcxx::FailSafeCode::RESOURCE_VIOLATION
Application attempted to allocate more resources than available.
nrfcxx::event_set_copy::set
void set(event_type events)
Add a new event to the set.
Definition: core.hpp:689
nrfcxx::nrf5::peripheral::BASE
const uintptr_t BASE
The address of the instance.
Definition: core.hpp:208
nrfcxx::systemState::state_type::reset_reas
reset_reas_type reset_reas
Captured data from NRF_POWER->RESETREAS.
Definition: core.hpp:1221
nrfcxx::systemState::heap_used
unsigned int heap_used() const
Return the current total heap allocation.
nrfcxx::failsafe
void failsafe(FailSafeCode code)
Record a critical system failure and reset the system.
Definition: core.hpp:1692
nrfcxx::systemState::watchdogCheckExtended
bool watchdogCheckExtended()
Check that the extended watchdog channels are current.
nrfcxx::systemState::operationalModeText
static const char *const operationalModeText[NUM_OPERATIONAL_MODES]
Text representations of each operational mode.
Definition: core.hpp:921
nrfcxx::watchdog_extended_channel::watchdog_extended_channel
watchdog_extended_channel(unsigned int interval_utt)
Construct and register an extended watchdog with the specified interval.
nrfcxx::FailSafeCode::BOARD_INIT_FAILURE
Board setup failed in some critical way.
nrfcxx::systemState::WFE
static void WFE()
Issue a __WFE instruction that records time spent sleeping.
nrfcxx::systemState::state_type::reset_reas_enum
reset_reas_enum
Defined bits in reset_reas.
Definition: core.hpp:1117
nrfcxx::nrf5::peripheral< NRF_PPI_Type >::Type
NRF_PPI_Type Type
The structure describing the instance content, such as NRF_GPIO_Type or NRF_UART_Type.
Definition: core.hpp:168
nrfcxx::event_set::make_setter
notifier_type make_setter(event_type events)
Return an invokable object that records events in this set.
Definition: core.hpp:637
nrfcxx::systemState::OM_HFCLK
Operational mode flag indicating that high-frequency clock is running.
Definition: core.hpp:881
nrfcxx::systemState::app_handler_type
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
core.hpp
API specific to the nRF51 series supporting <nrfcxx/core.hpp>.
nrfcxx::systemState::updateOperationalMode
static unsigned int updateOperationalMode(unsigned int om_clear=0, unsigned int om_set=0)
Update the operational mode.
nrfcxx::event_set_copy::event_set_copy
event_set_copy(event_set &events)
Capture events and reset the original set.
Definition: core.hpp:663
nrfcxx::nvic_SetPendingIRQ
static void nvic_SetPendingIRQ(int irqn)
Wrapper around NVIC_SetPending to work around issues with peripheral::IRQn.
Definition: core.hpp:403
nrfcxx::FailSafeCode::NO_SUCH_PERIPHERAL
Application attempted to retrieve a non-existent peripheral instance.
nrfcxx::stack_fill_unused
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.
nrfcxx::systemState::reset
void reset(unsigned int code, bool bypass_watchdog=false)
Record some information and reset the system.
Definition: core.hpp:1452
nrfcxx::systemState::state_type::RESET_REAS_WDTBARKED
Bit mask for reset_reas to indicate a captured watchdog reset.
Definition: core.hpp:1199
nrfcxx::nvic_BlockIRQ
RAII class to block a peripheral interrupt.
Definition: core.hpp:452
nrfcxx::watchdog_extended_channel::WATCHDOG_CHANNEL_COMMON
static constexpr uint8_t WATCHDOG_CHANNEL_COMMON
The underlying WDT channel used to support extended watchdog channels.
Definition: core.hpp:1659
nrfcxx::systemState::state
const state_type & state() const
Access a read-only instance of the state referenced by this object.
Definition: core.hpp:1330
nrfcxx::event_set::set
void set(event_type events)
Atomically record one or more events.
Definition: core.hpp:603
nrfcxx::mutex_irq
nvic_BlockIRQ as a template type.
Definition: core.hpp:497
nrfcxx::systemState::state_type::RESET_REAS_LPCOMP
Bit mask for reset_reas to indicate wakeup from system OFF mode due to ANADETECT from the LPCOMP peri...
Definition: core.hpp:1144
nrfcxx::FailSafeCode::API_VIOLATION
Application tried something that isn't allowed.
nrfcxx::systemState::state_type::sdfault_id
unsigned int sdfault_id
Data recording the id of a soft-device fault.
Definition: core.hpp:1073
nrfcxx::systemState::systemOff
static void systemOff(unsigned int preserve, int button_psel=-1)
Enter system off mode.
nrfcxx::systemState::state_type::RESET_REAS_NFC
Bit mask for reset_reas to indicate wakeup from system OFF mode due to NFC field detection.
Definition: core.hpp:1155
nrfcxx::FailSafeCode::INTERNAL_ERROR
Unspecified internal error.
nrfcxx::systemState::state_type::RESET_REAS_VBUS
Bit mask for reset_reas to indicate wakeup from system OFF mode due to VBUS becoming valid.
Definition: core.hpp:1162
nrfcxx::application_crc32
uint32_t application_crc32(bool exclude_data=false)
Calculate a checksum of the application content.
nrfcxx::nvic_GetPendingIRQ
static void nvic_GetPendingIRQ(int irqn)
Wrapper around NVIC_GetPending to work around issues with peripheral::IRQn.
Definition: core.hpp:394
nrfcxx::systemState::watchdogActive
bool watchdogActive() const
Check whether the watchdog infrastructure is active.
nrfcxx::nrf5::peripheral
Capture information about an nRF5 peripheral instance.
Definition: core.hpp:165
nrfcxx::systemState::current_pc
static uint32_t current_pc()
Return the address of the instruction after the call.
nrfcxx::nrf5::peripheral::instance
Type * instance() const
Get a type-correct pointer to the peripheral structure.
Definition: core.hpp:195
nrfcxx::event_set_copy::test_and_clear
bool test_and_clear(event_type evt)
Return true iff evt was set.
Definition: core.hpp:697
nrfcxx::systemState::state_type::code
unsigned int code
Data recording the code passed to reset() in the previous session.
Definition: core.hpp:1064
nrfcxx::systemState::state_type::last_pc
uint32_t last_pc
The program counter at which the reset was initiated.
Definition: core.hpp:1048
nrfcxx::systemState::state_type::RESET_REAS_SDFAULT
Bit mask for reset_reas to indicate a soft-device fault.
Definition: core.hpp:1206
nrfcxx::systemState::heap_reserved
unsigned int heap_reserved() const
Return the size of the reserved heap region.
nrfcxx::nrf5::peripheral::AUX
const uint8_t AUX
Auxiliary information relevant to the specific peripheral and type.
Definition: core.hpp:245
nrfcxx::nvic_ClearPendingIRQ
static void nvic_ClearPendingIRQ(int irqn)
Wrapper around NVIC_ClearPending to work around issues with peripheral::IRQn.
Definition: core.hpp:412
nrfcxx::FailSafeCode::SYSTEM_BASE
Base for system-assigned fail-safe codes.
nrfcxx::systemState::state_type::RESET_REAS_DIF
Bit mask for reset_reas to indicate wakeup from system OFF mode due to debug interface mode.
Definition: core.hpp:1148
nrfcxx::board::initialize
int initialize(bool enable_hfxt=false)
Perform board-specific initialization.
nrfcxx::FailSafeCode::PERSIST_VIOLATION
Default code for violation of a persisted memory region.
nrfcxx::event_set_copy
A helper class for processing snapshot nrfcxx::event_set values.
Definition: core.hpp:654
nrfcxx::nvic_EnableIRQ
static void nvic_EnableIRQ(int irqn)
Wrapper around NVIC_EnableIRQ to work around issues with peripheral::IRQn.
Definition: core.hpp:376
nrfcxx::nvic_GetPriority
static void nvic_GetPriority(int irqn)
Wrapper around NVIC_GetPriority to work around issues with peripheral::IRQn.
Definition: core.hpp:430
nrfcxx::systemState::state_type::om_updated
unsigned int om_updated
Time of last change to operational mode.
Definition: core.hpp:1018
nrfcxx::systemState::scoped_sleeper::track_om
const bool track_om
true iff time-in-mode is being tracked by this instance.
Definition: core.hpp:949
nrfcxx::nrf5::series::DELAY_US_OVERHEAD_cyc
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
nrfcxx::systemState::wfe_count
static unsigned int wfe_count()
Return the number of times WFE() has returned.
Definition: core.hpp:1541
nrfcxx::systemState::WATCHDOG_Hz
static constexpr unsigned int WATCHDOG_Hz
Frequency at which the watchdog clock runs.
Definition: core.hpp:1336
nrfcxx::systemState::state_type::RESET_REAS_OFF
Bit mask for reset_reas to indicate wakeup from system OFF mode due to DETECT from the GPIO periphera...
Definition: core.hpp:1140
nrfcxx::nrf5::GPIO_Instance
A traits type identifying GPIO peripheral instances.
Definition: core.hpp:260
nrfcxx::null_mutex
RAII class that performs no mutex operations.
Definition: core.hpp:319
nrfcxx::systemState::state_type::RESET_REAS_SREQ
Bit mask for reset_reas to indicate reset due to AIRCR.SYSRESETRQ.
Definition: core.hpp:1132
nrfcxx::nrf5::series::delay_cycles
void delay_cycles(unsigned int cycles)
Loop to delay for a requested number of cycles.
nrfcxx::systemState::OM_SLEEP
Operational mode flag indicating that CPU is turned off.
Definition: core.hpp:867
nrfcxx::systemState::state_type::RESET_REAS_DOG
Bit mask for reset_reas to indicate reset due to watchdog.
Definition: core.hpp:1128
nrfcxx::systemState::state_type
The raw data supporting cross-reset state transfer.
Definition: core.hpp:993
nrfcxx::systemState::state_type::RESET_REAS_CONTROLLED
Bit mask for reset_reas to indicate that the reset was controlled.
Definition: core.hpp:1215
core.hpp
API specific to the faked host-based unit testing supporting <nrfcxx/core.hpp>.
nrfcxx::event_set::cev
static void cev()
Safely clear the MCU event flag.
Definition: core.hpp:574
nrfcxx::FailSafeCode::MEMORY_POOL
Exceeded allocated space of a memory pool.
nrfcxx::systemState::state_type::RESET_REAS_RESETPIN
Bit mask for reset_reas to indicate reset due to the reset pin.
Definition: core.hpp:1124
nrfcxx::event_set::event_set
constexpr event_set(event_type events=0U)
Construct with an optional initial set of events.
Definition: core.hpp:550
nrfcxx::systemState::WATCHDOG_MASK_EXTENDED
static constexpr auto WATCHDOG_MASK_EXTENDED
Pseudo-channel used to support extended watchdog channels.
Definition: core.hpp:1342
nrfcxx::systemState::watchdogFeedMulti
void watchdogFeedMulti(unsigned int channel_mask) const
As with watchdogFeed() but feeds multiple channels.
nrfcxx::systemState::statep_
static state_type * statep_
Pointer to the #state_ field of the first systemState instance to be constructed.
Definition: core.hpp:1612
nrfcxx::systemState::OM_RADIO
Operational mode flag indicating that 2.4 GHz radio is running.
Definition: core.hpp:890
nrfcxx::systemState::state_type::RESET_REAS_FAILSAFE
Bit mask for reset_reas to indicate a fatal system error from which a reset will not recover.
Definition: core.hpp:1192
nrfcxx::event_set::copy_and_clear
event_set_copy copy_and_clear()
Create a non-mutex copy then clear the current event set.
Definition: core.hpp:710
nrfcxx::stack_infer_highwater
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...
nrfcxx::systemState::scoped_sleeper
RAII instance that configures the operational mode to enter OM_SLEEP when constructed,...
Definition: core.hpp:932
nrfcxx::watchdog_extended_channel
Support for extended watchdog channels.
Definition: core.hpp:1644
nrfcxx::event_set::reset
void reset(event_type events=0U)
Reset the event set to a specific state.
Definition: core.hpp:613
nrfcxx::event_set::fetch
event_type fetch() const
Return the current event set.
Definition: core.hpp:561
nrfcxx::FailSafeCode::HEAP_OVERRUN
Allocated too much from system heap.
nrfcxx::validate_stack_pointer
void validate_stack_pointer()
Force a system failure if the stack is beyond its configured maximum depth.
Definition: core.hpp:1731
nrfcxx::event_set_copy::event_type
event_set::event_type event_type
The type used to represent a (set of) event(s).
Definition: core.hpp:660
nrfcxx::systemState::state_type::total_uptime
uint64_t total_uptime
Total of last_uptime values since the state was reset.
Definition: core.hpp:1040
nrfcxx::systemState::watchdogFeed
void watchdogFeed(unsigned int channel) const
Reload a specific channel in the watchdog.
nrfcxx::FailSafeCode::INCOMPLETE_SETUP
Application failed to perform all steps required to run.
nrfcxx::systemState::state_type::RESET_REAS_INTERNAL
Bit mask for reset_reas to indicate framework-requested reset.
Definition: core.hpp:1176
nrfcxx::systemState::systemState
systemState(state_type &state, uint32_t magic, app_handler_type app_handler=nullptr)
Set up a wrapper around a cross-reset state object.
nrfcxx::systemState::state_type::om_value
uint8_t om_value
Bits identifying the current operational mode.
Definition: core.hpp:1263
nrfcxx::systemState::state_type::last_uptime
uint64_t last_uptime
Duration of the last system session, in uptime clock ticks.
Definition: core.hpp:1033
nrfcxx::notifier_type
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
nrfcxx::systemState::state_type::wdt_status
uint8_t wdt_status
Bits identifying watchdog channels that caused a system reset.
Definition: core.hpp:1252
nrfcxx::stack_space_remaining
int stack_space_remaining()
Determine how much of the reserved stack space remains.
Definition: core.hpp:1720
nrfcxx::systemState
A class supporting watchdog configuration and cross-reset retention of state.
Definition: core.hpp:853
nrfcxx::watchdog_extended_channel::feed
void feed()
Feed the extended watchdog.
nrfcxx::systemState::operationalModeBreakdown
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.
nrfcxx::systemState::om_enum
om_enum
Bits used to build up an operational mode.
Definition: core.hpp:860
nrfcxx::systemState::watchdogInit
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.
nrfcxx::systemState::state_type::magic
uint32_t magic
A magic number that is used to determine the state is valid.
Definition: core.hpp:1012
nrfcxx::FailSafeCode::APPLICATION_BASE
Base for application-assigned fail-safe codes.
nrfcxx::systemState::make_scoped_sleeper
static scoped_sleeper make_scoped_sleeper() noexcept
Construct an RAII object to track sleep periods.
Definition: core.hpp:972
nrfcxx::systemState::state_type::RESET_REAS_HARDWARE_Msk
Bit mask isolating the bits of reset_reas that are set from hardware.
Definition: core.hpp:1169
nrfcxx::systemState::stack_used
unsigned int stack_used() const
Return the size of stack region that has been used since startup.
nrfcxx::nrf5::peripheral::NO_INSTANCE
static constexpr uint8_t NO_INSTANCE
Flag value for INSTANCE indicating that the peripheral does not have enumerated instances (e....
Definition: core.hpp:177
nrfcxx::FailSafeCode::STACK_OVERFLOW
Detected stack pointer into heap memory.
nrfcxx::systemState::app_handler_
static app_handler_type app_handler_
Pointer to the application state handler for the first systemState instance to be constructed.
Definition: core.hpp:1616
nrfcxx::systemState::sd_fault_handler
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().
nrfcxx::systemState::state_type::RESET_REAS_LOCKUP
Bit mask for reset_reas to indicate reset due to CPU lock-up.
Definition: core.hpp:1136
core.hpp
API specific to the nRF52 series supporting <nrfcxx/core.hpp>.
nrfcxx::event_set::event_type
unsigned int event_type
The type used to represent a (set of) event(s).
Definition: core.hpp:547
nrfcxx::systemState::state_type::reset_reas_type
uint16_t reset_reas_type
Base type underlying bits stored in reset_reas.
Definition: core.hpp:1091
nrfcxx::systemState::stack_reserved
unsigned int stack_reserved() const
Return the size of the reserved stack region.
nrfcxx::systemState::NUM_OPERATIONAL_MODES
The number of distinct operational modes supported.
Definition: core.hpp:895
nrfcxx::systemState::total_now
static uint64_t total_now()
Return the aggregate time since the retained state was reset.
nrfcxx::nrf5::series::CLOCK_MHz
static constexpr unsigned int CLOCK_MHz
CPU clock speed in MHz.
Definition: core.hpp:50
nrfcxx::event_set_copy::event_set_copy
event_set_copy()=default
Create an empty event set.
nrfcxx::FailSafeCode
FailSafeCode
Enumerated constants used in failsafe() calls.
Definition: core.hpp:719
nrfcxx::systemState::state_type::reset_count
uint16_t reset_count
The number of times the system has been restarted with cross-reset state successfully transferred.
Definition: core.hpp:1088
nrfcxx::nrf5::peripheral::IRQn
const int8_t IRQn
The interrupt number associated with the instance, or negative if the instance has no dedicated inter...
Definition: core.hpp:221
nrfcxx::nrf5::peripheral::operator->
Type * operator->() const
Allow dereferencing the instance as though it were a pointer to the peripheral structure.
Definition: core.hpp:202
nrfcxx::nrf5::peripheral::peripheral
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
nrfcxx::nvic_DisableIRQ
static void nvic_DisableIRQ(int irqn)
Wrapper around NVIC_DisableIRQ to work around issues with peripheral::IRQn.
Definition: core.hpp:385
nrfcxx::primask
RAII class to block exceptions.
Definition: core.hpp:340
nrfcxx::nvic_SetPriority
static void nvic_SetPriority(int irqn, uint32_t priority)
Wrapper around NVIC_SetPriority to work around issues with peripheral::IRQn.
Definition: core.hpp:421
nrfcxx::delay_us
void delay_us(unsigned int number_of_us)
Delay for exactly the specified duration.
Definition: core.hpp:292
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
nrfcxx::systemState::state_type::DECL_MAGIC
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
nrfcxx::nrf5::peripheral::INSTANCE
const uint8_t INSTANCE
The peripheral instance, for peripherals like TIMER that have multiple instances.
Definition: core.hpp:228
nrfcxx::nvic_BlockIRQ::nvic_BlockIRQ
nvic_BlockIRQ(int irqn)
Variant required to work around lack of IRQn_Type when declaring nrfcxx::peripheral.
Definition: core.hpp:468
nrfcxx::event_set_copy::empty
bool empty() const
Return true iff no events remain.
Definition: core.hpp:677
nrfcxx::event_set
A record of events that occur asynchonously.
Definition: core.hpp:541
nrfcxx::event_set_copy::events
event_type events() const
Return the bitmask of remaining events.
Definition: core.hpp:683
nrfcxx::systemState::state_type::RESET_REAS_PROGRAMMATIC
Bit mask for reset_reas to indicate an application-requested reset.
Definition: core.hpp:1183
nrfcxx::FailSafeCode::EVENT_LOOP_TERMINATED
Application left the main loop.