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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use core::convert::TryInto;
use crate::{
peripherals::{
flash::Flash,
},
typestates::init_state::Enabled,
traits::{
flash::{
Error,
Result,
Read,
WriteErase,
},
},
};
use generic_array::{
GenericArray,
typenum::{U16, U512},
};
#[cfg(feature = "littlefs")]
use generic_array::typenum::{U256, U1022};
#[cfg(feature = "littlefs")]
use littlefs2::io::Result as LfsResult;
pub const READ_SIZE: usize = 16;
pub const WRITE_SIZE: usize = 512;
pub const PAGE_SIZE: usize = 512;
pub struct FlashGordon {
flash: Flash<Enabled>,
}
impl FlashGordon {
pub fn new(flash: Flash<Enabled>) -> Self {
flash.raw.event.write(|w| w.rst().set_bit());
while flash.raw.int_status.read().done().bit_is_clear() {}
debug_assert!(flash.raw.int_status.read().err().bit_is_clear());
debug_assert!(flash.raw.int_status.read().fail().bit_is_clear());
FlashGordon {
flash,
}
}
fn clear_status(&self) {
self.flash.raw.int_clr_status.write(|w| w
.done().set_bit()
.ecc_err().set_bit()
.err().set_bit()
.fail().set_bit()
);
}
fn status(&self) -> Result {
let status = self.flash.raw.int_status.read();
if status.err().bit_is_set() {
return Err(Error::Illegal);
}
if status.ecc_err().bit_is_set() {
return Err(Error::EccError);
}
if status.fail().bit_is_set() {
return Err(Error::Failure);
}
Ok(())
}
pub fn just_program_at(
&mut self,
address: usize,
) -> Result {
let flash = &self.flash.raw;
assert!(flash.int_status.read().done().bit_is_set());
self.clear_status();
flash.event.write(|w| w.rst().set_bit());
while flash.int_status.read().done().bit_is_clear() {}
self.status()?;
self.clear_status();
flash.starta.write(|w| unsafe { w.starta().bits((address >> 4) as u32) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Program as u32) });
while flash.int_status.read().done().bit_is_clear() {}
debug_assert!(flash.int_status.read().err().bit_is_clear());
debug_assert!(flash.int_status.read().fail().bit_is_clear());
self.status()?;
Ok(())
}
pub fn clear_page_register(&mut self) {
let flash = &self.flash.raw;
assert!(flash.int_status.read().done().bit_is_set());
self.clear_status();
for i in 0..32 {
for j in 0..4 {
flash.dataw[j].write(|w| unsafe { w.bits(0x0) });
}
flash.starta.write(|w| unsafe { w.starta().bits(i as u32) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Write as u32) });
while flash.int_status.read().done().bit_is_clear() {}
debug_assert!(flash.int_status.read().err().bit_is_clear());
debug_assert!(flash.int_status.read().fail().bit_is_clear());
assert!(self.status().is_ok());
}
}
pub fn write_u8(&mut self, address: usize, byte: u8) -> Result {
self.clear_page_register();
let flash = &self.flash.raw;
let page_register_column = (address & (512 - 1)) >> 4;
let mut word = [0u8; 4];
word[address % 4] = byte;
for j in 0..4 {
flash.dataw[j].write(|w| unsafe { w.bits(0) });
}
flash.dataw[(address >> 2) % 4].write(|w| unsafe { w.bits(u32::from_ne_bytes(word)) });
flash.starta.write(|w| unsafe { w.starta().bits(page_register_column as u32) } );
self.clear_status();
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Write as u32) });
while flash.int_status.read().done().bit_is_clear() {}
self.status()?;
self.clear_status();
flash.starta.write(|w| unsafe { w.starta().bits((address >> 4) as u32) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Program as u32) });
while flash.int_status.read().done().bit_is_clear() {}
self.status()?;
Ok(())
}
pub fn write_u32(&mut self, address: usize, word: u32) -> Result {
self.clear_page_register();
let flash = &self.flash.raw;
let page_register_column = (address & (512 - 1)) >> 4;
for j in 0..4 {
flash.dataw[j].write(|w| unsafe { w.bits(0) });
}
flash.dataw[(address >> 2) % 4].write(|w| unsafe { w.bits(word) });
flash.starta.write(|w| unsafe { w.starta().bits(page_register_column as u32) } );
self.clear_status();
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Write as u32) });
while flash.int_status.read().done().bit_is_clear() {}
self.status()?;
self.clear_status();
flash.starta.write(|w| unsafe { w.starta().bits((address >> 4) as u32) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Program as u32) });
while flash.int_status.read().done().bit_is_clear() {}
self.status()?;
Ok(())
}
pub fn write_u128(&mut self, address: usize, data: u128) -> Result {
let flash = &self.flash.raw;
let buf: [u8; 16] = data.to_ne_bytes();
for (i, chunk) in buf.chunks(4).enumerate() {
flash.dataw[i].write(|w| unsafe { w.bits(u32::from_ne_bytes(chunk.try_into().unwrap())) } );
}
flash.starta.write(|w| unsafe { w.starta().bits((address >> 4) as u32) } );
self.clear_status();
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Write as u32) });
while flash.int_status.read().done().bit_is_clear() {}
self.status()?;
self.clear_status();
flash.starta.write(|w| unsafe { w.starta().bits((address >> 4) as u32) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Program as u32) });
while flash.int_status.read().done().bit_is_clear() {}
self.status()?;
Ok(())
}
pub fn read_u128(&mut self, address: usize) -> u128 {
let mut buf = [0u8; 16];
self.read(address, &mut buf);
u128::from_ne_bytes(buf)
}
}
#[allow(dead_code)]
#[repr(C)]
enum FlashCommands {
Init = 0x0,
PowerDown = 0x1,
SetReadMode = 0x2,
ReadSingleWord = 0x3,
EraseRange = 0x4,
BlankCheck = 0x5,
MarginCheck = 0x6,
Checksum = 0x7,
Write = 0x8,
WriteProgram = 0xA,
Program = 0xC,
ReportEcc= 0xD,
}
#[cfg(feature = "littlefs")]
#[allow(non_camel_case_types)]
pub mod littlefs_params {
use super::*;
pub const BASE_OFFSET: usize = 0x0008_0000;
pub const READ_SIZE: usize = 16;
pub const WRITE_SIZE: usize = 512;
pub const BLOCK_SIZE: usize = 512;
pub const BLOCK_COUNT: usize = 256;
pub const BLOCK_CYCLES: isize = -1;
pub type CACHE_SIZE = U512;
pub type LOOKAHEADWORDS_SIZE = U16;
pub type FILENAME_MAX_PLUS_ONE = U256;
pub type PATH_MAX_PLUS_ONE = U256;
pub const FILEBYTES_MAX: usize = littlefs2::ll::LFS_FILE_MAX as _;
pub type ATTRBYTES_MAX = U1022;
}
#[cfg(feature = "littlefs")]
impl littlefs2::driver::Storage for FlashGordon {
const READ_SIZE: usize = littlefs_params::READ_SIZE;
const WRITE_SIZE: usize = littlefs_params::WRITE_SIZE;
const BLOCK_SIZE: usize = littlefs_params::BLOCK_SIZE;
const BLOCK_COUNT: usize = littlefs_params::BLOCK_COUNT;
const BLOCK_CYCLES: isize = littlefs_params::BLOCK_CYCLES;
type CACHE_SIZE = littlefs_params::CACHE_SIZE;
type LOOKAHEADWORDS_SIZE = littlefs_params::LOOKAHEADWORDS_SIZE;
type FILENAME_MAX_PLUS_ONE = littlefs_params::FILENAME_MAX_PLUS_ONE;
type PATH_MAX_PLUS_ONE = littlefs_params::PATH_MAX_PLUS_ONE;
const FILEBYTES_MAX: usize = littlefs_params::FILEBYTES_MAX;
type ATTRBYTES_MAX = littlefs_params::ATTRBYTES_MAX;
fn read(&self, off: usize, buf: &mut [u8]) -> LfsResult<usize> {
<Self as Read<U16>>::read(self, littlefs_params::BASE_OFFSET + off, buf);
Ok(buf.len())
}
fn write(&mut self, off: usize, data: &[u8]) -> LfsResult<usize> {
let ret = <Self as WriteErase<U512, U512>>::write(self, littlefs_params::BASE_OFFSET + off, data);
ret
.map(|_| data.len())
.map_err(|_| littlefs2::io::Error::Io)
}
fn erase(&mut self, off: usize, len: usize) -> LfsResult<usize> {
let first_page = (littlefs_params::BASE_OFFSET + off) / 512;
let pages = len / 512;
for i in 0..pages {
<Self as WriteErase<U512, U512>>::erase_page(self, first_page + i)
.map_err(|_| littlefs2::io::Error::Io)?;
}
Ok(512 * len)
}
}
impl Read<U16> for FlashGordon {
fn read_native(&self, address: usize, array: &mut GenericArray<u8, U16>) {
let flash = &self.flash.raw;
assert!(flash.int_status.read().done().bit_is_set());
self.clear_status();
assert!(self.status().is_ok());
let addr = address as u32;
debug_assert!(addr & (READ_SIZE as u32 - 1) == 0);
flash.starta.write(|w| unsafe { w.starta().bits(addr >> 4) } );
flash.dataw[0].write(|w| unsafe { w.bits(0) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::ReadSingleWord as u32) });
while flash.int_status.read().done().bit_is_clear() { continue; }
assert!(flash.int_status.read().err().bit_is_clear());
debug_assert!(flash.int_status.read().fail().bit_is_clear());
for (i, chunk) in array.chunks_mut(4).enumerate() {
chunk.copy_from_slice(&flash.dataw[i].read().bits().to_ne_bytes());
}
}
}
impl WriteErase<U512, U512> for FlashGordon {
fn status(&self) -> Result {
self.status()
}
fn erase_page(&mut self, page: usize) -> Result {
let starta = page * 32;
let flash = &self.flash.raw;
assert!(flash.int_status.read().done().bit_is_set());
self.clear_status();
assert!(flash.int_status.read().done().bit_is_clear());
flash.starta.write(|w| unsafe { w.starta().bits(starta as u32) } );
flash.stopa.write(|w| unsafe { w.stopa().bits(starta as u32) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::EraseRange as u32) });
while flash.int_status.read().done().bit_is_clear() {}
debug_assert!(flash.int_status.read().err().bit_is_clear());
debug_assert!(flash.int_status.read().fail().bit_is_clear());
self.status()?;
Ok(())
}
fn write_native(
&mut self,
address: usize,
array: &GenericArray<u8, U512>,
) -> Result {
let flash = &self.flash.raw;
assert!(flash.int_status.read().done().bit_is_set());
self.clear_status();
for (i, chunk) in array.chunks(16).enumerate() {
let starta = (address >> 4) + i;
flash.starta.write(|w| unsafe { w.starta().bits(starta as u32) } );
for (j, word) in chunk.chunks(4).enumerate() {
flash.dataw[j].write(|w| unsafe { w.bits(
u32::from_ne_bytes(word.try_into().unwrap())
) } );
}
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Write as u32) });
while flash.int_status.read().done().bit_is_clear() {}
debug_assert!(flash.int_status.read().err().bit_is_clear());
debug_assert!(flash.int_status.read().fail().bit_is_clear());
self.status()?;
}
self.clear_status();
let starta = address >> 4;
flash.starta.write(|w| unsafe { w.starta().bits(starta as u32) } );
flash.cmd.write(|w| unsafe { w.bits(FlashCommands::Program as u32) });
while flash.int_status.read().done().bit_is_clear() {}
debug_assert!(flash.int_status.read().err().bit_is_clear());
debug_assert!(flash.int_status.read().fail().bit_is_clear());
self.status()?;
Ok(())
}
}