From 0e2ccd30c4bac030bc33ed248564a640a63ea3c5 Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Mon, 19 Apr 2010 23:28:34 -0400 Subject: Aded two timer function to set max reload value and prescaler --- libmaple/timers.c | 43 +++++++++++++++++++++++++++++++++++++++++++ libmaple/timers.h | 3 +++ 2 files changed, 46 insertions(+) (limited to 'libmaple') diff --git a/libmaple/timers.c b/libmaple/timers.c index 903f970..b13cc3a 100644 --- a/libmaple/timers.c +++ b/libmaple/timers.c @@ -142,6 +142,49 @@ void timer_init(uint8 timer_num, uint16 prescale) { timer->CR1 |= 1; // Enable timer } +void timers_set_prescaler(uint32 timer_num, uint16 prescale) { + Timer *timer; + ASSERT(timer_num > 0 && timer_num <= 4); + + switch(timer_num) { + case 1: + timer = (Timer*)TIMER1_BASE; + break; + case 2: + timer = (Timer*)TIMER2_BASE; + break; + case 3: + timer = (Timer*)TIMER3_BASE; + break; + case 4: + timer = (Timer*)TIMER4_BASE; + break; + } + timer->PSC = prescale; +} + +void timers_set_reload(uint32 timer_num, uint16 max_reload) { + Timer *timer; + ASSERT(timer_num > 0 && timer_num <= 4); + + switch(timer_num) { + case 1: + timer = (Timer*)TIMER1_BASE; + break; + case 2: + timer = (Timer*)TIMER2_BASE; + break; + case 3: + timer = (Timer*)TIMER3_BASE; + break; + case 4: + timer = (Timer*)TIMER4_BASE; + break; + } + timer->ARR = max_reload; +} + + void timers_disable(void) { Timer *timer; int i; diff --git a/libmaple/timers.h b/libmaple/timers.h index bcc7f2f..1ee89a0 100644 --- a/libmaple/timers.h +++ b/libmaple/timers.h @@ -122,6 +122,8 @@ typedef volatile uint32* TimerCCR; void timer_init(uint8, uint16); void timers_disable(void); void timers_disable_channel(uint8, uint8); +void timers_set_prescaler(uint32 timer_num, uint16 prescale); +void timers_set_reload(uint32 timer_num, uint16 max_reload); /* Turn on PWM with duty_cycle on the specified channel in timer. * This function takes in a pointer to the corresponding CCR @@ -140,6 +142,7 @@ static inline void timer_pwm_write_ccr(TimerCCR CCR, uint16 duty_cycle) { *CCR = duty_cycle; } + #ifdef __cplusplus } // extern "C" #endif -- cgit v1.2.3