buzzer without pwm, and feedback with embassy Sync. Error when more then 4 tasks spawn

This commit is contained in:
Philipp_EndevourOS
2025-08-01 16:42:14 +02:00
parent fe6540ca3d
commit 5950279dc4
7 changed files with 214 additions and 123 deletions

View File

@@ -1,20 +0,0 @@
use embassy_time::{Duration, Timer};
use esp_hal::peripherals;
use log::{debug, error, info};
use crate::init;
#[embassy_executor::task]
pub async fn feedback_task(buzzer: peripherals::GPIO19<'static>) {
info!("Starting feedback task");
let mut buzzer = init::hardware::setup_buzzer(buzzer).await;
loop {
debug!("Buzzer feedback task running");
buzzer.set_high();
Timer::after(Duration::from_millis(100)).await;
buzzer.set_low();
Timer::after(Duration::from_millis(100)).await;
return ;
}
}

View File

@@ -1,10 +1,9 @@
use embassy_time::{Duration, Timer};
use esp_hal::{uart::Uart, Async};
use log::{ debug, info };
use esp_hal::{Async, uart::Uart};
use log::{debug, info};
use crate::TallyPublisher;
#[embassy_executor::task]
pub async fn rfid_reader_task(mut uart_device: Uart<'static, Async>, chan: TallyPublisher) {
let mut uart_buffer = [0u8; 64];
@@ -26,4 +25,4 @@ pub async fn rfid_reader_task(mut uart_device: Uart<'static, Async>, chan: Tally
}
Timer::after(Duration::from_millis(200)).await;
}
}
}

View File

@@ -1,79 +1,110 @@
use ds3231::{Alarm1Config, DS3231Error, Seconds, DS3231};
use embassy_time::{Timer, Duration};
use esp_hal::{i2c::{self, master::I2c}, peripherals, Async};
use log::{debug, error, info};
use crate::{drivers, init, UTC_TIME};
const RTC_ADDRESS: u8 = 0x57;
use embassy_time::{Duration, Timer};
use log::info;
#[embassy_executor::task]
async fn rtc_task(
i2c: i2c::master::I2c<'static, Async>,
sqw_pin: peripherals::GPIO21<'static>,
) {
debug!("init rtc interrupt");
let mut rtc_interrupt = init::hardware::setup_rtc_iterrupt(sqw_pin).await;
debug!("configuring rtc");
let mut rtc = drivers::rtc::rtc_config(i2c).await;
let mut utc_time = UTC_TIME.lock().await;
let timestamp_result = drivers::rtc::read_rtc_time(&mut rtc).await;
*utc_time = timestamp_result.unwrap_or(0);
pub async fn rtc_task() {
info!("RTC task started");
// Initialize I2C and RTC here
loop {
debug!("Waiting for RTC interrupt...");
rtc_interrupt.wait_for_falling_edge().await;
debug!("RTC interrupt triggered");
utc_time = UTC_TIME.lock().await;
let timestamp_result = drivers::rtc::read_rtc_time(&mut rtc).await;
*utc_time = timestamp_result.unwrap_or(0);
Timer::after(Duration::from_secs(1)).await; // Debounce delay
// 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");
}
}
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: 0, // set alarm every day 00:00:00 to sync time
minutes: 0,
seconds: 10,
is_pm: None, // 24-hour mode
};
// Replace 'main::UTC_TIME' with the correct path to UTC_TIME, for example 'crate::UTC_TIME'
let mut utc_time;
{
utc_time = crate::UTC_TIME.lock().await;
}
let naive_dt = chrono::NaiveDateTime::from_timestamp_opt(*utc_time as i64, 0)
.expect("Invalid timestamp for NaiveDateTime");
rtc.set_datetime(&naive_dt).await.unwrap_or_else(|e| {
error!("Failed to set RTC datetime: {:?}", e);
panic!();
});
if let Err(e) = rtc.set_alarm1(&daily_alarm).await {
error!("Failed to configure RTC: {:?}", e);
panic!();
}
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)
}
}
}
/* ************************************************************************************** */
// use ds3231::{Alarm1Config, DS3231, DS3231Error, Seconds};
// use embassy_time::{Duration, Timer};
// use esp_hal::{
// Async,
// i2c::{self, master::I2c},
// peripherals,
// };
// use log::{debug, error, info};
// use crate::{UTC_TIME, drivers, init};
// const RTC_ADDRESS: u8 = 0x57;
// #[embassy_executor::task]
// pub async fn rtc_task(
// //i2c: i2c::master::I2c<'static, Async>,
// //sqw_pin: peripherals::GPIO21<'static>,
// ) {
// //UTC_TIME.signal(155510);
// // debug!("init rtc interrupt");
// // let mut rtc_interrupt = init::hardware::setup_rtc_iterrupt(sqw_pin).await;
// // debug!("configuring rtc");
// // let mut rtc = drivers::rtc::rtc_config(i2c).await;
// // let timestamp_result = drivers::rtc::read_rtc_time(&mut rtc).await;
// // UTC_TIME.signal(timestamp_result.unwrap());
// debug!("rtc up");
// loop {
// info!("Current UTC time: {}", UTC_TIME.wait().await);
// // debug!("Waiting for RTC interrupt...");
// // rtc_interrupt.wait_for_falling_edge().await;
// // debug!("RTC interrupt triggered");
// // let timestamp_result = drivers::rtc::read_rtc_time(&mut rtc).await;
// // UTC_TIME.signal(timestamp_result.unwrap());
// // Timer::after(Duration::from_secs(1)).await; // Debounce delay
// }
// }
// 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: 0, // set alarm every day 00:00:00 to sync time
// minutes: 0,
// seconds: 10,
// is_pm: None, // 24-hour mode
// };
// let naive_dt = chrono::NaiveDateTime::from_timestamp_opt(*utc_time as i64, 0)
// .expect("Invalid timestamp for NaiveDateTime");
// rtc.set_datetime(&naive_dt).await.unwrap_or_else(|e| {
// error!("Failed to set RTC datetime: {:?}", e);
// panic!();
// });
// if let Err(e) = rtc.set_alarm1(&daily_alarm).await {
// error!("Failed to configure RTC: {:?}", e);
// panic!();
// }
// 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)
// }
// }
// }
// 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>> {
@@ -86,4 +117,4 @@ pub async fn read_rtc_time<'a>(rtc: &'a mut DS3231<I2c<'static, Async>>) -> Resu
}
}
}
*/
*/