From 4b041697f4b2c742412c841d33ba41c873bc56bf Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Apr 2012 11:56:42 +1200 Subject: Added convenience function for timer input selection. --- include/libopencm3/stm32/timer.h | 11 +++++++++++ lib/stm32/f1/timer.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/libopencm3/stm32/timer.h b/include/libopencm3/stm32/timer.h index 836e064..c7a4934 100644 --- a/include/libopencm3/stm32/timer.h +++ b/include/libopencm3/stm32/timer.h @@ -887,6 +887,16 @@ enum tim_ic_psc { TIM_IC_PSC_8, }; +/* Input Capture input prescaler */ +enum tim_ic_input { + TIM_IC_OUT = 0, + TIM_IC_IN_TI1 = 1, + TIM_IC_IN_TI2 = 2, + TIM_IC_IN_TRC = 3, + TIM_IC_IN_TI3 = 5, + TIM_IC_IN_TI4 = 6, +}; + /* --- TIM functions ------------------------------------------------------- */ void timer_reset(u32 timer_peripheral); void timer_enable_irq(u32 timer_peripheral, u32 irq); @@ -956,5 +966,6 @@ u32 timer_get_counter(u32 timer_peripheral); void timer_ic_set_filter(u32 timer, enum tim_ic_id ic, enum tim_ic_filter flt); void timer_ic_set_prescaler(u32 timer, enum tim_ic_id ic, enum tim_ic_psc psc); +void timer_ic_set_input(u32 timer, enum tim_ic_id ic, enum tim_ic_input in); #endif diff --git a/lib/stm32/f1/timer.c b/lib/stm32/f1/timer.c index 8303f89..4d0e88e 100644 --- a/lib/stm32/f1/timer.c +++ b/lib/stm32/f1/timer.c @@ -959,3 +959,33 @@ void timer_ic_set_prescaler(u32 timer, enum tim_ic_id ic, enum tim_ic_psc psc) } } +void timer_ic_set_input(u32 timer, enum tim_ic_id ic, enum tim_ic_input in) +{ + in &= 3; + + if (((ic == TIM_IC2) || (ic == TIM_IC4)) && + ((in == TIM_IC_IN_TI1) || (in = TIM_IC_IN_TI2))) { + /* Input select bits are flipped for these combinations */ + in ^= 3; + } + + switch (ic) { + case TIM_IC1: + TIM_CCMR1(timer) &= ~TIM_CCMR1_CC1S_MASK; + TIM_CCMR1(timer) |= in; + break; + case TIM_IC2: + TIM_CCMR1(timer) &= ~TIM_CCMR1_CC2S_MASK; + TIM_CCMR1(timer) |= in << 8; + break; + case TIM_IC3: + TIM_CCMR2(timer) &= ~TIM_CCMR2_CC3S_MASK; + TIM_CCMR2(timer) |= in; + break; + case TIM_IC4: + TIM_CCMR2(timer) &= ~TIM_CCMR2_CC4S_MASK; + TIM_CCMR2(timer) |= in << 8; + break; + } +} + -- cgit v1.2.3