From 2c2d3182172720af00657bf92f4628698f45b7fa Mon Sep 17 00:00:00 2001 From: key Date: Wed, 22 Dec 2021 17:08:59 +0100 Subject: [PATCH] Making timers great again pahse 1 / to be tested and finished so that the timerInit() function works properly --- bsl/csl/interfaces/timer.h | 7 ++++-- bsl/csl/stm32f042/Src/timer.c | 40 ++++++++++++++++++++++++----------- main.cpp | 7 +++--- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/bsl/csl/interfaces/timer.h b/bsl/csl/interfaces/timer.h index 8fe1733..94db6c4 100644 --- a/bsl/csl/interfaces/timer.h +++ b/bsl/csl/interfaces/timer.h @@ -35,6 +35,7 @@ typedef enum { } timerCountDirection_t; typedef enum { + counter, inputCapture, outputCompare, pwm, @@ -43,8 +44,8 @@ typedef enum { void timerReset(timerNo_t timer); void timerActivateBus(timerNo_t timer); -void timerEnableTimer(timerNo_t timer); -void timerDisableTimer(timerNo_t timer); +void timerEnable(timerNo_t timer); +void timerDisable(timerNo_t timer); void timerSetMode(timerNo_t timer, timerMode_t mode); void timerSetCountDirection(timerNo_t timer, timerCountDirection_t direction); void timerSetPrescaler(timerNo_t timer, uint32_t prescaler); @@ -56,12 +57,14 @@ void timerClearCounter(timerNo_t timer); /* Second stage configuration */ +void timerInit(timerNo_t timer, uint32_t prescaler, uint32_t period, timerCountDirection_t direction, timerMode_t mode); void timerSetHz(timerNo_t timer, uint16_t hz); void timerSetMs(timerNo_t timer, uint16_t ms); void timerSetNs(timerNo_t timer, uint16_t ns); void timerSetPs(timerNo_t timer, uint16_t ps); void timerSart(timerNo_t timer); +void timerHalt(timerNo_t timer); void timerStop(timerNo_t timer); uint32_t timerGetCount(timerNo_t timer); diff --git a/bsl/csl/stm32f042/Src/timer.c b/bsl/csl/stm32f042/Src/timer.c index 832bba7..904f97e 100644 --- a/bsl/csl/stm32f042/Src/timer.c +++ b/bsl/csl/stm32f042/Src/timer.c @@ -5,8 +5,7 @@ void timerReset(timerNo_t timer) { - // reset via RCC_APBxRSTR Register - + // To implement RM page 116 } void timerActivateBus(timerNo_t timer) @@ -16,23 +15,22 @@ void timerActivateBus(timerNo_t timer) RCC->APB1ENR |= (1<APB2ENR |= (1<CR1 |= 0x01; //all the timers have the same CEN bit in CR1 register pos 0 + BASE->CR1 |= TIM_CR1_CEN; //all the timers have the same CEN bit in CR1 register pos 0 } -void timerDisableTimer(timerNo_t timer) +void timerDisable(timerNo_t timer) { - BASE->CR1 &=~ 0x01; //all the timers have the same CEN bit in CR1 register pos 0 + BASE->CR1 &=~ TIM_CR1_CEN; //all the timers have the same CEN bit in CR1 register pos 0 } - void timerSetMode(timerNo_t timer, timerMode_t mode) { + //Implement } void timerSetCountDirection(timerNo_t timer, timerCountDirection_t direction) @@ -77,8 +75,21 @@ void timerClearUpdateInterrupt(timerNo_t timer) BASE->SR &= ~1; } -/* Second stage configuration */ +/* Second stage configuration */ +void timerInit( timerNo_t timer, + uint32_t prescaler, + uint32_t period, + timerCountDirection_t direction, + timerMode_t mode) +{ + timerActivateBus(timer); + timerReset(timer); + timerSetMode(mode); + timerSetCountDirection(timer,direction); + timerSetPrescaler(timer, prescaler); + timerSetAutoReload(timer, period); +} /* Bus Clock * ------------------------------ = Duty (Hz) @@ -100,7 +111,6 @@ void timerSetHz(timerNo_t timer, uint16_t hz) temp = 8000000/(prescaler*(period)); }while(temp >= hz); - timerActivateBus(timer); timerSetPrescaler(timer, prescaler-1); timerSetAutoReload(timer, period-1); timerClearCounter(timer); @@ -124,12 +134,18 @@ void timerSetPs(timerNo_t timer, uint16_t ps) void timerSart(timerNo_t timer) { - + timerEnable(timer); +} + +void timerHalt(timerNo_t timer) +{ + timerDisable(timer); } void timerStop(timerNo_t timer) { - + timerDisable(timer); + timerClearCounter(timer); } uint32_t timerGetCount(timerNo_t timer) diff --git a/main.cpp b/main.cpp index 6d3e3f8..566b616 100644 --- a/main.cpp +++ b/main.cpp @@ -14,10 +14,11 @@ int main(int argc, char *argv[]) uint8_t a = '0'; delayInitMs(8000000, 1000); - - timerSetHz(timer_2,10); - timerEnableTimer(timer_2); + //timerSetHz(timer_2,3); + + timerInit(timer_2, 7999, 999, counter, downCounting); + timerSart(timer_2); pinConfig(pinB3, output, pushPull, def_res, def_speed); pinConfig(pinA0, input, def_stage, pullDown, def_speed);