mfrc522.rs 1.7 KB

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