Skip to content

Commit

Permalink
Add flipperzero::furi::kernel module
Browse files Browse the repository at this point in the history
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
dcoles committed Oct 21, 2024
1 parent cb902c2 commit 2572724
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Added

- `flipperzero::dialogs::DialogFileBrowserOptions`
- `flipperzero::furi::kernel` module exposing most `furi_kernel_*` APIs

### Changed

Expand Down
1 change: 1 addition & 0 deletions crates/flipperzero/src/furi/mod.rs
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;
Expand Down
9 changes: 5 additions & 4 deletions crates/flipperzero/src/furi/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use core::cmp::Ordering;
use core::iter::Sum;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

use flipperzero_sys as sys;
use ufmt::derive::uDebug;

use crate::furi;

/// Maximum number of ticks a [`Duration`] can contain to be usable with [`Instant`].
const MAX_INTERVAL_DURATION_TICKS: u32 = u32::MAX / 2;

Expand All @@ -16,7 +17,7 @@ const MILLIS_PER_SEC: u32 = 1_000;

/// Converts the given number of nanoseconds to ticks.
fn ns_to_ticks(nanos: u64) -> u64 {
let rate = unsafe { sys::furi_kernel_get_tick_frequency() };
let rate = furi::kernel::get_tick_frequency();
if rate == MILLIS_PER_SEC {
// This can be up to around 2^45 ticks.
nanos / NANOS_PER_MILLI
Expand All @@ -31,7 +32,7 @@ fn ns_to_ticks(nanos: u64) -> u64 {
///
/// The upper 2 bits of the return value will always be zero.
fn ticks_to_ns(ticks: u32) -> u64 {
let rate = unsafe { sys::furi_kernel_get_tick_frequency() };
let rate = furi::kernel::get_tick_frequency();
if rate == MILLIS_PER_SEC {
// This can be up to around 2^52 nanoseconds.
(ticks as u64) * NANOS_PER_MILLI
Expand All @@ -49,7 +50,7 @@ impl Instant {
/// Returns an instant corresponding to "now".
#[must_use]
pub fn now() -> Instant {
Instant(unsafe { sys::furi_get_tick() })
Instant(furi::kernel::get_tick())
}

/// Returns the amount of time elapsed from another instant to this one.
Expand Down

0 comments on commit 2572724

Please sign in to comment.