pwm.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Enable clock on each of the channels
  47. pwm.enable(Channel::C1);
  48. pwm.enable(Channel::C2);
  49. pwm.enable(Channel::C3);
  50. //// Operations affecting all defined channels on the Timer
  51. // Adjust period to 0.5 seconds
  52. pwm.set_period(500.ms());
  53. asm::bkpt();
  54. // Return to the original frequency
  55. pwm.set_period(1.khz());
  56. asm::bkpt();
  57. let max = pwm.get_max_duty();
  58. //// Operations affecting single channels can be accessed through
  59. //// the Pwm object or via dereferencing to the pin.
  60. // Use the Pwm object to set C3 to full strength
  61. pwm.set_duty(Channel::C3, max);
  62. asm::bkpt();
  63. // Use the Pwm object to set C3 to be dim
  64. pwm.set_duty(Channel::C3, max / 4);
  65. asm::bkpt();
  66. // Use the Pwm object to set C3 to be zero
  67. pwm.set_duty(Channel::C3, 0);
  68. asm::bkpt();
  69. // Extract the PwmChannel for C3
  70. let mut pwm_channel = pwm.split().2;
  71. // Use the PwmChannel object to set C3 to be full strength
  72. pwm_channel.set_duty(max);
  73. asm::bkpt();
  74. // Use the PwmChannel object to set C3 to be dim
  75. pwm_channel.set_duty(max / 4);
  76. asm::bkpt();
  77. // Use the PwmChannel object to set C3 to be zero
  78. pwm_channel.set_duty(0);
  79. asm::bkpt();
  80. loop {}
  81. }