nRF51 SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Utility Functions and Definitions

Various types and definitions available to all applications. More...

Macros

#define NRF_APP_PRIORITY_THREAD   4
 
#define PACKED(TYPE)   __packed TYPE
 
#define STATIC_ASSERT(EXPR)   typedef char static_assert_failed[(EXPR) ? 1 : -1]
 Macro for doing static (i.e. compile time) assertion. More...
 
#define GPIO_PIN_CONFIG(PIN_NO, IO_DIR, IO_INPUT, IO_PULL, IO_DRIVE, IO_SENSE)   INTERN_GPIO_PIN_CONFIG(PIN_NO, IO_DIR, IO_INPUT, IO_PULL, IO_DRIVE, IO_SENSE)
 Macro to configure an I/O. More...
 
#define GPIO_LED_CONFIG(PIN_NO)
 Configure a PIN to be used as a LED. More...
 
#define GPIO_DEBUG_PIN_CONFIG(PIN_NO)
 Configure a PIN to be used as a debug pin (e.g. to be monitored using a logic analyzer). More...
 
#define GPIO_BUTTON_CONFIG(PIN_NO)
 Configure a PIN to be used as a Button. More...
 
#define GPIO_WAKEUP_BUTTON_CONFIG(PIN_NO)
 Configure a PIN to be used as a Button and a Wakeup source. More...
 
#define GPIO_BUTTON_WITH_PULLUP_CONFIG(PIN_NO)
 Configure a PIN to be used as a Button, with a pullup resistor enabled. More...
 
#define GPIO_WAKEUP_BUTTON_WITH_PULLUP_CONFIG(PIN_NO)
 Configure a PIN to be used as a Button and a Wakeup source, with a pullup resistor enabled. More...
 
#define CRITICAL_REGION_ENTER()
 Macro for entering a critical region. More...
 
#define CRITICAL_REGION_EXIT()
 Macro for leaving a critical region. More...
 
#define ROUNDED_DIV(A, B)   (((A) + ((B) / 2)) / (B))
 Perform rounded integer division (as opposed to truncating the result). More...
 
#define IS_POWER_OF_TWO(A)   ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
 Check if the integer provided is a power of two. More...
 
#define MSEC_TO_UNITS(TIME, RESOLUTION)   (((TIME) * 1000) / (RESOLUTION))
 To convert ticks to millisecond. More...
 
#define CEIL_DIV(A, B)
 Perform integer division, making sure the result is rounded up. More...
 

Typedefs

typedef uint8_t uint16_le_t [2]
 type for holding an encoded (i.e. little endian) 16 bit unsigned integer.
 
typedef uint8_t uint32_le_t [4]
 type for holding an encoded (i.e. little endian) 32 bit unsigned integer.
 

Enumerations

enum  app_irq_priority_t {
  APP_IRQ_PRIORITY_HIGH = 1,
  APP_IRQ_PRIORITY_LOW = 3
}
 The interrupt priorities available to the application while the softdevice is active.
 
enum  {
  UNIT_0_625_MS = 625,
  UNIT_1_25_MS = 1250,
  UNIT_10_MS = 10000
}
 

Functions

static __INLINE uint8_t uint16_encode (uint16_t value, uint8_t *p_encoded_data)
 Function for encoding a uint16 value. More...
 
static __INLINE uint8_t uint32_encode (uint32_t value, uint8_t *p_encoded_data)
 Function for encoding a uint32 value. More...
 
static __INLINE uint16_t uint16_decode (const uint8_t *p_encoded_data)
 Function for decoding a uint16 value. More...
 
static __INLINE uint32_t uint32_decode (const uint8_t *p_encoded_data)
 Function for decoding a uint32 value. More...
 
static __INLINE uint8_t current_int_priority_get (void)
 Function for finding the current interrupt level. More...
 
static __INLINE uint8_t battery_level_in_percent (const uint16_t mvolts)
 Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts. More...
 
static __INLINE bool is_word_aligned (void *p)
 Function for checking if a pointer value is aligned to a 4 byte boundary. More...
 

Data Structures

struct  uint8_array_t
 Byte array type. More...
 

Detailed Description

Macro Definition Documentation

#define NRF_APP_PRIORITY_THREAD   4

"Interrupt level" when running in Thread Mode.

#define STATIC_ASSERT (   EXPR)    typedef char static_assert_failed[(EXPR) ? 1 : -1]
Note
If the assertion fails when compiling using Keil, the compiler will report error message "error: #94: the size of an array must be greater than zero" (while gcc will list the symbol static_assert_failed, making the error message more readable). If the supplied expression can not be evaluated at compile time, Keil will report "error: #28: expression must have a constant value".
The macro is intentionally implemented not using do while(0), allowing it to be used outside function blocks (e.g. close to global type- and variable declarations). If used in a code block, it must be used before any executable code in this block.
Parameters
[in]EXPRConstant expression to be verified.
#define GPIO_PIN_CONFIG (   PIN_NO,
  IO_DIR,
  IO_INPUT,
  IO_PULL,
  IO_DRIVE,
  IO_SENSE 
)    INTERN_GPIO_PIN_CONFIG(PIN_NO, IO_DIR, IO_INPUT, IO_PULL, IO_DRIVE, IO_SENSE)

This macro configures a given I/O to input or output with pullup/buffer configuration.

Parameters
[in]PIN_NOI/O pin to configure.
[in]IO_DIRI/O pin direction.
[in]IO_INPUTConnect or disconnect input path.
[in]IO_PULLPull-up or -down configuration.
[in]IO_DRIVEDrive configuration.
[in]IO_SENSEPin sensing mechanism.
#define GPIO_LED_CONFIG (   PIN_NO)
Value:
do \
{ \
GPIO_PIN_CONFIG(PIN_NO, \
GPIO_PIN_CNF_DIR_Output, \
GPIO_PIN_CNF_INPUT_Disconnect,\
GPIO_PIN_CNF_PULL_Disabled, \
GPIO_PIN_CNF_DRIVE_S0S1, \
GPIO_PIN_CNF_SENSE_Disabled); \
} while (0)
Parameters
[in]PIN_NOInput pin to configure.
#define GPIO_DEBUG_PIN_CONFIG (   PIN_NO)
Value:
do \
{ \
GPIO_PIN_CONFIG(PIN_NO, \
GPIO_PIN_CNF_DIR_Output, \
GPIO_PIN_CNF_INPUT_Disconnect,\
GPIO_PIN_CNF_PULL_Disabled, \
GPIO_PIN_CNF_DRIVE_S0S1, \
GPIO_PIN_CNF_SENSE_Disabled); \
} while (0)
Parameters
[in]PIN_NOInput pin to configure.
#define GPIO_BUTTON_CONFIG (   PIN_NO)
Value:
do \
{ \
GPIO_PIN_CONFIG(PIN_NO, \
GPIO_PIN_CNF_DIR_Input, \
GPIO_PIN_CNF_INPUT_Connect, \
GPIO_PIN_CNF_PULL_Disabled, \
GPIO_PIN_CNF_DRIVE_S0S1, \
GPIO_PIN_CNF_SENSE_Disabled); \
} while (0)
Parameters
[in]PIN_NOPin to configure.
#define GPIO_WAKEUP_BUTTON_CONFIG (   PIN_NO)
Value:
do \
{ \
GPIO_PIN_CONFIG(PIN_NO, \
GPIO_PIN_CNF_DIR_Input, \
GPIO_PIN_CNF_INPUT_Connect, \
GPIO_PIN_CNF_PULL_Disabled, \
GPIO_PIN_CNF_DRIVE_S0S1, \
GPIO_PIN_CNF_SENSE_Low); \
} while (0)
Parameters
[in]PIN_NOPin to configure.
#define GPIO_BUTTON_WITH_PULLUP_CONFIG (   PIN_NO)
Value:
do \
{ \
GPIO_PIN_CONFIG(PIN_NO, \
GPIO_PIN_CNF_DIR_Input, \
GPIO_PIN_CNF_INPUT_Connect, \
GPIO_PIN_CNF_PULL_Pullup, \
GPIO_PIN_CNF_DRIVE_S0S1, \
GPIO_PIN_CNF_SENSE_Disabled); \
} while (0)
Parameters
[in]PIN_NOPin to configure.
#define GPIO_WAKEUP_BUTTON_WITH_PULLUP_CONFIG (   PIN_NO)
Value:
do \
{ \
GPIO_PIN_CONFIG(PIN_NO, \
GPIO_PIN_CNF_DIR_Input, \
GPIO_PIN_CNF_INPUT_Connect, \
GPIO_PIN_CNF_PULL_Pullup, \
GPIO_PIN_CNF_DRIVE_S0S1, \
GPIO_PIN_CNF_SENSE_Low); \
} while (0)
Parameters
[in]PIN_NOPin to configure.
#define CRITICAL_REGION_ENTER ( )
Value:
{ \
uint8_t IS_NESTED_CRITICAL_REGION = 0; \
uint32_t CURRENT_INT_PRI = current_int_priority_get(); \
if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \
{ \
uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION); \
{ \
__disable_irq(); \
} \
else \
{ \
APP_ERROR_CHECK(ERR_CODE); \
} \
}
Note
Due to implementation details, there must exist one and only one call to CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located in the same scope.
#define CRITICAL_REGION_EXIT ( )
Value:
if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \
{ \
uint32_t ERR_CODE; \
__enable_irq(); \
ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION); \
{ \
APP_ERROR_CHECK(ERR_CODE); \
} \
} \
}
Note
Due to implementation details, there must exist one and only one call to CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located in the same scope.
#define ROUNDED_DIV (   A,
 
)    (((A) + ((B) / 2)) / (B))
Parameters
[in]ANumerator.
[in]BDenominator.
Returns
Rounded (integer) result of dividing A by B.
#define IS_POWER_OF_TWO (   A)    ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
Parameters
[in]ANumber to be tested.
Returns
true if value is power of two.
false if value not power of two.
#define MSEC_TO_UNITS (   TIME,
  RESOLUTION 
)    (((TIME) * 1000) / (RESOLUTION))
Parameters
[in]timeNumber of millseconds that needs to be converted.
[in]resolutionUnits to be converted.
#define CEIL_DIV (   A,
 
)
Value:
/*lint -save -e573 */ \
((((A) - 1) / (B)) + 1) \
/*lint -restore */

One typical use for this is to compute the number of objects with size B is needed to hold A number of bytes.

Parameters
[in]ANumerator.
[in]BDenominator.
Returns
Integer result of dividing A by B, rounded up.

Enumeration Type Documentation

anonymous enum
Enumerator
UNIT_0_625_MS 

Number of microseconds in 0.625 milliseconds.

UNIT_1_25_MS 

Number of microseconds in 1.25 milliseconds.

UNIT_10_MS 

Number of microseconds in 10 milliseconds.

Function Documentation

static __INLINE uint8_t uint16_encode ( uint16_t  value,
uint8_t *  p_encoded_data 
)
static
Parameters
[in]valueValue to be encoded.
[out]p_encoded_dataBuffer where the encoded data is to be written.
Returns
Number of bytes written.
static __INLINE uint8_t uint32_encode ( uint32_t  value,
uint8_t *  p_encoded_data 
)
static
Parameters
[in]valueValue to be encoded.
[out]p_encoded_dataBuffer where the encoded data is to be written.
Returns
Number of bytes written.
static __INLINE uint16_t uint16_decode ( const uint8_t *  p_encoded_data)
static
Parameters
[in]p_encoded_dataBuffer where the encoded data is stored.
Returns
Decoded value.
static __INLINE uint32_t uint32_decode ( const uint8_t *  p_encoded_data)
static
Parameters
[in]p_encoded_dataBuffer where the encoded data is stored.
Returns
Decoded value.
static __INLINE uint8_t current_int_priority_get ( void  )
static
Returns
Current interrupt level.
Return values
APP_IRQ_PRIORITY_HIGHWe are running in Application High interrupt level.
APP_IRQ_PRIORITY_LOWWe are running in Application Low interrupt level.
APP_IRQ_PRIORITY_THREADWe are running in Thread Mode.
static __INLINE uint8_t battery_level_in_percent ( const uint16_t  mvolts)
static

The calculation is based on a linearized version of the battery's discharge curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and is considered to be the lower boundary.

The discharge curve for CR2032 is non-linear. In this model it is split into 4 linear sections:

  • Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
  • Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
  • Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
  • Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)

These numbers are by no means accurate. Temperature and load in the actual application is not accounted for!

Parameters
[in]mvoltsThe voltage in mV
Returns
Battery level in percent.
static __INLINE bool is_word_aligned ( void *  p)
static
Parameters
[in]pPointer value to be checked.
Returns
TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.