From 9523dc8a7809354fa768aefdeaafd7c487e00a56 Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sat, 8 Feb 2020 12:48:52 +0100 Subject: [PATCH] Fixed non-blocking delays not working * fixed ARR value - removed reset function --- src/drivers/timer.c | 28 +++++++++++++++++----------- src/drivers/timer.h | 4 ++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/drivers/timer.c b/src/drivers/timer.c index 6c4cfdd..acda55f 100644 --- a/src/drivers/timer.c +++ b/src/drivers/timer.c @@ -108,19 +108,22 @@ int timer_wait_ms(TIM_TypeDef* tmr, uint16_t ms, OnTick cb) { else if (tmr == TIM4) RCC->APB1ENR |= 1<<2; else return -1; // no such timer } + + // set period + tmr->ARR = 0xFFFFFFFF; } else { //non-blocking if(timer_config_cb(tmr, &clk, cb)) return -1; + + // set period + tmr->ARR = ms-1; } // set mode - tmr->CR1 |= (1<<7); //buffering + tmr->CR1 = (1<<7) | (1<<2); //buffering and update settings tmr->CR1 |= (1<<3); //one pulse mode - // set period - tmr->ARR = 0xFFFFFFFF; - - // set prescaler 1us + // set prescaler 1ms tmr->PSC = 8*(clk/1000)-1; //PSC = clk/f - 1 | don't know why 8 times.. timer_start(tmr); @@ -149,17 +152,20 @@ int timer_wait_us(TIM_TypeDef* tmr, uint16_t us, OnTick cb) { else return -1; // no such timer } + // set period + tmr->ARR = 0xFFFFFFFF; + } else { //non-blocking if(timer_config_cb(tmr, &clk, cb)) return -1; + + // set period + tmr->ARR = us-1; } // set mode - tmr->CR1 |= (1<<7); //buffering + tmr->CR1 = (1<<7) | (1<<2); //buffering and update settings tmr->CR1 |= (1<<3); //one pulse mode - // set period - tmr->ARR = 0xFFFFFFFF; - // set prescaler 1us tmr->PSC = 8*(clk/1000000)-1; //PSC = clk/f - 1 | don't know why 8 times.. @@ -229,8 +235,8 @@ int timer_tick_init(TIM_TypeDef *tmr, uint16_t tick_ms, OnTick cb) { tmr->SR &= !1; // set mode - tmr->CR1 = (1<<7); //buffering - tmr->DIER = (1<<0); //Enable interrupts + tmr->CR1 = (1<<7) | (1<<2); //buffering and update settings + tmr->DIER = (1<<0); //Enable interrupts // set prescaler 1ms tmr->PSC = 8*(clk/1000)-1; //PSC = clk/f - 1 | don't know why 8 times... diff --git a/src/drivers/timer.h b/src/drivers/timer.h index 772a047..b6cde97 100644 --- a/src/drivers/timer.h +++ b/src/drivers/timer.h @@ -42,6 +42,10 @@ void timer_start(TIM_TypeDef *tmr); void timer_stop(TIM_TypeDef *tmr); //------------------------------------------------------------------------------ +/** timer_enc_init + * setup timer to read encoder output and keep track of it's position in the + * CNT register whithout using CPU time + */ int timer_enc_init(TIM_TypeDef* tmr); //------------------------------------------------------------------------------