forked from lobaro/FreeRTOS-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rs
70 lines (61 loc) · 2.04 KB
/
utils.rs
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
use crate::base::*;
use crate::prelude::v1::*;
use crate::shim::*;
#[derive(Debug, Copy, Clone)]
pub struct TypeSizeError {
pub id: usize,
pub c_size: usize,
pub rust_size: usize,
}
#[cfg(feature = "cpu_clock")]
pub fn cpu_clock_hz() -> u32 {
unsafe { freertos_rs_get_configCPU_CLOCK_HZ() }
}
/// Perform checks whether the C FreeRTOS shim and Rust agree on the sizes of used types.
pub fn shim_sanity_check() -> Result<(), TypeSizeError> {
let checks = [
(0, mem::size_of::<FreeRtosVoidPtr>()),
(1, mem::size_of::<FreeRtosCharPtr>()),
(2, mem::size_of::<FreeRtosChar>()),
(10, mem::size_of::<FreeRtosBaseType>()),
(11, mem::size_of::<FreeRtosUBaseType>()),
(12, mem::size_of::<FreeRtosTickType>()),
(20, mem::size_of::<FreeRtosTaskHandle>()),
(21, mem::size_of::<FreeRtosQueueHandle>()),
(22, mem::size_of::<FreeRtosSemaphoreHandle>()),
(23, mem::size_of::<FreeRtosTaskFunction>()),
(24, mem::size_of::<FreeRtosTimerHandle>()),
(25, mem::size_of::<FreeRtosTimerCallback>()),
(30, mem::size_of::<FreeRtosTaskStatusFfi>()),
(31, mem::size_of::<FreeRtosTaskState>()),
(32, mem::size_of::<FreeRtosUnsignedLong>()),
(33, mem::size_of::<FreeRtosUnsignedShort>()),
];
for check in &checks {
let c_size = unsafe { freertos_rs_sizeof(check.0) };
if c_size != check.1 as u8 {
return Err(TypeSizeError {
id: check.0 as usize,
c_size: c_size as usize,
rust_size: check.1,
});
}
}
Ok(())
}
#[cfg(any(feature = "time", feature = "hooks", feature = "sync"))]
pub unsafe fn str_from_c_string(str: *const u8) -> Result<String, FreeRtosError> {
let mut buf = Vec::new();
let mut p = str;
loop {
if *p == 0 {
break;
}
buf.push(*p);
p = p.offset(1);
}
match String::from_utf8(buf) {
Ok(s) => Ok(s),
Err(_) => Err(FreeRtosError::StringConversionError),
}
}