mirror of
				https://github.com/Djeeberjr/fw-anwesenheit.git
				synced 2025-11-04 07:34:10 +00:00 
			
		
		
		
	worted on rtc task, still not tested
This commit is contained in:
		
							parent
							
								
									49027fed99
								
							
						
					
					
						commit
						d5c20bf348
					
				@ -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<I2c<'static, Async>> {
 | 
			
		||||
    let mut rtc: DS3231<I2c<'static, Async>> = 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<I2c<'static, Async>>
 | 
			
		||||
    rtc
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn read_rtc_time<'a>(rtc: &'a mut DS3231<I2c<'static, Async>>) -> Result<u64, DS3231Error<esp_hal::i2c::master::Error>> {
 | 
			
		||||
    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() {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										16
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user