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
#![no_std]
#[cfg(not(any(feature = "samd5x", feature = "samd21")))]
compile_error!("Please use this crate's feature flags to select a SAM micro-controller to target.");
#[macro_use]
extern crate bitflags;
extern crate smart_default;
#[cfg(feature = "samd51j19a")]
use atsamd51j19a as target_device;
#[cfg(feature = "samd51j20a")]
use atsamd51j20a as target_device;
#[cfg(feature = "samd51g19a")]
use atsamd51g19a as target_device;
#[cfg(feature = "samd21g18a")]
use atsamd21g18a as target_device;
#[cfg(feature = "samd21e18a")]
use atsamd21e18a as target_device;
#[cfg(feature = "samd21j18a")]
use atsamd21j18a as target_device;
mod channel;
mod types;
mod descriptors;
pub mod storage;
#[allow(unused_imports)]
use core::u32;
#[allow(unused_imports)]
use core::u16;
use target_device::DMAC;
use typenum::consts::*;
use typenum::{Unsigned, IsLess};
use storage::DmaStorage;
pub use self::channel::*;
pub use self::types::*;
pub use self::descriptors::*;
pub use typenum::consts;
pub struct DMAController<T: 'static + DmaStorage> {
#[cfg(feature = "samd5x")]
channels: u32,
#[cfg(feature = "samd21")]
channels: u16,
storage: &'static mut T,
dmac: DMAC,
}
impl<T: 'static + DmaStorage> DMAController<T> {
pub fn init(dmac: DMAC, storage: &'static mut T) -> DMAController<T> {
DMAController {
#[cfg(feature = "samd21")]
channels: u16::MAX >> 16 - T::Index::U16,
#[cfg(feature = "samd5x")]
channels: u32::MAX >> 32 - T::Index::U32,
storage,
dmac
}
}
pub fn disable(&mut self) {
self.dmac.ctrl.modify(|_, w| w.dmaenable().clear_bit());
}
pub fn enable(&mut self) {
self.dmac.ctrl.modify(|r, w| if r.dmaenable().bit_is_clear() {
w.dmaenable().set_bit()
} else {
w
});
}
pub fn is_enabled(&self) -> bool {
self.dmac.ctrl.read().dmaenable().bit_is_set()
}
pub fn take_channel<U: Unsigned>(&mut self) -> Option<Channel> where U: IsLess<T::Index, Output = True>{
if self.channels & (1 << U::USIZE) == 0 {
None
} else {
self.channels |= 1 << U::USIZE;
unsafe {
Some(Channel::new(U::U8,
self.storage.baseaddr().offset(U::to_isize()) as *mut TransferDescriptor,
self.storage.wbaddr().offset(U::to_isize()) as *mut TransferDescriptor))
}
}
}
pub fn return_channel(&mut self, mut channel: Channel) {
channel.disable();
channel.reset();
self.channels &= !(1 << channel.id());
}
pub fn enable_priority_level(&mut self, level: Priority) {
self.set_priority_level(level, true);
}
pub fn diable_priority_level(&mut self, level: Priority) {
self.set_priority_level(level, false);
}
fn set_priority_level(&mut self, level: Priority, value: bool) {
self.dmac.ctrl.modify(|_, w| match level {
Priority::Level0 => w.lvlen0().bit(value),
Priority::Level1 => w.lvlen1().bit(value),
Priority::Level2 => w.lvlen2().bit(value),
Priority::Level3 => w.lvlen3().bit(value),
})
}
pub fn priority_level_enabled(&self, level: Priority) -> bool {
match level {
Priority::Level0 => self.dmac.ctrl.read().lvlen0().bit(),
Priority::Level1 => self.dmac.ctrl.read().lvlen1().bit(),
Priority::Level2 => self.dmac.ctrl.read().lvlen2().bit(),
Priority::Level3 => self.dmac.ctrl.read().lvlen3().bit(),
}
}
pub fn set_priority_level_scheduling(&mut self, level: Priority, enable: bool) {
self.dmac.prictrl0.modify(|_, w| match level {
Priority::Level0 => w.rrlvlen0().bit(enable),
Priority::Level1 => w.rrlvlen1().bit(enable),
Priority::Level2 => w.rrlvlen2().bit(enable),
Priority::Level3 => w.rrlvlen3().bit(enable),
})
}
}