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
/***************************************************************************
*
* 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/>.
*
***************************************************************************/
//! Tick conversion traits for time-based operations.
//!
//! These traits provide conversion between high-level time types (like `Duration`)
//! and low-level RTOS tick counts.
//!
//! # Overview
//!
//! RTOS systems use a periodic tick interrupt for timing operations. The tick rate
//! (typically 100-1000 Hz) determines the time resolution of delays, timeouts, and
//! other timing operations.
//!
//! # Tick Rate
//!
//! The tick rate is configured at compile time and affects:
//! - Minimum delay/timeout resolution
//! - System responsiveness
//! - Interrupt overhead (higher rate = more overhead)
//! - Maximum timeout duration before overflow
//!
//! Common tick rates:
//! - 100 Hz (10ms per tick) - Lower overhead, suitable for many applications
//! - 1000 Hz (1ms per tick) - Higher resolution, common in modern systems
//!
//! # Conversion Accuracy
//!
//! Converting from `Duration` to ticks involves rounding. Sub-tick durations
//! are rounded up to ensure minimum delays are met.
//!
//! # Examples
//!
//! ```ignore
//! use osal_rs::traits::{ToTick, FromTick};
//! use core::time::Duration;
//!
//! // Convert Duration to ticks
//! let timeout = Duration::from_millis(100);
//! let ticks = timeout.to_ticks();
//!
//! // Convert ticks to Duration
//! let mut duration = Duration::from_secs(0);
//! duration.ticks(50);
//! ```
use crateTickType;
/// Converts a time value to RTOS ticks.
///
/// This trait is implemented by time types (like `Duration`) to allow
/// conversion to the underlying RTOS tick count. This is useful for API
/// functions that accept tick-based timeouts.
///
/// # Rounding Behavior
///
/// Conversions typically round up to ensure that the actual delay/timeout
/// is at least as long as requested. For example, with a 1ms tick rate:
/// - 500µs → rounds up to 1 tick (1ms)
/// - 1500µs → rounds up to 2 ticks (2ms)
///
/// # Requirements
///
/// Types implementing this trait must be `Sized + Copy` to allow efficient
/// passing by value in RTOS operations.
///
/// # Use Cases
///
/// - API functions that need tick-based timeouts
/// - Converting user-friendly durations to system ticks
/// - Allowing flexible timeout specifications in APIs
///
/// # Examples
///
/// ```ignore
/// use osal_rs::traits::ToTick;
/// use core::time::Duration;
///
/// // Convert milliseconds to ticks
/// let duration = Duration::from_millis(100);
/// let ticks = duration.to_ticks();
///
/// // Use with RTOS API
/// System::delay(Duration::from_millis(500).to_ticks());
///
/// // Or let the API handle the conversion
/// fn delay_for(timeout: impl ToTick) {
/// let ticks = timeout.to_ticks();
/// // Use ticks...
/// }
///
/// delay_for(Duration::from_secs(1));
/// delay_for(1000u32); // If u32 implements ToTick
/// ```
/// Converts RTOS ticks to a time value.
///
/// This trait allows time types to be constructed or updated from
/// raw tick counts, useful when working with RTOS APIs that return
/// tick-based values.
///
/// # Use Cases
///
/// - Converting tick counts from RTOS APIs to `Duration`
/// - Calculating elapsed time from tick differences
/// - Interpreting tick-based timestamps
///
/// # Tick Rate Dependency
///
/// The conversion depends on the configured tick rate. For example:
/// - With 1000 Hz (1ms/tick): 100 ticks = 100ms
/// - With 100 Hz (10ms/tick): 100 ticks = 1000ms (1 second)
///
/// # Examples
///
/// ```ignore
/// use osal_rs::traits::FromTick;
/// use core::time::Duration;
///
/// // Create duration from tick count
/// let mut duration = Duration::from_secs(0);
/// duration.ticks(100); // Set from 100 ticks
///
/// // Calculate elapsed time
/// let start_tick = System::get_tick_count();
/// // ... do work ...
/// let end_tick = System::get_tick_count();
/// let mut elapsed = Duration::from_secs(0);
/// elapsed.ticks(end_tick - start_tick);
/// ```