mfrc522.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #![deny(unsafe_code)]
  2. #![no_main]
  3. #![no_std]
  4. use panic_itm as _;
  5. use cortex_m::iprintln;
  6. use cortex_m_rt::entry;
  7. use embedded_hal::digital::{v1_compat::OldOutputPin, v2::OutputPin};
  8. use mfrc522::Mfrc522;
  9. use stm32f1xx_hal::{pac, prelude::*, spi::Spi};
  10. #[entry]
  11. fn main() -> ! {
  12. let mut cp = cortex_m::Peripherals::take().unwrap();
  13. let dp = pac::Peripherals::take().unwrap();
  14. let _stim = &mut cp.ITM.stim[0];
  15. let mut rcc = dp.RCC.constrain();
  16. let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
  17. let mut flash = dp.FLASH.constrain();
  18. let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
  19. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  20. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  21. let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
  22. let miso = gpioa.pa6;
  23. let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
  24. let spi = Spi::spi1(
  25. dp.SPI1,
  26. (sck, miso, mosi),
  27. &mut afio.mapr,
  28. mfrc522::MODE,
  29. 1.mhz(),
  30. clocks,
  31. &mut rcc.apb2,
  32. );
  33. let nss = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
  34. let mut mfrc522 = Mfrc522::new(spi, OldOutputPin::from(nss)).unwrap();
  35. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  36. led.set_high().unwrap();
  37. loop {
  38. if let Ok(atqa) = mfrc522.reqa() {
  39. if let Ok(uid) = mfrc522.select(&atqa) {
  40. iprintln!(_stim, "* {:?}", uid);
  41. }
  42. }
  43. }
  44. }