osal_rs/freertos/config.rs
1/***************************************************************************
2 *
3 * osal-rs
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
18 *
19 ***************************************************************************/
20
21//! FreeRTOS configuration access macros.
22//!
23//! This module provides macros to access FreeRTOS configuration values at runtime
24//! through FFI calls. These values are typically defined in FreeRTOSConfig.h and
25//! are made available to Rust code through C wrapper functions.
26//!
27//! # Available Configuration Values
28//!
29//! - **CPU Clock**: System clock frequency in Hz
30//! - **Tick Rate**: RTOS tick frequency (ticks per second)
31//! - **Max Priorities**: Number of priority levels available
32//! - **Minimal Stack Size**: Minimum stack size for tasks
33//! - **Max Task Name Length**: Maximum characters in task names
34//!
35//! # Examples
36//!
37//! ```ignore
38//! use osal_rs::{tick_rate_hz, cpu_clock_hz, max_priorities};
39//!
40//! let tick_rate = tick_rate_hz!();
41//! println!("Tick rate: {} Hz", tick_rate);
42//!
43//! let cpu_freq = cpu_clock_hz!();
44//! println!("CPU frequency: {} Hz", cpu_freq);
45//!
46//! let priorities = max_priorities!();
47//! println!("Max priorities: {}", priorities);
48//! ```
49
50/// FFI declarations for FreeRTOS configuration functions.
51///
52/// These C functions are implemented in the porting layer and return
53/// configuration values from FreeRTOSConfig.h.
54pub mod ffi {
55 use crate::freertos::types::{TickType, StackType};
56
57 unsafe extern "C" {
58 /// Returns the CPU clock frequency in Hz.
59 ///
60 /// Typically corresponds to `configCPU_CLOCK_HZ` in FreeRTOSConfig.h.
61 pub fn osal_rs_config_cpu_clock_hz() -> u64;
62
63 /// Returns the RTOS tick rate in Hz (ticks per second).
64 ///
65 /// Corresponds to `configTICK_RATE_HZ` in FreeRTOSConfig.h.
66 pub fn osal_rs_config_tick_rate_hz() -> TickType;
67
68 /// Returns the maximum number of priority levels.
69 ///
70 /// Corresponds to `configMAX_PRIORITIES` in FreeRTOSConfig.h.
71 pub fn osal_rs_config_max_priorities() -> u32;
72
73 /// Returns the minimum stack size for tasks.
74 ///
75 /// Corresponds to `configMINIMAL_STACK_SIZE` in FreeRTOSConfig.h.
76 pub fn osal_rs_config_minimal_stack_size() -> StackType;
77
78 /// Returns the maximum length for task names.
79 ///
80 /// Corresponds to `configMAX_TASK_NAME_LEN` in FreeRTOSConfig.h.
81 pub fn osal_rs_config_max_task_name_len() -> u32;
82 }
83}
84
85/// Returns the tick period in milliseconds.
86///
87/// This macro calculates the duration of one RTOS tick in milliseconds
88/// based on the configured tick rate.
89///
90/// # Returns
91///
92/// Tick rate in Hz (ticks per second)
93///
94/// # Examples
95///
96/// ```ignore
97/// use osal_rs::tick_period_ms;
98///
99/// let period = tick_period_ms!();
100/// println!("Each tick is {} Hz", period);
101/// ```
102///
103/// # Note
104///
105/// Currently returns tick rate, not period. May be updated in future.
106#[macro_export]
107macro_rules! tick_period_ms {
108 () => {
109 // CHECK (1000 / $crate::freertos::config::TICK_RATE_HZ)
110 (unsafe { $crate::os::config::ffi::osal_rs_config_tick_rate_hz() })
111 };
112}
113
114/// Returns the RTOS tick rate in Hz.
115///
116/// This is the frequency at which the RTOS tick interrupt occurs,
117/// determining the resolution of time-based operations.
118///
119/// # Returns
120///
121/// Tick rate in Hz (ticks per second)
122///
123/// # Examples
124///
125/// ```ignore
126/// use osal_rs::tick_rate_hz;
127///
128/// let rate = tick_rate_hz!();
129/// println!("Tick rate: {} Hz", rate);
130/// println!("Tick period: {} ms", 1000 / rate);
131/// ```
132#[macro_export]
133macro_rules! tick_rate_hz {
134 () => {
135 (unsafe { $crate::os::config::ffi::osal_rs_config_tick_rate_hz() })
136 };
137}
138
139/// Returns the CPU clock frequency in Hz.
140///
141/// This is the main system clock frequency used by the processor.
142///
143/// # Returns
144///
145/// CPU frequency in Hz
146///
147/// # Examples
148///
149/// ```ignore
150/// use osal_rs::cpu_clock_hz;
151///
152/// let freq = cpu_clock_hz!();
153/// println!("CPU running at {} MHz", freq / 1_000_000);
154/// ```
155#[macro_export]
156macro_rules! cpu_clock_hz {
157 () => {
158 (unsafe { $crate::os::config::ffi::osal_rs_config_cpu_clock_hz() })
159 };
160}
161
162/// Returns the maximum number of priority levels.
163///
164/// Tasks can have priorities from 0 (lowest) to max_priorities-1 (highest).
165///
166/// # Returns
167///
168/// Maximum number of priority levels
169///
170/// # Examples
171///
172/// ```ignore
173/// use osal_rs::max_priorities;
174///
175/// let max = max_priorities!();
176/// println!("Priority range: 0 to {}", max - 1);
177/// ```
178#[macro_export]
179macro_rules! max_priorities {
180 () => {
181 (unsafe { $crate::os::config::ffi::osal_rs_config_max_priorities() })
182 };
183}
184
185/// Returns the minimum stack size for tasks.
186///
187/// This is the smallest stack size that should be used when creating tasks,
188/// typically measured in words (not bytes).
189///
190/// # Returns
191///
192/// Minimum stack size in words
193///
194/// # Examples
195///
196/// ```ignore
197/// use osal_rs::minimal_stack_size;
198///
199/// let min_stack = minimal_stack_size!();
200/// println!("Minimum stack: {} words", min_stack);
201/// ```
202#[macro_export]
203macro_rules! minimal_stack_size {
204 () => {
205 ( unsafe { $crate::os::config::ffi::osal_rs_config_minimal_stack_size() })
206 };
207}
208
209/// Returns the maximum length for task names.
210///
211/// Task names longer than this will be truncated.
212///
213/// # Returns
214///
215/// Maximum number of characters in task names (including null terminator)
216///
217/// # Examples
218///
219/// ```ignore
220/// use osal_rs::max_task_name_len;
221///
222/// let max_len = max_task_name_len!();
223/// println!("Max task name length: {} characters", max_len);
224/// ```
225#[macro_export]
226macro_rules! max_task_name_len {
227 () => {
228 (unsafe { $crate::os::config::ffi::osal_rs_config_max_task_name_len() })
229 };
230}