Skip to content

Commit b4342ee

Browse files
committed
DMA module
1 parent f322f11 commit b4342ee

15 files changed

+1629
-853
lines changed

src/dma/desc.rs

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/dma/generic_ring.rs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
use volatile_register::{RO, RW};
2+
3+
use crate::dma::MTU;
4+
5+
#[cfg(all(not(feature = "stm32f1xx-hal"), feature = "f-series"))]
6+
pub(crate) const DESC_SIZE: usize = 8;
7+
8+
#[cfg(feature = "stm32f1xx-hal")]
9+
pub(crate) const DESC_SIZE: usize = 4;
10+
11+
#[cfg(feature = "stm32h7xx-hal")]
12+
pub(crate) const DESC_SIZE: usize = 4;
13+
14+
#[repr(C)]
15+
#[repr(align(4))]
16+
#[derive(Clone, Copy)]
17+
pub struct RawDescriptor {
18+
pub(crate) desc: [u32; DESC_SIZE],
19+
}
20+
21+
impl Default for RawDescriptor {
22+
fn default() -> Self {
23+
Self::new()
24+
}
25+
}
26+
27+
impl RawDescriptor {
28+
pub const fn new() -> Self {
29+
Self {
30+
desc: [0; DESC_SIZE],
31+
}
32+
}
33+
34+
fn r(&self, n: usize) -> &RO<u32> {
35+
let ro = &self.desc[n] as *const _ as *const RO<u32>;
36+
unsafe { &*ro }
37+
}
38+
39+
unsafe fn rw(&mut self, n: usize) -> &mut RW<u32> {
40+
let rw = &mut self.desc[n] as *mut _ as *mut RW<u32>;
41+
&mut *rw
42+
}
43+
44+
pub fn read(&self, n: usize) -> u32 {
45+
self.r(n).read()
46+
}
47+
48+
pub unsafe fn write(&mut self, n: usize, value: u32) {
49+
self.rw(n).write(value)
50+
}
51+
52+
pub unsafe fn modify<F>(&mut self, n: usize, f: F)
53+
where
54+
F: FnOnce(u32) -> u32,
55+
{
56+
self.rw(n).modify(f)
57+
}
58+
}
59+
60+
pub struct DescriptorRing<'data, T> {
61+
descriptors: &'data mut [T],
62+
buffers: &'data mut [[u8; MTU + 2]],
63+
}
64+
65+
impl<'data, T> DescriptorRing<'data, T> {
66+
pub fn new(descriptors: &'data mut [T], buffers: &'data mut [[u8; MTU + 2]]) -> Self {
67+
assert!(descriptors.len() == buffers.len());
68+
69+
Self {
70+
descriptors,
71+
buffers,
72+
}
73+
}
74+
75+
pub fn len(&self) -> usize {
76+
self.descriptors.len()
77+
}
78+
79+
pub fn descriptor(&self, index: usize) -> &T {
80+
&self.descriptors[index]
81+
}
82+
83+
pub fn get(&self, index: usize) -> (&T, &[u8]) {
84+
(&self.descriptors[index], &self.buffers[index])
85+
}
86+
87+
pub fn get_mut(&mut self, index: usize) -> (&mut T, &mut [u8]) {
88+
(&mut self.descriptors[index], &mut self.buffers[index])
89+
}
90+
91+
pub fn get_mut_and_next(&mut self, index: usize) -> (&mut T, &mut [u8], &mut T, &mut [u8]) {
92+
let next = (index + 1) % self.len();
93+
94+
macro_rules! mut_and_next {
95+
($array:expr) => {{
96+
let (index_slice, next_slice) = if next == 0 {
97+
let (next, index) = $array.split_at_mut(1);
98+
(index, next)
99+
} else {
100+
$array.split_at_mut(next)
101+
};
102+
103+
(&mut index_slice[index_slice.len() - 1], &mut next_slice[0])
104+
}};
105+
}
106+
107+
let (desc_index, desc_next) = mut_and_next!(self.descriptors);
108+
let (buf_index, buf_next) = mut_and_next!(self.buffers);
109+
110+
(desc_index, buf_index, desc_next, buf_next)
111+
}
112+
113+
pub fn descriptors_mut(&mut self) -> impl Iterator<Item = &mut T> {
114+
self.descriptors.iter_mut()
115+
}
116+
117+
pub fn descriptors(&self) -> impl Iterator<Item = &T> {
118+
self.descriptors.iter()
119+
}
120+
121+
pub fn last_descriptor_mut(&mut self) -> &mut T {
122+
&mut self.descriptors[self.descriptors.len() - 1]
123+
}
124+
125+
pub fn last_descriptor(&self) -> &T {
126+
&self.descriptors[self.descriptors.len() - 1]
127+
}
128+
129+
pub fn first_buffer(&self) -> &[u8] {
130+
&self.buffers[0]
131+
}
132+
133+
pub fn last_buffer(&self) -> &[u8] {
134+
&self.buffers[self.buffers.len() - 1]
135+
}
136+
137+
pub fn descriptors_and_buffers(
138+
&mut self,
139+
) -> impl Iterator<Item = (&mut T, &mut [u8; MTU + 2])> {
140+
self.descriptors.iter_mut().zip(self.buffers.iter_mut())
141+
}
142+
143+
pub fn descriptors_start_address(&self) -> *const T {
144+
self.descriptors.as_ptr()
145+
}
146+
}

0 commit comments

Comments
 (0)