embedded_c_sdk_bind_hal/
spi.rs

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
pub use crate::ll_api::{SpiBusBitOrder, SpiBusDataSize, SpiBusId, SpiBusMode};
use crate::{ll_api::ll_cmd::*, tick::Delay};
use core::{cmp::min, ptr};
use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::Operation};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Error {
    Code(i32),
}

/// Configuration structure for initializing a SPI (Serial Peripheral Interface) bus.
///
/// This struct holds the necessary parameters to configure a SPI bus before it can be used for communication.
///
/// # Fields
/// * `baudrate` - The baud rate at which the SPI bus operates, specifying the speed of the serial communication.
/// * `mode` - The SPI bus mode, determining the clock polarity and phase for data transmission and reception.
/// * `size` - The data size per word transferred over the SPI bus, typically 8 or 16 bits.
/// * `order` - The bit order for data transmission, either most significant bit first (MSB) or least significant bit first (LSB).
pub struct Config {
    pub baudrate: u32,
    pub mode: SpiBusMode,
    pub size: SpiBusDataSize,
    pub order: SpiBusBitOrder,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            baudrate: 8_000_000,
            mode: SpiBusMode::Mode0,
            size: SpiBusDataSize::DataSize8,
            order: SpiBusBitOrder::MsbFirst,
        }
    }
}

#[derive(Clone, Debug)]
pub struct SpiBus {
    bus: SpiBusId,
}

impl SpiBus {
    /// Initializes a new SPI bus with the given configuration.
    ///
    /// # Arguments
    /// * `bus` - The identifier for the SPI bus.
    /// * `config` - A reference to the configuration parameters for the SPI bus.
    ///
    /// # Returns
    /// A new `SpiBus` instance configured according to the provided parameters.
    pub fn new(bus: SpiBusId, config: &Config) -> Self {
        let flags = config.size as u32 | config.order as u32;
        ll_invoke_inner!(INVOKE_ID_SPI_INIT, bus, config.mode, flags, config.baudrate);
        SpiBus { bus }
    }

    /// Converts the SPI bus into a SPI device by associating it with a chip select pin.
    ///
    /// # Arguments
    /// * `nss` - The chip select pin configured as an output.
    ///
    /// # Returns
    /// A `SpiDevice` instance ready to communicate with a specific device.
    pub fn to_device<NSS: OutputPin>(self, nss: NSS) -> SpiDevice<NSS> {
        SpiDevice { bus: self, nss }
    }

    /// Performs a blocking read operation on the SPI bus.
    ///
    /// # Arguments
    /// * `words` - A mutable slice where the read data will be stored.
    ///
    /// # Returns
    /// The number of bytes read or an error code.
    fn blocking_read(&mut self, words: &mut [u8]) -> i32 {
        ll_invoke_inner!(
            INVOKE_ID_SPI_BLOCKING_RW,
            self.bus,
            ptr::null::<u8>(),
            words.as_mut_ptr(),
            words.len()
        )
    }

    /// Performs a blocking write operation on the SPI bus.
    ///
    /// # Arguments
    /// * `words` - A slice containing the data to be written.
    ///
    /// # Returns
    /// The number of bytes written or an error code.
    fn blocking_write(&mut self, words: &[u8]) -> i32 {
        ll_invoke_inner!(
            INVOKE_ID_SPI_BLOCKING_RW,
            self.bus,
            words.as_ptr(),
            ptr::null::<u8>(),
            words.len()
        )
    }

    /// Performs a blocking transfer operation on the SPI bus.
    ///
    /// # Arguments
    /// * `read` - A mutable slice where the received data will be stored.
    /// * `write` - A slice containing the data to be sent.
    ///
    /// # Returns
    /// The number of bytes transferred or an error code.
    fn blocking_transfer(&mut self, read: &mut [u8], write: &[u8]) -> i32 {
        let size = min(read.len(), write.len());
        ll_invoke_inner!(
            INVOKE_ID_SPI_BLOCKING_RW,
            self.bus,
            write.as_ptr(),
            read.as_ptr(),
            size
        )
    }

    /// Performs a blocking transfer operation in place on the SPI bus.
    ///
    /// # Arguments
    /// * `words` - A mutable slice for both sending and receiving data.
    ///
    /// # Returns
    /// The number of bytes transferred or an error code.
    fn blocking_transfer_in_place(&mut self, words: &mut [u8]) -> i32 {
        let rw_ptr = words.as_ptr();
        ll_invoke_inner!(
            INVOKE_ID_SPI_BLOCKING_RW,
            self.bus,
            rw_ptr,
            rw_ptr,
            words.len()
        )
    }
}

impl Drop for SpiBus {
    fn drop(&mut self) {
        ll_invoke_inner!(INVOKE_ID_SPI_DEINIT, self.bus);
    }
}

impl embedded_hal::spi::ErrorType for SpiBus {
    type Error = Error;
}

impl embedded_hal::spi::SpiBus for SpiBus {
    fn flush(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }

    fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
        let result = self.blocking_read(words);
        if result == 0 {
            return Ok(());
        }
        return Err(Error::Code(result));
    }

    fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
        let result = self.blocking_write(words);
        if result == 0 {
            return Ok(());
        }
        return Err(Error::Code(result));
    }

    fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
        let result = self.blocking_transfer(read, write);
        if result == 0 {
            return Ok(());
        }
        return Err(Error::Code(result));
    }

    fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
        let result = self.blocking_transfer_in_place(words);
        if result == 0 {
            return Ok(());
        }
        return Err(Error::Code(result));
    }
}

impl embedded_hal::spi::Error for Error {
    fn kind(&self) -> embedded_hal::spi::ErrorKind {
        match *self {
            Self::Code(_) => embedded_hal::spi::ErrorKind::Other,
        }
    }
}

pub struct SpiDevice<NSS> {
    bus: SpiBus,
    nss: NSS,
}

impl<NSS: OutputPin> SpiDevice<NSS> {
    /// Creates a new `SpiDevice` instance.
    ///
    /// # Arguments
    /// * `bus` - A reference to the SPI bus this device will communicate over.
    /// * `nss` - A pin configured as an output, used as the chip select line for the device.
    ///
    /// # Returns
    /// A new `SpiDevice` instance ready to communicate with the device over the provided SPI bus.
    pub fn new(bus: SpiBus, nss: NSS) -> Self {
        SpiDevice { bus, nss }
    }
}

impl<NSS: OutputPin> embedded_hal::spi::ErrorType for SpiDevice<NSS> {
    type Error = Error;
}

impl<NSS: OutputPin> embedded_hal::spi::SpiDevice for SpiDevice<NSS> {
    fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> {
        let mut result = Ok(());

        self.nss.set_low().ok();
        for op in operations {
            match op {
                Operation::Read(words) => {
                    let ret = self.bus.blocking_read(words);
                    if ret != 0 {
                        result = Err(Error::Code(ret));
                    }
                }
                Operation::Write(words) => {
                    let ret = self.bus.blocking_write(words);
                    if ret != 0 {
                        result = Err(Error::Code(ret));
                    }
                }
                Operation::Transfer(rd_words, wr_words) => {
                    let ret = self.bus.blocking_transfer(rd_words, wr_words);
                    if ret != 0 {
                        result = Err(Error::Code(ret));
                    }
                }
                Operation::TransferInPlace(words) => {
                    let ret = self.bus.blocking_transfer_in_place(words);
                    if ret != 0 {
                        result = Err(Error::Code(ret));
                    }
                }
                Operation::DelayNs(ns) => {
                    let mut delay = Delay::new();
                    delay.delay_ns(*ns);
                }
            }
        }
        self.nss.set_high().ok();

        result
    }
}