hal_arm/
lib.rs

1#![cfg_attr(all(not(test), not(feature = "host")), no_std)]
2
3use core::ffi::c_char;
4
5use hal_api::{Result, Schedable};
6
7pub mod asm;
8pub mod debug;
9pub mod excep;
10pub mod panic;
11pub mod sched;
12
13mod crit;
14mod print;
15
16mod bindings {
17    #![allow(non_upper_case_globals)]
18    #![allow(non_camel_case_types)]
19    #![allow(non_snake_case)]
20    #![allow(unused)]
21    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
22}
23
24#[link(name = "common", kind = "static", modifiers = "+whole-archive")]
25#[link(name = "device_native")]
26#[link(name = "hal_native")]
27#[link(name = "interface_native")]
28unsafe extern "C" {}
29
30include!(concat!(env!("OUT_DIR"), "/vector_table.rs"));
31
32pub struct ArmMachine;
33
34impl hal_api::Machinelike for ArmMachine {
35    fn init() {
36        unsafe {
37            bindings::init_hal();
38            bindings::init_debug_uart();
39            bindings::dwt_init();
40        }
41    }
42
43    fn print(s: &str) -> Result<()> {
44        use crate::asm;
45        let state = asm::disable_irq_save();
46
47        if (unsafe { bindings::write_debug_uart(s.as_ptr() as *const c_char, s.len() as i32) } != 0)
48        {
49            asm::enable_irq_restr(state);
50            Ok(())
51        } else {
52            asm::enable_irq_restr(state);
53            Err(hal_api::Error::default())
54        }
55    }
56
57    fn bench_start() {
58        unsafe {
59            bindings::dwt_reset();
60        }
61    }
62
63    fn bench_end() -> (u32, f32) {
64        let cycles = unsafe { bindings::dwt_read() };
65        let ns = unsafe { bindings::dwt_cycles_to_ns(cycles) };
66
67        (cycles as u32, ns)
68    }
69
70    fn monotonic_now() -> u64 {
71        unsafe { bindings::monotonic_now() }
72    }
73
74    fn monotonic_freq() -> u64 {
75        unsafe { bindings::monotonic_freq() }
76    }
77
78    fn systick_freq() -> u64 {
79        unsafe { bindings::systick_freq() }
80    }
81
82    type ExcepBacktrace = excep::ExcepBacktrace;
83    type ExcepStackFrame = excep::ExcepStackFrame;
84
85    fn backtrace(initial_fp: *const usize, stack_ptr: *const usize) -> Self::ExcepBacktrace {
86        let frame = excep::ExcepStackFrame::new(stack_ptr);
87        excep::ExcepBacktrace::new(frame, initial_fp)
88    }
89
90    fn stack_frame(stack_ptr: *const usize) -> Self::ExcepStackFrame {
91        excep::ExcepStackFrame::new(stack_ptr)
92    }
93
94    fn panic_handler(info: &core::panic::PanicInfo) -> ! {
95        panic::panic_handler(info)
96    }
97
98    type FaultStatus = excep::FaultStatus;
99    fn get_fault_status(fault: hal_api::Fault) -> Self::FaultStatus {
100        excep::FaultStatus { fault }
101    }
102}
103
104impl Schedable for ArmMachine {
105    fn trigger_reschedule() {
106        unsafe {
107            bindings::reschedule();
108        }
109    }
110}