stm32_rust_template/driver/spi/
mod.rs1#![allow(dead_code)]
10
11use bitflags::bitflags;
12use core::ops::FnMut;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum Mode {
17 Inactive,
18 Master,
19 Slave,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum FrameFormat {
25 CPOL0_CPHA0,
27 CPOL0_CPHA1,
29 CPOL1_CPHA0,
31 CPOL1_CPHA1,
33 TI_SSI,
35 Microwire,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum BitOrder {
42 MSB_LSB,
44 LSB_MSB,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum SlaveSelectMode {
51 MasterUnused,
53 MasterSoftware,
55 MasterHwOutput,
57 MasterHwInput,
59 SlaveHardware,
61 SlaveSoftware,
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub struct Status {
68 pub busy: bool,
70 pub data_lost: bool,
72 pub mode_fault: bool,
74}
75
76bitflags! {
77 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
79 pub struct Event: u32 {
80 const TRANSFER_COMPLETE = (1 << 0);
82 const DATA_LOST = (1 << 1);
84 const MODE_FAULT = (1 << 2);
86 }
87}
88
89pub type Error = i32;
91
92pub type Result<T> = core::result::Result<T, Error>;
94
95#[derive(Debug, Clone, PartialEq, Eq)]
97pub struct Config {
98 pub mode: Mode,
99 pub frame_format: FrameFormat,
100 pub bit_order: BitOrder,
101 pub slave_select_mode: SlaveSelectMode,
102 pub bus_speed_hz: u32,
103 pub data_bits: u8,
104}
105
106impl Default for Config {
107 fn default() -> Self {
108 Self {
109 mode: Mode::Master,
110 frame_format: FrameFormat::CPOL0_CPHA0,
111 bit_order: BitOrder::MSB_LSB,
112 slave_select_mode: SlaveSelectMode::MasterHwOutput,
113 bus_speed_hz: 1_000_000, data_bits: 8,
115 }
116 }
117}
118
119pub trait Spi<'a> {
121 fn initialize(&mut self, callback: impl FnMut(Event) + 'a) -> Result<()>;
123
124 fn uninitialize(&mut self) -> Result<()>;
126
127 fn configure(&mut self, config: &Config) -> Result<()>;
129
130 fn send(&mut self, data: &[u8]) -> Result<()>;
132
133 fn receive(&mut self, data: &mut [u8]) -> Result<()>;
135
136 fn transfer(&mut self, data_out: &[u8], data_in: &mut [u8]) -> Result<()>;
138
139 fn get_data_count(&self) -> u32;
141
142 fn get_status(&self) -> Status;
144
145 fn control_slave_select(&mut self, active: bool) -> Result<()>;
147}
148
149#[cfg(feature = "stm32f407")]
150pub mod stm32f407;
151
152#[cfg(feature = "stm32f401")]
153pub mod stm32f401;
154
155#[cfg(feature = "stm32f411")]
156pub mod stm32f411;
157
158#[cfg(feature = "stm32f103")]
159pub mod stm32f103;