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