Skip to content

Commit ad3a3e3

Browse files
Hotfix/sched register b4 start (#593)
1 parent 29c3137 commit ad3a3e3

5 files changed

Lines changed: 19 additions & 12 deletions

File tree

Inc/HALAL/Models/TimerDomain/TimerDomain.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#error Scheduler timer must be a 32 bit timer
2828
#endif
2929
extern TIM_TypeDef* Scheduler_global_timer;
30+
void Scheduler_global_timer_callback(void* raw);
31+
void Scheduler_start(void);
3032

3133
// NOTE: only works for static arrays
3234
#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*a))
@@ -676,7 +678,10 @@ TimerXList
676678

677679
static void init(std::span<const Config, N> cfgs) {
678680
Scheduler_global_timer = cmsis_timers[timer_idxmap[SCHEDULER_TIMER_DOMAIN]];
681+
callbacks[ST_LIB::timer_idxmap[SCHEDULER_TIMER_DOMAIN]] =
682+
Scheduler_global_timer_callback;
679683
rcc_enable_timer(Scheduler_global_timer);
684+
Scheduler_start();
680685

681686
for (std::size_t i = 0; i < N; i++) {
682687
const Config& e = cfgs[i];

Inc/HALAL/Services/Time/Scheduler.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#endif
2121

2222
extern TIM_TypeDef* Scheduler_global_timer;
23+
void Scheduler_global_timer_callback(void* raw);
24+
void Scheduler_start(void);
2325

2426
struct Scheduler {
2527
using callback_t = void (*)();
@@ -28,7 +30,8 @@ struct Scheduler {
2830
// if it isn't it could theoretically be used as an id in set_timeout
2931
static constexpr uint32_t INVALID_ID = 2 * kMaxTasks;
3032

31-
static void start();
33+
// temporary, will be removed
34+
[[deprecated]] static inline void start() {}
3235
static void update();
3336
static inline uint64_t get_global_tick() {
3437
return global_tick_us_ + Scheduler_global_timer->CNT;
@@ -42,6 +45,8 @@ struct Scheduler {
4245

4346
// internal
4447
static void on_timer_update();
48+
static void schedule_next_interval();
49+
static constexpr uint32_t FREQUENCY = 1'000'000u; // 1 MHz -> 1us precision
4550
#ifndef SIM_ON
4651
private:
4752
#endif
@@ -60,7 +65,6 @@ struct Scheduler {
6065
static_assert(INVALID_ID >= kMaxTasks, "INVALID_ID must not be a possible task id");
6166

6267
static_assert((kMaxTasks & (kMaxTasks - 1)) == 0, "kMaxTasks must be a power of two");
63-
static constexpr uint32_t FREQUENCY = 1'000'000u; // 1 MHz -> 1us precision
6468

6569
static std::array<Task, kMaxTasks> tasks_;
6670
static_assert(
@@ -86,8 +90,6 @@ struct Scheduler {
8690
static inline void release_slot(uint8_t id);
8791
static void insert_sorted(uint8_t id);
8892
static void remove_sorted(uint8_t id);
89-
static void schedule_next_interval();
90-
static inline void configure_timer_for_interval(uint32_t microseconds);
9193

9294
// helpers
9395
static inline uint8_t get_at(uint8_t idx);

Src/HALAL/Services/Time/Scheduler.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,18 @@ inline void Scheduler::global_timer_enable() {
6060
}
6161

6262
// ----------------------------
63-
void scheduler_global_timer_callback(void* raw) {
63+
void Scheduler_global_timer_callback(void* raw) {
6464
(void)raw;
6565
Scheduler::on_timer_update();
6666
}
6767
// ----------------------------
6868

69-
void Scheduler::start() {
69+
void Scheduler_start(void) {
7070
static_assert((Scheduler::FREQUENCY % 1'000'000) == 0u, "frequenct must be a multiple of 1MHz");
7171
Scheduler_global_timer =
7272
ST_LIB::TimerDomain::cmsis_timers[ST_LIB::timer_idxmap[SCHEDULER_TIMER_DOMAIN]];
73+
ST_LIB::TimerDomain::callbacks[ST_LIB::timer_idxmap[static_cast<uint8_t>(SCHEDULER_TIMER_DOMAIN
74+
)]] = Scheduler_global_timer_callback;
7375

7476
// TODO: change this to use TimerDomain::get_timer_clock()?
7577
uint32_t prescaler = (SystemCoreClock / Scheduler::FREQUENCY);
@@ -145,9 +147,6 @@ void Scheduler::start() {
145147
Scheduler_global_timer->CR1 =
146148
LL_TIM_CLOCKDIVISION_DIV1 | (Scheduler_global_timer->CR1 & ~TIM_CR1_CKD);
147149

148-
ST_LIB::TimerDomain::callbacks[ST_LIB::timer_idxmap[static_cast<uint8_t>(SCHEDULER_TIMER_DOMAIN
149-
)]] = scheduler_global_timer_callback;
150-
151150
Scheduler_global_timer->CNT = 0; /* Clear counter value */
152151

153152
NVIC_EnableIRQ(SCHEDULER_GLOBAL_TIMER_IRQn);

Tests/StateMachine/state_machine_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class StateMachineTest : public ::testing::Test {
155155
test_nested_machine.get_states()[1].unregister_all_timed_actions();
156156

157157
reset_test_state();
158+
159+
Scheduler_start();
158160
}
159161
};
160162

@@ -216,8 +218,6 @@ TEST_F(StateMachineTest, MasterStateChangeExitsNested) {
216218
TEST_F(StateMachineTest, CyclicActionsRun) {
217219
test_machine.start();
218220

219-
Scheduler::start();
220-
221221
tick_scheduler(100);
222222
tick_scheduler(10000);
223223

@@ -242,7 +242,6 @@ static int check_transition_task_count = 0;
242242

243243
TEST_F(StateMachineTest, StressTestWithScheduler) {
244244
test_machine.start();
245-
Scheduler::start();
246245

247246
custom_task_count = 0;
248247
check_transition_task_count = 0;

Tests/Time/scheduler_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class SchedulerTests : public ::testing::Test {
2727
TIM2_BASE->SR = 0;
2828
TIM2_BASE->CR1 = 0;
2929
TIM2_BASE->DIER = 0;
30+
31+
Scheduler_start();
3032
}
3133
};
3234

0 commit comments

Comments
 (0)