libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
Cortex Core Atomic support Defines

Atomic operation support. More...

Collaboration diagram for Cortex Core Atomic support Defines:

Macros

#define __CM_SAVER(state)
 
#define CM_ATOMIC_BLOCK()    for (uint32_t __CM_SAVER(true), __my = true; __my; __my = false)
 Cortex M Atomic Declare block. More...
 
#define CM_ATOMIC_CONTEXT()   uint32_t __CM_SAVER(true)
 Cortex M Atomic Declare context. More...
 

Functions

static uint32_t __cm_atomic_set (uint32_t *val)
 

Detailed Description

Atomic operation support.

Macro Definition Documentation

◆ __CM_SAVER

#define __CM_SAVER (   state)
Value:
__val = (state), \
__save __attribute__((__cleanup__(__cm_atomic_set))) = \
static uint32_t __cm_atomic_set(uint32_t *val)
Definition: cortex.h:166

Definition at line 171 of file cortex.h.

◆ CM_ATOMIC_BLOCK

#define CM_ATOMIC_BLOCK ( )     for (uint32_t __CM_SAVER(true), __my = true; __my; __my = false)

Cortex M Atomic Declare block.

This macro disables interrupts for the next command or block of code. The interrupt mask is automatically restored after exit of the boundary of the code block. Therefore restore of interrupt is done automatically after call of return or goto control sentence jumping outside of the block.

Warning
The usage of sentences break or continue is prohibited in the block due to implementation of this macro!
Note
It is safe to use this block inside normal code and in interrupt routine.

Basic usage of atomic block

uint64_t value; // This value is used somewhere in interrupt
...
CM_ATOMIC_BLOCK() { // interrupts are masked in this block
value = value * 1024 + 651; // access value as atomic
} // interrupts is restored automatically

Use of return inside block

uint64_t value; // This value is used somewhere in interrupt
...
uint64_t allocval(void)
{
CM_ATOMIC_BLOCK() { // interrupts are masked in this block
value = value * 1024 + 651; // do long atomic operation
return value; // interrupts is restored automatically
}
}
#define CM_ATOMIC_BLOCK()
Cortex M Atomic Declare block.
Definition: cortex.h:224

Definition at line 224 of file cortex.h.

◆ CM_ATOMIC_CONTEXT

#define CM_ATOMIC_CONTEXT ( )    uint32_t __CM_SAVER(true)

Cortex M Atomic Declare context.

This macro disables interrupts in the current block of code from the place where it is defined to the end of the block. The interrupt mask is automatically restored after exit of the boundary of the code block. Therefore restore of interrupt is done automatically after call of return, continue, break, or goto control sentence jumping outside of the block.

Note
This function is intended for use in for- cycles to enable the use of break and contine sentences inside the block, and for securing the atomic reader-like functions.
It is safe to use this block inside normal code and in interrupt routine.

Basic usage of atomic context

uint64_t value; // This value is used somewhere in interrupt
...
for (int i=0;i < 100; i++) {
CM_ATOMIC_CONTEXT(); // interrupts are masked in this block
value += 100; // access value as atomic
if ((value % 16) == 0) {
break; // restore interrupts and break cycle
}
} // interrupts is restored automatically
#define CM_ATOMIC_CONTEXT()
Cortex M Atomic Declare context.
Definition: cortex.h:278

Usage of atomic context inside atomic reader fcn.

uint64_t value; // This value is used somewhere in interrupt
...
uint64_t getnextval(void)
{
CM_ATOMIC_CONTEXT(); // interrupts are masked in this block
value = value + 3; // do long atomic operation
return value; // interrupts is restored automatically
}

Definition at line 278 of file cortex.h.

Function Documentation

◆ __cm_atomic_set()

static uint32_t __cm_atomic_set ( uint32_t *  val)
inlinestatic

Definition at line 166 of file cortex.h.

References cm_mask_interrupts().

Here is the call graph for this function: