Skip to main content

oxigdal_embedded/
config.rs

1//! Configuration for embedded systems
2//!
3//! Provides compile-time and runtime configuration options
4
5use crate::error::{EmbeddedError, Result};
6
7#[cfg(feature = "low-power")]
8use crate::power::PowerMode;
9
10// Stub PowerMode when feature is disabled
11/// Power mode configuration for embedded systems
12#[cfg(not(feature = "low-power"))]
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[repr(u8)]
15pub enum PowerMode {
16    /// Full performance, maximum power consumption
17    HighPerformance = 0,
18    /// Balanced performance and power
19    Balanced = 1,
20    /// Low power, reduced performance
21    LowPower = 2,
22    /// Ultra low power, minimal functionality
23    UltraLowPower = 3,
24}
25
26/// System configuration
27#[derive(Debug, Clone, Copy)]
28pub struct SystemConfig {
29    /// CPU frequency in MHz
30    pub cpu_freq_mhz: u32,
31    /// RAM size in bytes
32    pub ram_size: usize,
33    /// Flash size in bytes
34    pub flash_size: usize,
35    /// Default power mode
36    pub power_mode: PowerMode,
37    /// Enable debug logging
38    pub debug_enabled: bool,
39}
40
41impl SystemConfig {
42    /// Create a new system configuration
43    pub const fn new() -> Self {
44        Self {
45            cpu_freq_mhz: 80,            // Common embedded frequency
46            ram_size: 512 * 1024,        // 512KB
47            flash_size: 4 * 1024 * 1024, // 4MB
48            power_mode: PowerMode::Balanced,
49            debug_enabled: cfg!(debug_assertions),
50        }
51    }
52
53    /// Create configuration for high performance
54    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    /// Create configuration for low power
65    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    /// Validate configuration
76    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/// Memory configuration
96#[derive(Debug, Clone, Copy)]
97pub struct MemoryConfig {
98    /// Static pool size in bytes
99    pub static_pool_size: usize,
100    /// Block pool block size
101    pub block_size: usize,
102    /// Number of blocks in block pool
103    pub num_blocks: usize,
104    /// Enable memory guards (debug)
105    pub enable_guards: bool,
106}
107
108impl MemoryConfig {
109    /// Create a new memory configuration
110    pub const fn new() -> Self {
111        Self {
112            static_pool_size: 64 * 1024, // 64KB
113            block_size: 256,
114            num_blocks: 256,
115            enable_guards: cfg!(debug_assertions),
116        }
117    }
118
119    /// Create configuration for minimal memory
120    pub const fn minimal() -> Self {
121        Self {
122            static_pool_size: 4 * 1024, // 4KB
123            block_size: 64,
124            num_blocks: 64,
125            enable_guards: false,
126        }
127    }
128
129    /// Get total memory usage
130    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/// Real-time configuration
142#[derive(Debug, Clone, Copy)]
143pub struct RealtimeConfig {
144    /// Enable real-time scheduling
145    pub enabled: bool,
146    /// Maximum number of tasks
147    pub max_tasks: usize,
148    /// Default task priority
149    pub default_priority: u8,
150    /// Enable deadline monitoring
151    pub monitor_deadlines: bool,
152}
153
154impl RealtimeConfig {
155    /// Create a new real-time configuration
156    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    /// Create configuration with real-time enabled
166    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/// Communication configuration
183#[derive(Debug, Clone, Copy)]
184pub struct CommConfig {
185    /// UART baud rate
186    pub uart_baud: u32,
187    /// I2C frequency (Hz)
188    pub i2c_freq: u32,
189    /// SPI frequency (Hz)
190    pub spi_freq: u32,
191    /// Enable CRC checking
192    pub enable_crc: bool,
193}
194
195impl CommConfig {
196    /// Create a new communication configuration
197    pub const fn new() -> Self {
198        Self {
199            uart_baud: 115200,
200            i2c_freq: 100_000,   // 100kHz
201            spi_freq: 1_000_000, // 1MHz
202            enable_crc: false,
203        }
204    }
205
206    /// Create high-speed configuration
207    pub const fn high_speed() -> Self {
208        Self {
209            uart_baud: 921600,
210            i2c_freq: 400_000,    // 400kHz (fast mode)
211            spi_freq: 10_000_000, // 10MHz
212            enable_crc: true,
213        }
214    }
215}
216
217impl Default for CommConfig {
218    fn default() -> Self {
219        Self::new()
220    }
221}
222
223/// Complete embedded configuration
224#[derive(Debug, Clone, Copy)]
225pub struct EmbeddedConfig {
226    /// System configuration
227    pub system: SystemConfig,
228    /// Memory configuration
229    pub memory: MemoryConfig,
230    /// Real-time configuration
231    pub realtime: RealtimeConfig,
232    /// Communication configuration
233    pub comm: CommConfig,
234}
235
236impl EmbeddedConfig {
237    /// Create a new embedded configuration
238    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    /// Create high-performance configuration
248    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    /// Create low-power configuration
258    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    /// Validate the entire configuration
268    pub fn validate(&self) -> Result<()> {
269        self.system.validate()?;
270
271        // Check memory doesn't exceed RAM
272        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
289/// Configuration presets for common platforms
290pub mod presets {
291    use super::*;
292
293    /// ESP32 preset
294    pub const fn esp32() -> EmbeddedConfig {
295        EmbeddedConfig {
296            system: SystemConfig {
297                cpu_freq_mhz: 240,
298                ram_size: 520 * 1024, // 520KB
299                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    /// ESP32-C3 preset (RISC-V)
315    pub const fn esp32c3() -> EmbeddedConfig {
316        EmbeddedConfig {
317            system: SystemConfig {
318                cpu_freq_mhz: 160,
319                ram_size: 400 * 1024, // 400KB
320                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    /// ARM Cortex-M4 preset
336    pub const fn cortex_m4() -> EmbeddedConfig {
337        EmbeddedConfig {
338            system: SystemConfig {
339                cpu_freq_mhz: 168,
340                ram_size: 192 * 1024,    // 192KB
341                flash_size: 1024 * 1024, // 1MB
342                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    /// RISC-V preset (generic)
357    pub const fn riscv() -> EmbeddedConfig {
358        EmbeddedConfig {
359            system: SystemConfig {
360                cpu_freq_mhz: 100,
361                ram_size: 128 * 1024,        // 128KB
362                flash_size: 2 * 1024 * 1024, // 2MB
363                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    /// Ultra-low-power preset
378    pub const fn ultra_low_power() -> EmbeddedConfig {
379        EmbeddedConfig {
380            system: SystemConfig {
381                cpu_freq_mhz: 32,
382                ram_size: 64 * 1024,    // 64KB
383                flash_size: 512 * 1024, // 512KB
384                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}