123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- use core::marker::PhantomData;
- use core::mem;
- use cast::{u16, u32};
- use hal;
- use stm32::{TIM2, TIM3, TIM4};
- use afio::MAPR;
- use bb;
- use gpio::gpioa::{PA0, PA1, PA2, PA3, PA6, PA7};
- use gpio::gpiob::{PB0, PB1, PB6, PB7, PB8, PB9};
- use gpio::{Alternate, PushPull};
- use rcc::{APB1, Clocks};
- use time::Hertz;
- pub trait Pins<TIM> {
- const REMAP: u8;
- const C1: bool;
- const C2: bool;
- const C3: bool;
- const C4: bool;
- type Channels;
- }
- impl Pins<TIM2>
- for (
- PA0<Alternate<PushPull>>,
- PA1<Alternate<PushPull>>,
- PA2<Alternate<PushPull>>,
- PA3<Alternate<PushPull>>,
- )
- {
- const REMAP: u8 = 0b00;
- const C1: bool = true;
- const C2: bool = true;
- const C3: bool = true;
- const C4: bool = true;
- type Channels = (Pwm<TIM2, C1>, Pwm<TIM2, C2>, Pwm<TIM2, C3>, Pwm<TIM2, C4>);
- }
- impl Pins<TIM2> for PA0<Alternate<PushPull>> {
- const REMAP: u8 = 0b00;
- const C1: bool = true;
- const C2: bool = false;
- const C3: bool = false;
- const C4: bool = false;
- type Channels = Pwm<TIM2, C1>;
- }
- impl Pins<TIM3>
- for (
- PA6<Alternate<PushPull>>,
- PA7<Alternate<PushPull>>,
- PB0<Alternate<PushPull>>,
- PB1<Alternate<PushPull>>,
- )
- {
- const REMAP: u8 = 0b00;
- const C1: bool = true;
- const C2: bool = true;
- const C3: bool = true;
- const C4: bool = true;
- type Channels = (Pwm<TIM3, C1>, Pwm<TIM3, C2>, Pwm<TIM3, C3>, Pwm<TIM3, C4>);
- }
- impl Pins<TIM3> for (PB0<Alternate<PushPull>>, PB1<Alternate<PushPull>>) {
- const REMAP: u8 = 0b00;
- const C1: bool = false;
- const C2: bool = false;
- const C3: bool = true;
- const C4: bool = true;
- type Channels = (Pwm<TIM3, C3>, Pwm<TIM3, C4>);
- }
- impl Pins<TIM4>
- for (
- PB6<Alternate<PushPull>>,
- PB7<Alternate<PushPull>>,
- PB8<Alternate<PushPull>>,
- PB9<Alternate<PushPull>>,
- )
- {
- const REMAP: u8 = 0b0;
- const C1: bool = true;
- const C2: bool = true;
- const C3: bool = true;
- const C4: bool = true;
- type Channels = (Pwm<TIM4, C1>, Pwm<TIM4, C2>, Pwm<TIM4, C3>, Pwm<TIM4, C4>);
- }
- pub trait PwmExt: Sized {
- fn pwm<PINS, T>(
- self,
- PINS,
- mapr: &mut MAPR,
- frequency: T,
- clocks: Clocks,
- apb: &mut APB1,
- ) -> PINS::Channels
- where
- PINS: Pins<Self>,
- T: Into<Hertz>;
- }
- impl PwmExt for TIM2 {
- fn pwm<PINS, T>(
- self,
- _pins: PINS,
- mapr: &mut MAPR,
- freq: T,
- clocks: Clocks,
- apb: &mut APB1,
- ) -> PINS::Channels
- where
- PINS: Pins<Self>,
- T: Into<Hertz>,
- {
- mapr.mapr()
- .modify(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });
- tim2(self, _pins, freq.into(), clocks, apb)
- }
- }
- impl PwmExt for TIM3 {
- fn pwm<PINS, T>(
- self,
- _pins: PINS,
- mapr: &mut MAPR,
- freq: T,
- clocks: Clocks,
- apb: &mut APB1,
- ) -> PINS::Channels
- where
- PINS: Pins<Self>,
- T: Into<Hertz>,
- {
- mapr.mapr()
- .modify(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });
- tim3(self, _pins, freq.into(), clocks, apb)
- }
- }
- impl PwmExt for TIM4 {
- fn pwm<PINS, T>(
- self,
- _pins: PINS,
- mapr: &mut MAPR,
- freq: T,
- clocks: Clocks,
- apb: &mut APB1,
- ) -> PINS::Channels
- where
- PINS: Pins<Self>,
- T: Into<Hertz>,
- {
- mapr.mapr()
- .modify(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));
- tim4(self, _pins, freq.into(), clocks, apb)
- }
- }
- pub struct Pwm<TIM, CHANNEL> {
- _channel: PhantomData<CHANNEL>,
- _tim: PhantomData<TIM>,
- }
- pub struct C1;
- pub struct C2;
- pub struct C3;
- pub struct C4;
- macro_rules! hal {
- ($($TIMX:ident: ($timX:ident, $timXen:ident, $timXrst:ident),)+) => {
- $(
- fn $timX<PINS>(
- tim: $TIMX,
- _pins: PINS,
- freq: Hertz,
- clocks: Clocks,
- apb: &mut APB1,
- ) -> PINS::Channels
- where
- PINS: Pins<$TIMX>,
- {
- apb.enr().modify(|_, w| w.$timXen().set_bit());
- apb.rstr().modify(|_, w| w.$timXrst().set_bit());
- apb.rstr().modify(|_, w| w.$timXrst().clear_bit());
- if PINS::C1 {
- tim.ccmr1_output
- .modify(|_, w| w.oc1pe().set_bit().oc1m().bits(6));
- }
- if PINS::C2 {
- tim.ccmr1_output
- .modify(|_, w| w.oc2pe().set_bit().oc2m().bits(6));
- }
- if PINS::C3 {
- tim.ccmr2_output
- .modify(|_, w| w.oc3pe().set_bit().oc3m().bits(6));
- }
- if PINS::C4 {
- tim.ccmr2_output
- .modify(|_, w| w.oc4pe().set_bit().oc4m().bits(6));
- }
- let clk = clocks.pclk1().0 * if clocks.ppre1() == 1 { 1 } else { 2 };
- let freq = freq.0;
- let ticks = clk / freq;
- let psc = u16(ticks / (1 << 16)).unwrap();
- tim.psc.write(|w| w.psc().bits(psc));
- let arr = u16(ticks / u32(psc + 1)).unwrap();
- tim.arr.write(|w| w.arr().bits(arr));
- tim.cr1.write(|w| unsafe {
- w.cms()
- .bits(0b00)
- .dir()
- .clear_bit()
- .opm()
- .clear_bit()
- .cen()
- .set_bit()
- });
- unsafe { mem::uninitialized() }
- }
- impl hal::PwmPin for Pwm<$TIMX, C1> {
- type Duty = u16;
- fn disable(&mut self) {
- unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 0) }
- }
- fn enable(&mut self) {
- unsafe { bb::set(&(*$TIMX::ptr()).ccer, 0) }
- }
- fn get_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).ccr1.read().ccr1().bits() }
- }
- fn get_max_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
- }
- fn set_duty(&mut self, duty: u16) {
- unsafe { (*$TIMX::ptr()).ccr1.write(|w| w.ccr1().bits(duty)) }
- }
- }
- impl hal::PwmPin for Pwm<$TIMX, C2> {
- type Duty = u16;
- fn disable(&mut self) {
- unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 4) }
- }
- fn enable(&mut self) {
- unsafe { bb::set(&(*$TIMX::ptr()).ccer, 4) }
- }
- fn get_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).ccr2.read().ccr2().bits() }
- }
- fn get_max_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
- }
- fn set_duty(&mut self, duty: u16) {
- unsafe { (*$TIMX::ptr()).ccr2.write(|w| w.ccr2().bits(duty)) }
- }
- }
- impl hal::PwmPin for Pwm<$TIMX, C3> {
- type Duty = u16;
- fn disable(&mut self) {
- unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 8) }
- }
- fn enable(&mut self) {
- unsafe { bb::set(&(*$TIMX::ptr()).ccer, 8) }
- }
- fn get_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).ccr3.read().ccr3().bits() }
- }
- fn get_max_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
- }
- fn set_duty(&mut self, duty: u16) {
- unsafe { (*$TIMX::ptr()).ccr3.write(|w| w.ccr3().bits(duty)) }
- }
- }
- impl hal::PwmPin for Pwm<$TIMX, C4> {
- type Duty = u16;
- fn disable(&mut self) {
- unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 12) }
- }
- fn enable(&mut self) {
- unsafe { bb::set(&(*$TIMX::ptr()).ccer, 12) }
- }
- fn get_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).ccr4.read().ccr4().bits() }
- }
- fn get_max_duty(&self) -> u16 {
- unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
- }
- fn set_duty(&mut self, duty: u16) {
- unsafe { (*$TIMX::ptr()).ccr4.write(|w| w.ccr4().bits(duty)) }
- }
- }
- )+
- }
- }
- hal! {
- TIM2: (tim2, tim2en, tim2rst),
- TIM3: (tim3, tim3en, tim3rst),
- TIM4: (tim4, tim4en, tim4rst),
- }
|