pwm.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //! Testing PWM output
  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 panic_semihosting;
  9. extern crate stm32f1xx_hal as hal;
  10. use cortex_m::asm;
  11. use hal::prelude::*;
  12. use hal::stm32f103xx;
  13. use rt::{entry, exception, ExceptionFrame};
  14. #[entry]
  15. fn main() -> ! {
  16. let p = stm32f103xx::Peripherals::take().unwrap();
  17. let mut flash = p.FLASH.constrain();
  18. let mut rcc = p.RCC.constrain();
  19. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  20. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  21. // let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  22. let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  23. // TIM2
  24. // let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
  25. // let c2 = gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl);
  26. // let c3 = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  27. // let c4 = gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl);
  28. // TIM3
  29. // let c1 = gpioa.pa6.into_alternate_push_pull(&mut gpioa.crl);
  30. // let c2 = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
  31. // let c3 = gpiob.pb0.into_alternate_push_pull(&mut gpiob.crl);
  32. // let c4 = gpiob.pb1.into_alternate_push_pull(&mut gpiob.crl);
  33. // TIM4
  34. let c1 = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  35. let c2 = gpiob.pb7.into_alternate_push_pull(&mut gpiob.crl);
  36. let c3 = gpiob.pb8.into_alternate_push_pull(&mut gpiob.crh);
  37. let c4 = gpiob.pb9.into_alternate_push_pull(&mut gpiob.crh);
  38. let mut pwm = p
  39. .TIM4
  40. .pwm(
  41. (c1, c2, c3, c4),
  42. &mut afio.mapr,
  43. 1.khz(),
  44. clocks,
  45. &mut rcc.apb1,
  46. )
  47. .3;
  48. let max = pwm.get_max_duty();
  49. pwm.enable();
  50. // full
  51. pwm.set_duty(max);
  52. asm::bkpt();
  53. // dim
  54. pwm.set_duty(max / 4);
  55. asm::bkpt();
  56. // zero
  57. pwm.set_duty(0);
  58. asm::bkpt();
  59. loop {}
  60. }
  61. #[exception]
  62. fn HardFault(ef: &ExceptionFrame) -> ! {
  63. panic!("{:#?}", ef);
  64. }
  65. #[exception]
  66. fn DefaultHandler(irqn: i16) {
  67. panic!("Unhandled exception (IRQn = {})", irqn);
  68. }