hal_api/
lib.rs

1#![cfg_attr(not(test), no_std)]
2
3use core::{fmt::Display, ops::Range};
4
5pub mod mem;
6pub mod stack;
7
8#[derive(Default, Debug, PartialEq, Eq, Clone)]
9pub enum Error {
10    #[default]
11    Generic,
12    OutOfMemory(usize),
13    OutOfBoundsPtr(usize, Range<usize>),
14    InvalidAddress(usize),
15}
16
17pub enum Fault {
18    Hard,
19    MemManage,
20    Bus,
21    Usage,
22}
23
24impl Display for Error {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            Error::Generic => write!(f, "Generic"),
28            Error::OutOfMemory(size) => write!(f, "Out of memory (requested {size} bytes)"),
29            Error::OutOfBoundsPtr(ptr, range) => {
30                write!(
31                    f,
32                    "Pointer {:p} out of bounds (expected in {:p}..{:p})",
33                    *ptr as *const u8, range.start as *const u8, range.end as *const u8
34                )
35            }
36            Error::InvalidAddress(addr) => write!(f, "Invalid address {:p}", *addr as *const u8),
37        }
38    }
39}
40
41pub type Result<T> = core::result::Result<T, Error>;
42
43pub trait Machinelike {
44    fn init();
45    fn print(s: &str) -> Result<()>;
46
47    fn bench_start();
48    fn bench_end() -> (u32, f32);
49
50    fn monotonic_now() -> u64;
51    fn monotonic_freq() -> u64;
52    // Returns the frequency of the machine's systick timer in Hz.
53    fn systick_freq() -> u64;
54
55    type ExcepBacktrace: Display;
56    type ExcepStackFrame: Display;
57    fn backtrace(initial_fp: *const usize, stack_ptr: *const usize) -> Self::ExcepBacktrace;
58    fn stack_frame(stack_ptr: *const usize) -> Self::ExcepStackFrame;
59
60    type FaultStatus: Display;
61    fn get_fault_status(fault: Fault) -> Self::FaultStatus;
62
63    fn panic_handler(info: &core::panic::PanicInfo) -> !;
64}
65
66pub trait Schedable {
67    fn trigger_reschedule();
68}