osal_rs/lib.rs
1/***************************************************************************
2 *
3 * osal-rs
4 * Copyright (C) 2023/2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***************************************************************************/
19
20//! # OSAL-RS - Operating System Abstraction Layer for Rust
21//!
22//! A cross-platform abstraction layer for embedded and real-time operating systems.
23//!
24//! ## Overview
25//!
26//! OSAL-RS provides a unified, safe Rust API for working with different real-time
27//! operating systems. Currently supports FreeRTOS with planned support for POSIX
28//! and other RTOSes.
29//!
30//! ## Features
31//!
32//! - **Thread Management**: Create and control threads with priorities
33//! - **Synchronization**: Mutexes, semaphores, and event groups
34//! - **Communication**: Queues for inter-thread message passing
35//! - **Timers**: Software timers for periodic and one-shot operations
36//! - **Time Management**: Duration-based timing with tick conversion
37//! - **No-std Support**: Works in bare-metal embedded environments
38//! - **Type Safety**: Leverages Rust's type system for correctness
39//!
40//! ## Quick Start
41//!
42//! ### Basic Thread Example
43//!
44//! ```ignore
45//! use osal_rs::os::*;
46//! use core::time::Duration;
47//!
48//! fn main() {
49//! // Create a thread
50//! let thread = Thread::new(
51//! "worker",
52//! 4096, // stack size
53//! 5, // priority
54//! || {
55//! loop {
56//! println!("Working...");
57//! Duration::from_secs(1).sleep();
58//! }
59//! }
60//! ).unwrap();
61//!
62//! thread.start().unwrap();
63//!
64//! // Start the scheduler
65//! System::start();
66//! }
67//! ```
68//!
69//! ### Mutex Example
70//!
71//! ```ignore
72//! use osal_rs::os::*;
73//! use alloc::sync::Arc;
74//!
75//! let counter = Arc::new(Mutex::new(0));
76//! let counter_clone = counter.clone();
77//!
78//! let thread = Thread::new("incrementer", 2048, 5, move || {
79//! let mut guard = counter_clone.lock().unwrap();
80//! *guard += 1;
81//! }).unwrap();
82//! ```
83//!
84//! ### Queue Example
85//!
86//! ```ignore
87//! use osal_rs::os::*;
88//! use core::time::Duration;
89//!
90//! let queue = Queue::new(10, 4).unwrap();
91//!
92//! // Send data
93//! let data = [1u8, 2, 3, 4];
94//! queue.post(&data, 100).unwrap();
95//!
96//! // Receive data
97//! let mut buffer = [0u8; 4];
98//! queue.fetch(&mut buffer, 100).unwrap();
99//! ```
100//!
101//! ### Semaphore Example
102//!
103//! ```ignore
104//! use osal_rs::os::*;
105//! use core::time::Duration;
106//!
107//! let sem = Semaphore::new(1, 1).unwrap();
108//!
109//! if sem.wait(Duration::from_millis(100)).into() {
110//! // Critical section
111//! sem.signal();
112//! }
113//! ```
114//!
115//! ### Timer Example
116//!
117//! ```ignore
118//! use osal_rs::os::*;
119//! use core::time::Duration;
120//!
121//! let timer = Timer::new_with_to_tick(
122//! "periodic",
123//! Duration::from_millis(500),
124//! true, // auto-reload
125//! None,
126//! |_, _| {
127//! println!("Timer tick");
128//! Ok(None)
129//! }
130//! ).unwrap();
131//!
132//! timer.start_with_to_tick(Duration::from_millis(10));
133//! ```
134//!
135//! ## Module Organization
136//!
137//! - [`os`] - Main module containing all OS abstractions
138//! - Threads, mutexes, semaphores, queues, event groups, timers
139//! - System-level functions
140//! - Type definitions
141//! - [`utils`] - Utility types and error definitions
142//! - [`log`] - Logging macros
143//!
144//! ## Features
145//!
146//! - `freertos` - Enable FreeRTOS support (default)
147//! - `posix` - Enable POSIX support (planned)
148//! - `std` - Enable standard library support for testing
149//!
150//! ## Requirements
151//!
152//! When using with FreeRTOS:
153//! - FreeRTOS must be properly configured
154//! - Link the C porting layer from `osal-rs-porting/freertos/`
155//! - Set appropriate `FreeRTOSConfig.h` options
156//!
157//! ## Platform Support
158//!
159//! Currently tested on:
160//! - ARM Cortex-M (Raspberry Pi Pico/RP2040, RP2350)
161//! - ARM Cortex-M4F
162//! - ARM Cortex-M7
163//!
164//! ## Safety
165//!
166//! This library uses `unsafe` internally to interface with C APIs but provides
167//! safe Rust abstractions. All public APIs are designed to be memory-safe when
168//! used correctly.
169//!
170//! ## License
171//!
172//! GPL-3.0 - See LICENSE file for details
173
174#![cfg_attr(not(feature = "std"), no_std)]
175
176// Suppress warnings from FreeRTOS FFI bindings being included in multiple modules
177#![allow(clashing_extern_declarations)]
178#![allow(dead_code)]
179extern crate alloc;
180
181#[cfg(feature = "freertos")]
182mod freertos;
183
184#[cfg(feature = "posix")]
185mod posix;
186
187pub mod log;
188
189mod traits;
190
191pub mod utils;
192
193#[cfg(feature = "freertos")]
194use crate::freertos as osal;
195
196#[cfg(feature = "posix")]
197use crate::posix as osal;
198
199pub mod os {
200
201 #[cfg(not(feature = "disable_panic"))]
202 use crate::osal::allocator::Allocator;
203
204
205 #[cfg(not(feature = "disable_panic"))]
206 #[global_allocator]
207 pub static ALLOCATOR: Allocator = Allocator;
208
209 #[allow(unused_imports)]
210 pub use crate::osal::duration::*;
211 pub use crate::osal::event_group::*;
212 pub use crate::osal::mutex::*;
213 pub use crate::osal::queue::*;
214 pub use crate::osal::semaphore::*;
215 pub use crate::osal::system::*;
216 pub use crate::osal::thread::*;
217 pub use crate::osal::timer::*;
218 pub use crate::traits::*;
219 pub use crate::osal::config as config;
220 pub use crate::osal::types as types;
221
222}
223
224
225// Panic handler for no_std library - only when building as final binary
226// Examples with std will provide their own
227#[cfg(not(feature = "disable_panic"))]
228#[panic_handler]
229
230fn panic(info: &core::panic::PanicInfo) -> ! {
231 println!("Panic occurred: {}", info);
232 #[allow(clippy::empty_loop)]
233 loop {}
234}
235