-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
flipperzero::furi::kernel
module
This module exposes most of the `furi_kernel_*` APIs. Centralizing these lets us remove a number of `unsafe` calls peppered across the codebase and allows things like providing optimized versions.
- Loading branch information
Showing
4 changed files
with
96 additions
and
4 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//! Furi Kernel primitives. | ||
use flipperzero_sys as sys; | ||
use ufmt::derive::uDebug; | ||
|
||
use crate::furi; | ||
|
||
/// Check if CPU is in IRQ; or kernel running and IRQ is masked. | ||
pub fn is_irq_or_masked() -> bool { | ||
unsafe { sys::furi_kernel_is_irq_or_masked() } | ||
} | ||
|
||
/// Check if kernel is running. | ||
pub fn is_running() -> bool { | ||
unsafe { sys::furi_kernel_is_irq_or_masked() } | ||
} | ||
|
||
/// Kernel lock state. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, uDebug)] | ||
pub enum LockState { | ||
/// normal scheduling | ||
Unlocked = 0, | ||
/// scheduling paused | ||
Locked = 1, | ||
} | ||
|
||
impl From<i32> for LockState { | ||
fn from(value: i32) -> Self { | ||
match value { | ||
0 => LockState::Unlocked, | ||
1 => LockState::Locked, | ||
x => panic!("invalid lock state value: {x}"), | ||
} | ||
} | ||
} | ||
|
||
impl From<LockState> for i32 { | ||
fn from(value: LockState) -> Self { | ||
value as i32 | ||
} | ||
} | ||
|
||
/// Lock kernel, pause process scheduling. | ||
/// | ||
/// **warning** This should never be called in an interrupt request context. | ||
/// | ||
/// Returns previous lock state. | ||
pub fn lock() -> furi::Result<LockState> { | ||
let status = sys::furi::Status::from(unsafe { sys::furi_kernel_lock() }); | ||
|
||
status.err_or_else(|status| status.0.into()) | ||
} | ||
|
||
/// Unlock kernel, resume process scheduling. | ||
/// | ||
/// **warning** This should never be called in an interrupt request context. | ||
/// | ||
/// Returns previous lock state. | ||
pub fn unlock() -> furi::Result<LockState> { | ||
let status = sys::furi::Status::from(unsafe { sys::furi_kernel_unlock() }); | ||
|
||
status.err_or_else(|status| status.0.into()) | ||
} | ||
|
||
/// Restore kernel lock state. | ||
/// | ||
/// **warning** This should never be called in an interrupt request context. | ||
/// | ||
/// Returns previous lock state. | ||
pub fn restore_lock(state: LockState) -> furi::Result<LockState> { | ||
let status = sys::furi::Status::from(unsafe { sys::furi_kernel_restore_lock(state.into()) }); | ||
|
||
status.err_or_else(|status| status.0.into()) | ||
} | ||
|
||
/// Return kernel tick frequency in hertz. | ||
#[inline] | ||
pub fn get_tick_frequency() -> u32 { | ||
unsafe { sys::furi_kernel_get_tick_frequency() } | ||
} | ||
|
||
/// Return current kernel tick value. | ||
/// | ||
/// The duration of a tick depends on kernel configuration. | ||
/// The value can be discovered with [`get_tick_frequency`]. | ||
#[inline] | ||
pub fn get_tick() -> u32 { | ||
unsafe { sys::furi_get_tick() } | ||
} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//! Furi API. | ||
pub mod io; | ||
pub mod kernel; | ||
pub mod log; | ||
pub mod message_queue; | ||
pub mod rng; | ||
|
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