|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use hal::rcc::{Clocks, APB1}; |
| 5 | +use testsuite as _; |
| 6 | + |
| 7 | +use core::convert::TryInto; |
| 8 | + |
| 9 | +use stm32f3xx_hal as hal; |
| 10 | + |
| 11 | +use hal::prelude::*; |
| 12 | + |
| 13 | +use hal::gpio::{PushPull, AF6}; |
| 14 | +use hal::gpio::{PC10, PC11, PC12}; |
| 15 | +use hal::hal::spi::MODE_0; |
| 16 | +use hal::pac; |
| 17 | +use hal::pac::SPI3; |
| 18 | +use hal::spi::Spi; |
| 19 | + |
| 20 | +const TEST_MSG: [u8; 8] = [0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xaa, 0xbb]; |
| 21 | + |
| 22 | +// TODO: PC10 (SckPin) is not really needed but is forced to be set, |
| 23 | +// because of the current implement types. |
| 24 | +type Spi3<T> = Spi<SPI3, (PC10<T>, PC11<T>, PC12<T>), u8>; |
| 25 | + |
| 26 | +struct State { |
| 27 | + spi: Option<Spi3<AF6<PushPull>>>, |
| 28 | + clocks: Clocks, |
| 29 | + apb1: APB1, |
| 30 | +} |
| 31 | + |
| 32 | +#[defmt_test::tests] |
| 33 | +mod tests { |
| 34 | + use super::*; |
| 35 | + use defmt::{self, assert_eq, unwrap}; |
| 36 | + use hal::gpio::GpioExt; |
| 37 | + use testsuite::SpiPair; |
| 38 | + |
| 39 | + #[init] |
| 40 | + fn init() -> super::State { |
| 41 | + let dp = unwrap!(pac::Peripherals::take()); |
| 42 | + |
| 43 | + let mut rcc = dp.RCC.constrain(); |
| 44 | + let mut flash = dp.FLASH.constrain(); |
| 45 | + let clocks = rcc |
| 46 | + .cfgr |
| 47 | + .use_hse(8.MHz()) |
| 48 | + .sysclk(72.MHz()) |
| 49 | + .freeze(&mut flash.acr); |
| 50 | + |
| 51 | + let mut gpioc = dp.GPIOC.split(&mut rcc.ahb); |
| 52 | + |
| 53 | + let spi_pins = SpiPair { |
| 54 | + 0: gpioc |
| 55 | + .pc10 |
| 56 | + .into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh), |
| 57 | + 1: gpioc |
| 58 | + .pc11 |
| 59 | + .into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh), |
| 60 | + 2: gpioc |
| 61 | + .pc12 |
| 62 | + .into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh), |
| 63 | + }; |
| 64 | + |
| 65 | + let spi = Spi::new( |
| 66 | + dp.SPI3, |
| 67 | + (spi_pins.0, spi_pins.1, spi_pins.2), |
| 68 | + MODE_0, |
| 69 | + 10.MHz().try_into().unwrap(), |
| 70 | + clocks, |
| 71 | + &mut rcc.apb1, |
| 72 | + ); |
| 73 | + |
| 74 | + super::State { |
| 75 | + spi: Some(spi), |
| 76 | + clocks, |
| 77 | + apb1: rcc.apb1, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_transfer(state: &mut super::State) { |
| 83 | + for freq in [100.Hz(), 100_000.Hz(), 10_000_000.Hz(), 32_000_000.Hz()] { |
| 84 | + let (spi, pins) = unwrap!(state.spi.take()).free(); |
| 85 | + |
| 86 | + let mut spi = Spi::new(spi, pins, MODE_0, freq, state.clocks, &mut state.apb1); |
| 87 | + let mut transfer_buffer = TEST_MSG; |
| 88 | + |
| 89 | + unwrap!(spi.transfer(&mut transfer_buffer)); |
| 90 | + assert_eq!(transfer_buffer, TEST_MSG); |
| 91 | + |
| 92 | + state.spi = Some(spi); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments