Skip to content

Commit 2602f10

Browse files
ErrorHandler -> WARNING, prescaler calc -> TimerDomain
1 parent ce5c4a6 commit 2602f10

2 files changed

Lines changed: 5 additions & 72 deletions

File tree

Inc/HALAL/Models/TimerDomain/TimerDomain.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ struct TimerDomain {
818818
}
819819
}
820820

821+
// NOTE: This is a bit slower than timerwrapper.get_clock_frequency(), so preferrably use that
821822
static inline uint32_t get_timer_frequency(TIM_TypeDef* tim) {
822823
uint32_t result = 0;
823824
if ((tim == TIM2) || (tim == TIM3) || (tim == TIM4) || (tim == TIM5) || (tim == TIM6) ||

Src/HALAL/Services/Time/Scheduler.cpp

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
#include "HALAL/Services/Time/Scheduler.hpp"
88
#include "HALAL/Models/TimerDomain/TimerDomain.hpp"
9-
#include "ErrorHandler/ErrorHandler.hpp"
9+
#include "HALAL/Services/InfoWarning/InfoWarning.hpp"
1010

1111
#include <stdint.h>
1212

@@ -58,83 +58,15 @@ void Scheduler_start(void) {
5858
ST_LIB::TimerDomain::callbacks[ST_LIB::timer_idxmap[static_cast<uint8_t>(SCHEDULER_TIMER_DOMAIN
5959
)]] = Scheduler_global_timer_callback;
6060

61-
// TODO: change this to use TimerDomain::get_timer_clock()?
62-
uint32_t prescaler = (SystemCoreClock / Scheduler::FREQUENCY);
63-
// setup prescaler
64-
{
65-
// ref manual: section 8.7.7 RCC domain 1 clock configuration register
66-
uint32_t ahb_prescaler = RCC->D1CFGR & RCC_D1CFGR_HPRE_Msk;
67-
if ((ahb_prescaler & 0b1000) != 0) {
68-
switch (ahb_prescaler) {
69-
case 0b1000:
70-
prescaler /= 2;
71-
break;
72-
case 0b1001:
73-
prescaler /= 4;
74-
break;
75-
case 0b1010:
76-
prescaler /= 8;
77-
break;
78-
case 0b1011:
79-
prescaler /= 16;
80-
break;
81-
case 0b1100:
82-
prescaler /= 64;
83-
break;
84-
case 0b1101:
85-
prescaler /= 128;
86-
break;
87-
case 0b1110:
88-
prescaler /= 256;
89-
break;
90-
case 0b1111:
91-
prescaler /= 512;
92-
break;
93-
}
94-
}
95-
96-
// ref manual: section 8.7.8: RCC domain 2 clock configuration register
97-
uint32_t apb1_prescaler = (RCC->D2CFGR & RCC_D2CFGR_D2PPRE1_Msk) >> RCC_D2CFGR_D2PPRE1_Pos;
98-
if ((apb1_prescaler & 0b100) != 0) {
99-
switch (apb1_prescaler) {
100-
case 0b100:
101-
prescaler /= 2;
102-
break;
103-
case 0b101:
104-
prescaler /= 4;
105-
break;
106-
case 0b110:
107-
prescaler /= 8;
108-
break;
109-
case 0b111:
110-
prescaler /= 16;
111-
break;
112-
}
113-
}
114-
// tim2clk = 2 x pclk1 when apb1_prescaler != 1
115-
if (apb1_prescaler != 1) {
116-
prescaler *= 2;
117-
}
118-
119-
if (prescaler > 1) {
120-
prescaler--;
121-
}
122-
}
123-
124-
if (prescaler == 0 || prescaler > 0xFFFF) {
125-
ErrorHandler("Invalid prescaler value: %u", prescaler);
126-
return;
127-
}
128-
129-
Scheduler_global_timer->PSC = (uint16_t)prescaler;
61+
uint16_t prescaler = (uint16_t)(ST_LIB::TimerDomain::get_timer_frequency(Scheduler_global_timer) / Scheduler::FREQUENCY);
62+
Scheduler_global_timer->PSC = prescaler;
13063
Scheduler_global_timer->ARR = 0;
13164
Scheduler_global_timer->DIER |= LL_TIM_DIER_UIE;
13265
Scheduler_global_timer->CR1 =
13366
LL_TIM_CLOCKDIVISION_DIV1 | (Scheduler_global_timer->CR1 & ~TIM_CR1_CKD);
13467

13568
Scheduler_global_timer->CNT = 0; /* Clear counter value */
13669

137-
NVIC_EnableIRQ(SCHEDULER_GLOBAL_TIMER_IRQn);
13870
CLEAR_BIT(Scheduler_global_timer->SR, LL_TIM_SR_UIF); /* clear update interrupt flag */
13971

14072
Scheduler::schedule_next_interval();
@@ -143,7 +75,7 @@ void Scheduler_start(void) {
14375
void Scheduler::update() {
14476
// NOTE: Only _one_ id will be shown per call to update()
14577
if(failing_id != Scheduler::INVALID_ID) [[unlikely]] {
146-
ErrorHandler("Too slow, could not execute task %u in time", failing_id);
78+
WARNING("Too slow, could not execute task %u in time", failing_id);
14779
failing_id = Scheduler::INVALID_ID;
14880
}
14981

0 commit comments

Comments
 (0)