-
Notifications
You must be signed in to change notification settings - Fork 165
Add ability to set priority grouping. #431
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
Open
reitermarkus
wants to merge
4
commits into
rust-embedded:master
Choose a base branch
from
reitermarkus:priority-grouping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -119,6 +119,88 @@ mod fpu_consts { | |
#[cfg(has_fpu)] | ||
use self::fpu_consts::*; | ||
|
||
/// Priority Grouping | ||
/// | ||
/// Determines the split of preemption priority from sub-priority. | ||
#[derive(Debug, Clone, Copy)] | ||
#[repr(u8)] | ||
pub enum PriorityGrouping<const NVIC_PRIO_BITS: u8> { | ||
/// Priority grouping 0 | ||
Prigroup0 = 0, | ||
/// Priority grouping 1 | ||
Prigroup1 = 1, | ||
/// Priority grouping 2 | ||
Prigroup2 = 2, | ||
/// Priority grouping 3 | ||
Prigroup3 = 3, | ||
/// Priority grouping 4 | ||
Prigroup4 = 4, | ||
/// Priority grouping 5 | ||
Prigroup5 = 5, | ||
/// Priority grouping 6 | ||
Prigroup6 = 6, | ||
/// Priority grouping 7 | ||
Prigroup7 = 7, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these values are actually swapped, at least compared to the ST HAL. |
||
} | ||
|
||
impl<const NVIC_PRIO_BITS: u8> PriorityGrouping<NVIC_PRIO_BITS> { | ||
#[inline] | ||
const fn preemption_priority_bits(&self) -> u8 { | ||
let bits = 7 - *self as u8; | ||
|
||
if bits > NVIC_PRIO_BITS { | ||
NVIC_PRIO_BITS | ||
} else { | ||
bits | ||
} | ||
} | ||
|
||
#[inline] | ||
const fn sub_priority_bits(&self) -> u8 { | ||
let bits = *self as u8 + NVIC_PRIO_BITS; | ||
|
||
if bits <= 7 { | ||
0 | ||
} else { | ||
bits - 7 | ||
} | ||
} | ||
|
||
/// Encode `preemption_priority` and `sub_priority` to fit in `NVIC_PRIO_BITS`. | ||
#[inline] | ||
pub const fn encode_priority(&self, preemption_priority: u8, sub_priority: u8) -> u8 { | ||
let preemption_priority_bits = self.preemption_priority_bits(); | ||
let sub_priority_bits = self.sub_priority_bits(); | ||
|
||
let premption_priority_mask = (1 << preemption_priority_bits) - 1; | ||
let sub_priority_mask = (1 << sub_priority_bits) - 1; | ||
|
||
debug_assert!(preemption_priority <= premption_priority_mask); | ||
debug_assert!(sub_priority <= sub_priority_mask); | ||
|
||
let priority = ((preemption_priority & premption_priority_mask) << sub_priority_bits) | ||
| (sub_priority & sub_priority_mask); | ||
|
||
// Priority is stored in the highest bits. | ||
priority << (8 - NVIC_PRIO_BITS) | ||
} | ||
|
||
/// Decode the priority stored in `NVIC_PRIO_BITS` into a tuple consisting of | ||
/// the preemption priority and sub-priority. | ||
#[inline] | ||
pub const fn decode_priority(&self, mut priority: u8) -> (u8, u8) { | ||
// Priority is stored in the highest bits. | ||
priority >>= 8 - NVIC_PRIO_BITS; | ||
|
||
let sub_priority_bits = self.sub_priority_bits(); | ||
|
||
let preemption_priority = priority >> sub_priority_bits; | ||
let sub_priority = priority & ((1 << sub_priority_bits) - 1); | ||
|
||
(preemption_priority, sub_priority) | ||
} | ||
} | ||
|
||
#[cfg(has_fpu)] | ||
impl SCB { | ||
/// Shorthand for `set_fpu_access_mode(FpuAccessMode::Disabled)` | ||
|
@@ -839,7 +921,8 @@ impl SCB { | |
} | ||
|
||
const SCB_AIRCR_VECTKEY: u32 = 0x05FA << 16; | ||
const SCB_AIRCR_PRIGROUP_MASK: u32 = 0x7 << 8; | ||
const SCB_AIRCR_PRIGROUP_POS: u32 = 8; | ||
const SCB_AIRCR_PRIGROUP_MASK: u32 = 0x7 << SCB_AIRCR_PRIGROUP_POS; | ||
const SCB_AIRCR_SYSRESETREQ: u32 = 1 << 2; | ||
|
||
impl SCB { | ||
|
@@ -848,20 +931,49 @@ impl SCB { | |
pub fn sys_reset() -> ! { | ||
crate::asm::dsb(); | ||
unsafe { | ||
(*Self::PTR).aircr.modify( | ||
|r| { | ||
SCB_AIRCR_VECTKEY | // otherwise the write is ignored | ||
r & SCB_AIRCR_PRIGROUP_MASK | // keep priority group unchanged | ||
SCB_AIRCR_SYSRESETREQ | ||
}, // set the bit | ||
) | ||
(*Self::PTR).aircr.modify(|r| { | ||
SCB_AIRCR_VECTKEY | // Unlock for writing. | ||
r & SCB_AIRCR_PRIGROUP_MASK | // Keep priority grouping unchanged. | ||
SCB_AIRCR_SYSRESETREQ // Set reset bit. | ||
}) | ||
}; | ||
crate::asm::dsb(); | ||
loop { | ||
// wait for the reset | ||
crate::asm::nop(); // avoid rust-lang/rust#28728 | ||
} | ||
} | ||
|
||
/// Set the priority grouping. | ||
#[inline] | ||
pub fn set_priority_grouping<const NVIC_PRIO_BITS: u8>( | ||
&mut self, | ||
grouping: PriorityGrouping<NVIC_PRIO_BITS>, | ||
) { | ||
unsafe { | ||
self.aircr.write({ | ||
SCB_AIRCR_VECTKEY | // Unlock for writing. | ||
(grouping as u32) << SCB_AIRCR_PRIGROUP_POS | ||
}); | ||
} | ||
} | ||
|
||
/// Get the priority grouping. | ||
#[inline] | ||
pub fn get_priority_grouping<const NVIC_PRIO_BITS: u8>( | ||
&self, | ||
) -> PriorityGrouping<NVIC_PRIO_BITS> { | ||
match self.aircr.read() & SCB_AIRCR_PRIGROUP_MASK >> SCB_AIRCR_PRIGROUP_POS { | ||
0 => PriorityGrouping::Prigroup0, | ||
1 => PriorityGrouping::Prigroup1, | ||
2 => PriorityGrouping::Prigroup2, | ||
3 => PriorityGrouping::Prigroup3, | ||
4 => PriorityGrouping::Prigroup4, | ||
5 => PriorityGrouping::Prigroup5, | ||
6 => PriorityGrouping::Prigroup6, | ||
_ => PriorityGrouping::Prigroup7, | ||
} | ||
} | ||
} | ||
|
||
const SCB_ICSR_PENDSVSET: u32 = 1 << 28; | ||
|
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.
I think these values are actually swapped, at least compared to the ST HAL.