import cbaos v0.1
[cbaos.git] / arch / arm-cortex-m0 / include / lock.h
1 #ifndef _ARCH_LOCK_H_
2 #define _ARCH_LOCK_H_
3
4 #include <types.h>
5
6 struct lock {
7 u32 primask;
8 };
9
10 #define LOCK_INIT (struct lock) {}
11
12 static inline void lock_init(struct lock *lock)
13 {
14 }
15
16 // source:
17 // http://embeddedfreak.wordpress.com/2009/08/14/cortex-m3-global-interruptexception-control/
18 //
19 // confirm this is correct by printing primask before after
20
21 // note arm architecture v7m rm... setting PRIMASK raises priority to 0
22
23 static inline void lock(struct lock *lock)
24 {
25 u32 tmp;
26 asm volatile (
27 "mrs %0, PRIMASK\n\t"
28 "cpsid i\n\t"
29 : "=r" (tmp) );
30 lock->primask = tmp;
31 }
32
33 static inline void unlock(struct lock *lock)
34 {
35 asm volatile (
36 "msr PRIMASK, %0\n\t"
37 : : "r" (lock->primask) );
38 }
39
40 #endif