-
Notifications
You must be signed in to change notification settings - Fork 71
/
Atomic.h
97 lines (68 loc) · 2.78 KB
/
Atomic.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef __ATOMIC_H__
#define __ATOMIC_H__
#ifdef ARDUINO
#include <Arduino.h>
#if ARDUINO_ARCH_AVR
#include <util/atomic.h>
#else
/*
* http://www.stm32duino.com/viewtopic.php?f=3&t=258&hilit=ATOMIC_BLOCK&start=10
*/
#ifdef __arm__
#ifndef _CORTEX_M3_ATOMIC_H_
#define _CORTEX_M3_ATOMIC_H_
static __inline__ uint32_t __get_primask(void) \
{ uint32_t primask = 0; \
__asm__ volatile ("MRS %[result], PRIMASK\n\t":[result]"=r"(primask)::); \
return primask; } // returns 0 if interrupts enabled, 1 if disabled
static __inline__ void __set_primask(uint32_t setval) \
{ __asm__ volatile ("MSR PRIMASK, %[value]\n\t""dmb\n\t""dsb\n\t""isb\n\t"::[value]"r"(setval):); \
__asm__ volatile ("" ::: "memory");}
static __inline__ uint32_t __iSeiRetVal(void) \
{ __asm__ volatile ("CPSIE i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
__asm__ volatile ("" ::: "memory"); return 1; }
static __inline__ uint32_t __iCliRetVal(void) \
{ __asm__ volatile ("CPSID i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
__asm__ volatile ("" ::: "memory"); return 1; }
static __inline__ void __iSeiParam(const uint32_t *__s) \
{ __asm__ volatile ("CPSIE i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
__asm__ volatile ("" ::: "memory"); (void)__s; }
static __inline__ void __iCliParam(const uint32_t *__s) \
{ __asm__ volatile ("CPSID i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
__asm__ volatile ("" ::: "memory"); (void)__s; }
static __inline__ void __iRestore(const uint32_t *__s) \
{ __set_primask(*__s); __asm__ volatile ("dmb\n\t""dsb\n\t""isb\n\t"); \
__asm__ volatile ("" ::: "memory"); }
#define ATOMIC_BLOCK(type) \
for ( type, __ToDo = __iCliRetVal(); __ToDo ; __ToDo = 0 )
#define ATOMIC_RESTORESTATE \
uint32_t primask_save __attribute__((__cleanup__(__iRestore))) = __get_primask()
#define ATOMIC_FORCEON \
uint32_t primask_save __attribute__((__cleanup__(__iSeiParam))) = 0
#define NONATOMIC_BLOCK(type) \
for ( type, __ToDo = __iSeiRetVal(); __ToDo ; __ToDo = 0 )
#define NONATOMIC_RESTORESTATE \
uint32_t primask_save __attribute__((__cleanup__(__iRestore))) = __get_primask()
#define NONATOMIC_FORCEOFF \
uint32_t primask_save __attribute__((__cleanup__(__iCliParam))) = 0
#endif
#endif
#endif
#else // ARDUINO
#define ATOMIC_BLOCK(type)
#include <stdint.h>
#endif
#ifdef ARDUINO_ARCH_ESP32
static __inline__ void SA_iRestore(const uint32_t *__s)
{
XTOS_RESTORE_INTLEVEL(*__s);
}
// Note value can be 0-15, 0 = Enable all interrupts, 15 = no interrupts
#define SA_ATOMIC_RESTORESTATE uint32_t _sa_saved \
__attribute__((__cleanup__(SA_iRestore))) = XTOS_DISABLE_LOWPRI_INTERRUPTS
/*************** MACRO **********************/
#define ATOMIC_BLOCK(STATE) \
for ( SA_ATOMIC_RESTORESTATE, _sa_done = 1; \
_sa_done; _sa_done = 0 )
#endif
#endif