Struct vm_superio::rtc_pl031::Rtc
source · pub struct Rtc<EV: RtcEvents> { /* private fields */ }
Expand description
A PL031 Real Time Clock (RTC) that emulates a long time base counter.
This structure emulates the registers for the RTC.
Example
let mut data = [0; 4];
let mut rtc = Rtc::new();
const RTCDR: u16 = 0x0; // Data Register.
const RTCLR: u16 = 0x8; // Load Register.
// Write system time since UNIX_EPOCH in seconds to the load register.
let v = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as u32;
data = v.to_le_bytes();
rtc.write(RTCLR, &data);
// Read the value back out of the load register.
rtc.read(RTCLR, &mut data);
assert_eq!(v, u32::from_le_bytes(data));
// Sleep for 1.5 seconds to let the counter tick.
let delay = Duration::from_millis(1500);
thread::sleep(delay);
// Read the current RTC value from the Data Register.
rtc.read(RTCDR, &mut data);
assert!(u32::from_le_bytes(data) > v);
Implementations
sourceimpl<EV: RtcEvents> Rtc<EV>
impl<EV: RtcEvents> Rtc<EV>
sourcepub fn from_state(state: &RtcState, rtc_events: EV) -> Self
pub fn from_state(state: &RtcState, rtc_events: EV) -> Self
Creates a new AMBA PL031 RTC
instance from a given state
and that is able to track
events during operation using the passed rtc_events
object.
For creating the instance from a fresh state, with_events
or
new
methods can be used.
Arguments
state
- A reference to the state from which theRtc
is constructed.rtc_events
- TheRtcEvents
implementation used to track the occurrence of failure or missed events in the RTC operation.
sourcepub fn with_events(rtc_events: EV) -> Self
pub fn with_events(rtc_events: EV) -> Self
Creates a new AMBA PL031 RTC
instance that is able to track events during operation using
the passed rtc_events
object. The instance is created from the default state.
Arguments
rtc_events
- TheRtcEvents
implementation used to track the occurrence of failure or missed events in the RTC operation.
sourcepub fn write(&mut self, offset: u16, data: &[u8; 4])
pub fn write(&mut self, offset: u16, data: &[u8; 4])
Handles a write request from the driver at offset
offset from the
base register address.
Arguments
offset
- The offset from the base register specifying the register to be written.data
- The little endian, 4 byte array to write to the register
Example
You can see an example of how to use this function in the
Example
section from Rtc
.
sourcepub fn read(&mut self, offset: u16, data: &mut [u8; 4])
pub fn read(&mut self, offset: u16, data: &mut [u8; 4])
Handles a read request from the driver at offset
offset from the
base register address.
Arguments
offset
- The offset from the base register specifying the register to be read.data
- The little-endian, 4 byte array storing the read value.
Example
You can see an example of how to use this function in the
Example
section from Rtc
.