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
use core::mem::transmute;
use vcell::VolatileCell;
use target::SPIFI;
use target::spifi;
use target::spifi::cmd;
pub struct FlashInterface {
spifi: SPIFI,
}
impl FlashInterface {
pub fn new(spifi: SPIFI) -> Self {
FlashInterface { spifi }
}
fn wait(&self) {
while self.spifi.stat.read()
.cmd().bit_is_set() {}
}
pub fn set_addr(&self, addr: u32) {
self.spifi.addr.write(|w| unsafe {
w.address().bits(addr)
})
}
pub fn command(
&self,
opcode: u8, data: &mut [u8], dout: cmd::DOUTW, fieldform: cmd::FIELDFORMW, intlen: u8, frameform: cmd::FRAMEFORMW
) {
let datalen = data.len() as u16;
self.spifi.cmd.write(
|w| unsafe {
w
.datalen().bits(datalen)
.dout().variant(dout)
.fieldform().variant(fieldform)
.intlen().bits(intlen)
.frameform().variant(frameform)
.opcode().bits(opcode)
}
);
let data_byte = unsafe {
transmute::<&spifi::DATA, &VolatileCell<u8>>(&self.spifi.data)
};
for d in data.iter_mut() {
*d = data_byte.get();
}
self.wait();
}
}