-
Notifications
You must be signed in to change notification settings - Fork 0
Scheduler implementation #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 85 commits
Commits
Show all changes
116 commits
Select commit
Hold shift + click to select a range
2a537ab
Add initial implementation of scheduler
victor-Lopez25 3f72fbe
Try to fix -Wuninitialized warning
victor-Lopez25 8620d88
Try to remove Wuninitialized warning temporarily
victor-Lopez25 87404bd
Remove unnecessary clear of task element
victor-Lopez25 7ea80c7
fix: Readability and todo for validation
victor-Lopez25 ed50013
fix: allocate_slot
victor-Lopez25 4f6bbac
Add explenation of sorted_task_ids_, static_assert for the bitmaps
victor-Lopez25 4323671
Remove long_wait_remaining_us_
victor-Lopez25 a10df84
fix: better get_at
victor-Lopez25 0234463
fix: better front_id
victor-Lopez25 a7cad06
fix: endianness in pop_front
victor-Lopez25 db13d89
Added unlikely to release_slot conditional
victor-Lopez25 022578a
fix: Correct comment explenation
victor-Lopez25 6addcb2
No bit field insert, it annoyed me
victor-Lopez25 4a6e6e5
No more diagnostic ignore
victor-Lopez25 8681ca3
fix: loss in accuracy by not adding 1: https://github.com/HyperloopUP…
victor-Lopez25 1ff50c7
fix: Moved active_task_count_ modification
victor-Lopez25 a2e6076
Actually set used_bitmap_ in allocate_slot
victor-Lopez25 cdfe081
only include sources when cross-compiling
g0nz4I0 2e0ce1b
modify CMakeLists inside Tests
g0nz4I0 8605cab
feat: use ugly ifdef guards for host testing purposes
g0nz4I0 affc234
add sample tests for Scheduler
g0nz4I0 58af59f
add emulated ticking
g0nz4I0 3d8bb6b
test for execution count
g0nz4I0 e24c637
fix: CI
g0nz4I0 f670cab
fix: expected result in test case + explanation
g0nz4I0 a75dd54
add compiler specific and CoreMacros
g0nz4I0 f8592f4
copy files from LL and CMSIS for TIM Registers
g0nz4I0 a88f569
bridge between LL MMIO Timer and user defined TIMER
g0nz4I0 53bddb9
reduce the number of ugly macros
g0nz4I0 b94ffbc
compile the new mock file
g0nz4I0 51676f6
update test
g0nz4I0 4dcc328
enable debugging of tests with TestMate C++ extension
g0nz4I0 d0d23e5
feat: add DSB and ISB intructions in x86_64
g0nz4I0 884c2ab
feat: add NVIC
g0nz4I0 29aeea5
add NVIC logic
g0nz4I0 0ad4c7b
feat: use NVIC and remove print
g0nz4I0 244eb90
fix: move extern"C" block to allow using <iostream>
g0nz4I0 a3a4a78
add Register class to allow for side effects
g0nz4I0 b09fe57
feat: use newly created Register class for side effects, neat trick i…
g0nz4I0 c97c4a2
feat: be as realistic as possible in test, by using CNT++
g0nz4I0 eb8094c
feat: remove unnecessary include
g0nz4I0 f763f0e
feat: add an __RBIT impl for x86_64
g0nz4I0 49e4f50
fix: compilation
g0nz4I0 cc4d72b
fix: used_bitmap_ -> free_bitmap_
victor-Lopez25 a5110ff
Merge branch 'feat/scheduler' of https://github.com/HyperloopUPV-H8/S…
victor-Lopez25 68b341e
Merge remote-tracking branch 'remotes/origin/MockTim' into feat/sched…
victor-Lopez25 51f073c
Added tests check in CI/CD
jorgesg82 74e86d3
fix(ci/cd): output folder
jorgesg82 292cc08
Added ARM specific instructions to be able to compile in ARM hosts (Mac)
jorgesg82 44e8d8c
Fix: compiler barrier for msvc, hopefully works
victor-Lopez25 a3fc792
Fix: Try to fix -Wchanges-meaning warning
victor-Lopez25 869883c
Fix typo
victor-Lopez25 7e5fe46
Update Inc/MockedDrivers/common.hpp
victor-Lopez25 7e42ef9
Actually do the change github didn't do :/
victor-Lopez25 712bd60
Fix typo p2
victor-Lopez25 82acd27
Fix another typo
victor-Lopez25 c0d5639
Fix unsigned long - 64bit when testing using LL functions
victor-Lopez25 91a3b75
Minor fixes to scheduler
StefanCostea 1ab3d5c
Fix: uint32_t* cast of a word instead of the ptr
victor-Lopez25 1a30868
Merge branch 'feat/scheduler' of https://github.com/HyperloopUPV-H8/S…
victor-Lopez25 069a288
Fix: remove active_task_count_ decrement in pop_front()
victor-Lopez25 fb5b90a
Try fix Wstrict-aliasing in front_id
victor-Lopez25 065cdad
Try fix Wstrict-aliasing again
victor-Lopez25 11a3aab
Fix typo in allocate_slot
victor-Lopez25 dee9879
fix: test UsedBitmap -> FreeBitmap
victor-Lopez25 8038b7b
used_bitmap_ -> free_bitmap_ in test part 2
victor-Lopez25 e7fdc30
fixed tests discovery
jorgesg82 78ffa82
fix: set_timeout() test
victor-Lopez25 26a69f2
Add a test, fix the error (kind of)
victor-Lopez25 12a50f3
Fix: underflow in schedule_next_interval
victor-Lopez25 6b06740
Fix: call schedule_next_interval() in start()
victor-Lopez25 21c5c15
Fix: invert condition, that was stupid
victor-Lopez25 6cfd3f3
feat: add test showcasing possible usage within StateMachine logic
g0nz4I0 1ac4c8c
fix: Calculate correct prescaler & add to HALAL.hpp
victor-Lopez25 03aad19
Try to fix tests
victor-Lopez25 b15ae75
try to fix tests part 2
victor-Lopez25 0c4bb24
Try to fix tests part 3
victor-Lopez25 8095c72
part 4 - add the wrapper.c to cmake, will this work?
victor-Lopez25 18c9915
part 5 - fix cmake stm32h723xx_wrapper.c path
victor-Lopez25 662df45
fix: try to fix tests part 6
victor-Lopez25 de82133
fix: volatile warnings
victor-Lopez25 e8a9141
fix: active_PSC is now set when PSC is set
victor-Lopez25 ce37e28
Merge branch 'development' into feat/scheduler
victor-Lopez25 a0ad409
fix: remove outdated comment
victor-Lopez25 0d627b4
fix: tests part 7, should work now
victor-Lopez25 978b282
fix: remove unecessary if, return if cancelled in cancel_timeout
victor-Lopez25 4f27b48
Pop all due tasks, several might be due in the same tick
victor-Lopez25 6014d29
Revert force-push
victor-Lopez25 a11f4bc
fix: off by one error + configure_timer_for_interval should use uint32_t
victor-Lopez25 f38b762
fix: off by one error in schedule_next_interval()
victor-Lopez25 79f327e
All 15 tasks in MultipleTasks test, and it works
victor-Lopez25 344618d
Add SameTaskMultipleTimes test, does all 16 tasks too
victor-Lopez25 5d3aa61
larger NUM_TICKS just in case
victor-Lopez25 238c062
fix bug mentioned in previous cancel_timeout()
victor-Lopez25 09fc585
fix: id uint32_t -> uint16_t + bug fix
victor-Lopez25 04ed0f7
fix: remove TimerPeripheral::start and Time::start from HALAL::start
victor-Lopez25 9dcffe1
try to fix ci/cd
victor-Lopez25 b92aebc
try to fix ci/cd part 2
victor-Lopez25 181590a
try to fix ci/cd part 3
victor-Lopez25 d48e00a
clean up ci/cd fix
victor-Lopez25 a210bc2
Remove unnecessary warning ignore
victor-Lopez25 01b24b6
fix: use wrapper instead of interface for stm32h7xx_ll_tim.h
victor-Lopez25 e8a90e6
fix ci/cd and add the other submodule to be automatically added
victor-Lopez25 cab07f9
fix: actually add stm32h7xx_ll_tim_wrapper.h
victor-Lopez25 86d3008
try to fix ci/cd warning
victor-Lopez25 8856395
try fix warnings again
victor-Lopez25 e8b7d8d
fix: add ignore Woverflow to stm32h7xx_ll_tim
victor-Lopez25 8506d92
fix: remove need to ignore Woverflow warnings
victor-Lopez25 9313709
fix: Remove tim_register_definitions.hpp
victor-Lopez25 2e8fb0f
Fix overflow calculation and a test
victor-Lopez25 9e742c7
Add link to wiki
victor-Lopez25 90be546
Merge remote-tracking branch 'origin/development' into feat/scheduler
victor-Lopez25 f619df5
fix: revert .vscode settings change
victor-Lopez25 76d38a7
revert .vscode settings change part 2
victor-Lopez25 93d0fda
finish reverting change
victor-Lopez25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| name: Run tests | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| pull_request: | ||
| branches: [ development ] | ||
|
|
||
| jobs: | ||
| tests: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Install build dependencies | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y cmake ninja-build build-essential | ||
|
|
||
| - name: Configure (CMake) | ||
| run: | | ||
| cmake --preset simulator | ||
|
|
||
| - name: Build | ||
| run: | | ||
| cmake --build out/build/simulator | ||
|
|
||
| - name: Run tests (ctest) | ||
| working-directory: out/build/simulator | ||
| run: | | ||
| ctest --output-on-failure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Scheduler.hpp | ||
| * | ||
| * Created on: 17 nov. 2025 | ||
| * Author: Victor (coauthor Stephan) | ||
| */ | ||
| #pragma once | ||
|
|
||
| #ifndef TESTING_ENV | ||
| #include "stm32h7xx_ll_tim.h" | ||
| #else | ||
| #include "MockedDrivers/ll_tim_interface.h" | ||
| #endif | ||
| #include <array> | ||
| #include <cstdint> | ||
| #include <functional> | ||
|
|
||
| /* NOTE(vic): Pido perdón a Boris pero es la mejor manera que se me ha ocurrido hacer esto | ||
| * Cambiar el SCHEDULER_TIMER_IDX si es mejor usar otro timer que no sea TIM2 | ||
| */ | ||
| #ifndef SCHEDULER_TIMER_IDX | ||
| # define SCHEDULER_TIMER_IDX 2 | ||
| #endif | ||
|
|
||
| #define glue_(a,b) a ## b | ||
| #define glue(a,b) glue_(a,b) | ||
| #define SCHEDULER_TIMER_BASE glue(TIM, glue(SCHEDULER_TIMER_IDX, _BASE)) | ||
|
|
||
| // Used to reserve a TimerPeripheral | ||
| #ifndef TESTING_ENV | ||
| #include "stm32h7xx_hal_tim.h" | ||
| #define SCHEDULER_HAL_TIM glue(htim, SCHEDULER_TIMER_IDX) | ||
| extern TIM_HandleTypeDef SCHEDULER_HAL_TIM; | ||
| #endif | ||
|
|
||
| struct Scheduler { | ||
| using callback_t = void (*)(); | ||
| static constexpr uint32_t INVALID_ID = 0xFFu; | ||
|
|
||
| static void start(); | ||
| static void update(); | ||
| static inline uint64_t get_global_tick(); | ||
|
|
||
| static inline uint8_t register_task(uint32_t period_us, callback_t func) { | ||
| if(period_us == 0) [[unlikely]] period_us = 1; | ||
| return register_task(period_us, func, true); | ||
| } | ||
| static bool unregister_task(uint8_t id); | ||
|
|
||
| static inline uint8_t set_timeout(uint32_t microseconds, callback_t func) { | ||
| if(microseconds == 0) [[unlikely]] microseconds = 1; | ||
| return register_task(microseconds, func, false); | ||
| } | ||
| static inline void cancel_timeout(uint8_t id) { | ||
| /* NOTE: This does not fix this case: | ||
| 1. id = set_timeout(x, func) | ||
| 2. timeout ends, func gets called and removed internally | ||
| 3. id_2 = set_timeout(y, func_2) // id will be equal to id_2 | ||
| 4. clear_timeout(id) -> will remove the second timeout | ||
| */ | ||
| if(tasks_[id].repeating) return; | ||
|
victor-Lopez25 marked this conversation as resolved.
Outdated
|
||
| unregister_task(id); | ||
| } | ||
|
|
||
| // static void global_timer_callback(); | ||
|
|
||
| // Have to be public because SCHEDULER_GLOBAL_TIMER_CALLBACK won't work otherwise | ||
| //static const uint32_t global_timer_base = SCHEDULER_TIMER_BASE; | ||
| static void on_timer_update(); | ||
|
|
||
| #ifndef TESTING_ENV | ||
| private: | ||
| #endif | ||
| struct Task { | ||
| uint64_t next_fire_us{0}; | ||
|
victor-Lopez25 marked this conversation as resolved.
Outdated
|
||
| callback_t callback{}; | ||
| uint32_t period_us{0}; | ||
| bool repeating{false}; | ||
| }; | ||
|
|
||
| static constexpr std::size_t kMaxTasks = 16; | ||
| static_assert((kMaxTasks & (kMaxTasks - 1)) == 0, "kMaxTasks must be a power of two"); | ||
| static constexpr uint32_t FREQUENCY = 1'000'000u; // 1 MHz -> 1us precision | ||
|
|
||
| static std::array<Task, kMaxTasks> tasks_; | ||
| static_assert(kMaxTasks == 16, "kMaxTasks must be 16, if more is needed, sorted_task_ids_ must change"); | ||
| /* sorted_task_ids_ is a sorted queue with 4bits for each id in the scheduler's current ids */ | ||
| static uint64_t sorted_task_ids_; | ||
|
|
||
| static uint32_t active_task_count_; | ||
| static_assert(kMaxTasks <= 32, "kMaxTasks must be <= 32, if more is needed, the bitmaps must change"); | ||
| static uint32_t ready_bitmap_; | ||
| static uint32_t free_bitmap_; | ||
| static uint64_t global_tick_us_; | ||
| static uint64_t current_interval_us_; | ||
|
|
||
| static inline uint8_t allocate_slot(); | ||
| static inline void release_slot(uint8_t id); | ||
| static void insert_sorted(uint8_t id); | ||
| static void remove_sorted(uint8_t id); | ||
| static void schedule_next_interval(); | ||
| static inline void configure_timer_for_interval(uint64_t microseconds); | ||
| static uint8_t register_task(uint32_t period_us, callback_t func, bool repeating); | ||
|
|
||
| // helpers | ||
| static inline uint8_t get_at(uint8_t idx); | ||
| static inline void set_at(uint8_t idx, uint8_t id); | ||
| static inline void pop_front(); | ||
| static inline uint8_t front_id(); | ||
|
|
||
| static inline void global_timer_disable(); | ||
| static inline void global_timer_enable(); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #pragma once | ||
|
|
||
| #include "stm32h723xx_wrapper.h" | ||
| #include "MockedDrivers/common.hpp" | ||
| #include "MockedDrivers/compiler_specific.hpp" | ||
|
|
||
| class NVIC_Type | ||
| { | ||
| public: | ||
| volatile uint32_t ISER[8U]; /*!< Offset: 0x000 (R/W) Interrupt Set Enable Register */ | ||
| uint32_t RESERVED0[24U]; | ||
| volatile uint32_t ICER[8U]; /*!< Offset: 0x080 (R/W) Interrupt Clear Enable Register */ | ||
| uint32_t RESERVED1[24U]; | ||
| volatile uint32_t ISPR[8U]; /*!< Offset: 0x100 (R/W) Interrupt Set Pending Register */ | ||
| uint32_t RESERVED2[24U]; | ||
| volatile uint32_t ICPR[8U]; /*!< Offset: 0x180 (R/W) Interrupt Clear Pending Register */ | ||
| uint32_t RESERVED3[24U]; | ||
| volatile uint32_t IABR[8U]; /*!< Offset: 0x200 (R/W) Interrupt Active bit Register */ | ||
| uint32_t RESERVED4[56U]; | ||
| volatile uint8_t IP[240U]; /*!< Offset: 0x300 (R/W) Interrupt Priority Register (8Bit wide) */ | ||
| uint32_t RESERVED5[644U]; | ||
| volatile uint32_t STIR; /*!< Offset: 0xE00 ( /W) Software Trigger Interrupt Register */ | ||
| }; | ||
|
|
||
| extern NVIC_Type* NVIC; | ||
|
|
||
| extern "C"{ | ||
| void NVIC_EnableIRQ(IRQn_Type IRQn); | ||
|
|
||
| uint32_t NVIC_GetEnableIRQ(IRQn_Type IRQn); | ||
|
|
||
| void NVIC_DisableIRQ(IRQn_Type IRQn); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #pragma once | ||
| #include <cstdint> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| template<typename EnumType, EnumType Reg> | ||
| struct RegisterTraits { | ||
| static void write(uint32_t& target, uint32_t val) { | ||
| target = val; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| template<typename EnumType, EnumType Reg> | ||
| class RegisterBase { | ||
| public: | ||
| uint32_t reg = 0; | ||
|
|
||
| RegisterBase() = default; | ||
| RegisterBase(uint32_t val) : reg(val) {} | ||
|
|
||
| RegisterBase& operator=(uint32_t val) { | ||
| set(val); | ||
| return *this; | ||
| } | ||
| RegisterBase& operator+=(uint32_t val){ | ||
| set(reg+val); | ||
| return *this; | ||
| } | ||
| RegisterBase& operator-=(uint32_t val){ | ||
| set(reg-val); | ||
| return *this; | ||
| } | ||
|
|
||
| RegisterBase& operator&=(uint32_t mask) { | ||
| set(reg & mask); | ||
| return *this; | ||
| } | ||
|
|
||
| RegisterBase& operator|=(uint32_t mask) { | ||
| set(reg | mask); | ||
| return *this; | ||
| } | ||
|
|
||
| RegisterBase& operator^=(uint32_t mask) { | ||
| set(reg ^ mask); | ||
| return *this; | ||
| } | ||
| RegisterBase& operator<<=(int shift) { | ||
| set(reg << shift); // Triggers Traits | ||
| return *this; | ||
| } | ||
|
|
||
| RegisterBase& operator>>=(int shift) { | ||
| set(reg >> shift); // Triggers Traits | ||
| return *this; | ||
| } | ||
|
|
||
| // --- Shift Read (Does NOT modify Register) --- | ||
| // Usage: uint32_t val = REG >> 4; | ||
| uint32_t operator<<(int shift) const { | ||
| return reg << shift; | ||
| } | ||
|
|
||
| uint32_t operator>>(int shift) const { | ||
| return reg >> shift; | ||
| } | ||
| RegisterBase& operator++() { | ||
| set(reg + 1); | ||
| return *this; | ||
| } | ||
|
|
||
| // --- Postfix Increment (REG++) --- | ||
| // int argument is a dummy flag for C++ to distinguish postfix | ||
| uint32_t operator++(int) { | ||
| uint32_t old_val = reg; | ||
| set(reg + 1); | ||
| return old_val; | ||
| } | ||
|
|
||
| RegisterBase& operator--() { | ||
| set(reg - 1); | ||
| return *this; | ||
| } | ||
|
|
||
| uint32_t operator--(int) { | ||
| uint32_t old_val = reg; | ||
| set(reg - 1); | ||
| return old_val; | ||
| } | ||
| /** | ||
| * COMPARISON | ||
| */ | ||
| bool operator!=(uint32_t val) const { | ||
| return reg != val; | ||
| } | ||
| bool operator==(uint32_t val) const { | ||
| return reg == val; | ||
| } | ||
| /** | ||
| * BITWISE COMPARISONS | ||
| */ | ||
| operator uint32_t(){ | ||
| return reg; | ||
| } | ||
| operator uint32_t() const volatile { | ||
| return reg; | ||
| } | ||
| operator uint32_t() const { | ||
| return reg; | ||
| } | ||
|
|
||
| protected: | ||
| void set(uint32_t val) { | ||
| RegisterTraits<EnumType, Reg>::write(this->reg, val); | ||
| } | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be moved into a variable, MockedSources or something like that