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
/***************************************************************************
*
* 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/>.
*
***************************************************************************/
//! # OSAL-RS - Operating System Abstraction Layer for Rust
//!
//! A cross-platform abstraction layer for embedded and real-time operating systems.
//!
//! ## Overview
//!
//! OSAL-RS provides a unified, safe Rust API for working with different real-time
//! operating systems. It currently ships two backends, selected at compile time
//! via feature flags - there is no default, exactly one must be enabled:
//! - **FreeRTOS** (feature `freertos`) - for bare-metal embedded targets
//! - **POSIX** (feature `posix`) - runs on any pthreads-capable host (Linux, macOS)
//! so applications, tests and doc examples can execute natively without
//! embedded hardware or a cross toolchain
//!
//! Application code written against `osal_rs::os::*` is portable between the
//! two backends by switching feature flags.
//!
//! ## Features
//!
//! - **Thread Management**: Create and control threads with priorities
//! - **Synchronization**: Mutexes, semaphores, and event groups
//! - **Communication**: Queues for inter-thread message passing
//! - **Timers**: Software timers for periodic and one-shot operations
//! - **Time Management**: Duration-based timing with tick conversion
//! - **No-std Support**: Works in bare-metal embedded environments (`freertos` backend)
//! - **Host Testing**: Runs under `std` on POSIX hosts for native testing (`posix` backend)
//! - **Type Safety**: Leverages Rust's type system for correctness
//! - **Async/Await**: Backend-agnostic `async`/`await` without Tokio (feature `async`)
//!
//! ## Quick Start
//!
//! ### Basic Thread Example
//!
//! ```no_run
//! use osal_rs::os::*;
//!
//! fn main() {
//! // Create a thread
//! let mut thread = Thread::new(
//! "worker",
//! 4096, // stack size
//! 5, // priority
//! );
//!
//! thread.spawn_simple(|| {
//! loop {
//! println!("Working...");
//! System::delay(1000);
//! }
//! }).unwrap();
//!
//! // Start the scheduler (never returns)
//! System::start();
//! }
//! ```
//!
//! ### Mutex Example
//!
//! ```
//! use osal_rs::os::*;
//! use std::sync::Arc;
//!
//! let counter = Arc::new(Mutex::new(0));
//! let counter_clone = counter.clone();
//!
//! let mut thread = Thread::new("incrementer", 2048, 5);
//! thread.spawn_simple(move || {
//! let mut guard = counter_clone.lock().unwrap();
//! *guard += 1;
//! Ok(Arc::new(()))
//! }).unwrap();
//! ```
//!
//! ### Queue Example
//!
//! ```
//! use osal_rs::os::*;
//!
//! let queue = Queue::new(10, 4).unwrap();
//!
//! // Send data
//! let data = [1u8, 2, 3, 4];
//! queue.post(&data, 100).unwrap();
//!
//! // Receive data
//! let mut buffer = [0u8; 4];
//! queue.fetch(&mut buffer, 100).unwrap();
//! ```
//!
//! ### Semaphore Example
//!
//! ```
//! use osal_rs::os::*;
//! use osal_rs::utils::OsalRsBool;
//! use core::time::Duration;
//!
//! let sem = Semaphore::new(1, 1).unwrap();
//!
//! if sem.wait(Duration::from_millis(100)) == OsalRsBool::True {
//! // Critical section
//! sem.signal();
//! }
//! ```
//!
//! ### Timer Example
//!
//! ```
//! extern crate alloc;
//! use osal_rs::os::*;
//! use alloc::sync::Arc;
//! use core::time::Duration;
//!
//! let timer = Timer::new_with_to_tick(
//! "periodic",
//! Duration::from_millis(500),
//! true, // auto-reload
//! None,
//! |_timer, _param| {
//! println!("Timer tick");
//! Ok(Arc::new(()))
//! }
//! ).unwrap();
//!
//! timer.start_with_to_tick(Duration::from_millis(10));
//! ```
//!
//! ### Async/Await Example (feature `async`)
//!
//! use osal_rs::os::{block_on, AsyncMutex, AsyncQueue, AsyncSemaphore};
//!
//! // Drive a future to completion on the calling RTOS task — no Tokio needed.
//! block_on(async {
//! let mutex = AsyncMutex::new(0u32);
//! {
//! let mut guard = mutex.lock().await;
//! *guard += 1;
//! }
//!
//! let sem = AsyncSemaphore::new(1, 0).unwrap();
//! sem.signal();
//! sem.wait_async().await;
//!
//! let queue = AsyncQueue::new(8, 4).unwrap();
//! queue.post_async(&[1u8, 2, 3, 4]).await.unwrap();
//! let mut buf = [0u8; 4];
//! queue.fetch_async(&mut buf).await.unwrap();
//! });
//! ```
//!
//! ## Module Organization
//!
//! - [`os`] - Main module containing all OS abstractions
//! - Threads, mutexes, semaphores, queues, event groups, timers
//! - System-level functions
//! - Type definitions
//! - `block_on`, `AsyncQueue`, `AsyncSemaphore`, `AsyncMutex` (feature `async`)
//! - [`async_primitives`] - Async wrappers for OSAL primitives (feature `async`)
//! - [`utils`] - Utility types and error definitions
//! - [`log`] - Logging macros
//! - `traits` - Private module defining the trait abstractions
//! - `freertos` - Private FreeRTOS implementation (enabled with `freertos` feature)
//! - `posix` - Private POSIX implementation (enabled with `posix` feature)
//!
//! ## Features
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `freertos` | ❌ | FreeRTOS backend |
//! | `posix` | ❌ | POSIX/host backend |
//! | `async` | ❌ | Async/await without Tokio |
//! | `serde` | ❌ | Serialization via `osal-rs-serde` |
//! | `real_time` | ❌ | POSIX only: schedules spawned threads with `SCHED_FIFO` instead of inheriting the creating thread's policy/priority. Not meant to be requested by hand - the build script auto-enables it when the host OS/kernel supports real-time scheduling |
//!
//! There is no default backend: exactly one of `freertos`/`posix` must be
//! enabled explicitly. Enabling neither trips the `compile_error!` below;
//! enabling both is equally unsupported (the two are mutually exclusive by
//! `cfg`, so the crate fails to build either way).
//!
//! ## Requirements
//!
//! When using with FreeRTOS:
//! - FreeRTOS kernel must be properly configured
//! - Link the C porting layer from `osal-rs-porting/freertos/`
//! - Set appropriate `FreeRTOSConfig.h` options:
//! - `configTICK_RATE_HZ` - Defines the tick frequency
//! - `configUSE_MUTEXES` - Must be 1 for mutex support
//! - `configUSE_COUNTING_SEMAPHORES` - Must be 1 for semaphore support
//! - `configUSE_TIMERS` - Must be 1 for timer support
//! - `configSUPPORT_DYNAMIC_ALLOCATION` - Must be 1 for dynamic allocation
//!
//! When using with POSIX:
//! - A POSIX-compliant host implementing pthreads, `timer_create(2)`/`sigwait(3)`
//! and `CLOCK_MONOTONIC` (glibc/Linux is part of this crate's test suite)
//! - No special build steps: unlike `freertos`, this backend links only
//! against the host's libc/libpthread - no cross toolchain or RTOS kernel
//! sources required
//! - Disables `no_std`: the `posix` feature builds the crate against `std`
//!
//! ## Platform Support
//!
//! Currently tested on:
//! - ARM Cortex-M (Raspberry Pi Pico/RP2040, RP2350) - `freertos` backend
//! - ARM Cortex-M4F (STM32F4 series) - `freertos` backend
//! - ARM Cortex-M7 (STM32H7 series) - `freertos` backend
//! - RISC-V (RP2350 RISC-V cores) - `freertos` backend
//! - glibc/Linux - `posix` backend
//!
//! ## Thread Safety
//!
//! All types are designed with thread safety in mind:
//! - Most operations are thread-safe and can be called from multiple threads
//! - Methods with `_from_isr` suffix are ISR-safe (callable from interrupt context)
//! - Regular methods (without `_from_isr`) must not be called from ISR context
//! - Mutexes use priority inheritance to prevent priority inversion
//!
//! ## ISR Context
//!
//! Operations in ISR context have restrictions (applies to the `freertos`
//! backend; POSIX has no interrupt context - `_from_isr` variants are
//! provided there for API compatibility but behave like their regular
//! counterparts):
//! - Cannot block or use timeouts (must use zero timeout or `_from_isr` variants)
//! - Must be extremely fast to avoid blocking other interrupts
//! - Use semaphores or queues to defer work to task context
//! - Event groups and notifications are ISR-safe for signaling
//!
//! ## Safety
//!
//! This library uses `unsafe` internally to interface with C APIs but provides
//! safe Rust abstractions. All public APIs are designed to be memory-safe when
//! used correctly:
//! - Type safety through generic parameters
//! - RAII patterns for automatic resource management
//! - Rust's ownership system prevents data races
//! - FFI boundaries are carefully validated
//!
//! ## Performance Considerations
//!
//! - `freertos` backend: allocations happen on the FreeRTOS heap (via the
//! `#[global_allocator]` in [`os`]), not the system heap
//! - `posix` backend: uses the system allocator and native OS threads; each
//! [`os::Timer`] spawns a dedicated background thread
//! - Stack sizes must be carefully tuned for each thread
//! - Priority inversion is mitigated through priority inheritance
//! - Context switches are triggered by blocking operations
//!
//! ## Best Practices
//!
//! 1. **Thread Creation**: Always specify appropriate stack sizes based on usage
//! 2. **Mutexes**: Prefer scoped locking with guards to prevent deadlocks
//! 3. **Queues**: Use type-safe `QueueStreamed` when possible
//! 4. **Semaphores**: Use binary semaphores for signaling, counting for resources
//! 5. **ISR Handlers**: Keep ISR code minimal, defer work to tasks
//! 6. **Error Handling**: Always check `Result` return values
//! 7. **Async Tasks**: Call `block_on` once per RTOS task; do not nest executors
//!
//! ## License
//!
//! LGPL-2.1-or-later - See LICENSE file for details
extern crate alloc;
compile_error!;
/// FreeRTOS implementation of OSAL traits.
///
/// This module contains the concrete implementation of all OSAL abstractions
/// for FreeRTOS, including threads, mutexes, queues, timers, etc.
///
/// Enabled with the `freertos` feature flag (no default backend - must be
/// requested explicitly).
/// POSIX implementation of OSAL traits.
///
/// This module contains the concrete implementation of all OSAL abstractions
/// on top of the POSIX/pthreads API (threads, mutexes, semaphores, queues,
/// event groups, timers), so applications - and their tests and doc examples -
/// can run natively on any POSIX host (Linux, macOS) without embedded hardware
/// or a cross toolchain. See [`posix`] for details.
///
/// Enabled with the `posix` feature flag.
/// Trait definitions for OSAL abstractions.
///
/// This private module defines all the trait interfaces that concrete
/// implementations must satisfy. Traits are re-exported through the `os` module.
/// Async executor (block_on).
/// Async primitives (AsyncQueue, AsyncSemaphore, AsyncMutex).
/// Select FreeRTOS as the active OSAL backend.
use cratefreertos as osal;
/// Select POSIX as the active OSAL backend.
use crateposix as osal;
/// Main OSAL module re-exporting all OS abstractions and traits.
///
/// This module provides a unified interface to all OSAL functionality through `osal_rs::os::*`.
/// It re-exports:
/// - Thread management types (`Thread`, `ThreadNotification`)
/// - Synchronization primitives (`Mutex`, `Semaphore`, `EventGroup`)
/// - Communication types (`Queue`, `QueueStreamed`)
/// - Timer types (`Timer`)
/// - System functions (`System`)
/// - All trait definitions from the `traits` module
/// - Type definitions and configuration from the active backend
///
/// The actual implementation (FreeRTOS or POSIX) is selected at compile time via features.
///
/// # Examples
///
/// ```ignore
/// use osal_rs::os::*;
///
/// fn main() {
/// // Create and start a thread
/// let thread = Thread::new("worker", 4096, 5, || {
/// println!("Worker thread running");
/// }).unwrap();
///
/// thread.start().unwrap();
/// System::start();
/// }
/// ```
/// Default panic handler for `no_std` environments.
///
/// This panic handler is active for the `freertos` backend.
/// It prints panic information and enters an infinite loop to halt execution.
///
/// # Behavior
///
/// 1. Attempts to print panic information using the `println!` macro
/// 2. Enters an infinite empty loop, halting the program
///
/// # Feature Flag
///
/// - Enabled for `freertos`
/// - Automatically disabled when using `posix`
///
/// # Safety
///
/// This handler is intentionally simple and does not attempt cleanup or recovery.
/// In production embedded systems, consider:
/// - Logging panic info to persistent storage
/// - Performing safe shutdown procedures
/// - Resetting the system via watchdog
///
!