|
| 1 | +//! API for the integrate SPI peripherals |
| 2 | +//! |
| 3 | +//! The spi bus acts as the master (generating the clock) and you need to handle the CS separately. |
| 4 | +//! |
| 5 | +//! The most significant bit is transmitted first & only 8-bit transfers are supported |
| 6 | +//! |
| 7 | +//! # Example |
| 8 | +//! Echo incoming data in the next transfer |
| 9 | +//! ``` no_run |
| 10 | +//! use stm32f0xx_hal as hal; |
| 11 | +//! |
| 12 | +//! use crate::hal::stm32; |
| 13 | +//! use crate::hal::prelude::*; |
| 14 | +//! use crate::hal::spi::{Spi, Mode, Phase, Polarity}; |
| 15 | +//! |
| 16 | +//! let p = stm32::Peripherals::take().unwrap(); |
| 17 | +//! let clcks = p.RCC.constrain().cfgr.freeze(); |
| 18 | +//! |
| 19 | +//! let gpioa = p.GPIOA.split(); |
| 20 | +//! |
| 21 | +//! // Configure pins for SPI |
| 22 | +//! let sck = gpioa.pa5.into_alternate_af0(); |
| 23 | +//! let miso = gpioa.pa6.into_alternate_af0(); |
| 24 | +//! let mosi = gpioa.pa7.into_alternate_af0(); |
| 25 | +//! |
| 26 | +//! // Configure SPI with 1MHz rate |
| 27 | +//! let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), Mode { |
| 28 | +//! polarity: Polarity::IdleHigh, |
| 29 | +//! phase: Phase::CaptureOnSecondTransition, |
| 30 | +//! }, 1.mhz(), clocks); |
| 31 | +//! |
| 32 | +//! let mut data = [ 0 ]; |
| 33 | +//! loop { |
| 34 | +//! spi.transfer(&mut data).unwrap(); |
| 35 | +//! } |
| 36 | +//! ``` |
| 37 | +
|
1 | 38 | #[allow(unused)]
|
2 | 39 | use core::{ops::Deref, ptr};
|
3 | 40 |
|
@@ -142,6 +179,7 @@ macro_rules! spi {
|
142 | 179 | ($($SPI:ident: ($spi:ident, $spiXen:ident, $spiXrst:ident, $apbenr:ident, $apbrstr:ident),)+) => {
|
143 | 180 | $(
|
144 | 181 | impl<SCKPIN, MISOPIN, MOSIPIN> Spi<$SPI, SCKPIN, MISOPIN, MOSIPIN> {
|
| 182 | + /// Creates a new spi instance |
145 | 183 | pub fn $spi<F>(
|
146 | 184 | spi: $SPI,
|
147 | 185 | pins: (SCKPIN, MISOPIN, MOSIPIN),
|
|
0 commit comments