embedded_sdmmc/
blockdevice.rs1#[derive(Clone)]
14pub struct Block {
15 pub contents: [u8; Block::LEN],
17}
18
19#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
24#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
25pub struct BlockIdx(pub u32);
26
27#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
31#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
32pub struct BlockCount(pub u32);
33
34pub struct BlockIter {
36 inclusive_end: BlockIdx,
37 current: BlockIdx,
38}
39
40pub trait BlockDevice {
43 type Error: core::fmt::Debug;
45 fn read(
47 &self,
48 blocks: &mut [Block],
49 start_block_idx: BlockIdx,
50 reason: &str,
51 ) -> Result<(), Self::Error>;
52 fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
54 fn num_blocks(&self) -> Result<BlockCount, Self::Error>;
56}
57
58impl Block {
59 pub const LEN: usize = 512;
63
64 pub const LEN_U32: u32 = 512;
66
67 pub fn new() -> Block {
69 Block {
70 contents: [0u8; Self::LEN],
71 }
72 }
73}
74
75impl Default for Block {
76 fn default() -> Self {
77 Self::new()
78 }
79}
80
81impl core::ops::Add<BlockCount> for BlockIdx {
82 type Output = BlockIdx;
83 fn add(self, rhs: BlockCount) -> BlockIdx {
84 BlockIdx(self.0 + rhs.0)
85 }
86}
87
88impl core::ops::AddAssign<BlockCount> for BlockIdx {
89 fn add_assign(&mut self, rhs: BlockCount) {
90 self.0 += rhs.0
91 }
92}
93
94impl core::ops::Add<BlockCount> for BlockCount {
95 type Output = BlockCount;
96 fn add(self, rhs: BlockCount) -> BlockCount {
97 BlockCount(self.0 + rhs.0)
98 }
99}
100
101impl core::ops::AddAssign<BlockCount> for BlockCount {
102 fn add_assign(&mut self, rhs: BlockCount) {
103 self.0 += rhs.0
104 }
105}
106
107impl core::ops::Sub<BlockCount> for BlockIdx {
108 type Output = BlockIdx;
109 fn sub(self, rhs: BlockCount) -> BlockIdx {
110 BlockIdx(self.0 - rhs.0)
111 }
112}
113
114impl core::ops::SubAssign<BlockCount> for BlockIdx {
115 fn sub_assign(&mut self, rhs: BlockCount) {
116 self.0 -= rhs.0
117 }
118}
119
120impl core::ops::Sub<BlockCount> for BlockCount {
121 type Output = BlockCount;
122 fn sub(self, rhs: BlockCount) -> BlockCount {
123 BlockCount(self.0 - rhs.0)
124 }
125}
126
127impl core::ops::SubAssign<BlockCount> for BlockCount {
128 fn sub_assign(&mut self, rhs: BlockCount) {
129 self.0 -= rhs.0
130 }
131}
132
133impl core::ops::Deref for Block {
134 type Target = [u8; 512];
135 fn deref(&self) -> &[u8; 512] {
136 &self.contents
137 }
138}
139
140impl core::ops::DerefMut for Block {
141 fn deref_mut(&mut self) -> &mut [u8; 512] {
142 &mut self.contents
143 }
144}
145
146impl core::fmt::Debug for Block {
147 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
148 writeln!(fmt, "Block:")?;
149 for line in self.contents.chunks(32) {
150 for b in line {
151 write!(fmt, "{:02x}", b)?;
152 }
153 write!(fmt, " ")?;
154 for &b in line {
155 if (0x20..=0x7F).contains(&b) {
156 write!(fmt, "{}", b as char)?;
157 } else {
158 write!(fmt, ".")?;
159 }
160 }
161 writeln!(fmt)?;
162 }
163 Ok(())
164 }
165}
166
167impl BlockIdx {
168 pub fn into_bytes(self) -> u64 {
172 (u64::from(self.0)) * (Block::LEN as u64)
173 }
174
175 pub fn range(self, num: BlockCount) -> BlockIter {
178 BlockIter::new(self, self + BlockCount(num.0))
179 }
180}
181
182impl BlockCount {
183 pub const fn from_bytes(byte_count: u32) -> BlockCount {
194 let mut count = byte_count / Block::LEN_U32;
195 if (count * Block::LEN_U32) != byte_count {
196 count += 1;
197 }
198 BlockCount(count)
199 }
200
201 pub fn offset_bytes(self, offset: u32) -> Self {
204 BlockCount(self.0 + (offset / Block::LEN_U32))
205 }
206}
207
208impl BlockIter {
209 pub const fn new(start: BlockIdx, inclusive_end: BlockIdx) -> BlockIter {
212 BlockIter {
213 inclusive_end,
214 current: start,
215 }
216 }
217}
218
219impl core::iter::Iterator for BlockIter {
220 type Item = BlockIdx;
221 fn next(&mut self) -> Option<Self::Item> {
222 if self.current.0 >= self.inclusive_end.0 {
223 None
224 } else {
225 let this = self.current;
226 self.current += BlockCount(1);
227 Some(this)
228 }
229 }
230}
231
232