aboutsummaryrefslogtreecommitdiff
path: root/02-usart/include/common.h
blob: 653279e39a0017467db33457ab1b749e872980d6 (plain) (blame)
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
#ifndef COMMON__H
#define COMMON__H

#include <stdint.h>
#include <stddef.h>
#include <assert.h>

#define WEAK __attribute__((weak))
#define NORETURN __attribute__((noreturn))

#ifndef static_assert
#define static_assert(a, b)
#endif

/* Define __IO to be volatile if it's not already. */
#ifndef __IO
#define __IO volatile
#endif

#define CTZ(n) __builtin_ctz(n)

#define bool int
#ifndef __cplusplus
#define true 1
#define false 0
#endif

#define PACKED __attribute__((packed))
#define BIT(n) (1 << (n))

#define RESERVED_CONCAT_IMPL(x, y) x ## y
#define RESERVED_MACRO_CONCAT(x, y) RESERVED_CONCAT_IMPL(x, y)
#define RESERVED(n) \
  bits_t RESERVED_MACRO_CONCAT(_r, __COUNTER__) :n
  
#define RESERVE(type) \
  __IO type RESERVED_MACRO_CONCAT(_r, __COUNTER__)

#define ptr2reg(ptr) \
  ((uint32_t) (ptrdiff_t) (ptr))

typedef __IO uint32_t bits_t;

#define regset(reg, mask, val) \
  ((reg) = ((reg) & ~mask) | (val << CTZ(mask)))

#define regget(reg, mask) \
  (((reg) & mask) >> (CTZ(mask)))

#endif /* COMMON_H */