pwm_custom.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //! Testing PWM output for custom pin combinations
  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. gpio::gpiob::{PB4, PB5},
  9. gpio::{Alternate, PushPull},
  10. pac::{self,TIM3},
  11. prelude::*,
  12. pwm::{Pins, Pwm, C1, C2},
  13. timer::Timer,
  14. };
  15. use cortex_m_rt::entry;
  16. // Using PB5 channel for TIM3 PWM output
  17. // struct MyChannels(PB5<Alternate<PushPull>>);
  18. // impl Pins<TIM3> for MyChannels {
  19. // const REMAP: u8 = 0b10;
  20. // const C1: bool = false;
  21. // const C2: bool = true;
  22. // const C3: bool = false;
  23. // const C4: bool = false;
  24. // type Channels = Pwm<TIM3, C2>;
  25. // }
  26. // Using PB4 and PB5 channels for TIM3 PWM output
  27. struct MyChannels(PB4<Alternate<PushPull>>, PB5<Alternate<PushPull>>);
  28. impl Pins<TIM3> for MyChannels {
  29. const REMAP: u8 = 0b10;
  30. const C1: bool = true;
  31. const C2: bool = true;
  32. const C3: bool = false;
  33. const C4: bool = false;
  34. type Channels = (Pwm<TIM3, C1>, Pwm<TIM3, C2>);
  35. }
  36. #[entry]
  37. fn main() -> ! {
  38. let p = pac::Peripherals::take().unwrap();
  39. let mut flash = p.FLASH.constrain();
  40. let mut rcc = p.RCC.constrain();
  41. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  42. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  43. let gpioa = p.GPIOA.split(&mut rcc.apb2);
  44. let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  45. let (_pa15, _pb3, pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4);
  46. // TIM3
  47. let p0 = pb4.into_alternate_push_pull(&mut gpiob.crl);
  48. let p1 = gpiob.pb5.into_alternate_push_pull(&mut gpiob.crl);
  49. let mut pwm = Timer::tim3(p.TIM3, &clocks, &mut rcc.apb1)
  50. .pwm(
  51. MyChannels(p0, p1),
  52. &mut afio.mapr,
  53. 1.khz(),
  54. );
  55. let max = pwm.0.get_max_duty();
  56. pwm.0.enable();
  57. pwm.1.enable();
  58. // full
  59. pwm.0.set_duty(max);
  60. pwm.1.set_duty(max);
  61. asm::bkpt();
  62. // dim
  63. pwm.1.set_duty(max / 4);
  64. asm::bkpt();
  65. // zero
  66. pwm.0.set_duty(0);
  67. pwm.1.set_duty(0);
  68. asm::bkpt();
  69. loop {}
  70. }