Compare commits

..

2 Commits

Author SHA1 Message Date
a015d6b983 fixed embassy taks arena size 2025-08-04 18:52:03 +02:00
Philipp_EndevourOS
1ae5250449 reworked RTC without alarms and without own thread 2025-08-04 18:12:46 +02:00
5 changed files with 88 additions and 148 deletions

41
Cargo.lock generated
View File

@@ -151,9 +151,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cc"
version = "1.2.30"
version = "1.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "deec109607ca693028562ed836a5f1c4b8bd77755c4e132fc5ce11b0b6211ae7"
checksum = "c3a42d84bb6b69d3a8b3eaacf0d88f179e1929695e1ad012b6cf64d9caaa5fd2"
dependencies = [
"shlex",
]
@@ -285,7 +285,7 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95249b50c6c185bee49034bcb378a49dc2b5dff0be90ff6616d31d64febab05d"
dependencies = [
"litrs 0.4.1",
"litrs 0.4.2",
]
[[package]]
@@ -351,14 +351,31 @@ checksum = "6207c84e9bc8df8ef3c155196df290f2a51f010bd60c2e78366e51979988bdb5"
[[package]]
name = "embassy-embedded-hal"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8578db196d74db92efdd5ebc546736dac1685499ee245b22eff92fa5e4b57945"
checksum = "8c62a3bf127e03832fb97d8b01a058775e617653bc89e2a12c256485a7fb54c1"
dependencies = [
"embassy-embedded-hal 0.4.0",
"embassy-futures",
"embassy-sync 0.6.2",
"embassy-time",
"embedded-hal 0.2.7",
"embedded-hal 1.0.0",
"embedded-hal-async",
"embedded-storage",
"embedded-storage-async",
"nb 1.1.0",
]
[[package]]
name = "embassy-embedded-hal"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1611b7a7ab5d1fbed84c338df26d56fd9bded58006ebb029075112ed2c5e039"
dependencies = [
"embassy-futures",
"embassy-hal-internal",
"embassy-sync 0.7.0",
"embassy-time",
"embedded-hal 0.2.7",
"embedded-hal 1.0.0",
"embedded-hal-async",
@@ -669,7 +686,7 @@ dependencies = [
"delegate",
"digest",
"document-features",
"embassy-embedded-hal",
"embassy-embedded-hal 0.3.2",
"embassy-futures",
"embassy-sync 0.6.2",
"embedded-can",
@@ -735,7 +752,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbece384edaf0d1eabfa45afa96d910634d4158638ef983b2d419a8dec832246"
dependencies = [
"document-features",
"litrs 0.4.1",
"litrs 0.4.2",
"object",
"proc-macro-crate",
"proc-macro2",
@@ -1177,9 +1194,9 @@ dependencies = [
[[package]]
name = "litrs"
version = "0.4.1"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5"
checksum = "f5e54036fe321fd421e10d732f155734c4e4afd610dd556d9a82833ab3ee0bed"
dependencies = [
"proc-macro2",
]
@@ -1600,9 +1617,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.141"
version = "1.0.142"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3"
checksum = "030fedb782600dcbd6f02d479bf0d817ac3bb40d644745b769d6a96bc3afc5a7"
dependencies = [
"itoa",
"memchr",

View File

@@ -40,7 +40,7 @@ bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "a5
"macros",
] }
critical-section = "1.2.0"
embassy-executor = { version = "0.7.0", features = ["task-arena-size-20480"] }
embassy-executor = { version = "0.7.0", features = ["nightly"] }
embassy-time = { version = "0.4.0", features = ["generic-queue-8"] }
esp-hal-embassy = { version = "0.9.0", features = ["esp32c6"] }
esp-wifi = { version = "0.15.0", features = [

View File

@@ -1,64 +1,43 @@
use core::time;
use chrono::NaiveDate;
use ds3231::{
Alarm1Config, Config, DS3231, DS3231Error, InterruptControl, Oscillator, Seconds,
SquareWaveFrequency, TimeRepresentation,
Config, DS3231, DS3231Error, InterruptControl, Oscillator, SquareWaveFrequency,
TimeRepresentation,
};
use embassy_time::{Duration, Timer, WithTimeout};
use esp_hal::{
Async,
i2c::{self, master::I2c},
peripherals,
};
use log::{debug, error, info};
use crate::{FEEDBACK_STATE, UTC_TIME, drivers, feedback, init};
use chrono::{NaiveDateTime, TimeZone, Utc};
use crate::{FEEDBACK_STATE, drivers, feedback};
use chrono::{TimeZone, Utc};
include!(concat!(env!("OUT_DIR"), "/build_time.rs"));
const RTC_ADDRESS: u8 = 0x68;
#[embassy_executor::task]
pub async fn rtc_task(
i2c: i2c::master::I2c<'static, Async>,
sqw_pin: peripherals::GPIO21<'static>,
) {
UTC_TIME.signal(BUILD_UNIX_TIME);
info!("Build time: {}", BUILD_UNIX_TIME);
pub struct RTCClock {
dev: DS3231<I2c<'static, Async>>,
}
// i2c.write_async(RTC_ADDRESS, &[0x0E, 0b00000000]) // Clear control register
// .await
// .unwrap_or_else(|e| {
// FEEDBACK_STATE.signal(feedback::FeedbackState::Error);
// error!("Failed to clear RTC control register: {:?}", e);
// });
impl RTCClock {
pub async fn new(i2c: i2c::master::I2c<'static, Async>) -> Self {
debug!("configuring rtc...");
let rtc = drivers::rtc::rtc_config(i2c).await;
debug!("rtc up");
// debug!("init rtc interrupt");
// let mut rtc_interrupt = init::hardware::setup_rtc_iterrupt(sqw_pin).await;
RTCClock { dev: rtc }
}
debug!("configuring rtc");
let mut rtc = drivers::rtc::rtc_config(i2c).await;
debug!("rtc up");
loop {
//set_rtc_alarm(&mut rtc).await;
// debug!("Waiting for RTC interrupt...");
// rtc_interrupt.wait_for_falling_edge().await;
// debug!("RTC interrupt triggered");
Timer::after(Duration::from_millis(1000)).await;
//TODO use pub sub channel or something similar to send the time when needed
let timestamp = drivers::rtc::read_rtc_time(&mut rtc).await;
match timestamp {
Ok(ts) => {
UTC_TIME.signal(ts);
info!("Current UTC time: {}", UTC_TIME.wait().await);
pub async fn get_time(&mut self) -> u64 {
match self.dev.datetime().await {
Ok(datetime) => {
let utc_time = datetime.and_utc().timestamp() as u64;
utc_time
}
Err(e) => {
FEEDBACK_STATE.signal(feedback::FeedbackState::Error);
error!("Failed to read RTC datetime: {:?}", e);
0
}
}
}
@@ -94,7 +73,6 @@ pub async fn rtc_config(i2c: I2c<'static, Async>) -> DS3231<I2c<'static, Async>>
});
info!("RTC datetime set to: {}", naive_dt);
match rtc.status().await {
Ok(mut status) => {
status.set_alarm1_flag(false);
@@ -110,67 +88,9 @@ 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>> {
let timestamp_result = rtc.datetime().await?;
Ok(timestamp_result.and_utc().timestamp() as u64)
}
// match rtc.datetime().await {
// Ok(datetime) => {
// let utc_time = datetime.and_utc().timestamp() as u64;
// Ok(utc_time)
// }
// Err(e) => {
// FEEDBACK_STATE.signal(feedback::FeedbackState::Error);
// error!("Failed to read RTC datetime: {:?}", e);
// Err(e)
// }
// }
// }
// let alarm_config = Alarm1Config::AtSeconds { seconds: 0};
// match rtc.set_alarm1(&alarm_config).await {
// Ok(_) => info!("Alarm 1 set to trigger at seconds"),
// Err(e) => {
// FEEDBACK_STATE.signal(feedback::FeedbackState::Error);
// error!("Failed to set Alarm 1: {:?}", e);
// }
// }
/* ************************************************************************************** */
// #[embassy_executor::task]
// pub async fn rtc_task() {
// info!("RTC task started");
// // Initialize I2C and RTC here
// loop {
// // Read RTC time and update UTC_TIME signal
// // let utc_time = read_rtc_time(&mut rtc).await.unwrap();
// // UTC_TIME.signal(utc_time);
// // Simulate waiting for an interrupt or event
// Timer::after(Duration::from_millis(1000)).await;
// info!("RTC tick");
// }
// }
// }
// TODO Update time when device is connected other device over Wifi
/* pub async fn update_rtc_time<'a>(rtc: &'a mut DS3231<I2c<'static, Async>>, datetime: u64) -> Result<(), DS3231Error<esp_hal::i2c::master::Error>> {
match rtc.set_datetime(datetime).await {
info!("RTC datetime updated to: {}", datetime);
Ok(_) => Ok(()),
Err(e) => {
error!("Failed to update RTC datetime: {:?}", e);
Err(e)
}
}
}
*/

View File

@@ -1,32 +1,35 @@
use embassy_executor::Spawner;
use embassy_net::{driver, Stack};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_net::{Stack, driver};
use embassy_sync::blocking_mutex::Mutex;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use esp_hal::config;
use esp_hal::gpio::{Input, Pull};
use esp_hal::i2c::master::Config;
use esp_hal::peripherals::{self, GPIO0, GPIO1, GPIO3, GPIO4, GPIO5, GPIO6, GPIO7, GPIO19, GPIO21, GPIO22, GPIO23, I2C0, UART1};
use esp_hal::peripherals::{
self, GPIO0, GPIO1, GPIO3, GPIO4, GPIO5, GPIO6, GPIO7, GPIO19, GPIO21, GPIO22, GPIO23, I2C0,
UART1,
};
use esp_hal::time::Rate;
use esp_hal::{
Async,
clock::CpuClock,
gpio::{Output, OutputConfig},
i2c::master::I2c,
timer::{systimer::SystemTimer, timg::TimerGroup},
uart::Uart,
i2c::master::I2c,
gpio::{Output, OutputConfig}
};
use esp_println::logger::init_logger;
use log::{debug, error};
use crate::init::wifi;
use crate::init::network;
use crate::init::wifi;
/*************************************************
* GPIO Pinout Xiao Esp32c6
* GPIO Pinout Xiao Esp32c6
*
* D0 -> GPIO0 -> Level Shifter OE
* D1 -> GPIO1 -> Level Shifter A0 -> LED
* D3 -> GPIO21 -> SQW Interrupt RTC
* D3 -> GPIO21 -> SQW Interrupt RTC //not in use anymore
* D4 -> GPIO22 -> SDA
* D5 -> GPIO23 -> SCL
* D7 -> GPIO17 -> Level Shifter A1 -> NFC Reader
@@ -35,15 +38,22 @@ use crate::init::network;
*************************************************/
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
fn panic(info: &core::panic::PanicInfo) -> ! {
loop {
error!("PANIC: {info}");
}
}
esp_bootloader_esp_idf::esp_app_desc!();
pub async fn hardware_init(spawner: &mut Spawner) -> (Uart<'static, Async>, Stack<'static>, I2c<'static, Async>, GPIO21<'static>, GPIO19<'static>) {
pub async fn hardware_init(
spawner: &mut Spawner,
) -> (
Uart<'static, Async>,
Stack<'static>,
I2c<'static, Async>,
GPIO19<'static>,
) {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
@@ -68,19 +78,17 @@ pub async fn hardware_init(spawner: &mut Spawner) -> (Uart<'static, Async>, Stac
let i2c_device = setup_i2c(peripherals.I2C0, peripherals.GPIO22, peripherals.GPIO23);
//RTC Interrupt pin
let sqw_pin = peripherals.GPIO21;
let buzzer_gpio = peripherals.GPIO19;
debug!("hardware init done");
(uart_device, stack, i2c_device, sqw_pin, buzzer_gpio)
(uart_device, stack, i2c_device, buzzer_gpio)
}
// Initialize the level shifter for the NFC reader and LED (output-enable (OE) input is low, all outputs are placed in the high-impedance (Hi-Z) state)
fn init_lvl_shifter(oe_pin: GPIO0<'static>){
let mut oe_lvl_shifter = Output::new(oe_pin, esp_hal::gpio::Level::Low, OutputConfig::default());
fn init_lvl_shifter(oe_pin: GPIO0<'static>) {
let mut oe_lvl_shifter =
Output::new(oe_pin, esp_hal::gpio::Level::Low, OutputConfig::default());
oe_lvl_shifter.set_high();
}
@@ -125,13 +133,11 @@ pub async fn setup_rtc_iterrupt(sqw_pin: GPIO21<'static>) -> Input<'static> {
}
pub fn setup_buzzer(buzzer_gpio: GPIO19<'static>) -> Output<'static> {
let config = esp_hal::gpio::OutputConfig::default().with_drive_strength(esp_hal::gpio::DriveStrength::_40mA);
let config = esp_hal::gpio::OutputConfig::default()
.with_drive_strength(esp_hal::gpio::DriveStrength::_40mA);
let buzzer = Output::new(buzzer_gpio, esp_hal::gpio::Level::Low, config);
buzzer
}
fn setup_spi_led() {
}
fn setup_spi_led() {}

View File

@@ -6,15 +6,12 @@
use embassy_executor::Spawner;
use embassy_net::Stack;
use embassy_sync::{
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex},
pubsub::{
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}, channel::Channel, pubsub::{
PubSubChannel, Publisher,
WaitResult::{Lagged, Message},
},
signal::Signal,
}, signal::Signal
};
use embassy_time::{Duration, Timer};
use esp_alloc::EspHeap;
use log::{debug, info};
use static_cell::make_static;
@@ -26,7 +23,6 @@ mod init;
mod store;
mod webserver;
static UTC_TIME: Signal<CriticalSectionRawMutex, u64> = Signal::new();
static FEEDBACK_STATE: Signal<CriticalSectionRawMutex, feedback::FeedbackState> = Signal::new();
type TallyChannel = PubSubChannel<NoopRawMutex, TallyID, 8, 2, 1>;
@@ -34,7 +30,7 @@ type TallyPublisher = Publisher<'static, NoopRawMutex, TallyID, 8, 2, 1>;
#[esp_hal_embassy::main]
async fn main(mut spawner: Spawner) {
let (uart_device, stack, _i2c, sqw_pin, buzzer_gpio) =
let (uart_device, stack, _i2c, buzzer_gpio) =
init::hardware::hardware_init(&mut spawner).await;
wait_for_stack_up(stack).await;
@@ -47,6 +43,8 @@ async fn main(mut spawner: Spawner) {
let publisher = chan.publisher().unwrap();
let mut rtc = drivers::rtc::RTCClock::new(_i2c).await;
/****************************** Spawning tasks ***********************************/
debug!("spawing NFC reader task...");
spawner.must_spawn(drivers::nfc_reader::rfid_reader_task(
@@ -54,9 +52,6 @@ async fn main(mut spawner: Spawner) {
publisher,
));
debug!("spawing rtc task");
spawner.must_spawn(drivers::rtc::rtc_task(_i2c, sqw_pin));
debug!("spawing feedback task..");
spawner.must_spawn(feedback::feedback_task(buzzer_gpio));
/******************************************************************************/
@@ -67,8 +62,10 @@ async fn main(mut spawner: Spawner) {
FEEDBACK_STATE.signal(feedback::FeedbackState::Startup);
loop {
info!("running in main loop");
rtc.get_time().await;
info!("Current RTC time: {}", rtc.get_time().await);
Timer::after(Duration::from_millis(1000)).await;
// let wait_result = sub.next_message().await;
// match wait_result {
// Lagged(_) => debug!("Lagged"),