qei.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //! Testing the Quadrature Encoder Interface
  2. #![deny(unsafe_code)]
  3. #![deny(warnings)]
  4. #![no_main]
  5. #![no_std]
  6. extern crate cortex_m;
  7. extern crate cortex_m_rt as rt;
  8. extern crate cortex_m_semihosting as semihosting;
  9. extern crate panic_semihosting;
  10. extern crate stm32f1xx_hal as hal;
  11. use core::fmt::Write;
  12. use hal::delay::Delay;
  13. use hal::prelude::*;
  14. use hal::qei::Qei;
  15. use hal::stm32f103xx;
  16. use rt::{entry, exception, ExceptionFrame};
  17. use semihosting::hio;
  18. #[entry]
  19. fn main() -> ! {
  20. let dp = stm32f103xx::Peripherals::take().unwrap();
  21. let cp = cortex_m::Peripherals::take().unwrap();
  22. let mut flash = dp.FLASH.constrain();
  23. let mut rcc = dp.RCC.constrain();
  24. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  25. let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
  26. // let gpioa = dp.GPIOA.split(&mut rcc.apb2);
  27. let gpiob = dp.GPIOB.split(&mut rcc.apb2);
  28. // TIM2
  29. // let c1 = gpioa.pa0;
  30. // let c2 = gpioa.pa1;
  31. // TIM3
  32. // let c1 = gpioa.pa6;
  33. // let c2 = gpioa.pa7;
  34. // TIM4
  35. let c1 = gpiob.pb6;
  36. let c2 = gpiob.pb7;
  37. let qei = Qei::tim4(dp.TIM4, (c1, c2), &mut afio.mapr, &mut rcc.apb1);
  38. let mut delay = Delay::new(cp.SYST, clocks);
  39. let mut hstdout = hio::hstdout().unwrap();
  40. loop {
  41. let before = qei.count();
  42. delay.delay_ms(1_000_u16);
  43. let after = qei.count();
  44. let elapsed = after.wrapping_sub(before) as i16;
  45. writeln!(hstdout, "{}", elapsed).unwrap();
  46. }
  47. }
  48. #[exception]
  49. fn HardFault(ef: &ExceptionFrame) -> ! {
  50. panic!("{:#?}", ef);
  51. }
  52. #[exception]
  53. fn DefaultHandler(irqn: i16) {
  54. panic!("Unhandled exception (IRQn = {})", irqn);
  55. }