1use crate::error::{EmbeddedError, Result};
6
7#[cfg(feature = "low-power")]
8use crate::power::PowerMode;
9
10#[cfg(not(feature = "low-power"))]
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[repr(u8)]
15pub enum PowerMode {
16 HighPerformance = 0,
18 Balanced = 1,
20 LowPower = 2,
22 UltraLowPower = 3,
24}
25
26#[derive(Debug, Clone, Copy)]
28pub struct SystemConfig {
29 pub cpu_freq_mhz: u32,
31 pub ram_size: usize,
33 pub flash_size: usize,
35 pub power_mode: PowerMode,
37 pub debug_enabled: bool,
39}
40
41impl SystemConfig {
42 pub const fn new() -> Self {
44 Self {
45 cpu_freq_mhz: 80, ram_size: 512 * 1024, flash_size: 4 * 1024 * 1024, power_mode: PowerMode::Balanced,
49 debug_enabled: cfg!(debug_assertions),
50 }
51 }
52
53 pub const fn high_performance() -> Self {
55 Self {
56 cpu_freq_mhz: 240,
57 ram_size: 512 * 1024,
58 flash_size: 4 * 1024 * 1024,
59 power_mode: PowerMode::HighPerformance,
60 debug_enabled: false,
61 }
62 }
63
64 pub const fn low_power() -> Self {
66 Self {
67 cpu_freq_mhz: 40,
68 ram_size: 512 * 1024,
69 flash_size: 4 * 1024 * 1024,
70 power_mode: PowerMode::LowPower,
71 debug_enabled: false,
72 }
73 }
74
75 pub fn validate(&self) -> Result<()> {
77 if self.cpu_freq_mhz == 0 {
78 return Err(EmbeddedError::InvalidParameter);
79 }
80
81 if self.ram_size == 0 {
82 return Err(EmbeddedError::InvalidParameter);
83 }
84
85 Ok(())
86 }
87}
88
89impl Default for SystemConfig {
90 fn default() -> Self {
91 Self::new()
92 }
93}
94
95#[derive(Debug, Clone, Copy)]
97pub struct MemoryConfig {
98 pub static_pool_size: usize,
100 pub block_size: usize,
102 pub num_blocks: usize,
104 pub enable_guards: bool,
106}
107
108impl MemoryConfig {
109 pub const fn new() -> Self {
111 Self {
112 static_pool_size: 64 * 1024, block_size: 256,
114 num_blocks: 256,
115 enable_guards: cfg!(debug_assertions),
116 }
117 }
118
119 pub const fn minimal() -> Self {
121 Self {
122 static_pool_size: 4 * 1024, block_size: 64,
124 num_blocks: 64,
125 enable_guards: false,
126 }
127 }
128
129 pub const fn total_size(&self) -> usize {
131 self.static_pool_size + (self.block_size * self.num_blocks)
132 }
133}
134
135impl Default for MemoryConfig {
136 fn default() -> Self {
137 Self::new()
138 }
139}
140
141#[derive(Debug, Clone, Copy)]
143pub struct RealtimeConfig {
144 pub enabled: bool,
146 pub max_tasks: usize,
148 pub default_priority: u8,
150 pub monitor_deadlines: bool,
152}
153
154impl RealtimeConfig {
155 pub const fn new() -> Self {
157 Self {
158 enabled: false,
159 max_tasks: 8,
160 default_priority: 2,
161 monitor_deadlines: false,
162 }
163 }
164
165 pub const fn enabled() -> Self {
167 Self {
168 enabled: true,
169 max_tasks: 16,
170 default_priority: 2,
171 monitor_deadlines: true,
172 }
173 }
174}
175
176impl Default for RealtimeConfig {
177 fn default() -> Self {
178 Self::new()
179 }
180}
181
182#[derive(Debug, Clone, Copy)]
184pub struct CommConfig {
185 pub uart_baud: u32,
187 pub i2c_freq: u32,
189 pub spi_freq: u32,
191 pub enable_crc: bool,
193}
194
195impl CommConfig {
196 pub const fn new() -> Self {
198 Self {
199 uart_baud: 115200,
200 i2c_freq: 100_000, spi_freq: 1_000_000, enable_crc: false,
203 }
204 }
205
206 pub const fn high_speed() -> Self {
208 Self {
209 uart_baud: 921600,
210 i2c_freq: 400_000, spi_freq: 10_000_000, enable_crc: true,
213 }
214 }
215}
216
217impl Default for CommConfig {
218 fn default() -> Self {
219 Self::new()
220 }
221}
222
223#[derive(Debug, Clone, Copy)]
225pub struct EmbeddedConfig {
226 pub system: SystemConfig,
228 pub memory: MemoryConfig,
230 pub realtime: RealtimeConfig,
232 pub comm: CommConfig,
234}
235
236impl EmbeddedConfig {
237 pub const fn new() -> Self {
239 Self {
240 system: SystemConfig::new(),
241 memory: MemoryConfig::new(),
242 realtime: RealtimeConfig::new(),
243 comm: CommConfig::new(),
244 }
245 }
246
247 pub const fn high_performance() -> Self {
249 Self {
250 system: SystemConfig::high_performance(),
251 memory: MemoryConfig::new(),
252 realtime: RealtimeConfig::enabled(),
253 comm: CommConfig::high_speed(),
254 }
255 }
256
257 pub const fn low_power() -> Self {
259 Self {
260 system: SystemConfig::low_power(),
261 memory: MemoryConfig::minimal(),
262 realtime: RealtimeConfig::new(),
263 comm: CommConfig::new(),
264 }
265 }
266
267 pub fn validate(&self) -> Result<()> {
269 self.system.validate()?;
270
271 if self.memory.total_size() > self.system.ram_size {
273 return Err(EmbeddedError::BufferTooSmall {
274 required: self.memory.total_size(),
275 available: self.system.ram_size,
276 });
277 }
278
279 Ok(())
280 }
281}
282
283impl Default for EmbeddedConfig {
284 fn default() -> Self {
285 Self::new()
286 }
287}
288
289pub mod presets {
291 use super::*;
292
293 pub const fn esp32() -> EmbeddedConfig {
295 EmbeddedConfig {
296 system: SystemConfig {
297 cpu_freq_mhz: 240,
298 ram_size: 520 * 1024, flash_size: 4 * 1024 * 1024,
300 power_mode: PowerMode::HighPerformance,
301 debug_enabled: false,
302 },
303 memory: MemoryConfig {
304 static_pool_size: 128 * 1024,
305 block_size: 512,
306 num_blocks: 128,
307 enable_guards: false,
308 },
309 realtime: RealtimeConfig::enabled(),
310 comm: CommConfig::high_speed(),
311 }
312 }
313
314 pub const fn esp32c3() -> EmbeddedConfig {
316 EmbeddedConfig {
317 system: SystemConfig {
318 cpu_freq_mhz: 160,
319 ram_size: 400 * 1024, flash_size: 4 * 1024 * 1024,
321 power_mode: PowerMode::Balanced,
322 debug_enabled: false,
323 },
324 memory: MemoryConfig {
325 static_pool_size: 64 * 1024,
326 block_size: 256,
327 num_blocks: 128,
328 enable_guards: false,
329 },
330 realtime: RealtimeConfig::enabled(),
331 comm: CommConfig::new(),
332 }
333 }
334
335 pub const fn cortex_m4() -> EmbeddedConfig {
337 EmbeddedConfig {
338 system: SystemConfig {
339 cpu_freq_mhz: 168,
340 ram_size: 192 * 1024, flash_size: 1024 * 1024, power_mode: PowerMode::Balanced,
343 debug_enabled: false,
344 },
345 memory: MemoryConfig {
346 static_pool_size: 32 * 1024,
347 block_size: 128,
348 num_blocks: 128,
349 enable_guards: false,
350 },
351 realtime: RealtimeConfig::enabled(),
352 comm: CommConfig::new(),
353 }
354 }
355
356 pub const fn riscv() -> EmbeddedConfig {
358 EmbeddedConfig {
359 system: SystemConfig {
360 cpu_freq_mhz: 100,
361 ram_size: 128 * 1024, flash_size: 2 * 1024 * 1024, power_mode: PowerMode::Balanced,
364 debug_enabled: false,
365 },
366 memory: MemoryConfig {
367 static_pool_size: 16 * 1024,
368 block_size: 128,
369 num_blocks: 64,
370 enable_guards: false,
371 },
372 realtime: RealtimeConfig::new(),
373 comm: CommConfig::new(),
374 }
375 }
376
377 pub const fn ultra_low_power() -> EmbeddedConfig {
379 EmbeddedConfig {
380 system: SystemConfig {
381 cpu_freq_mhz: 32,
382 ram_size: 64 * 1024, flash_size: 512 * 1024, power_mode: PowerMode::UltraLowPower,
385 debug_enabled: false,
386 },
387 memory: MemoryConfig::minimal(),
388 realtime: RealtimeConfig::new(),
389 comm: CommConfig::new(),
390 }
391 }
392}
393
394#[cfg(test)]
395mod tests {
396 use super::*;
397
398 #[test]
399 fn test_system_config() {
400 let config = SystemConfig::new();
401 assert!(config.validate().is_ok());
402
403 let hp_config = SystemConfig::high_performance();
404 assert_eq!(hp_config.power_mode, PowerMode::HighPerformance);
405 }
406
407 #[test]
408 fn test_memory_config() {
409 let config = MemoryConfig::new();
410 assert!(config.total_size() > 0);
411
412 let minimal = MemoryConfig::minimal();
413 assert!(minimal.total_size() < config.total_size());
414 }
415
416 #[test]
417 fn test_embedded_config_validation() {
418 let config = EmbeddedConfig::new();
419 assert!(config.validate().is_ok());
420
421 let hp_config = EmbeddedConfig::high_performance();
422 assert!(hp_config.validate().is_ok());
423 }
424
425 #[test]
426 fn test_presets() {
427 let esp32_config = presets::esp32();
428 assert!(esp32_config.validate().is_ok());
429 assert_eq!(esp32_config.system.cpu_freq_mhz, 240);
430
431 let cortex_config = presets::cortex_m4();
432 assert!(cortex_config.validate().is_ok());
433 }
434}