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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use core::{
    marker::PhantomData,
    ops::{Deref, DerefMut},
    pin::Pin,
    ptr,
};

use as_slice::{AsMutSlice, AsSlice};
use embedded_time::rate::Hertz;

use crate::dma::{self, Buffer};

use crate::gpio::gpioa::*;
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
use crate::gpio::gpiob::*;

use crate::gpio::{AltMode, Analog};
use crate::hal;
use crate::pac::SPI1;
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
use crate::pac::SPI2;
use crate::rcc::{Enable, Rcc};

pub use hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};

/// SPI error
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    Busy,
    FrameError,
    /// Overrun occurred
    Overrun,
    /// Mode fault occurred
    ModeFault,
    /// CRC error
    Crc,
}

pub trait Pins<SPI> {
    fn setup(&self);
}
pub trait PinSck<SPI> {
    fn setup(&self);
}
pub trait PinMiso<SPI> {
    fn setup(&self);
}
pub trait PinMosi<SPI> {
    fn setup(&self);
}

impl<SPI, SCK, MISO, MOSI> Pins<SPI> for (SCK, MISO, MOSI)
where
    SCK: PinSck<SPI>,
    MISO: PinMiso<SPI>,
    MOSI: PinMosi<SPI>,
{
    fn setup(&self) {
        self.0.setup();
        self.1.setup();
        self.2.setup();
    }
}

/// A filler type for when the SCK pin is unnecessary
pub struct NoSck;
impl NoSck {
    fn set_alt_mode(&self, _some: Option<u32>) {}
}
/// A filler type for when the Miso pin is unnecessary
pub struct NoMiso;
impl NoMiso {
    fn set_alt_mode(&self, _some: Option<u32>) {}
}
/// A filler type for when the Mosi pin is unnecessary
pub struct NoMosi;
impl NoMosi {
    fn set_alt_mode(&self, _some: Option<u32>) {}
}

macro_rules! pins {
    ($($SPIX:ty:
        SCK: [$([$SCK:ty, $ALTMODESCK:path]),*]
        MISO: [$([$MISO:ty, $ALTMODEMISO:path]),*]
        MOSI: [$([$MOSI:ty, $ALTMODEMOSI:path]),*])+) => {
        $(
            $(
                impl PinSck<$SPIX> for $SCK {
                    fn setup(&self) {
                        self.set_alt_mode($ALTMODESCK);
                    }
                }
            )*
            $(
                impl PinMiso<$SPIX> for $MISO {
                    fn setup(&self) {
                        self.set_alt_mode($ALTMODEMISO);
                    }
                }
            )*
            $(
                impl PinMosi<$SPIX> for $MOSI {
                    fn setup(&self) {
                        self.set_alt_mode($ALTMODEMOSI);
                    }
                }
            )*
        )+
    }
}

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
pins! {
    SPI1:
        SCK: [
            [NoSck, None],
            [PB3<Analog>, AltMode::AF0],
            [PA5<Analog>, AltMode::AF0]
        ]
        MISO: [
            [NoMiso, None],
            [PA6<Analog>, AltMode::AF0],
            [PA11<Analog>, AltMode::AF0],
            [PB4<Analog>, AltMode::AF0]
        ]
        MOSI: [
            [NoMosi, None],
            [PA7<Analog>, AltMode::AF0],
            [PA12<Analog>, AltMode::AF0],
            [PB5<Analog>, AltMode::AF0]
        ]
}

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
pins! {
    SPI2:
        SCK: [
            [NoSck, None],
            [PB13<Analog>, AltMode::AF0]
        ]
        MISO: [
            [NoMiso, None],
            [PB14<Analog>, AltMode::AF0]
        ]
        MOSI: [
            [NoMosi, None],
            [PB15<Analog>, AltMode::AF0]
        ]
}

#[cfg(feature = "stm32l0x1")]
pins! {
    SPI1:
        SCK: [
            [NoSck, None],
            [PA5<Analog>, AltMode::AF0]
        ]
        MISO: [
            [NoMiso, None],
            [PA6<Analog>, AltMode::AF0]
        ]
        MOSI: [
            [NoMosi, None],
            [PA7<Analog>, AltMode::AF0]
        ]
}

#[derive(Debug)]
pub struct Spi<SPI, PINS> {
    spi: SPI,
    pins: PINS,
}

pub trait SpiExt<SPI>: Sized {
    fn spi<PINS, T>(self, pins: PINS, mode: Mode, freq: T, rcc: &mut Rcc) -> Spi<SPI, PINS>
    where
        PINS: Pins<SPI>,
        T: Into<Hertz>;
}

macro_rules! spi {
    ($($SPIX:ident: ($spiX:ident, $pclkX:ident),)+) => {
        $(
            impl<PINS> Spi<$SPIX, PINS> {
                pub fn $spiX<T>(
                    spi: $SPIX,
                    pins: PINS,
                    mode: Mode,
                    freq: T,
                    rcc: &mut Rcc
                ) -> Self
                where
                PINS: Pins<$SPIX>,
                T: Into<Hertz>
                {
                    pins.setup();

                    // Enable clock for SPI
                    <$SPIX>::enable(rcc);

                    spi.cr2.write(|w| {
                        // disable SS output
                        w.ssoe().clear_bit();
                        // enable DMA reception
                        w.rxdmaen().set_bit();
                        // enable DMA transmission
                        w.txdmaen().set_bit()
                    });

                    let spi_freq = freq.into().0;
                    let apb_freq = rcc.clocks.$pclkX().0;
                    let br = match apb_freq / spi_freq {
                        0 => unreachable!(),
                        1..=2 => 0b000,
                        3..=5 => 0b001,
                        6..=11 => 0b010,
                        12..=23 => 0b011,
                        24..=47 => 0b100,
                        48..=95 => 0b101,
                        96..=191 => 0b110,
                        _ => 0b111,
                    };

                    // mstr: master configuration
                    // lsbfirst: MSB first
                    // ssm: enable software slave management (NSS pin free for other uses)
                    // ssi: set nss high = master mode
                    // dff: 8 bit frames
                    // bidimode: 2-line unidirectional
                    // spe: enable the SPI bus
                    #[allow(unused)]
                    spi.cr1.write(|w| unsafe {
                        w.cpha()
                            .bit(mode.phase == Phase::CaptureOnSecondTransition)
                            .cpol()
                            .bit(mode.polarity == Polarity::IdleHigh)
                            .mstr()
                            .set_bit()
                            .br()
                            .bits(br)
                            .lsbfirst()
                            .clear_bit()
                            .ssm()
                            .set_bit()
                            .ssi()
                            .set_bit()
                            .rxonly()
                            .clear_bit()
                            .dff()
                            .clear_bit()
                            .bidimode()
                            .clear_bit()
                            .spe()
                            .set_bit()
                    });

                    Spi { spi, pins }
                }

                pub fn free(self) -> ($SPIX, PINS) {
                    (self.spi, self.pins)
                }

                pub fn read_all<Channel, Buffer>(
                    self,
                    dma:     &mut dma::Handle,
                    channel: Channel,
                    buffer:  Pin<Buffer>,
                ) -> Transfer<Self, Rx<$SPIX>, Channel, Buffer, dma::Ready>
                    where
                        Rx<$SPIX>:      dma::Target<Channel>,
                        Channel:        dma::Channel,
                        Buffer:         DerefMut + 'static,
                        Buffer::Target: AsMutSlice<Element=u8>,
                {
                    let num_words = buffer.len();
                    self.read_some(dma, channel, buffer, num_words)
                }

                pub fn read_some<Channel, Buffer>(
                    self,
                    dma:     &mut dma::Handle,
                    channel: Channel,
                    buffer:  Pin<Buffer>,
                    num_words: usize,
                ) -> Transfer<Self, Rx<$SPIX>, Channel, Buffer, dma::Ready>
                    where
                        Rx<$SPIX>:      dma::Target<Channel>,
                        Channel:        dma::Channel,
                        Buffer:         DerefMut + 'static,
                        Buffer::Target: AsMutSlice<Element=u8>,
                {
                    let token = Rx(PhantomData);
                    let address = &unsafe { &*$SPIX::ptr() }.dr as *const _ as u32;
                    // Safe, because the trait bounds of this method guarantee that the
                    // buffer can be written to.
                    let inner = unsafe {
                        dma::Transfer::new(
                            dma,
                            token,
                            channel,
                            buffer,
                            num_words,
                            address,
                            dma::Priority::high(),
                            dma::Direction::peripheral_to_memory(),
                            false,
                        )
                    };
                    Transfer {
                        target: self,
                        inner,
                    }
                }

                pub fn write_all<Channel, Buffer>(
                    self,
                    dma:     &mut dma::Handle,
                    channel: Channel,
                    buffer:  Pin<Buffer>,
                ) -> Transfer<Self, Tx<$SPIX>, Channel, Buffer, dma::Ready>
                    where
                        Tx<$SPIX>:      dma::Target<Channel>,
                        Channel:        dma::Channel,
                        Buffer:         Deref + 'static,
                        Buffer::Target: AsSlice<Element=u8>,
                {
                    let num_words = buffer.len();
                    self.write_some(dma, channel, buffer, num_words)
                }

                pub fn write_some<Channel, Buffer>(
                    self,
                    dma:     &mut dma::Handle,
                    channel: Channel,
                    buffer:  Pin<Buffer>,
                    num_words: usize,
                ) -> Transfer<Self, Tx<$SPIX>, Channel, Buffer, dma::Ready>
                    where
                        Tx<$SPIX>:      dma::Target<Channel>,
                        Channel:        dma::Channel,
                        Buffer:         Deref + 'static,
                        Buffer::Target: AsSlice<Element=u8>,
                {
                    let token = Tx(PhantomData);
                    let address = &unsafe { &*$SPIX::ptr() }.dr as *const _ as u32;
                    // Safe, because the trait bounds of this method guarantee that the
                    // buffer can be written to.
                    let inner = unsafe {
                        dma::Transfer::new(
                            dma,
                            token,
                            channel,
                            buffer,
                            num_words,
                            address,
                            dma::Priority::high(),
                            dma::Direction::memory_to_peripheral(),
                            false,
                        )
                    };
                    Transfer {
                        target: self,
                        inner,
                    }
                }
            }

            impl SpiExt<$SPIX> for $SPIX {
                fn spi<PINS, T>(self, pins: PINS, mode: Mode, freq: T, rcc: &mut Rcc) -> Spi<$SPIX, PINS>
                where
                    PINS: Pins<$SPIX>,
                    T: Into<Hertz>
                    {
                        Spi::$spiX(self, pins, mode, freq, rcc)
                    }
            }

            impl<PINS> hal::spi::FullDuplex<u8> for Spi<$SPIX, PINS> {
                type Error = Error;

                fn read(&mut self) -> nb::Result<u8, Error> {
                    let sr = self.spi.sr.read();

                    Err(if sr.ovr().bit_is_set() {
                        nb::Error::Other(Error::Overrun)
                    } else if sr.modf().bit_is_set() {
                        nb::Error::Other(Error::ModeFault)
                    } else if sr.crcerr().bit_is_set() {
                        nb::Error::Other(Error::Crc)
                    } else if sr.rxne().bit_is_set() {
                        // NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
                        // reading a half-word)
                        return Ok(unsafe {
                            ptr::read_volatile(&self.spi.dr as *const _ as *const u8)
                        });
                    } else {
                        nb::Error::WouldBlock
                    })
                }

                fn send(&mut self, byte: u8) -> nb::Result<(), Error> {
                    let sr = self.spi.sr.read();

                    Err(if sr.ovr().bit_is_set() {
                        nb::Error::Other(Error::Overrun)
                    } else if sr.modf().bit_is_set() {
                        nb::Error::Other(Error::ModeFault)
                    } else if sr.crcerr().bit_is_set() {
                        nb::Error::Other(Error::Crc)
                    } else if sr.txe().bit_is_set() {
                        // NOTE(write_volatile) see note above
                        unsafe { ptr::write_volatile(&self.spi.dr as *const _ as *mut u8, byte) }
                        return Ok(());
                    } else {
                        nb::Error::WouldBlock
                    })
                }

            }

            impl<PINS> crate::hal::blocking::spi::transfer::Default<u8> for Spi<$SPIX, PINS> {}

            impl<PINS> crate::hal::blocking::spi::write::Default<u8> for Spi<$SPIX, PINS> {}
        )+
    }
}

spi! {
    SPI1: (spi1, apb2_clk),
}

#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
spi! {
    SPI2: (spi2, apb1_clk),
}

/// Token used for DMA transfers
///
/// This is an implementation detail. The user doesn't have to deal with this
/// directly.
pub struct Tx<I>(PhantomData<I>);

/// Token used for DMA transfers
///
/// This is an implementation detail. The user doesn't have to deal with this
/// directly.
pub struct Rx<I>(PhantomData<I>);

/// Wrapper around a [`dma::Transfer`].
pub struct Transfer<Target, Token, Channel, Buffer, State> {
    target: Target,
    inner: dma::Transfer<Token, Channel, Buffer, State>,
}

impl<Target, Token, Channel, Buffer> Transfer<Target, Token, Channel, Buffer, dma::Ready>
where
    Token: dma::Target<Channel>,
    Channel: dma::Channel,
{
    /// Enables the provided interrupts
    ///
    /// This setting only affects this transfer. It doesn't affect transfer on
    /// other channels, or subsequent transfers on the same channel.
    pub fn enable_interrupts(&mut self, interrupts: dma::Interrupts) {
        self.inner.enable_interrupts(interrupts);
    }

    /// Start the DMA transfer
    ///
    /// Consumes this instance of `Transfer` and returns a new one, with its
    /// state changed to indicate that the transfer has been started.
    pub fn start(self) -> Transfer<Target, Token, Channel, Buffer, dma::Started> {
        Transfer {
            target: self.target,
            inner: self.inner.start(),
        }
    }
}

impl<Target, Token, Channel, Buffer> Transfer<Target, Token, Channel, Buffer, dma::Started>
where
    Channel: dma::Channel,
{
    /// Indicates whether the transfer is still ongoing
    pub fn is_active(&self) -> bool {
        self.inner.is_active()
    }

    /// Waits for the transfer to finish and returns the owned resources
    ///
    /// This function will busily wait until the transfer is finished. If you
    /// don't want this, please call this function only once you know that the
    /// transfer has finished.
    ///
    /// This function will return immediately, if [`Transfer::is_active`]
    /// returns `false`.
    pub fn wait(self) -> dma::TransferResourcesResult<Target, Channel, Buffer> {
        // Need to move `target` out of `self`, otherwise the closure captures
        // `self` completely.
        let target = self.target;

        let map_resources = |res: dma::TransferResources<_, _, _>| dma::TransferResources {
            target,
            channel: res.channel,
            buffer: res.buffer,
        };

        match self.inner.wait() {
            Ok(res) => Ok(map_resources(res)),
            Err((res, err)) => Err((map_resources(res), err)),
        }
    }
}