osal-rs 0.4.5

Operating System Abstraction Layer for Rust with support for FreeRTOS and POSIX
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/***************************************************************************
 *
 * osal-rs
 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 *
 ***************************************************************************/

//! System-level functions and utilities for FreeRTOS.
//!
//! This module provides access to system-wide functionality including:
//! - Scheduler control (start, stop, suspend, resume)
//! - System time and delay functions
//! - Thread enumeration and state inspection
//! - Critical sections
//! - Heap memory information

use core::fmt::Debug;
use core::ops::Deref;
use core::time::Duration;

use alloc::vec::Vec;

use super::ffi::{
    BLOCKED, DELETED, READY, RUNNING, SUSPENDED, TaskStatus, eTaskGetState, osal_rs_critical_section_enter, osal_rs_critical_section_exit, osal_rs_port_end_switching_isr, osal_rs_port_yield_from_isr, uxTaskGetNumberOfTasks, uxTaskGetSystemState, vTaskDelay, vTaskEndScheduler, vTaskStartScheduler, vTaskSuspendAll, xPortGetFreeHeapSize, xTaskDelayUntil, xTaskGetCurrentTaskHandle, xTaskGetTickCount, xTaskResumeAll, osal_rs_task_enter_critical, osal_rs_task_enter_critical_from_isr, osal_rs_task_exit_critical, osal_rs_task_exit_critical_from_isr
};
use super::thread::{ThreadState, ThreadMetadata};
use super::types::{BaseType, TickType, UBaseType};
use crate::tick_period_ms;
use crate::traits::{SystemFn, ToTick};
use crate::utils::{CpuRegisterSize::*, register_bit_size, OsalRsBool};

/// Represents a snapshot of the system state including all threads.
///
/// Contains metadata for all threads in the system and total runtime statistics.
/// This is useful for monitoring, debugging, and profiling.
///
/// # Examples
///
/// ```ignore
/// use osal_rs::os::{System, SystemFn};
/// 
/// let state = System::get_all_thread();
/// 
/// println!("Total threads: {}", state.tasks.len());
/// println!("Total runtime: {}", state.total_run_time);
/// 
/// for thread in &state.tasks {
///     println!("Thread: {} - Priority: {} - State: {:?}",
///         thread.name,
///         thread.priority,
///         thread.state
///     );
/// }
/// ```
#[derive(Debug, Clone)]
pub struct SystemState {
    /// List of all thread metadata in the system
    pub tasks: Vec<ThreadMetadata>,
    /// Total runtime counter across all threads (if enabled)
    pub total_run_time: u32
}

/// Provides access to the task list as a slice.
impl Deref for SystemState {
    type Target = [ThreadMetadata];

    fn deref(&self) -> &Self::Target {
        &self.tasks
    }
}

/// 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
///
/// ```ignore
/// use osal_rs::os::{System, SystemFn};
/// 
/// // Create threads, queues, etc.
/// // ...
/// 
/// // Start the scheduler (never returns in normal operation)
/// System::start();
/// ```
///
/// ## Delays and timing
///
/// ```ignore
/// 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
///
/// ```ignore
/// use osal_rs::os::{System, SystemFn};
/// 
/// // Protect shared data
/// System::critical_section_enter();
/// // Access shared data here
/// // ...
/// System::critical_section_exit();
/// ```
///
/// ## Thread enumeration
///
/// ```ignore
/// 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
///
/// ```ignore
/// use osal_rs::os::{System, SystemFn};
/// 
/// let free_heap = System::get_free_heap_size();
/// println!("Free heap: {} bytes", free_heap);
/// ```
///
/// ## Scheduler suspend/resume
///
/// ```ignore
/// 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.
pub struct System;

impl System {
    /// Delays execution using a type that implements `ToTick`.
    ///
    /// Convenience method that accepts `Duration` or other tick-convertible types.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// use core::time::Duration;
    /// 
    /// System::delay_with_to_tick(Duration::from_millis(100));
    /// ```
    #[inline]
    pub fn delay_with_to_tick(ticks: impl ToTick){
        Self::delay(ticks.to_ticks());
    }

    /// 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
    ///
    /// ```ignore
    /// 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));
    /// }
    /// ```
    #[inline]
    pub fn delay_until_with_to_tick(previous_wake_time: &mut TickType, time_increment: impl ToTick) { 
        Self::delay_until(previous_wake_time, time_increment.to_ticks());
    }
}

impl SystemFn for System {
    /// Starts the RTOS scheduler.
    ///
    /// This function never returns if successful. All created threads will
    /// begin execution according to their priorities.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// 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();
    /// ```
    fn start() {
        unsafe {
            vTaskStartScheduler();
        }
    }

    /// Gets the state of the currently executing thread.
    ///
    /// # Returns
    ///
    /// Current thread state enum value
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn, ThreadState};
    /// 
    /// let state = System::get_state();
    /// match state {
    ///     ThreadState::Running => println!("Currently running"),
    ///     _ => println!("Other state"),
    /// }
    /// ```
    fn get_state() -> ThreadState {
        use super::thread::ThreadState::*;
        let state = unsafe { eTaskGetState(xTaskGetCurrentTaskHandle()) };
        match state {
            RUNNING => Running,
            READY => Ready,
            BLOCKED => Blocked,
            SUSPENDED => Suspended,
            DELETED => Deleted,
            _ => Invalid, // INVALID or unknown state
        }
    }

    /// 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
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// System::suspend_all();
    /// // Perform critical operations
    /// System::resume_all();
    /// ```
    fn suspend_all() {
        unsafe {
            vTaskSuspendAll();
        }
    }
    
    /// Resumes all suspended tasks.
    ///
    /// # Returns
    ///
    /// Non-zero if a context switch should occur
    ///
    /// # Examples
    ///
    /// ```ignore
    /// System::resume_all();
    /// ```
    fn resume_all() -> BaseType {
        unsafe { xTaskResumeAll() }
    }

    /// Stops the RTOS scheduler.
    ///
    /// All threads will stop executing. Rarely used in embedded systems.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// System::stop();
    /// ```
    fn stop() {
        unsafe {
            vTaskEndScheduler();
        }
    }

    /// Returns the current tick count.
    ///
    /// The tick count increments with each RTOS tick interrupt.
    ///
    /// # Returns
    ///
    /// Current tick count value
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// let ticks = System::get_tick_count();
    /// println!("Current ticks: {}", ticks);
    /// ```
    fn get_tick_count() -> TickType {
        unsafe { xTaskGetTickCount() }
    }

    /// 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
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// let uptime = System::get_current_time_us();
    /// println!("System uptime: {:?}", uptime);
    /// ```
    fn get_current_time_us () -> Duration {
        let ticks = Self::get_tick_count();
        Duration::from_millis( 1_000 * ticks as u64 / tick_period_ms!() as u64 )
    }

    /// 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
    ///
    /// ```ignore
    /// 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);
    /// ```
    fn get_us_from_tick(duration: &Duration) -> TickType {
        let millis = duration.as_millis() as TickType;
        millis / (1_000 * tick_period_ms!() as TickType) 
    }

    /// Returns the number of threads currently in the system.
    ///
    /// Includes threads in all states (running, ready, blocked, suspended).
    ///
    /// # Returns
    ///
    /// Total number of threads
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// let count = System::count_threads();
    /// println!("Total threads: {}", count);
    /// ```
    fn count_threads() -> usize {
        unsafe { uxTaskGetNumberOfTasks() as usize }
    }

    /// 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
    ///
    /// ```ignore
    /// 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
    ///     );
    /// }
    /// ```
    fn get_all_thread() -> SystemState {
        let threads_count = Self::count_threads();
        let mut threads: Vec<TaskStatus> = Vec::with_capacity(threads_count);
        let mut total_run_time: u32 = 0;

        unsafe {
            let count = uxTaskGetSystemState(
                threads.as_mut_ptr(),
                threads_count as UBaseType,
                &raw mut total_run_time,
            ) as usize;
            
            // Set the length only after data has been written by FreeRTOS
            threads.set_len(count);
        }

        let tasks = threads.into_iter()
            .map(|task_status| {
                ThreadMetadata::from((
                    task_status.xHandle, 
                    task_status
                ))
            }).collect();

        SystemState {
            tasks,
            total_run_time
        }
    }


    /// 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
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// System::delay(100);  // Delay 100 ticks
    /// ```
    fn delay(ticks: TickType){
        unsafe {
            vTaskDelay(ticks);
        }
    }

    /// 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
    ///
    /// ```ignore
    /// 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
    /// }
    /// ```
    fn delay_until(previous_wake_time: &mut TickType, time_increment: TickType) {
        unsafe {
            xTaskDelayUntil(
                previous_wake_time,
                time_increment,
            );
        }
    }

    /// Enters a critical section.
    ///
    /// Disables interrupts or increments the scheduler lock nesting count.
    /// Must be paired with `critical_section_exit()`.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// System::critical_section_enter();
    /// // Critical code - no task switches or interrupts
    /// System::critical_section_exit();
    /// ```
    fn critical_section_enter() {
        unsafe {
            osal_rs_critical_section_enter();
        }
    }
    
    /// Exits a critical section.
    ///
    /// Re-enables interrupts or decrements the scheduler lock nesting count.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// System::critical_section_exit();
    /// ```
    fn critical_section_exit() {
        unsafe {
            osal_rs_critical_section_exit();
        }   
    }
    
    /// 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
    ///
    /// ```ignore
    /// 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");
    /// }
    /// ```
    fn check_timer(timestamp: &Duration, time: &Duration) -> OsalRsBool {
        let temp_tick_time = Self::get_current_time_us();
        
        let time_passing = if temp_tick_time >= *timestamp {
            temp_tick_time - *timestamp
        } else {
            if register_bit_size() == Bit32 {
                // Handle tick count overflow for 32-bit TickType
                let overflow_correction = Duration::from_micros(0xff_ff_ff_ff_u64);
                overflow_correction - *timestamp + temp_tick_time
            } else {
                // Handle tick count overflow for 64-bit TickType
                let overflow_correction = Duration::from_micros(0xff_ff_ff_ff_ff_ff_ff_ff_u64);
                overflow_correction - *timestamp + temp_tick_time
            }
        };

        if time_passing >= *time {
            OsalRsBool::True
        } else {
            OsalRsBool::False
        }
    }

    /// 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
    ///
    /// ```ignore
    /// // In ISR:
    /// let mut woken = pdFALSE;
    /// // ... ISR operations that might wake a task ...
    /// System::yield_from_isr(woken);
    /// ```
    fn yield_from_isr(higher_priority_task_woken: BaseType) {
        unsafe {
            osal_rs_port_yield_from_isr(higher_priority_task_woken);
        }
    }

    /// 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
    ///
    /// ```ignore
    /// 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);
    /// ```
    fn end_switching_isr( switch_required: BaseType ) {
        unsafe {
            osal_rs_port_end_switching_isr( switch_required );
        }
    }

    /// Enters a critical section at task level.
    ///
    /// Disables scheduler and interrupts to protect shared resources.
    /// Must be paired with [`exit_critical()`](Self::exit_critical).
    /// This is the task-level version; for ISR context use 
    /// [`enter_critical_from_isr()`](Self::enter_critical_from_isr).
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// System::enter_critical();
    /// // Access shared resource safely
    /// System::exit_critical();
    /// ```
    fn enter_critical() {
        unsafe {
            osal_rs_task_enter_critical();
        }
    }

    /// Exits a critical section at task level.
    ///
    /// Re-enables scheduler and interrupts after [`enter_critical()`](Self::enter_critical).
    /// Must be called from the same task that called `enter_critical()`.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// System::enter_critical();
    /// // Critical section code
    /// System::exit_critical();
    /// ```
    fn exit_critical() {
        unsafe {
            osal_rs_task_exit_critical();
        }
    }

    /// 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()`](Self::exit_critical_from_isr).
    /// Use this instead of [`enter_critical()`](Self::enter_critical) when in interrupt context.
    ///
    /// # Returns
    ///
    /// Saved interrupt status to be restored on exit
    ///
    /// # Examples
    ///
    /// ```ignore
    /// 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);
    /// ```
    fn enter_critical_from_isr() -> UBaseType {
        unsafe {
            osal_rs_task_enter_critical_from_isr()
        }
    }

    /// Exits a critical section from an ISR context.
    ///
    /// Restores the interrupt mask to the state saved by 
    /// [`enter_critical_from_isr()`](Self::enter_critical_from_isr).
    ///
    /// # Parameters
    ///
    /// * `saved_interrupt_status` - Interrupt status returned by `enter_critical_from_isr()`
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// let saved = System::enter_critical_from_isr();
    /// // Protected ISR operations
    /// System::exit_critical_from_isr(saved);
    /// ```
    fn exit_critical_from_isr(saved_interrupt_status: UBaseType) {
        unsafe {
            osal_rs_task_exit_critical_from_isr(saved_interrupt_status);
        }
    }


    /// Returns the amount of free heap space.
    ///
    /// Useful for monitoring memory usage and detecting leaks.
    ///
    /// # Returns
    ///
    /// Number of free heap bytes
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use osal_rs::os::{System, SystemFn};
    /// 
    /// let free = System::get_free_heap_size();
    /// println!("Free heap: {} bytes", free);
    /// ```
    fn get_free_heap_size() -> usize {
        unsafe {
            xPortGetFreeHeapSize()
        }
    }

}