Skip to main content

mpfs_pac/
lib.rs

1#![no_std]
2
3mod bindings;
4pub use bindings::*;
5
6mod clint;
7pub use clint::*;
8
9mod encoding;
10pub use encoding::*;
11
12mod ethernet;
13pub use ethernet::*;
14mod gpio;
15pub use gpio::*;
16
17mod sysreg;
18pub use sysreg::*;
19
20mod timer;
21pub use timer::*;
22
23mod uart;
24pub use uart::*;
25
26mod usb;
27pub use usb::*;
28
29pub mod spi;
30pub use spi::{QSPI, SPI0, SPI1};
31
32/// Add to a regular address to get the same address in uncached memory
33#[cfg(feature = "upper-memory-layout")]
34pub const UNCACHED_MEMORY_OFFSET: usize = 0x4_0000_0000;
35#[cfg(not(feature = "upper-memory-layout"))]
36pub const UNCACHED_MEMORY_OFFSET: usize = 0x1000_0000;
37
38#[inline]
39pub fn hart_id() -> usize {
40    let mut hart_id: usize;
41    unsafe {
42        core::arch::asm!("csrr {}, mhartid", out(reg) hart_id);
43    }
44    hart_id
45}
46
47pub fn last_linked_address() -> usize {
48    &raw mut __app_hart_common_end as usize
49}
50
51pub fn last_address() -> usize {
52    let base_address = if cfg!(feature = "upper-memory-layout") {
53        0x10_0000_0000
54    } else {
55        0x8000_0000
56    };
57    let size_ram = if cfg!(feature = "beaglev-fire") {
58        // 2GB
59        0x8000_0000
60    } else {
61        panic!("Unsupported board: No RAM size defined");
62    };
63
64    base_address + size_ram
65}
66
67pub fn heap_start() -> usize {
68    last_linked_address()
69}
70
71pub fn heap_end() -> usize {
72    let reserved_memory_size = match option_env!("MPFS_RESERVED_MEMORY_SIZE") {
73        Some(size) => usize::from_str_radix(size, 16)
74            .expect("MPFS_RESERVED_MEMORY_SIZE must be a valid hexadecimal number"),
75        None => 0x0,
76    };
77
78    last_address() - reserved_memory_size
79}