mfrc522.rs 1.4 KB

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