YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
bitfield_defs.h
1/*
2 * bitfield_defs.h
3 *
4 * Created on: 14.10.2022
5 * Author: andreas
6 */
7
8#ifndef _BITFIELD_DEFS_H_
9#define _BITFIELD_DEFS_H_
10
11#include <stdint.h>
12
14// Helper templates and macros
16template <typename T, int Offset, int Bits>
18{
19 private:
20 volatile T value; // This is the value which is 'mirrored' by the union
21 static const T maskl = (T(1) << Bits) - T(1);
22
23 public:
24 inline operator T() const {
25 return (value >> Offset) & maskl;
26 }
27};
28
29template <typename T, int Offset, int Bits>
31{
32 private:
33 volatile T value; // This is the value which is 'mirrored' by the union
34 static const T maskl = (T(1) << Bits) - T(1);
35 static const T masks = maskl << Offset;
36
37 public:
38 // Assignment
39 inline void operator = (T v) {
40 value = (value & ~masks) | ((v & maskl) << Offset);
41 }
42 // Standard shortcut operators
43 inline void operator |= (T v) {
44 value |= ((v & maskl) << Offset);
45 }
46 inline void operator &= (T v) {
47 value &= ~masks | ((v & maskl) << Offset);
48 }
49 // Optimized shortcut operator for
50 // *.SET and *.CLR registers
51 inline void operator <<= (T v) {
52 value = v << Offset;
53 }
54};
55
56template <typename T, int Offset, int Bits>
58{
59 private:
60 volatile T value; // This is the value which is 'mirrored' by the union
61 static const T maskl = (T(1) << Bits) - T(1);
62 static const T masks = maskl << Offset;
63
64 public:
65 inline operator T() const {
66 return (value >> Offset) & maskl;
67 }
68 // Assignment
69 inline void operator=(T v) {
70 value = (value & ~masks) | ((v & maskl) << Offset);
71 }
72 // Standard shortcut operators
73 inline void operator |= (T v) {
74 value |= ((v & maskl) << Offset);
75 }
76 inline void operator &= (T v) {
77 value &= ~masks | ((v & maskl) << Offset);
78 }
79 // Optimized shortcut operator for
80 // *.SET and *.CLR registers
81 inline void operator <<= (T v) {
82 value = v << Offset;
83 }
84};
85
86#define BEGIN_TYPE(typeName, T) \
87 union typeName \
88 { \
89 volatile T value; \
90 inline void operator = (T v) { value = v; } \
91 inline operator volatile T& () { return value; } \
92 inline operator T () const { return value; } \
93 typedef T basicType;
94
95#define ADD_BITFIELD_RO(memberName, offset, bits) \
96 add_bitfield_RO<basicType, offset, bits> memberName;
97
98#define ADD_BITFIELD_WO(memberName, offset, bits) \
99 add_bitfield_WO<basicType, offset, bits> memberName;
100
101#define ADD_BITFIELD_RW(memberName, offset, bits) \
102 add_bitfield_RW<basicType, offset, bits> memberName;
103
104#define END_TYPE() \
105 };
106
107#endif // _BITFIELD_DEFS_H_