Compare commits

..

2 Commits

Author SHA1 Message Date
Philipp_EndevourOS
b551f4521f sd card detection works (own embassy task) 2025-09-09 17:24:47 +02:00
Philipp_EndevourOS
adcbe87bd7 there are some problems to implemet the IO Mux interrupt handler 2025-09-09 16:50:14 +02:00
3 changed files with 114 additions and 33 deletions

View File

@@ -1,18 +1,18 @@
use embassy_time::{Delay, Duration, Timer}; use embassy_time::{Delay, Duration, Timer};
use esp_hal::{delay, gpio::Output, peripherals, rmt::ConstChannelAccess}; use esp_hal::{delay, gpio::Output, peripherals, rmt::ConstChannelAccess};
use esp_hal_smartled::SmartLedsAdapterAsync; use esp_hal_smartled::SmartLedsAdapterAsync;
use log::{debug, error, info};
use init::hardware; use init::hardware;
use log::{debug, error, info};
use smart_leds::SmartLedsWriteAsync;
use smart_leds::colors::{BLACK, GREEN, RED, YELLOW}; use smart_leds::colors::{BLACK, GREEN, RED, YELLOW};
use smart_leds::{brightness, colors::BLUE}; use smart_leds::{brightness, colors::BLUE};
use smart_leds::SmartLedsWriteAsync;
use crate::{FEEDBACK_STATE, init}; use crate::{FEEDBACK_STATE, init};
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum FeedbackState { pub enum FeedbackState {
Ack, Ack,
Nak, Nack,
Error, Error,
Startup, Startup,
WIFI, WIFI,
@@ -21,24 +21,40 @@ pub enum FeedbackState {
const LED_LEVEL: u8 = 255; const LED_LEVEL: u8 = 255;
//TODO ERROR STATE: 1 Blink = unknows error, 3 Blink = no sd card //TODO ERROR STATE: 1 Blink = unknows error, 3 Blink = no sd card
#[embassy_executor::task] #[embassy_executor::task]
pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, { init::hardware::LED_BUFFER_SIZE }>, buzzer: peripherals::GPIO21<'static>) { pub async fn feedback_task(
mut led: SmartLedsAdapterAsync<
ConstChannelAccess<esp_hal::rmt::Tx, 0>,
{ init::hardware::LED_BUFFER_SIZE },
>,
buzzer: peripherals::GPIO21<'static>,
) {
debug!("Starting feedback task"); debug!("Starting feedback task");
let mut buzzer = init::hardware::setup_buzzer(buzzer); let mut buzzer = init::hardware::setup_buzzer(buzzer);
loop { loop {
let feedback_state = FEEDBACK_STATE.wait().await; let feedback_state = FEEDBACK_STATE.wait().await;
match feedback_state { match feedback_state {
FeedbackState::Ack => { FeedbackState::Ack => {
led.write(brightness([GREEN; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap(); led.write(brightness(
[GREEN; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
buzzer.set_high(); buzzer.set_high();
Timer::after(Duration::from_millis(100)).await; Timer::after(Duration::from_millis(100)).await;
buzzer.set_low(); buzzer.set_low();
Timer::after(Duration::from_millis(50)).await; Timer::after(Duration::from_millis(50)).await;
} }
FeedbackState::Nak => { FeedbackState::Nack => {
led.write(brightness([YELLOW; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap(); led.write(brightness(
[YELLOW; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
buzzer.set_high(); buzzer.set_high();
Timer::after(Duration::from_millis(100)).await; Timer::after(Duration::from_millis(100)).await;
buzzer.set_low(); buzzer.set_low();
@@ -46,10 +62,20 @@ pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp
buzzer.set_high(); buzzer.set_high();
Timer::after(Duration::from_millis(100)).await; Timer::after(Duration::from_millis(100)).await;
buzzer.set_low(); buzzer.set_low();
led.write(brightness([BLACK; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap(); led.write(brightness(
[BLACK; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
} }
FeedbackState::Error => { FeedbackState::Error => {
led.write(brightness([RED; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap(); led.write(brightness(
[RED; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
buzzer.set_high(); buzzer.set_high();
Timer::after(Duration::from_millis(500)).await; Timer::after(Duration::from_millis(500)).await;
buzzer.set_low(); buzzer.set_low();
@@ -59,7 +85,12 @@ pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp
buzzer.set_low(); buzzer.set_low();
} }
FeedbackState::Startup => { FeedbackState::Startup => {
led.write(brightness([GREEN; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap(); led.write(brightness(
[GREEN; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
buzzer.set_high(); buzzer.set_high();
Timer::after(Duration::from_millis(10)).await; Timer::after(Duration::from_millis(10)).await;
buzzer.set_low(); buzzer.set_low();
@@ -71,20 +102,34 @@ pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp
buzzer.set_high(); buzzer.set_high();
Timer::after(Duration::from_millis(100)).await; Timer::after(Duration::from_millis(100)).await;
buzzer.set_low(); buzzer.set_low();
led.write(brightness([BLACK; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap(); led.write(brightness(
[BLACK; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
} }
FeedbackState::WIFI => { FeedbackState::WIFI => {
led.write(brightness([BLUE; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap(); led.write(brightness(
[BLUE; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
} }
FeedbackState::Idle => { FeedbackState::Idle => {
// Do nothing led.write(brightness(
[GREEN; init::hardware::NUM_LEDS].into_iter(),
LED_LEVEL,
))
.await
.unwrap();
} }
}; };
debug!("Feedback state: {:?}", feedback_state); debug!("Feedback state: {:?}", feedback_state);
} }
} }
// async fn beep_ack() { // async fn beep_ack() {
// buzzer.set_high(); // buzzer.set_high();
// buzzer.set_low(); // buzzer.set_low();

View File

@@ -1,8 +1,13 @@
use core::cell::RefCell;
use bleps::att::Att; use bleps::att::Att;
use critical_section::Mutex;
use ds3231::InterruptControl;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_net::Stack; use embassy_net::Stack;
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use esp_hal::gpio::{Input, InputConfig};
use esp_hal::i2c::master::Config; use esp_hal::i2c::master::Config;
use esp_hal::peripherals::{ use esp_hal::peripherals::{
GPIO0, GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2, GPIO0, GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2,
@@ -27,6 +32,7 @@ use esp_println::dbg;
use esp_println::logger::init_logger; use esp_println::logger::init_logger;
use log::{debug, error, info}; use log::{debug, error, info};
use crate::FEEDBACK_STATE;
use crate::init::network; use crate::init::network;
use crate::init::sd_card::setup_sdcard; use crate::init::sd_card::setup_sdcard;
use crate::init::wifi; use crate::init::wifi;
@@ -53,6 +59,8 @@ use crate::store::persistence::Persistence;
pub const NUM_LEDS: usize = 66; pub const NUM_LEDS: usize = 66;
pub const LED_BUFFER_SIZE: usize = NUM_LEDS * 25; pub const LED_BUFFER_SIZE: usize = NUM_LEDS * 25;
static SD_DET: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
#[panic_handler] #[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! { fn panic(info: &core::panic::PanicInfo) -> ! {
loop { loop {
@@ -70,6 +78,7 @@ pub async fn hardware_init(
I2c<'static, Async>, I2c<'static, Async>,
SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, LED_BUFFER_SIZE>, SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, LED_BUFFER_SIZE>,
GPIO21<'static>, GPIO21<'static>,
GPIO0<'static>,
) { ) {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config); let peripherals = esp_hal::init(config);
@@ -90,12 +99,13 @@ pub async fn hardware_init(
let stack = network::setup_network(network_seed, interfaces.ap, spawner); let stack = network::setup_network(network_seed, interfaces.ap, spawner);
Timer::after(Duration::from_millis(1)).await; Timer::after(Duration::from_millis(1)).await;
init_lvl_shifter(peripherals.GPIO0);
let uart_device = setup_uart(peripherals.UART1, peripherals.GPIO16, peripherals.GPIO17); let uart_device = setup_uart(peripherals.UART1, peripherals.GPIO16, peripherals.GPIO17);
let i2c_device = setup_i2c(peripherals.I2C0, peripherals.GPIO22, peripherals.GPIO23); let i2c_device = setup_i2c(peripherals.I2C0, peripherals.GPIO22, peripherals.GPIO23);
let mut sd_det_gpio = peripherals.GPIO0;
let spi_bus = setup_spi( let spi_bus = setup_spi(
peripherals.SPI2, peripherals.SPI2,
peripherals.GPIO19, peripherals.GPIO19,
@@ -119,19 +129,14 @@ pub async fn hardware_init(
debug!("hardware init done"); debug!("hardware init done");
(uart_device, stack, i2c_device, led, buzzer_gpio) (
} uart_device,
stack,
// 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) i2c_device,
fn init_lvl_shifter(oe_pin: GPIO0<'static>) { led,
let mut oe_lvl_shifter = Output::new( buzzer_gpio,
oe_pin, sd_det_gpio,
esp_hal::gpio::Level::Low, )
OutputConfig::default()
.with_drive_mode(esp_hal::gpio::DriveMode::PushPull)
.with_drive_strength(esp_hal::gpio::DriveStrength::_10mA),
);
oe_lvl_shifter.set_high();
} }
fn setup_uart( fn setup_uart(

View File

@@ -6,16 +6,21 @@
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_net::Stack; use embassy_net::Stack;
use embassy_sync::{ use embassy_sync::{
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}, channel::Channel, pubsub::{ blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex},
channel::Channel,
pubsub::{
PubSubChannel, Publisher, PubSubChannel, Publisher,
WaitResult::{Lagged, Message}, WaitResult::{Lagged, Message},
}, signal::Signal },
signal::Signal,
}; };
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use esp_hal::gpio::Input;
use esp_hal::{gpio::InputConfig, peripherals};
use log::{debug, info}; use log::{debug, info};
use static_cell::make_static; use static_cell::make_static;
use crate::{store::TallyID}; use crate::store::TallyID;
extern crate alloc; extern crate alloc;
@@ -32,7 +37,7 @@ type TallyPublisher = Publisher<'static, NoopRawMutex, TallyID, 8, 2, 1>;
#[esp_hal_embassy::main] #[esp_hal_embassy::main]
async fn main(mut spawner: Spawner) { async fn main(mut spawner: Spawner) {
let (uart_device, stack, _i2c, _led, buzzer_gpio) = let (uart_device, stack, _i2c, _led, buzzer_gpio, sd_det_gpio) =
init::hardware::hardware_init(&mut spawner).await; init::hardware::hardware_init(&mut spawner).await;
wait_for_stack_up(stack).await; wait_for_stack_up(stack).await;
@@ -56,6 +61,9 @@ async fn main(mut spawner: Spawner) {
debug!("spawing feedback task.."); debug!("spawing feedback task..");
spawner.must_spawn(feedback::feedback_task(_led, buzzer_gpio)); spawner.must_spawn(feedback::feedback_task(_led, buzzer_gpio));
debug!("spawn sd detect task");
spawner.must_spawn(sd_detect_task(sd_det_gpio));
/******************************************************************************/ /******************************************************************************/
let mut sub = chan.subscriber().unwrap(); let mut sub = chan.subscriber().unwrap();
@@ -76,6 +84,29 @@ async fn main(mut spawner: Spawner) {
} }
} }
#[embassy_executor::task]
async fn sd_detect_task(sd_det_gpio: peripherals::GPIO0<'static>) {
let mut sd_det = Input::new(sd_det_gpio, InputConfig::default());
sd_det.wait_for(esp_hal::gpio::Event::AnyEdge);
loop {
sd_det.wait_for_any_edge().await;
{
if sd_det.is_high() {
FEEDBACK_STATE.signal(feedback::FeedbackState::Ack);
debug!("card insert");
}
//card is not insert on low
else {
FEEDBACK_STATE.signal(feedback::FeedbackState::Nack);
debug!("card removed");
}
}
//debounce time
Timer::after(Duration::from_millis(100)).await;
}
}
async fn wait_for_stack_up(stack: Stack<'static>) { async fn wait_for_stack_up(stack: Stack<'static>) {
loop { loop {
if stack.is_link_up() { if stack.is_link_up() {