# spi.rs - source
Source of the Rust file `/home/runner/.cargo/git/checkouts/k210-hal-1069b16e3180bc3a/795ac3f/src/spi.rs`.
Keywords: rust, rustlang, rust-lang

    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

    //! (TODO) Serial Peripheral Interface (SPI)

    use crate::pac::SPI0;
    use crate::clock::Clocks;
    use crate::sysctl::{self, APB0};
    pub use embedded_hal::spi::{Mode, Polarity, Phase};
    use core::convert::Infallible;

    ///
    pub struct Spi<SPI> {
        spi: SPI
    }

    impl Spi<SPI0> {
        pub fn spi0(
            spi: SPI0,
            mode: Mode,
            frame_format: FrameFormat,
            endian: Endian,
            clock: &Clocks,
            apb0: &mut APB0
        ) -> Self {
            let work_mode = hal_mode_to_pac(mode);
            let frame_format = frame_format_to_pac(frame_format);
            let tmod = crate::pac::spi0::ctrlr0::TMOD_A::TRANS_RECV; // todo other modes
            let endian = endian as u32;
            let data_bit_length = 8; // todo more length
            let _ = clock; // todo
            unsafe {
                // no interrupts for now
                spi.imr.write(|w| w.bits(0x00));
                // no dma for now
                spi.dmacr.write(|w| w.bits(0x00));
                spi.dmatdlr.write(|w| w.bits(0x10));
                spi.dmardlr.write(|w| w.bits(0x00));
                // no slave access for now
                spi.ser.write(|w| w.bits(0x00));
                spi.ssienr.write(|w| w.bits(0x00));
                // set control registers
                spi.ctrlr0.write(|w| {
                    w.work_mode()
                        .variant(work_mode)
                        .tmod()
                        .variant(tmod)
                        .frame_format()
                        .variant(frame_format)
                        .data_length()
                        .bits(data_bit_length - 1)
                });
                spi.spi_ctrlr0.reset(); // standard
                spi.endian.write(|w| w.bits(endian));
            }
            // enable APB0 bus
            apb0.enable();
            // enable peripheral via sysctl
            sysctl::clk_en_peri().modify(|_r, w|
                w.spi0_clk_en().set_bit());
            Spi { spi }
        }

        pub fn release(self) -> SPI0 {
            // power off
            sysctl::clk_en_peri().modify(|_r, w|
                w.spi0_clk_en().clear_bit());
            self.spi
        }
    }

    impl embedded_hal::spi::FullDuplex<u8> for Spi<SPI0> {
        /// An enumeration of SPI errors
        type Error = Infallible;

        /// Reads the word stored in the shift register
        ///
        /// **NOTE** A word must be sent to the slave before attempting to call this
        /// method.
        fn try_read(&mut self) -> nb::Result<u8, Self::Error> {
            todo!()
        }

        /// Sends a word to the slave
        fn try_send(&mut self, word: u8) -> nb::Result<(), Self::Error> {
            todo!("{}", word)
        }
    }

    #[derive(Clone, Copy, PartialEq, Eq)]
    pub enum FrameFormat {
        Standard,
        Dual,
        Quad,
        Octal,
    }
    #[derive(Clone, Copy, PartialEq, Eq)]
    #[repr(u32)]
    pub enum Endian {
        Little = 0,
        Big = 1,
    }

    #[inline]
    fn hal_mode_to_pac(mode: Mode) -> crate::pac::spi0::ctrlr0::WORK_MODE_A {
        use crate::pac::spi0::ctrlr0::WORK_MODE_A;
        use {Polarity::*, Phase::*};
        match (mode.polarity, mode.phase) {
            (IdleLow, CaptureOnFirstTransition) => WORK_MODE_A::MODE0,
            (IdleLow, CaptureOnSecondTransition) => WORK_MODE_A::MODE1,
            (IdleHigh, CaptureOnFirstTransition) => WORK_MODE_A::MODE2,
            (IdleHigh, CaptureOnSecondTransition) => WORK_MODE_A::MODE3,
        }
    }

    #[inline]
    fn frame_format_to_pac(frame_format: FrameFormat) -> crate::pac::spi0::ctrlr0::FRAME_FORMAT_A {
        use crate::pac::spi0::ctrlr0::FRAME_FORMAT_A;
        match frame_format {
            FrameFormat::Standard => FRAME_FORMAT_A::STANDARD,
            FrameFormat::Dual => FRAME_FORMAT_A::DUAL,
            FrameFormat::Quad => FRAME_FORMAT_A::QUAD,
            FrameFormat::Octal => FRAME_FORMAT_A::OCTAL,
        }
    }