00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef _UTIL_ATOMIC_H_
00038 #define _UTIL_ATOMIC_H_ 1
00039
00040 #include <avr/io.h>
00041 #include <avr/interrupt.h>
00042
00043 #if !defined(__DOXYGEN__)
00044
00045 static __inline__ uint8_t __iSeiRetVal(void)
00046 {
00047 sei();
00048 return 1;
00049 }
00050
00051 static __inline__ uint8_t __iCliRetVal(void)
00052 {
00053 cli();
00054 return 1;
00055 }
00056
00057 static __inline__ void __iSeiParam(const uint8_t *__s)
00058 {
00059 sei();
00060 __asm__ volatile ("" ::: "memory");
00061 (void)__s;
00062 }
00063
00064 static __inline__ void __iCliParam(const uint8_t *__s)
00065 {
00066 cli();
00067 __asm__ volatile ("" ::: "memory");
00068 (void)__s;
00069 }
00070
00071 static __inline__ void __iRestore(const uint8_t *__s)
00072 {
00073 SREG = *__s;
00074 __asm__ volatile ("" ::: "memory");
00075 }
00076 #endif
00077
00203 #if defined(__DOXYGEN__)
00204 #define ATOMIC_BLOCK(type)
00205 #else
00206 #define ATOMIC_BLOCK(type) for ( type, __ToDo = __iCliRetVal(); \
00207 __ToDo ; __ToDo = 0 )
00208 #endif
00209
00224 #if defined(__DOXYGEN__)
00225 #define NONATOMIC_BLOCK(type)
00226 #else
00227 #define NONATOMIC_BLOCK(type) for ( type, __ToDo = __iSeiRetVal(); \
00228 __ToDo ; __ToDo = 0 )
00229 #endif
00230
00242 #if defined(__DOXYGEN__)
00243 #define ATOMIC_RESTORESTATE
00244 #else
00245 #define ATOMIC_RESTORESTATE uint8_t sreg_save \
00246 __attribute__((__cleanup__(__iRestore))) = SREG
00247 #endif
00248
00263 #if defined(__DOXYGEN__)
00264 #define ATOMIC_FORCEON
00265 #else
00266 #define ATOMIC_FORCEON uint8_t sreg_save \
00267 __attribute__((__cleanup__(__iSeiParam))) = 0
00268 #endif
00269
00281 #if defined(__DOXYGEN__)
00282 #define NONATOMIC_RESTORESTATE
00283 #else
00284 #define NONATOMIC_RESTORESTATE uint8_t sreg_save \
00285 __attribute__((__cleanup__(__iRestore))) = SREG
00286 #endif
00287
00302 #if defined(__DOXYGEN__)
00303 #define NONATOMIC_FORCEOFF
00304 #else
00305 #define NONATOMIC_FORCEOFF uint8_t sreg_save \
00306 __attribute__((__cleanup__(__iCliParam))) = 0
00307 #endif
00308
00309 #endif