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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Virtualize a SPI master bus to enable multiple users of the SPI bus.

use core::cell::Cell;
use kernel::collections::list::{List, ListLink, ListNode};
use kernel::deferred_call::{DeferredCall, DeferredCallClient};
use kernel::hil;
use kernel::hil::spi::SpiMasterClient;
use kernel::utilities::cells::{MapCell, OptionalCell};
use kernel::utilities::leasable_buffer::SubSliceMut;
use kernel::ErrorCode;

/// The Mux struct manages multiple Spi clients. Each client may have
/// at most one outstanding Spi request.
pub struct MuxSpiMaster<'a, Spi: hil::spi::SpiMaster<'a>> {
    spi: &'a Spi,
    devices: List<'a, VirtualSpiMasterDevice<'a, Spi>>,
    inflight: OptionalCell<&'a VirtualSpiMasterDevice<'a, Spi>>,
    deferred_call: DeferredCall,
}

impl<'a, Spi: hil::spi::SpiMaster<'a>> hil::spi::SpiMasterClient for MuxSpiMaster<'a, Spi> {
    fn read_write_done(
        &self,
        write_buffer: SubSliceMut<'static, u8>,
        read_buffer: Option<SubSliceMut<'static, u8>>,
        status: Result<usize, ErrorCode>,
    ) {
        let dev = self.inflight.take();
        // Need to do next op before signaling so we get some kind of
        // sharing. Otherwise a call to read_write in the callback
        // can allow this client to never relinquish the device.
        // -pal 7/30/21
        self.do_next_op();
        dev.map(move |device| {
            device.read_write_done(write_buffer, read_buffer, status);
        });
    }
}

impl<'a, Spi: hil::spi::SpiMaster<'a>> MuxSpiMaster<'a, Spi> {
    pub fn new(spi: &'a Spi) -> Self {
        Self {
            spi,
            devices: List::new(),
            inflight: OptionalCell::empty(),
            deferred_call: DeferredCall::new(),
        }
    }

    fn do_next_op(&self) {
        if self.inflight.is_none() {
            let mnode = self
                .devices
                .iter()
                .find(|node| node.operation.get() != Op::Idle);
            mnode.map(|node| {
                let configuration = node.configuration.get();
                let cs = configuration.chip_select;
                let _ = self.spi.specify_chip_select(cs);

                let op = node.operation.get();
                // Need to set idle here in case callback changes state
                node.operation.set(Op::Idle);
                match op {
                    Op::ReadWriteBytes => {
                        // Only async operations want to block by setting
                        // the devices as inflight.
                        self.inflight.set(node);
                        node.txbuffer.take().map(|txbuffer| {
                            let rresult = self.spi.set_rate(configuration.rate);
                            let polresult = self.spi.set_polarity(configuration.polarity);
                            let phaseresult = self.spi.set_phase(configuration.phase);
                            if rresult.is_err() || polresult.is_err() || phaseresult.is_err() {
                                node.txbuffer.replace(txbuffer);
                                node.operation.set(Op::ReadWriteDone(Err(ErrorCode::INVAL)));
                                self.do_next_op_async();
                            } else {
                                let rxbuffer = node.rxbuffer.take();
                                if let Err((e, write_buffer, read_buffer)) =
                                    self.spi.read_write_bytes(txbuffer, rxbuffer)
                                {
                                    node.txbuffer.replace(write_buffer);
                                    read_buffer.map(|buffer| {
                                        node.rxbuffer.replace(buffer);
                                    });
                                    node.operation.set(Op::ReadWriteDone(Err(e)));
                                    self.do_next_op_async();
                                }
                            }
                        });
                    }
                    Op::ReadWriteDone(status) => {
                        node.txbuffer.take().map(|write_buffer| {
                            let read_buffer = node.rxbuffer.take();
                            self.read_write_done(write_buffer, read_buffer, status);
                        });
                    }
                    Op::Idle => {} // Can't get here...
                }
            });
        } else {
            self.inflight.map(|node| {
                match node.operation.get() {
                    // we have to report an error
                    Op::ReadWriteDone(status) => {
                        node.txbuffer.take().map(|write_buffer| {
                            let read_buffer = node.rxbuffer.take();
                            self.read_write_done(write_buffer, read_buffer, status);
                        });
                    }
                    _ => {} // Something is really in flight
                }
            });
        }
    }

    /// Asynchronously executes the next operation, if any. Used by calls
    /// to trigger do_next_op such that it will execute after the call
    /// returns. This is important in case the operation triggers an error,
    /// requiring a callback with an error condition; if the operation
    /// is executed synchronously, the callback may be reentrant (executed
    /// during the downcall). Please see
    /// <https://github.com/tock/tock/issues/1496>
    fn do_next_op_async(&self) {
        self.deferred_call.set();
    }
}

impl<'a, Spi: hil::spi::SpiMaster<'a>> DeferredCallClient for MuxSpiMaster<'a, Spi> {
    fn handle_deferred_call(&self) {
        self.do_next_op();
    }

    fn register(&'static self) {
        self.deferred_call.register(self);
    }
}

#[derive(Copy, Clone, PartialEq)]
enum Op {
    Idle,
    ReadWriteBytes,
    ReadWriteDone(Result<usize, ErrorCode>),
}

// Structure used to store the SPI configuration of a client/virtual device,
// so it can restored on each operation.
struct SpiConfiguration<'a, Spi: hil::spi::SpiMaster<'a>> {
    chip_select: Spi::ChipSelect,
    polarity: hil::spi::ClockPolarity,
    phase: hil::spi::ClockPhase,
    rate: u32,
}

// Have to do this manually because otherwise the Copy and Clone are parameterized
// by Spi::ChipSelect and don't work for Cells.
// https://stackoverflow.com/questions/63132174/how-do-i-fix-the-method-clone-exists-but-the-following-trait-bounds-were-not
impl<'a, Spi: hil::spi::SpiMaster<'a>> Copy for SpiConfiguration<'a, Spi> {}
impl<'a, Spi: hil::spi::SpiMaster<'a>> Clone for SpiConfiguration<'a, Spi> {
    fn clone(&self) -> SpiConfiguration<'a, Spi> {
        *self
    }
}

pub struct VirtualSpiMasterDevice<'a, Spi: hil::spi::SpiMaster<'a>> {
    mux: &'a MuxSpiMaster<'a, Spi>,
    configuration: Cell<SpiConfiguration<'a, Spi>>,
    txbuffer: MapCell<SubSliceMut<'static, u8>>,
    rxbuffer: MapCell<SubSliceMut<'static, u8>>,
    operation: Cell<Op>,
    next: ListLink<'a, VirtualSpiMasterDevice<'a, Spi>>,
    client: OptionalCell<&'a dyn hil::spi::SpiMasterClient>,
}

impl<'a, Spi: hil::spi::SpiMaster<'a>> VirtualSpiMasterDevice<'a, Spi> {
    pub fn new(
        mux: &'a MuxSpiMaster<'a, Spi>,
        chip_select: Spi::ChipSelect,
    ) -> VirtualSpiMasterDevice<'a, Spi> {
        VirtualSpiMasterDevice {
            mux,
            configuration: Cell::new(SpiConfiguration {
                chip_select,
                polarity: hil::spi::ClockPolarity::IdleLow,
                phase: hil::spi::ClockPhase::SampleLeading,
                rate: 100_000,
            }),
            txbuffer: MapCell::empty(),
            rxbuffer: MapCell::empty(),
            operation: Cell::new(Op::Idle),
            next: ListLink::empty(),
            client: OptionalCell::empty(),
        }
    }

    /// Must be called right after `static_init!()`.
    pub fn setup(&'a self) {
        self.mux.devices.push_head(self);
    }
}

impl<'a, Spi: hil::spi::SpiMaster<'a>> hil::spi::SpiMasterClient
    for VirtualSpiMasterDevice<'a, Spi>
{
    fn read_write_done(
        &self,
        write_buffer: SubSliceMut<'static, u8>,
        read_buffer: Option<SubSliceMut<'static, u8>>,
        status: Result<usize, ErrorCode>,
    ) {
        self.client.map(move |client| {
            client.read_write_done(write_buffer, read_buffer, status);
        });
    }
}

impl<'a, Spi: hil::spi::SpiMaster<'a>> ListNode<'a, VirtualSpiMasterDevice<'a, Spi>>
    for VirtualSpiMasterDevice<'a, Spi>
{
    fn next(&'a self) -> &'a ListLink<'a, VirtualSpiMasterDevice<'a, Spi>> {
        &self.next
    }
}

impl<'a, Spi: hil::spi::SpiMaster<'a>> hil::spi::SpiMasterDevice<'a>
    for VirtualSpiMasterDevice<'a, Spi>
{
    fn set_client(&self, client: &'a dyn SpiMasterClient) {
        self.client.set(client);
    }

    fn configure(
        &self,
        cpol: hil::spi::ClockPolarity,
        cpal: hil::spi::ClockPhase,
        rate: u32,
    ) -> Result<(), ErrorCode> {
        if self.operation.get() == Op::Idle {
            let mut configuration = self.configuration.get();
            configuration.polarity = cpol;
            configuration.phase = cpal;
            configuration.rate = rate;
            self.configuration.set(configuration);
            Ok(())
        } else {
            Err(ErrorCode::BUSY)
        }
    }

    fn read_write_bytes(
        &self,
        write_buffer: SubSliceMut<'static, u8>,
        mut read_buffer: Option<SubSliceMut<'static, u8>>,
    ) -> Result<
        (),
        (
            ErrorCode,
            SubSliceMut<'static, u8>,
            Option<SubSliceMut<'static, u8>>,
        ),
    > {
        if self.operation.get() == Op::Idle {
            self.txbuffer.replace(write_buffer);
            if let Some(rb) = read_buffer.take() {
                self.rxbuffer.put(rb);
            }
            self.operation.set(Op::ReadWriteBytes);
            self.mux.do_next_op();
            Ok(())
        } else {
            Err((ErrorCode::BUSY, write_buffer, read_buffer))
        }
    }

    fn set_polarity(&self, cpol: hil::spi::ClockPolarity) -> Result<(), ErrorCode> {
        if self.operation.get() == Op::Idle {
            let mut configuration = self.configuration.get();
            configuration.polarity = cpol;
            self.configuration.set(configuration);
            Ok(())
        } else {
            Err(ErrorCode::BUSY)
        }
    }

    fn set_phase(&self, cpal: hil::spi::ClockPhase) -> Result<(), ErrorCode> {
        if self.operation.get() == Op::Idle {
            let mut configuration = self.configuration.get();
            configuration.phase = cpal;
            self.configuration.set(configuration);
            Ok(())
        } else {
            Err(ErrorCode::BUSY)
        }
    }

    fn set_rate(&self, rate: u32) -> Result<(), ErrorCode> {
        if self.operation.get() == Op::Idle {
            let mut configuration = self.configuration.get();
            configuration.rate = rate;
            self.configuration.set(configuration);
            Ok(())
        } else {
            Err(ErrorCode::BUSY)
        }
    }

    fn get_polarity(&self) -> hil::spi::ClockPolarity {
        self.configuration.get().polarity
    }

    fn get_phase(&self) -> hil::spi::ClockPhase {
        self.configuration.get().phase
    }

    fn get_rate(&self) -> u32 {
        self.configuration.get().rate
    }
}

pub struct SpiSlaveDevice<'a, Spi: hil::spi::SpiSlave<'a>> {
    spi: &'a Spi,
    client: OptionalCell<&'a dyn hil::spi::SpiSlaveClient>,
}

impl<'a, Spi: hil::spi::SpiSlave<'a>> SpiSlaveDevice<'a, Spi> {
    pub const fn new(spi: &'a Spi) -> SpiSlaveDevice<'a, Spi> {
        SpiSlaveDevice {
            spi,
            client: OptionalCell::empty(),
        }
    }
}

impl<'a, Spi: hil::spi::SpiSlave<'a>> hil::spi::SpiSlaveClient for SpiSlaveDevice<'a, Spi> {
    fn read_write_done(
        &self,
        write_buffer: Option<&'static mut [u8]>,
        read_buffer: Option<&'static mut [u8]>,
        len: usize,
        status: Result<(), ErrorCode>,
    ) {
        self.client.map(move |client| {
            client.read_write_done(write_buffer, read_buffer, len, status);
        });
    }

    fn chip_selected(&self) {
        self.client.map(move |client| {
            client.chip_selected();
        });
    }
}

impl<'a, Spi: hil::spi::SpiSlave<'a>> hil::spi::SpiSlaveDevice<'a> for SpiSlaveDevice<'a, Spi> {
    fn set_client(&self, client: &'a dyn hil::spi::SpiSlaveClient) {
        self.client.set(client);
    }

    fn configure(
        &self,
        cpol: hil::spi::ClockPolarity,
        cpal: hil::spi::ClockPhase,
    ) -> Result<(), ErrorCode> {
        self.spi.set_polarity(cpol)?;
        self.spi.set_phase(cpal)
    }

    fn read_write_bytes(
        &self,
        write_buffer: Option<&'static mut [u8]>,
        read_buffer: Option<&'static mut [u8]>,
        len: usize,
    ) -> Result<
        (),
        (
            ErrorCode,
            Option<&'static mut [u8]>,
            Option<&'static mut [u8]>,
        ),
    > {
        self.spi.read_write_bytes(write_buffer, read_buffer, len)
    }

    fn set_polarity(&self, cpol: hil::spi::ClockPolarity) -> Result<(), ErrorCode> {
        self.spi.set_polarity(cpol)
    }

    fn set_phase(&self, cpal: hil::spi::ClockPhase) -> Result<(), ErrorCode> {
        self.spi.set_phase(cpal)
    }

    fn get_polarity(&self) -> hil::spi::ClockPolarity {
        self.spi.get_polarity()
    }

    fn get_phase(&self) -> hil::spi::ClockPhase {
        self.spi.get_phase()
    }
}