pwm.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //! Testing PWM output for pre-defined pin combination: all pins for default mapping
  2. #![deny(unsafe_code)]
  3. #![no_main]
  4. #![no_std]
  5. use panic_halt as _;
  6. use cortex_m::asm;
  7. use stm32f1xx_hal::{
  8. prelude::*,
  9. pac,
  10. timer::{Tim2NoRemap, Timer},
  11. time::U32Ext,
  12. pwm::Channel
  13. };
  14. use cortex_m_rt::entry;
  15. #[entry]
  16. fn main() -> ! {
  17. let p = pac::Peripherals::take().unwrap();
  18. let mut flash = p.FLASH.constrain();
  19. let mut rcc = p.RCC.constrain();
  20. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  21. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  22. let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  23. // let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  24. // TIM2
  25. let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
  26. let c2 = gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl);
  27. let c3 = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  28. // If you don't want to use all channels, just leave some out
  29. // let c4 = gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl);
  30. let pins = (c1, c2, c3);
  31. // TIM3
  32. // let c1 = gpioa.pa6.into_alternate_push_pull(&mut gpioa.crl);
  33. // let c2 = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
  34. // let c3 = gpiob.pb0.into_alternate_push_pull(&mut gpiob.crl);
  35. // let c4 = gpiob.pb1.into_alternate_push_pull(&mut gpiob.crl);
  36. // TIM4 (Only available with the "medium" density feature)
  37. // let c1 = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  38. // let c2 = gpiob.pb7.into_alternate_push_pull(&mut gpiob.crl);
  39. // let c3 = gpiob.pb8.into_alternate_push_pull(&mut gpiob.crh);
  40. // let c4 = gpiob.pb9.into_alternate_push_pull(&mut gpiob.crh);
  41. let mut pwm = Timer::tim2(p.TIM2, &clocks, &mut rcc.apb1)
  42. .pwm::<Tim2NoRemap, _, _, _>(pins, &mut afio.mapr, 1.khz());
  43. //// Operations affecting all defined channels on the Timer
  44. // Adjust period to 0.5 seconds
  45. pwm.set_period(500.ms());
  46. asm::bkpt();
  47. // Return to the original frequency
  48. pwm.set_period(1.khz());
  49. asm::bkpt();
  50. let max = pwm.get_max_duty();
  51. //// Operations affecting single channels can be accessed through
  52. //// the Pwm object or via dereferencing to the pin.
  53. // Use the Pwm object to set C3 to full strength
  54. pwm.set_duty(Channel::C3, max);
  55. asm::bkpt();
  56. // Use the Pwm object to set C3 to be dim
  57. pwm.set_duty(Channel::C3, max / 4);
  58. asm::bkpt();
  59. // Use the Pwm object to set C3 to be zero
  60. pwm.set_duty(Channel::C3, 0);
  61. asm::bkpt();
  62. // Extract the PwmChannel for C3
  63. let mut pwm_channel = pwm.split().2;
  64. // Use the PwmChannel object to set C3 to be full strength
  65. pwm_channel.set_duty(max);
  66. asm::bkpt();
  67. // Use the PwmChannel object to set C3 to be dim
  68. pwm_channel.set_duty(max / 4);
  69. asm::bkpt();
  70. // Use the PwmChannel object to set C3 to be zero
  71. pwm_channel.set_duty(0);
  72. asm::bkpt();
  73. loop {}
  74. }