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
use stm32l4xx_hal::rtc::Rtc;
use crate::system::bms::BatteryManagement;
use crate::system::notification::NotificationManager;
use crate::application::application_manager::ApplicationManager;
pub const DMA_HALF_BYTES: usize = 64;
pub const CPU_USAGE_POLL_HZ: u32 = 1;
pub const SYSTICK_HZ: u32 = 3;
pub const TSC_HZ: u32 = (8 * 3);
pub const SYS_CLK_HZ: u32 = 16_000_000;
pub const SPI_MHZ: u32 = SYS_CLK_HZ / 2_000_000;
pub const I2C_KHZ: u32 = 100;
pub const IDLE_TIMEOUT_SECONDS: u32 = 15;
pub struct System {
rtc: Rtc,
bms: BatteryManagement,
nm: NotificationManager,
am: ApplicationManager,
stats: Stats,
}
impl System {
pub fn new(rtc: Rtc, bms: BatteryManagement, nm: NotificationManager, am: ApplicationManager) -> Self {
Self {
rtc,
bms,
nm,
am,
stats: Stats::default(),
}
}
pub fn rtc(&mut self) -> &mut Rtc {
&mut self.rtc
}
pub fn bms(&mut self) -> &mut BatteryManagement {
&mut self.bms
}
pub fn am(&mut self) -> &mut ApplicationManager {
&mut self.am
}
pub fn nm(&mut self) -> &mut NotificationManager {
&mut self.nm
}
pub fn ss(&mut self) -> &mut Stats {
&mut self.stats
}
pub fn is_idle(&mut self) -> bool {
(self.ss().idle_count / SYSTICK_HZ) > IDLE_TIMEOUT_SECONDS
}
pub fn get_free_stack() -> usize {
unsafe {
extern "C" {
static __ebss: u32;
static __sdata: u32;
}
let ebss = &__ebss as *const u32 as usize;
let start = &__sdata as *const u32 as usize;
let total = ebss - start;
(16 * 1024) - total
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Stats {
pub cpu_usage: f32,
pub tsc_events: u32,
pub idle_count: u32,
pub tsc_threshold: u16,
}
impl Default for Stats {
fn default() -> Self {
Self {
cpu_usage: 0.0,
tsc_events: 0,
idle_count: 0,
tsc_threshold: 0,
}
}
}