pwm.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 cortex_m_rt::entry;
  8. use stm32f1xx_hal::{
  9. pac,
  10. prelude::*,
  11. pwm::Channel,
  12. time::U32Ext,
  13. timer::{Tim2NoRemap, Timer},
  14. };
  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).pwm::<Tim2NoRemap, _, _, _>(
  42. pins,
  43. &mut afio.mapr,
  44. 1.khz(),
  45. );
  46. //// Operations affecting all defined channels on the Timer
  47. // Adjust period to 0.5 seconds
  48. pwm.set_period(500.ms());
  49. asm::bkpt();
  50. // Return to the original frequency
  51. pwm.set_period(1.khz());
  52. asm::bkpt();
  53. let max = pwm.get_max_duty();
  54. //// Operations affecting single channels can be accessed through
  55. //// the Pwm object or via dereferencing to the pin.
  56. // Use the Pwm object to set C3 to full strength
  57. pwm.set_duty(Channel::C3, max);
  58. asm::bkpt();
  59. // Use the Pwm object to set C3 to be dim
  60. pwm.set_duty(Channel::C3, max / 4);
  61. asm::bkpt();
  62. // Use the Pwm object to set C3 to be zero
  63. pwm.set_duty(Channel::C3, 0);
  64. asm::bkpt();
  65. // Extract the PwmChannel for C3
  66. let mut pwm_channel = pwm.split().2;
  67. // Use the PwmChannel object to set C3 to be full strength
  68. pwm_channel.set_duty(max);
  69. asm::bkpt();
  70. // Use the PwmChannel object to set C3 to be dim
  71. pwm_channel.set_duty(max / 4);
  72. asm::bkpt();
  73. // Use the PwmChannel object to set C3 to be zero
  74. pwm_channel.set_duty(0);
  75. asm::bkpt();
  76. loop {}
  77. }