forked from stm32-rs/stm32f1xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfrc522.rs
58 lines (48 loc) · 1.51 KB
/
mfrc522.rs
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
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use panic_itm as _;
use cortex_m::iprintln;
use cortex_m_rt::entry;
use embedded_hal::digital::v1_compat::OldOutputPin;
use embedded_hal::spi::{Mode, Phase, Polarity};
use mfrc522::Mfrc522;
use stm32f1xx_hal::{pac, prelude::*, spi::Spi};
pub const MODE: Mode = Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
};
#[entry]
fn main() -> ! {
let mut cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let _stim = &mut cp.ITM.stim[0];
let rcc = dp.RCC.constrain();
let mut afio = dp.AFIO.constrain();
let mut flash = dp.FLASH.constrain();
let mut gpioa = dp.GPIOA.split();
let mut gpioc = dp.GPIOC.split();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let spi = Spi::spi1(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
MODE,
1.MHz(),
clocks,
);
let nss = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
let mut mfrc522 = Mfrc522::new(spi, OldOutputPin::from(nss)).unwrap();
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
led.set_high();
loop {
if let Ok(atqa) = mfrc522.reqa() {
if let Ok(uid) = mfrc522.select(&atqa) {
iprintln!(_stim, "* {:?}", uid.as_bytes());
}
}
}
}