75 lines
2.2 KiB
Rust
75 lines
2.2 KiB
Rust
use crate::fat_error::FatResult;
|
|
use crate::hal::Box;
|
|
use async_trait::async_trait;
|
|
use bincode::{Decode, Encode};
|
|
use chrono::{DateTime, Utc};
|
|
use ds323x::ic::DS3231;
|
|
use ds323x::interface::I2cInterface;
|
|
use ds323x::{DateTimeAccess, Ds323x};
|
|
use eeprom24x::addr_size::TwoBytes;
|
|
use eeprom24x::page_size::B32;
|
|
use eeprom24x::unique_serial::No;
|
|
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
|
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
use embedded_storage::{ReadStorage, Storage};
|
|
use esp_hal::delay::Delay;
|
|
use esp_hal::i2c::master::I2c;
|
|
use esp_hal::Blocking;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
pub const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC);
|
|
pub const EEPROM_PAGE: usize = 32;
|
|
//
|
|
#[async_trait(?Send)]
|
|
pub trait RTCModuleInteraction {
|
|
async fn get_rtc_time(&mut self) -> FatResult<DateTime<Utc>>;
|
|
async fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> FatResult<()>;
|
|
|
|
fn write(&mut self, offset: u32, data: &[u8]) -> FatResult<()>;
|
|
fn read(&mut self, offset: u32, data: &mut [u8]) -> FatResult<()>;
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Encode, Decode)]
|
|
pub struct BackupHeader {
|
|
pub timestamp: i64,
|
|
pub(crate) crc16: u16,
|
|
pub size: u16,
|
|
}
|
|
//
|
|
pub struct DS3231Module {
|
|
pub(crate) rtc: Ds323x<
|
|
I2cInterface<I2cDevice<'static, CriticalSectionRawMutex, I2c<'static, Blocking>>>,
|
|
DS3231,
|
|
>,
|
|
|
|
pub storage: eeprom24x::Storage<
|
|
I2cDevice<'static, CriticalSectionRawMutex, I2c<'static, Blocking>>,
|
|
B32,
|
|
TwoBytes,
|
|
No,
|
|
Delay,
|
|
>,
|
|
}
|
|
|
|
#[async_trait(?Send)]
|
|
impl RTCModuleInteraction for DS3231Module {
|
|
async fn get_rtc_time(&mut self) -> FatResult<DateTime<Utc>> {
|
|
Ok(self.rtc.datetime()?.and_utc())
|
|
}
|
|
|
|
async fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> FatResult<()> {
|
|
let naive_time = time.naive_utc();
|
|
Ok(self.rtc.set_datetime(&naive_time)?)
|
|
}
|
|
|
|
fn write(&mut self, offset: u32, data: &[u8]) -> FatResult<()> {
|
|
self.storage.write(offset, data)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn read(&mut self, offset: u32, data: &mut [u8]) -> FatResult<()> {
|
|
self.storage.read(offset, data)?;
|
|
Ok(())
|
|
}
|
|
}
|