From 7d85aa1c0fdd9fb0f4ed8397b2b8b9708250d04c Mon Sep 17 00:00:00 2001 From: key Date: Wed, 22 Dec 2021 16:31:04 +0100 Subject: [PATCH] Timer added the timerSetHz() --- bsl/csl/interfaces/timer.h | 4 ---- bsl/csl/stm32f042/Src/timer.c | 30 ++++++++++++++++++++---------- main.cpp | 8 +++----- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/bsl/csl/interfaces/timer.h b/bsl/csl/interfaces/timer.h index 76d4482..8fe1733 100644 --- a/bsl/csl/interfaces/timer.h +++ b/bsl/csl/interfaces/timer.h @@ -45,10 +45,6 @@ void timerReset(timerNo_t timer); void timerActivateBus(timerNo_t timer); void timerEnableTimer(timerNo_t timer); void timerDisableTimer(timerNo_t timer); -// -> clock source is set by the clock tree and cant be individually set. This should be done -// in the clock domain and nt here in the timerdomain. For reference one can look at the nucleo -// stm32f042k6 clock tree diagramm in reference Manual -//void timerSetClkSource(timerNo_t timer, clkSources_t clk); void timerSetMode(timerNo_t timer, timerMode_t mode); void timerSetCountDirection(timerNo_t timer, timerCountDirection_t direction); void timerSetPrescaler(timerNo_t timer, uint32_t prescaler); diff --git a/bsl/csl/stm32f042/Src/timer.c b/bsl/csl/stm32f042/Src/timer.c index c7c8f3f..832bba7 100644 --- a/bsl/csl/stm32f042/Src/timer.c +++ b/bsl/csl/stm32f042/Src/timer.c @@ -30,10 +30,6 @@ void timerDisableTimer(timerNo_t timer) BASE->CR1 &=~ 0x01; //all the timers have the same CEN bit in CR1 register pos 0 } -void timerSetClkSource(timerNo_t timer, clkSources_t clk) -{ - -} void timerSetMode(timerNo_t timer, timerMode_t mode) { @@ -82,18 +78,32 @@ void timerClearUpdateInterrupt(timerNo_t timer) } /* Second stage configuration */ + + +/* Bus Clock + * ------------------------------ = Duty (Hz) + * (Prescaler-1) * (Period-1) + */ void timerSetHz(timerNo_t timer, uint16_t hz) { - uint32_t prescaler; - uint32_t period; + uint32_t prescaler = 8000000; + uint32_t period = 0; + uint32_t temp = 0; + + do{ + prescaler = prescaler / 10; + }while(prescaler > 0xffff); + - + do{ + period = period + 1; + temp = 8000000/(prescaler*(period)); + }while(temp >= hz); timerActivateBus(timer); - timerSetPrescaler(timer, 1599); - timerSetAutoReload(timer, 9999); + timerSetPrescaler(timer, prescaler-1); + timerSetAutoReload(timer, period-1); timerClearCounter(timer); - timerEnableTimer(timer); } void timerSetMs(timerNo_t timer, uint16_t ms) diff --git a/main.cpp b/main.cpp index 1888357..6d3e3f8 100644 --- a/main.cpp +++ b/main.cpp @@ -15,12 +15,10 @@ int main(int argc, char *argv[]) delayInitMs(8000000, 1000); - timerActivateBus(timer_2); - timerSetPrescaler(timer_2, 8000-1); - timerSetAutoReload(timer_2, 1000-1); - timerClearCounter(timer_2); - timerEnableTimer(timer_2); + timerSetHz(timer_2,10); + timerEnableTimer(timer_2); + pinConfig(pinB3, output, pushPull, def_res, def_speed); pinConfig(pinA0, input, def_stage, pullDown, def_speed);