[UPDATED V1.2.1b]
Hi all,
I have been working on a project that requires safety from interrupts so I did a bit of research, and found the ATOMIC_BLOCK macros.
They seem to be widely accepted as an easy way of controlling the global interrupt flag. After using it for a while I decided I'm not to keen on the syntax created from using the macros, my main reason is it looks like a function definition nested inside another function, which is not allowed in c++. Also, I have a need for some objects to be able to automatically control their atomic operations, I needed a more user friendly approach.
The idea I have come up with is quite simple. All you have to do is declare a variable of the class of blocking operation you want. The class has no callable methods. It works purely on side effects. The atomic operation lasts for the lifetime of the variable, therefore you can return from a function and still expect the global interrupt flag to be restored. This removes the need to use a temporary to store the value while the interrupt flag is restored before return.
Here is a comparison breakdown.
ATOMIC_BLOCK macros
Blocking type.
- ATOMIC_BLOCK
- NONATOMIC_BLOCK
Blocking mode.
- ATOMIC_RESTORESTATE
- NONATOMIC_RESTORESTATE
- ATOMIC_FORCEON
- NONATOMIC_FORCEOFF
New method. (V1.1)
Blocking type.
- AtomicBlock
- NonAtomicBlock
Blocking mode.
- Atomic_RestoreState
- Atomic_Force
Extensions for inline atomic blocks.
- Protect function
V1.2 Now supports:
AVR 8-bit processors
- Arduino 8-bit family ( Compiled / Tested )
- Teensy 2.0 ( Not Compiled / Not Tested )
- Teensy++ 2.0 ( Not Compiled / Not Tested )
ARM Cortex-M3
- Arduino DUE ( Compiled / Not Tested )
- Olimex STM32 / Leaflabs Maple. ( Compiled / Not Tested )
ARM Cortex-M4
- Teensy 3.0 ( Compiled / Not Tested )
I have also extended the functionality with a safe mode ( v1.0) . ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) simply writes an old copy of the status register into SREG. With the safe mode, only the global interrupt bit is touched. This can be used by adding a 'Safe' suffix to the blocking type, or passing '_Safe' with the blocking mode.
There are 12 blocking variations, 8 of which are unique. A complete breakdown of the library is at the top of the file provided ( AtomicBlock.h ). Without safe mode on ( safe mode has no effect when using blocking mode 'Atomic_Force' ) the library will emulate ATOMIC_BLOCK functionality with no extra overhead.
Here is a short example of how to use the library with variable declarations.
#include "AtomicBlock.h"
void setup() { return; }
//New method
void loop()
{
AtomicBlock< Atomic_RestoreState > a_Block;
{
NonAtomicBlock< Atomic_Force > a_NoBlock;
}
return;
}
/*
//Old method ( macros )
void loop()
{
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ){
NONATOMIC_BLOCK( NONATOMIC_FORCEOFF ){
}
}
return;
}
*/
The 'Protect' function usage.
Each blocking type contains a function named 'Protect'. It can be used to
protect any kind of element.
E.g.
- Typedefs make using 'Protect' easier.
typedef AtomicBlock< Atomic_Force > MyBlock;
For the sake of these examples 'i' is an 'int', and inc is the funciton below.
void inc( int &i_Val ) { ++i_Val; }
- Protected writes.
MyBlock::Protect( i ) = analogRead( A0 );
- Protected reads.
Serial.println( MyBlock::Protect( i ) );
- Protected non-member function calls.
MyBlock::Protect( inc )( i );
- Protected pointers.
*( &MyBlock::Protect( i ) ) = 77;
(*MyBlock::Protect( &i ))++;
- No unnessecary instructions.
- This will not produce any code.
MyBlock::Protect( 1 + 2 + 3 + 4 + 5 );
- These two lines produce exactly the same code.
digitalWrite( MyBlock::Protect( 13 ), digitalRead( MyBlock::Protect( 13 ) ) ^ 1 );
digitalWrite( 13, digitalRead( 13 ) ^ 1 );
- This will only turn interrupts on and off once.
MyBlock::Protect( MyBlock::Protect( MyBlock::Protect( MyBlock::Protect( i ) ) ) );
CAUTION: 'Protect' will only protect one element. Statements as arguments are not going to work as you may expect.
E.g.
- Wrong use. (argument statement will not be blocked. If you use the result, it will be inside the atomic block.)
MyBlock::Protect( PORTK |= _BV( 5 ) );
- Correct usage. (just 'Protect' PORTK)
MyBlock::Protect( PORTK ) |= _BV( 5 );
LIMITATIONS:
- I have chosen not to support any sort of member function protection. Once I have validated the current system
I can look further into it. The required interface seems to generalise the type system too much and breaks some
existing functionality as the compiler cannot disambiguate the control paths.
If you aren't logged in you can also download the code from 'google code' here.
I'm after any feedback/ideas for improvment.
Cheers,
Chris.
The files listed in this post are:
- AtomicBlock.h This file is the original AVR stable version.
- AtomicBlock_121b.h This file is the new multi-platform (beta) version.
AtomicBlock.h (10.3 KB)
AtomicBlock_121b.h (21 KB)