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
use core::convert::Infallible;

use embedded_hal::{
    blocking::spi::{Operation, Transactional, Transfer, Write, WriteIter},
    spi::FullDuplex,
};

use crate::spi::SpiConfig;

use super::{Pins, SpiBus, SpiX};

/// SPI exclusive device abstraction
pub struct SpiExclusiveDevice<SPI, PINS> {
    bus: SpiBus<SPI, PINS>,
}

impl<SPI, PINS> SpiExclusiveDevice<SPI, PINS>
where
    SPI: SpiX,
    PINS: Pins<SPI>,
{
    /// Create [SpiExclusiveDevice] using the existing [SpiBus](super::SpiBus)
    /// with the given [SpiConfig]
    pub fn new(mut bus: SpiBus<SPI, PINS>, config: &SpiConfig) -> Self
    where
        PINS: Pins<SPI>,
    {
        bus.configure(config, PINS::CS_INDEX);

        Self { bus }
    }

    /// Releases the Bus back deconstructing it
    pub fn release(self) -> (SPI, PINS) {
        self.bus.release()
    }
}

impl<SPI, PINS> FullDuplex<u8> for SpiExclusiveDevice<SPI, PINS>
where
    SPI: SpiX,
    PINS: Pins<SPI>,
{
    type Error = Infallible;

    fn read(&mut self) -> nb::Result<u8, Infallible> {
        self.bus.read()
    }

    fn send(&mut self, byte: u8) -> nb::Result<(), Infallible> {
        self.bus.send(byte)
    }
}

impl<SPI, PINS> Transfer<u8> for SpiExclusiveDevice<SPI, PINS>
where
    SPI: SpiX,
    PINS: Pins<SPI>,
{
    type Error = Infallible;

    fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
        self.bus.start_frame();
        let result = self.bus.transfer(words);
        self.bus.end_frame();

        result
    }
}

impl<SPI, PINS> Write<u8> for SpiExclusiveDevice<SPI, PINS>
where
    SPI: SpiX,
    PINS: Pins<SPI>,
{
    type Error = Infallible;

    fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
        self.bus.start_frame();
        let result = self.bus.write(words);
        self.bus.end_frame();

        result
    }
}

impl<SPI, PINS> WriteIter<u8> for SpiExclusiveDevice<SPI, PINS>
where
    SPI: SpiX,
    PINS: Pins<SPI>,
{
    type Error = Infallible;

    fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
    where
        WI: IntoIterator<Item = u8>,
    {
        self.bus.start_frame();
        let result = self.bus.write_iter(words);
        self.bus.end_frame();

        result
    }
}

impl<SPI, PINS> Transactional<u8> for SpiExclusiveDevice<SPI, PINS>
where
    SPI: SpiX,
    PINS: Pins<SPI>,
{
    type Error = Infallible;

    fn exec<'op>(&mut self, operations: &mut [Operation<'op, u8>]) -> Result<(), Infallible> {
        self.bus.start_frame();
        let result = self.bus.exec(operations);
        self.bus.end_frame();

        result
    }
}