forked from Traumflug/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_barrier.h
65 lines (47 loc) · 1.81 KB
/
memory_barrier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef _MEMORY_BARRIER_H_
#define _MEMORY_BARRIER_H_
#include "cpu.h"
#if defined __AVR__
#include <avr/version.h>
// Provide a memory barrier to the compiler. This informs
// the compiler that is should write any cached values that
// are destined for a global variable and discard any other
// cached values from global variables.
//
// Note that this behavior does apply to all global variables,
// not just volatile ones. However, cached local variables
// are not affected as they are not externally visible.
#define MEMORY_BARRIER() __asm volatile( "" ::: "memory" )
// There is a bug in the CLI/SEI functions in older versions of
// avr-libc - they should be defined to include a memory barrier.
// This macro is used to define the barrier in the code so that
// it will be easy to remove once the bug has become ancient history.
// At the moment the bug is included in most of the distributed
// compilers.
#if __AVR_LIBC_VERSION__ < 10700UL
#define CLI_SEI_BUG_MEMORY_BARRIER() MEMORY_BARRIER()
#else
#define CLI_SEI_BUG_MEMORY_BARRIER()
#endif
#define ATOMIC_START { \
uint8_t save_reg = SREG; \
cli(); \
CLI_SEI_BUG_MEMORY_BARRIER();
#define ATOMIC_END MEMORY_BARRIER(); \
SREG = save_reg; \
}
#elif defined __ARMEL__
#define ATOMIC_START cli();
#define ATOMIC_END sei();
#define MEMORY_BARRIER()
#elif defined SIMULATOR
#define CLI_SEI_BUG_MEMORY_BARRIER()
#define MEMORY_BARRIER()
#define ATOMIC_START { \
uint8_t save_reg = sim_interrupts; \
cli();
#define ATOMIC_END MEMORY_BARRIER(); \
if (save_reg) sei(); \
}
#endif /* __AVR__, __ARMEL__, SIMULATOR */
#endif /* _MEMORY_BARRIER_H_ */