YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
bitfield_defs.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// Definitions and templates for register
15// bitmap handling (reading/writing of
16// specific bitfields within a register).
17// Related C++ operators are provided.
18//
19#ifndef _BITFIELD_DEFS_H_
20#define _BITFIELD_DEFS_H_
21
22#include <stdint.h>
23
25// Helper templates and macros
27template <typename T, int Offset, int Bits>
28struct add_bitfield_RO
29{
30 private:
31 volatile T value; // This is the value which is 'mirrored' by the union
32 static const T maskl = (T(1) << Bits) - T(1);
33
34 public:
35 inline operator T() const {
36 return (value >> Offset) & maskl;
37 }
38};
39
40template <typename T, int Offset, int Bits>
41struct add_bitfield_WO
42{
43 private:
44 volatile T value; // This is the value which is 'mirrored' by the union
45 static const T maskl = (T(1) << Bits) - T(1);
46 static const T masks = maskl << Offset;
47
48 public:
49 // Assignment
50 inline void operator = (T v) {
51 value = (value & ~masks) | ((v & maskl) << Offset);
52 }
53 // Standard shortcut operators
54 inline void operator |= (T v) {
55 value |= ((v & maskl) << Offset);
56 }
57 inline void operator &= (T v) {
58 value &= ~masks | ((v & maskl) << Offset);
59 }
60 // Optimized shortcut operator for
61 // *.SET and *.CLR registers
62 inline void operator <<= (T v) {
63 value = v << Offset;
64 }
65};
66
67template <typename T, int Offset, int Bits>
68struct add_bitfield_RW
69{
70 private:
71 volatile T value; // This is the value which is 'mirrored' by the union
72 static const T maskl = (T(1) << Bits) - T(1);
73 static const T masks = maskl << Offset;
74
75 public:
76 inline operator T() const {
77 return (value >> Offset) & maskl;
78 }
79 // Assignment
80 inline void operator=(T v) {
81 value = (value & ~masks) | ((v & maskl) << Offset);
82 }
83 // Standard shortcut operators
84 inline void operator |= (T v) {
85 value |= ((v & maskl) << Offset);
86 }
87 inline void operator &= (T v) {
88 value &= ~masks | ((v & maskl) << Offset);
89 }
90 // Optimized shortcut operator for
91 // *.SET and *.CLR registers
92 inline void operator <<= (T v) {
93 value = v << Offset;
94 }
95};
96
97#define BEGIN_TYPE(typeName, T) \
98 union typeName \
99 { \
100 volatile T value; \
101 inline void operator = (T v) { value = v; } \
102 inline operator volatile T& () { return value; } \
103 inline operator T () const { return value; } \
104 typedef T basicType;
105
106#define ADD_BITFIELD_RO(memberName, offset, bits) \
107 add_bitfield_RO<basicType, offset, bits> memberName;
108
109#define ADD_BITFIELD_WO(memberName, offset, bits) \
110 add_bitfield_WO<basicType, offset, bits> memberName;
111
112#define ADD_BITFIELD_RW(memberName, offset, bits) \
113 add_bitfield_RW<basicType, offset, bits> memberName;
114
115#define END_TYPE() \
116 };
117
118#endif // _BITFIELD_DEFS_H_