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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#![cfg_attr(feature = "std", allow(unreachable_code, unused_variables))]
use crate::reg::{
field::{RegFieldBit, RegFieldBits, WWRegField, WWRegFieldBit, WWRegFieldBits},
tag::RegAtomic,
RReg, Reg, RegHold, RegRef, WReg, WRegAtomic,
};
use drone_core::bitfield::Bitfield;
pub trait RwRegAtomic<'a, T: RegAtomic>: RReg<T> + WRegAtomic<'a, T> + RegRef<'a, T> {
fn modify<F>(&'a self, f: F)
where
F: for<'b> Fn(
&'b mut <Self as RegRef<'a, T>>::Hold,
) -> &'b mut <Self as RegRef<'a, T>>::Hold;
}
pub trait WRwRegFieldAtomic<T: RegAtomic>
where
Self: WWRegField<T>,
Self::Reg: RReg<T> + WReg<T>,
{
fn modify<F>(&self, f: F)
where
F: Fn(&mut <Self::Reg as Reg<T>>::Val);
}
pub trait WRwRegFieldBitAtomic<T: RegAtomic>
where
Self: WRwRegFieldAtomic<T> + RegFieldBit<T>,
Self::Reg: RReg<T> + WReg<T>,
{
fn set_bit(&self);
fn clear_bit(&self);
fn toggle_bit(&self);
}
pub trait WRwRegFieldBitsAtomic<T: RegAtomic>
where
Self: WRwRegFieldAtomic<T> + RegFieldBits<T>,
Self::Reg: RReg<T> + WReg<T>,
{
fn write_bits(&self, bits: <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits);
}
pub trait AtomicBits: Sized {
unsafe fn load_excl(address: usize) -> Self;
unsafe fn store_excl(self, address: usize) -> bool;
}
impl<'a, T, R> RwRegAtomic<'a, T> for R
where
T: RegAtomic,
R: RReg<T> + WRegAtomic<'a, T> + RegRef<'a, T>,
<R::Val as Bitfield>::Bits: AtomicBits,
{
#[inline]
fn modify<F>(&'a self, f: F)
where
F: for<'b> Fn(
&'b mut <Self as RegRef<'a, T>>::Hold,
) -> &'b mut <Self as RegRef<'a, T>>::Hold,
{
loop {
let mut val = unsafe { self.hold(load_excl::<T, Self>()) };
f(&mut val);
if unsafe { store_excl::<T, Self>(val.val()) } {
break;
}
}
}
}
impl<T, R> WRwRegFieldAtomic<T> for R
where
T: RegAtomic,
R: WWRegField<T>,
R::Reg: RReg<T> + WReg<T>,
<<R::Reg as Reg<T>>::Val as Bitfield>::Bits: AtomicBits,
{
#[inline]
fn modify<F>(&self, f: F)
where
F: Fn(&mut <Self::Reg as Reg<T>>::Val),
{
loop {
let mut val = unsafe { load_excl::<T, Self::Reg>() };
f(&mut val);
if unsafe { store_excl::<T, Self::Reg>(val) } {
break;
}
}
}
}
impl<T, R> WRwRegFieldBitAtomic<T> for R
where
T: RegAtomic,
R: WRwRegFieldAtomic<T> + RegFieldBit<T>,
R::Reg: RReg<T> + WReg<T>,
{
#[inline]
fn set_bit(&self) {
self.modify(|val| {
self.set(val);
});
}
#[inline]
fn clear_bit(&self) {
self.modify(|val| {
self.clear(val);
});
}
#[inline]
fn toggle_bit(&self) {
self.modify(|val| {
self.toggle(val);
});
}
}
impl<T, R> WRwRegFieldBitsAtomic<T> for R
where
T: RegAtomic,
R: WRwRegFieldAtomic<T> + RegFieldBits<T>,
R::Reg: RReg<T> + WReg<T>,
{
#[inline]
fn write_bits(&self, bits: <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits) {
self.modify(|val| {
self.write(val, bits);
});
}
}
unsafe fn load_excl<T, R>() -> R::Val
where
T: RegAtomic,
R: Reg<T>,
<R::Val as Bitfield>::Bits: AtomicBits,
{
R::val_from(<R::Val as Bitfield>::Bits::load_excl(R::ADDRESS))
}
unsafe fn store_excl<T, R>(val: R::Val) -> bool
where
T: RegAtomic,
R: Reg<T>,
<R::Val as Bitfield>::Bits: AtomicBits,
{
val.bits().store_excl(R::ADDRESS)
}
macro_rules! atomic_bits {
($type:ty, $ldrex:expr, $strex:expr) => {
impl AtomicBits for $type {
unsafe fn load_excl(address: usize) -> Self {
#[cfg(feature = "std")]
return unimplemented!();
let raw: Self;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile"
);
raw
}
unsafe fn store_excl(self, address: usize) -> bool {
#[cfg(feature = "std")]
return unimplemented!();
let status: Self;
asm!($strex
: "=r"(status)
: "r"(self), "r"(address)
:
: "volatile"
);
status == 0
}
}
};
}
atomic_bits!(u32, "ldrex $0, [$1]", "strex $0, $1, [$2]");
atomic_bits!(u16, "ldrexh $0, [$1]", "strexh $0, $1, [$2]");
atomic_bits!(u8, "ldrexb $0, [$1]", "strexb $0, $1, [$2]");