/
set_alarm.rs
85 lines (69 loc) · 3.09 KB
/
set_alarm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
extern crate rv3028c7_rtc;
use core::ops::{Add};
use linux_embedded_hal::I2cdev;
use chrono::{NaiveDateTime, Utc};
use rv3028c7_rtc::{RV3028};
use std::time::Duration;
use std::thread::sleep;
use rtcc::DateTimeAccess;
fn get_sys_timestamp() -> (NaiveDateTime, u32) {
let now = Utc::now();
let now_timestamp = now.timestamp();
(now.naive_utc(), now_timestamp.try_into().unwrap() )
}
const MUX_I2C_ADDRESS: u8 = 0x70;
const MUX_CHAN_FIRST:u8 = 0b0000_0001 ; //channel 0, LSB
const MUX_CHAN_SECOND:u8 = 0b1000_0000 ; // channel 7, MSB
fn main() {
// This is a specific configuration for Raspberry Pi -- YMMV
// Initialize the I2C device
let i2c = I2cdev::new("/dev/i2c-1").expect("Failed to open I2C device");
let i2c_bus = shared_bus::BusManagerSimple::new(i2c);
// Create a new instance of the RV3028 driver
// let mut rtc = RV3028::new(i2c);
let mut rtc1 = RV3028::new_with_mux(i2c_bus.acquire_i2c(), MUX_I2C_ADDRESS, MUX_CHAN_FIRST);
let mut rtc2 = RV3028::new_with_mux(i2c_bus.acquire_i2c(), MUX_I2C_ADDRESS, MUX_CHAN_SECOND);
let mut rtc3 = RV3028::new(i2c_bus.acquire_i2c());
let (sys_datetime, sys_unix_timestamp) = get_sys_timestamp();
// use the set_datetime method to ensure all the timekeeping registers on
// the rtc are aligned to the same values
rtc3.set_datetime(&sys_datetime).unwrap();
rtc1.set_datetime(&sys_datetime).unwrap();
rtc2.set_datetime(&sys_datetime).unwrap();
let rtc_unix_time = rtc3.get_unix_time().unwrap();
// verify that the individual year, month, day registers are set correctly
let rtc3_dt = rtc3.datetime().unwrap();
println!("start sys {} rtc {} {}" , sys_unix_timestamp, rtc_unix_time, rtc3_dt);
// disable alarm interrupts to begin with
// rtc2.toggle_alarm_int_enable(false).unwrap();
rtc1.clear_all_int_out_bits().unwrap();
rtc2.clear_all_int_out_bits().unwrap();
rtc3.clear_all_int_out_bits().unwrap();
rtc1.toggle_plain_clockout(false).unwrap();
rtc2.toggle_plain_clockout(false).unwrap();
rtc3.toggle_plain_clockout(false).unwrap();
rtc1.check_and_clear_alarm().unwrap();
rtc2.check_and_clear_alarm().unwrap();
rtc3.check_and_clear_alarm().unwrap();
println!("INT disabled, pausing...");
sleep(Duration::from_secs(3));
let init_dt = rtc2.datetime().unwrap();
let alarm_dt = init_dt.add(Duration::from_secs(60));
println!("init_dt : {}", init_dt);
println!("alarm_dt: {}", alarm_dt);
rtc1.set_alarm(&alarm_dt, None, false, false, true).unwrap();
rtc2.set_alarm(&alarm_dt, None, false, false, true).unwrap();
rtc3.set_alarm(&alarm_dt, None, false, false, true).unwrap();
let (next_alarm_dt, _, _, _, _) =
rtc1.get_alarm_datetime_wday_matches().unwrap();
rtc1.toggle_alarm_int_enable(true).unwrap();
println!("rtc1 alarm at {} ", next_alarm_dt);
let (next_alarm_dt, _, _, _, _) =
rtc2.get_alarm_datetime_wday_matches().unwrap();
rtc2.toggle_alarm_int_enable(true).unwrap();
println!("rtc2 alarm at {} ", next_alarm_dt);
let (next_alarm_dt, _, _, _, _) =
rtc3.get_alarm_datetime_wday_matches().unwrap();
rtc3.toggle_alarm_int_enable(false).unwrap();
println!("rtc3 alarm at {} ", next_alarm_dt);
}