Skip to content

Commit 91a3b75

Browse files
StefanCosteavictor-Lopez25
authored andcommitted
Minor fixes to scheduler
Rebased from remotes/origin/MockTim
1 parent c0d5639 commit 91a3b75

3 files changed

Lines changed: 86 additions & 21 deletions

File tree

Inc/MockedDrivers/common.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef enum
4545

4646
#define READ_REG(REG) ((REG))
4747

48-
#define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))
48+
#define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(REG)) & (~(uint32_t)(CLEARMASK))) | (SETMASK)))
4949

5050
#define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))
5151

@@ -65,7 +65,7 @@ typedef enum
6565
do { \
6666
uint32_t val; \
6767
do { \
68-
val = __LDREXW((__IO uint32_t *)&(REG)) & ~(BIT); \
68+
val = __LDREXW((__IO uint32_t *)&(REG)) & ~(uint32_t)(BIT); \
6969
} while ((__STREXW(val,(__IO uint32_t *)&(REG))) != 0U); \
7070
} while(0)
7171

@@ -74,7 +74,7 @@ typedef enum
7474
do { \
7575
uint32_t val; \
7676
do { \
77-
val = (__LDREXW((__IO uint32_t *)&(REG)) & ~(CLEARMSK)) | (SETMASK); \
77+
val = (__LDREXW((__IO uint32_t *)&(REG)) & ~(uint32_t)(CLEARMSK)) | (SETMASK); \
7878
} while ((__STREXW(val,(__IO uint32_t *)&(REG))) != 0U); \
7979
} while(0)
8080

@@ -92,7 +92,7 @@ typedef enum
9292
do { \
9393
uint16_t val; \
9494
do { \
95-
val = __LDREXH((__IO uint16_t *)&(REG)) & ~(BIT); \
95+
val = __LDREXH((__IO uint16_t *)&(REG)) & ~(uint16_t)(BIT); \
9696
} while ((__STREXH(val,(__IO uint16_t *)&(REG))) != 0U); \
9797
} while(0)
9898

@@ -101,6 +101,6 @@ typedef enum
101101
do { \
102102
uint16_t val; \
103103
do { \
104-
val = (__LDREXH((__IO uint16_t *)&(REG)) & ~(CLEARMSK)) | (SETMASK); \
104+
val = (__LDREXH((__IO uint16_t *)&(REG)) & ~(uint16_t)(CLEARMSK)) | (SETMASK); \
105105
} while ((__STREXH(val,(__IO uint16_t *)&(REG))) != 0U); \
106106
} while(0)

Src/HALAL/Services/Time/Scheduler.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ uint64_t Scheduler::current_interval_us_{0};
4040
inline uint8_t Scheduler::get_at(uint8_t idx) {
4141
int word_idx = idx > 7;
4242
uint32_t shift = (idx & 7) << 2;
43-
return (((uint32_t*)sorted_task_ids_)[word_idx] & (0x0F << shift)) >> shift;
43+
return (((uint32_t*)&sorted_task_ids_)[word_idx] & (0x0F << shift)) >> shift;
4444
}
4545
inline void Scheduler::set_at(uint8_t idx, uint8_t id) {
4646
uint32_t shift = idx*4;
@@ -49,7 +49,7 @@ inline void Scheduler::set_at(uint8_t idx, uint8_t id) {
4949
// sorted_task_ids_ |= ((id & 0x0F) << shift); // This is also an option in case id is incorrect, I don't think it's necessary though
5050
}
5151
inline uint8_t Scheduler::front_id() {
52-
return ((uint32_t*)sorted_task_ids_)[0] & 0xF;
52+
return ((uint32_t*)&sorted_task_ids_)[0] & 0xF;
5353
}
5454
inline void Scheduler::pop_front() {
5555
// O(1) remove of logical index 0
@@ -94,17 +94,17 @@ void Scheduler::start() {
9494
Scheduler_global_timer->CNT = 0; /* Clear counter value */
9595

9696
NVIC_EnableIRQ(SCHEDULER_GLOBAL_TIMER_IRQn);
97-
LL_TIM_ClearFlag_UPDATE(Scheduler_global_timer);
97+
CLEAR_BIT(Scheduler_global_timer->SR, LL_TIM_SR_UIF); /* clear update interrupt flag */
9898
// NOTE(vic): We don't need to set the flag since there won't be any tasks at the start/it will get set in schedule_next_interval()
9999
Scheduler::global_timer_enable();
100100
//Scheduler::schedule_next_interval();
101101
}
102102

103-
SCHEDULER_GLOBAL_TIMER_CALLBACK() {
104-
/* clear update interrupt flag */
105-
LL_TIM_ClearFlag_UPDATE(Scheduler_global_timer);
103+
SCHEDULER_GLOBAL_TIMER_CALLBACK() {
104+
CLEAR_BIT(Scheduler_global_timer->SR, TIM_SR_UIF);
106105
Scheduler::on_timer_update();
107106
}
107+
108108
void Scheduler::update() {
109109
while(ready_bitmap_ != 0u) {
110110
uint32_t bit_index = static_cast<uint32_t>(__builtin_ctz(ready_bitmap_));
@@ -183,6 +183,7 @@ void Scheduler::insert_sorted(uint8_t id) {
183183
}
184184

185185
sorted_task_ids_ = ((uint64_t)hi << 32) | lo;
186+
active_task_count_++;
186187
}
187188

188189
void Scheduler::remove_sorted(uint8_t id) {
@@ -215,6 +216,7 @@ void Scheduler::remove_sorted(uint8_t id) {
215216

216217
// Remove element (lower part | higher pushing nibble out of mask)
217218
Scheduler::sorted_task_ids_ = (Scheduler::sorted_task_ids_ & mask) | ((Scheduler::sorted_task_ids_ >> 4) & ~mask);
219+
active_task_count_--;
218220
}
219221

220222
void Scheduler::schedule_next_interval() {

Tests/Time/scheduler_test.cpp

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,95 @@
22
#include <thread>
33
#include<chrono>
44
#include "HALAL/Services/Time/Scheduler.hpp"
5+
56
int count = 0;
67
void fake_workload(){
78
count++;
89
}
910

10-
TEST(SchedulerTests, UsedBitmap) {
11+
class SchedulerTests : public ::testing::Test {
12+
protected:
13+
void SetUp() override {
14+
Scheduler::active_task_count_ = 0;
15+
Scheduler::used_bitmap_ = 0;
16+
Scheduler::ready_bitmap_ = 0;
17+
Scheduler::sorted_task_ids_ = 0;
18+
Scheduler::global_tick_us_ = 0;
19+
Scheduler::current_interval_us_ = 0;
20+
21+
// Reset global callback task count
22+
count = 0;
23+
24+
// Reset Timer
25+
TIM2_BASE->CNT = 0;
26+
TIM2_BASE->ARR = 0;
27+
TIM2_BASE->SR = 0;
28+
TIM2_BASE->CR1 = 0;
29+
TIM2_BASE->DIER = 0;
30+
}
31+
};
32+
33+
TEST_F(SchedulerTests, UsedBitmap) {
1134
Scheduler::register_task(10,&fake_workload);
1235
EXPECT_EQ(Scheduler::used_bitmap_,1);
1336
}
1437

15-
TEST(SchedulerTests, TaskRegistration) {
38+
TEST_F(SchedulerTests, TaskRegistration) {
1639
Scheduler::register_task(10,&fake_workload);
1740
EXPECT_EQ(Scheduler::tasks_[0].callback,fake_workload);
1841
}
1942

20-
TEST(SchedulerTests, TaskExecution) {
43+
TEST_F(SchedulerTests, TaskExecutionShort) {
44+
Scheduler::register_task(10,&fake_workload);
45+
Scheduler::start();
46+
// TIM2_BASE->ARR = 500;
47+
// TIM2_BASE->generate_update();
48+
49+
constexpr int NUM_TICKS = 1'000;
50+
for(int i = 0; i < NUM_TICKS; i++){
51+
TIM2_BASE->CNT++;
52+
Scheduler::update();
53+
}
54+
// 1000 ticks / 10 ticks/task = 100 executions.
55+
EXPECT_EQ(count, 100);
56+
}
57+
58+
TEST_F(SchedulerTests, TaskExecutionLong) {
2159
Scheduler::register_task(10,&fake_workload);
2260
Scheduler::start();
61+
// TIM2_BASE->ARR = 500;
62+
// TIM2_BASE->generate_update();
63+
2364
constexpr int NUM_TICKS = 1'000'000;
24-
TIM2_BASE->ARR = 500;
25-
TIM2_BASE->generate_update();
26-
for(int i = 0; i <= NUM_TICKS; i++){
65+
for(int i = 0; i < NUM_TICKS; i++){
2766
TIM2_BASE->CNT++;
2867
Scheduler::update();
2968
}
30-
// one tick is 1us, and we register a task that executes every 10us
31-
// thus it should execute NUM_TICKS/10
32-
EXPECT_EQ(count,100'000);
33-
}
69+
EXPECT_EQ(count, 100'000);
70+
}
71+
72+
TEST_F(SchedulerTests, SetTimeout) {
73+
Scheduler::set_timeout(10, &fake_workload);
74+
Scheduler::start();
75+
76+
constexpr int NUM_TICKS = 100;
77+
for(int i = 0; i < NUM_TICKS; i++){
78+
TIM2_BASE->CNT++;
79+
Scheduler::update();
80+
}
81+
EXPECT_EQ(count, 1);
82+
}
83+
84+
TEST_F(SchedulerTests, GlobalTickOverflow) {
85+
Scheduler::global_tick_us_ = 0xFFFFFFF0ULL; // Near 32-bit max
86+
Scheduler::register_task(20, &fake_workload);
87+
Scheduler::start();
88+
89+
constexpr int NUM_TICKS = 100;
90+
for(int i = 0; i < NUM_TICKS; i++){
91+
TIM2_BASE->CNT++;
92+
Scheduler::update();
93+
}
94+
// 100 ticks /20 ticks/task = 5 executions.
95+
EXPECT_EQ(count, 5);
96+
}

0 commit comments

Comments
 (0)