base/memory/mio.rs
1/// # Safety
2///
3/// We label the mmio function unsafe since
4/// we will be working with raw memory. Rust cannot
5/// make any guarantees when we do this.
6pub unsafe fn Write(address: usize, offset: usize, value: u8) {
7 // Set the pointer based off of the address
8 let register = address as *mut u8;
9
10 // write_volatile is a member of the *mut raw
11 // and we can use the .add() to give us another pointer
12 // at an offset based on the original pointer's memory
13 // address. NOTE: The add uses pointer arithmetic so it is
14 // new_pointer = old_pointer + sizeof(pointer_type) * offset
15 register.add(offset).write_volatile(value);
16}
17
18/// # Safety
19///
20/// We label the mmio function unsafe since
21/// we will be working with raw memory. Rust cannot
22/// make any guarantees when we do this.
23pub unsafe fn Read(address: usize) -> Optional<u8> {
24 // Set the pointer based off of the address
25 let register = address as *mut u8;
26
27 // read_volatile() is much like write_volatile() except it
28 // will grab 8-bits from the pointer and give that value to us.
29 return if register.add(5).read_volatile() & 1 == 0 {
30 // No data.
31 Optional::None
32 } else {
33 // Data found!
34 Optional::Some(register.add(0).read_volatile())
35 };
36}
37
38// IMPORTS //
39
40use crate::optional::Optional;