Skip to main content

osal_rs/freertos/
config.rs

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