Skip to main content

System

Struct System 

Source
pub struct System;
Expand description

System-level functions (scheduler, timing, critical sections). System-level operations and utilities.

Provides a collection of static methods for controlling the FreeRTOS scheduler and accessing system-wide information. All methods are static.

§Examples

§Starting the scheduler

use osal_rs::os::{System, SystemFn};
 
// Create threads, queues, etc.
// ...
 
// Start the scheduler (never returns in normal operation)
System::start();

§Delays and timing

use osal_rs::os::{System, SystemFn};
use core::time::Duration;
 
// Simple delay
System::delay_with_to_tick(Duration::from_millis(500));
 
// Get current system time
let now = System::get_current_time_us();
println!("Uptime: {:?}", now);
 
// Periodic execution using delay_until
let mut last_wake = System::get_tick_count();
loop {
    System::delay_until_with_to_tick(&mut last_wake, Duration::from_millis(100));
    println!("Periodic task");
}

§Critical sections

use osal_rs::os::{System, SystemFn};
 
// Protect shared data
System::critical_section_enter();
// Access shared data here
// ...
System::critical_section_exit();

§Thread enumeration

use osal_rs::os::{System, SystemFn};
 
let count = System::count_threads();
println!("Active threads: {}", count);
 
let state = System::get_all_thread();
for thread in &state.tasks {
    println!("Thread: {} - Stack high water: {}",
        thread.name,
        thread.stack_high_water_mark
    );
}

§Heap monitoring

use osal_rs::os::{System, SystemFn};
 
let free_heap = System::get_free_heap_size();
println!("Free heap: {} bytes", free_heap);

§Scheduler suspend/resume

use osal_rs::os::{System, SystemFn};
 
// Suspend scheduler for atomic operations
System::suspend_all();
// Perform atomic operations
// ...
System::resume_all();

System-level operations and scheduler control.

Provides static methods for controlling the RTOS scheduler, timing, and system-wide operations.

Implementations§

Source§

impl System

Source

pub fn delay_with_to_tick(ticks: impl ToTick)

Delays execution using a type that implements ToTick.

Convenience method that accepts Duration or other tick-convertible types.

§Examples
use osal_rs::os::{System, SystemFn};
use core::time::Duration;
 
System::delay_with_to_tick(Duration::from_millis(100));
Source

pub fn delay_until_with_to_tick( previous_wake_time: &mut TickType, time_increment: impl ToTick, )

Delays until an absolute time point with tick conversion.

Used for precise periodic timing.

§Parameters
  • previous_wake_time - Previous wake time (updated by this function)
  • time_increment - Time increment for next wake
§Examples
use osal_rs::os::{System, SystemFn};
use core::time::Duration;
 
let mut last_wake = System::get_tick_count();
loop {
    // Do work...
    System::delay_until_with_to_tick(&mut last_wake, Duration::from_millis(100));
}

Trait Implementations§

Source§

impl System for System

Source§

fn start()

Starts the RTOS scheduler.

This function never returns if successful. All created threads will begin execution according to their priorities.

§Examples
use osal_rs::os::{System, SystemFn, Thread};
 
// Create threads...
let thread = Thread::new("worker", 2048, 5, || {
    loop { /* work */ }
}).unwrap();
 
thread.start().unwrap();
 
// Start scheduler (does not return)
System::start();
Source§

fn get_state() -> ThreadState

Gets the state of the currently executing thread.

§Returns

Current thread state enum value

§Examples
use osal_rs::os::{System, SystemFn, ThreadState};
 
let state = System::get_state();
match state {
    ThreadState::Running => println!("Currently running"),
    _ => println!("Other state"),
}
Source§

fn suspend_all()

Suspends all tasks in the scheduler.

No context switches will occur until resume_all() is called. Use this to create atomic sections spanning multiple operations.

§Examples
use osal_rs::os::{System, SystemFn};
 
System::suspend_all();
// Perform critical operations
System::resume_all();
Source§

fn resume_all() -> BaseType

Resumes all suspended tasks.

§Returns

Non-zero if a context switch should occur

§Examples
System::resume_all();
Source§

fn stop()

Stops the RTOS scheduler.

All threads will stop executing. Rarely used in embedded systems.

§Examples
System::stop();
Source§

fn get_tick_count() -> TickType

Returns the current tick count.

The tick count increments with each RTOS tick interrupt.

§Returns

Current tick count value

§Examples
use osal_rs::os::{System, SystemFn};
 
let ticks = System::get_tick_count();
println!("Current ticks: {}", ticks);
Source§

fn get_current_time_us() -> Duration

Returns the current system time as a Duration.

Converts the current tick count to microseconds and returns it as a standard Duration type.

§Returns

Current system uptime as Duration

§Examples
use osal_rs::os::{System, SystemFn};
 
let uptime = System::get_current_time_us();
println!("System uptime: {:?}", uptime);
Source§

fn get_us_from_tick(duration: &Duration) -> TickType

Converts a Duration to microsecond ticks.

Helper function for converting duration values to system tick counts in microsecond resolution.

§Parameters
  • duration - Duration to convert
§Returns

Equivalent tick count in microseconds

§Examples
use osal_rs::os::{System, SystemFn};
use core::time::Duration;
 
let duration = Duration::from_millis(100);
let us_ticks = System::get_us_from_tick(&duration);
Source§

fn count_threads() -> usize

Returns the number of threads currently in the system.

Includes threads in all states (running, ready, blocked, suspended).

§Returns

Total number of threads

§Examples
use osal_rs::os::{System, SystemFn};
 
let count = System::count_threads();
println!("Total threads: {}", count);
Source§

fn get_all_thread() -> SystemState

Retrieves a snapshot of all threads in the system.

Returns detailed metadata for every thread including state, priority, stack usage, and runtime statistics.

§Returns

SystemState containing all thread information

§Examples
use osal_rs::os::{System, SystemFn};
 
let state = System::get_all_thread();
 
for thread in &state.tasks {
    println!("Thread: {} - Stack remaining: {}",
        thread.name,
        thread.stack_high_water_mark
    );
}
Source§

fn delay(ticks: TickType)

Delays the current thread for the specified number of ticks.

The thread will enter the Blocked state for the delay period.

§Parameters
  • ticks - Number of ticks to delay
§Examples
use osal_rs::os::{System, SystemFn};
 
System::delay(100);  // Delay 100 ticks
Source§

fn delay_until(previous_wake_time: &mut TickType, time_increment: TickType)

Delays until an absolute time point.

Used for creating precise periodic timing. The previous_wake_time is updated automatically for the next period.

§Parameters
  • previous_wake_time - Pointer to last wake time (will be updated)
  • time_increment - Period in ticks
§Examples
use osal_rs::os::{System, SystemFn};
 
let mut last_wake = System::get_tick_count();
loop {
    // Periodic task code...
    System::delay_until(&mut last_wake, 100);  // 100 tick period
}
Source§

fn critical_section_enter()

Enters a critical section.

Disables interrupts or increments the scheduler lock nesting count. Must be paired with critical_section_exit().

§Examples
use osal_rs::os::{System, SystemFn};
 
System::critical_section_enter();
// Critical code - no task switches or interrupts
System::critical_section_exit();
Source§

fn critical_section_exit()

Exits a critical section.

Re-enables interrupts or decrements the scheduler lock nesting count.

§Examples
System::critical_section_exit();
Source§

fn check_timer(timestamp: &Duration, time: &Duration) -> OsalRsBool

Checks if a timer has elapsed.

Compares the elapsed time since a timestamp against a target duration, handling tick counter overflow correctly for both 32-bit and 64-bit systems.

§Parameters
  • timestamp - Starting time reference
  • time - Target duration to wait for
§Returns
  • OsalRsBool::True - Timer has elapsed
  • OsalRsBool::False - Timer has not yet elapsed
§Examples
use osal_rs::os::{System, SystemFn};
use core::time::Duration;
 
let start = System::get_current_time_us();
let timeout = Duration::from_secs(1);
 
// Later...
if System::check_timer(&start, &timeout).into() {
    println!("Timeout occurred");
}
Source§

fn yield_from_isr(higher_priority_task_woken: BaseType)

Yields to a higher priority task from ISR context.

Should be called when an ISR operation wakes a higher priority task.

§Parameters
  • higher_priority_task_woken - pdTRUE if higher priority task was woken
§Examples
// In ISR:
let mut woken = pdFALSE;
// ... ISR operations that might wake a task ...
System::yield_from_isr(woken);
Source§

fn end_switching_isr(switch_required: BaseType)

Ends ISR and performs context switch if needed.

This function should be called at the end of an interrupt service routine to trigger a context switch if a higher priority task was woken during the ISR.

§Parameters
  • switch_required - pdTRUE if context switch is required, pdFALSE otherwise
§Examples
use osal_rs::os::{System, SystemFn};
use osal_rs::os::ffi::pdTRUE;
 
// In ISR:
let mut switch_required = pdFALSE;
// ... ISR operations that might require context switch ...
System::end_switching_isr(switch_required);
Source§

fn enter_critical()

Enters a critical section at task level.

Disables scheduler and interrupts to protect shared resources. Must be paired with exit_critical(). This is the task-level version; for ISR context use enter_critical_from_isr().

§Examples
use osal_rs::os::{System, SystemFn};
 
System::enter_critical();
// Access shared resource safely
System::exit_critical();
Source§

fn exit_critical()

Exits a critical section at task level.

Re-enables scheduler and interrupts after enter_critical(). Must be called from the same task that called enter_critical().

§Examples
use osal_rs::os::{System, SystemFn};
 
System::enter_critical();
// Critical section code
System::exit_critical();
Source§

fn enter_critical_from_isr() -> UBaseType

Enters a critical section from an ISR context.

ISR-safe version of critical section entry. Returns the interrupt mask state that must be passed to exit_critical_from_isr(). Use this instead of enter_critical() when in interrupt context.

§Returns

Saved interrupt status to be restored on exit

§Examples
use osal_rs::os::{System, SystemFn};
 
// In an interrupt handler
let saved_status = System::enter_critical_from_isr();
// Critical ISR code
System::exit_critical_from_isr(saved_status);
Source§

fn exit_critical_from_isr(saved_interrupt_status: UBaseType)

Exits a critical section from an ISR context.

Restores the interrupt mask to the state saved by enter_critical_from_isr().

§Parameters
  • saved_interrupt_status - Interrupt status returned by enter_critical_from_isr()
§Examples
use osal_rs::os::{System, SystemFn};
 
let saved = System::enter_critical_from_isr();
// Protected ISR operations
System::exit_critical_from_isr(saved);
Source§

fn get_free_heap_size() -> usize

Returns the amount of free heap space.

Useful for monitoring memory usage and detecting leaks.

§Returns

Number of free heap bytes

§Examples
use osal_rs::os::{System, SystemFn};
 
let free = System::get_free_heap_size();
println!("Free heap: {} bytes", free);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.