From d5c20bf3484266a38a5b0bbc03678b6e82223d40 Mon Sep 17 00:00:00 2001 From: Philipp_EndevourOS Date: Mon, 28 Jul 2025 17:53:26 +0200 Subject: [PATCH] worted on rtc task, still not tested --- src/init/hardware.rs | 23 ++++++++++++++++++----- src/main.rs | 16 ++++++++-------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/init/hardware.rs b/src/init/hardware.rs index 06e8c41..6fcf46a 100644 --- a/src/init/hardware.rs +++ b/src/init/hardware.rs @@ -1,6 +1,6 @@ use core::slice::RChunks; -use ds3231::{DS3231, Alarm1Config}; +use ds3231::{Alarm1Config, DS3231Error, DS3231}; use embassy_executor::Spawner; use embassy_net::{driver, Stack}; use embassy_sync::mutex::Mutex; @@ -105,16 +105,16 @@ fn setup_i2c( } pub async fn rtc_init_iterrupt(sqw_pin: GPIO21<'static>) -> Input<'static> { - let config = esp_hal::gpio::InputConfig::default().with_pull(Pull::Up); - let mut sqw_interrupt = Input::new(sqw_pin, config); + let config = esp_hal::gpio::InputConfig::default().with_pull(Pull::Up); //Active low interrupt in rtc + let sqw_interrupt = Input::new(sqw_pin, config); sqw_interrupt } pub async fn rtc_config(i2c: I2c<'static, Async>) -> DS3231> { let mut rtc: DS3231> = DS3231::new(i2c, RTC_ADDRESS); let daily_alarm = Alarm1Config::AtTime { - hours: 9, - minutes: 30, + hours: 0, // set alarm every day 00:00:00 to sync time + minutes: 0, seconds: 0, is_pm: None, // 24-hour mode }; @@ -125,6 +125,19 @@ pub async fn rtc_config(i2c: I2c<'static, Async>) -> DS3231> rtc } +pub async fn read_rtc_time<'a>(rtc: &'a mut DS3231>) -> Result> { + match rtc.datetime().await { + Ok(datetime) => { + let utc_time = datetime.and_utc().timestamp() as u64; + Ok(utc_time) + } + Err(e) => { + error!("Failed to read RTC datetime: {:?}", e); + Err(e) + } + } +} + fn setup_spi_led() { diff --git a/src/main.rs b/src/main.rs index 60baffc..d396432 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,21 +70,21 @@ async fn wait_for_stack_up(stack: Stack<'static>) { #[embassy_executor::task] async fn rtc_task( - mut i2c: i2c::master::I2c<'static, Async>, + i2c: i2c::master::I2c<'static, Async>, sqw_pin: peripherals::GPIO21<'static>, ) { let mut rtc_interrupt = init::hardware::rtc_init_iterrupt(sqw_pin).await; let mut rtc = init::hardware::rtc_config(i2c).await; + let mut utc_time = UTC_TIME.lock().await; + let timestamp_result = init::hardware::read_rtc_time(&mut rtc).await; + *utc_time = timestamp_result.unwrap_or(0); + loop { rtc_interrupt.wait_for_falling_edge().await; debug!("RTC interrupt triggered"); - if let Ok(datetime) = rtc.datetime().await { - let mut utc_time = UTC_TIME.lock().await; - *utc_time = datetime.and_utc().timestamp() as u64; - info!("RTC updated UTC_TIME: {}", *utc_time); - } else { - info!("Failed to read RTC datetime"); - } + utc_time = UTC_TIME.lock().await; + let timestamp_result = init::hardware::read_rtc_time(&mut rtc).await; + *utc_time = timestamp_result.unwrap_or(0); } } \ No newline at end of file