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
use core::cell::Cell;
use core::cmp;
use kernel::ReturnCode;
use kernel::common::take_cell::TakeCell;
use kernel::hil;
pub static mut TXBUFFER: [u8; 512] = [0; 512];
pub static mut RXBUFFER: [u8; 512] = [0; 512];
pub static mut KERNEL_TXBUFFER: [u8; 512] = [0; 512];
pub static mut KERNEL_RXBUFFER: [u8; 512] = [0; 512];
const SPI_SPEED: u32 = 4000000;
#[allow(dead_code)]
enum Opcodes {
WriteEnable = 0x06,
WriteDisable = 0x04,
ReadStatusRegister = 0x05,
WriteStatusRegister = 0x01,
ReadMemory = 0x03,
WriteMemory = 0x02,
}
#[derive(Clone, Copy, PartialEq)]
enum State {
Idle,
ReadStatus,
WriteEnable,
WriteMemory,
ReadMemory,
}
pub trait FM25CLCustom {
fn read_status(&self) -> ReturnCode;
}
pub trait FM25CLClient {
fn status(&self, status: u8);
fn read(&self, data: &'static mut [u8], len: usize);
fn done(&self, buffer: &'static mut [u8]);
}
pub struct FM25CL<'a, S: hil::spi::SpiMasterDevice + 'a> {
spi: &'a S,
state: Cell<State>,
txbuffer: TakeCell<'static, [u8]>,
rxbuffer: TakeCell<'static, [u8]>,
client: Cell<Option<&'static hil::nonvolatile_storage::NonvolatileStorageClient>>,
client_custom: Cell<Option<&'static FM25CLClient>>,
client_buffer: TakeCell<'static, [u8]>,
client_write_address: Cell<u16>,
client_write_len: Cell<u16>,
}
impl<'a, S: hil::spi::SpiMasterDevice + 'a> FM25CL<'a, S> {
pub fn new(
spi: &'a S,
txbuffer: &'static mut [u8],
rxbuffer: &'static mut [u8],
) -> FM25CL<'a, S> {
FM25CL {
spi: spi,
state: Cell::new(State::Idle),
txbuffer: TakeCell::new(txbuffer),
rxbuffer: TakeCell::new(rxbuffer),
client: Cell::new(None),
client_custom: Cell::new(None),
client_buffer: TakeCell::empty(),
client_write_address: Cell::new(0),
client_write_len: Cell::new(0),
}
}
pub fn set_client<C: FM25CLClient>(&self, client: &'static C) {
self.client_custom.set(Some(client));
}
fn configure_spi(&self) {
self.spi.configure(
hil::spi::ClockPolarity::IdleLow,
hil::spi::ClockPhase::SampleLeading,
SPI_SPEED,
);
}
pub fn write(&self, address: u16, buffer: &'static mut [u8], len: u16) -> ReturnCode {
self.configure_spi();
self.txbuffer
.take()
.map_or(ReturnCode::ERESERVE, move |txbuffer| {
txbuffer[0] = Opcodes::WriteEnable as u8;
let write_len = cmp::min(txbuffer.len(), len as usize);
self.client_buffer.replace(buffer);
self.client_write_address.set(address);
self.client_write_len.set(write_len as u16);
self.state.set(State::WriteEnable);
self.spi.read_write_bytes(txbuffer, None, 1)
})
}
pub fn read(&self, address: u16, buffer: &'static mut [u8], len: u16) -> ReturnCode {
self.configure_spi();
self.txbuffer
.take()
.map_or(ReturnCode::ERESERVE, |txbuffer| {
self.rxbuffer
.take()
.map_or(ReturnCode::ERESERVE, move |rxbuffer| {
txbuffer[0] = Opcodes::ReadMemory as u8;
txbuffer[1] = ((address >> 8) & 0xFF) as u8;
txbuffer[2] = (address & 0xFF) as u8;
self.client_buffer.replace(buffer);
let read_len = cmp::min(rxbuffer.len() - 3, len as usize);
self.state.set(State::ReadMemory);
self.spi
.read_write_bytes(txbuffer, Some(rxbuffer), read_len + 3)
})
})
}
}
impl<'a, S: hil::spi::SpiMasterDevice + 'a> hil::spi::SpiMasterClient for FM25CL<'a, S> {
fn read_write_done(
&self,
write_buffer: &'static mut [u8],
read_buffer: Option<&'static mut [u8]>,
len: usize,
) {
match self.state.get() {
State::ReadStatus => {
self.state.set(State::Idle);
self.txbuffer.replace(write_buffer);
read_buffer.map(|read_buffer| {
let status = read_buffer[1];
self.rxbuffer.replace(read_buffer);
self.client_custom.get().map(|client| client.status(status));
});
}
State::WriteEnable => {
self.state.set(State::WriteMemory);
self.client_buffer.map(move |buffer| {
write_buffer[0] = Opcodes::WriteMemory as u8;
write_buffer[1] = ((self.client_write_address.get() >> 8) & 0xFF) as u8;
write_buffer[2] = (self.client_write_address.get() & 0xFF) as u8;
let write_len =
cmp::min(write_buffer.len(), self.client_write_len.get() as usize);
for i in 0..write_len {
write_buffer[(i + 3) as usize] = buffer[i as usize];
}
self.spi
.read_write_bytes(write_buffer, read_buffer, write_len + 3);
});
}
State::WriteMemory => {
self.state.set(State::Idle);
let write_len = cmp::min(write_buffer.len(), self.client_write_len.get() as usize);
self.txbuffer.replace(write_buffer);
read_buffer.map(|read_buffer| {
self.rxbuffer.replace(read_buffer);
});
self.client_buffer.take().map(move |buffer| {
self.client
.get()
.map(move |client| client.write_done(buffer, write_len));
});
}
State::ReadMemory => {
self.state.set(State::Idle);
self.txbuffer.replace(write_buffer);
read_buffer.map(|read_buffer| {
self.client_buffer.take().map(move |buffer| {
let read_len = cmp::min(buffer.len(), len);
for i in 0..(read_len - 3) {
buffer[i] = read_buffer[i + 3];
}
self.rxbuffer.replace(read_buffer);
self.client
.get()
.map(move |client| client.read_done(buffer, read_len - 3));
});
});
}
_ => {}
}
}
}
impl<'a, S: hil::spi::SpiMasterDevice + 'a> FM25CLCustom for FM25CL<'a, S> {
fn read_status(&self) -> ReturnCode {
self.configure_spi();
self.txbuffer
.take()
.map_or(ReturnCode::ERESERVE, |txbuffer| {
self.rxbuffer
.take()
.map_or(ReturnCode::ERESERVE, move |rxbuffer| {
txbuffer[0] = Opcodes::ReadStatusRegister as u8;
self.spi.read_write_bytes(txbuffer, Some(rxbuffer), 4);
self.state.set(State::ReadStatus);
ReturnCode::SUCCESS
})
})
}
}
impl<'a, S: hil::spi::SpiMasterDevice + 'a> hil::nonvolatile_storage::NonvolatileStorage
for FM25CL<'a, S> {
fn set_client(&self, client: &'static hil::nonvolatile_storage::NonvolatileStorageClient) {
self.client.set(Some(client));
}
fn read(&self, buffer: &'static mut [u8], address: usize, length: usize) -> ReturnCode {
self.read(address as u16, buffer, length as u16)
}
fn write(&self, buffer: &'static mut [u8], address: usize, length: usize) -> ReturnCode {
self.write(address as u16, buffer, length as u16)
}
}