-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.h
71 lines (57 loc) · 1.39 KB
/
common.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
// Copyright (c) 2020 Jan Brittenson
// See LICENSE for details.
#ifndef _COMMON_H_
#define _COMMON_H_
#include <msp430.h>
#include <stdint.h>
#include <stddef.h>
#include <strings.h>
typedef unsigned int uint;
// Various compiler stuff; GCC extensions also supported by TI-CC
#define _weak_ __attribute__((weak))
#define _packed_ __attribute__((packed))
#define _used_ __attribute__((used))
#define _noreturn_ __attribute__((noreturn))
#define _intr_(VEC) __interrupt __attribute__((interrupt(VEC)))
// These two wrap the MSP430 compiler intrinsics to factor it out as a compiler
// dependency.
static inline void
enable_interrupt() {
__enable_interrupt();
}
static inline void
disable_interrupt() {
__disable_interrupt();
}
// RAII scope guard, reentrant
class NoInterrupt {
uint16_t _saved;
public:
NoInterrupt() {
_saved = __get_SR_register() & GIE;
__bic_SR_register(GIE);
__no_operation();
}
~NoInterrupt() {
__bis_SR_register(_saved);
__no_operation();
}
};
template <typename T>
static inline const
T& max(const T& a, const T& b) {
return a < b ? b : a;
}
template <typename T>
static inline const
T& min(const T& a, const T& b) {
return b < a ? b : a;
}
template <typename T>
static inline T swap(T& a, const T& b) {
T tmp = a;
a = b;
return tmp;
}
#define NELEM(A) (sizeof(A) / sizeof(A[0]))
#endif // _COMMON_H_