sd card detection works (own embassy task)

This commit is contained in:
Philipp_EndevourOS
2025-09-09 17:24:47 +02:00
parent adcbe87bd7
commit b551f4521f
3 changed files with 105 additions and 65 deletions

View File

@@ -7,9 +7,8 @@ use embassy_executor::Spawner;
use embassy_net::Stack;
use embassy_time::{Duration, Timer};
use esp_hal::gpio::{Input, InputConfig, Io};
use esp_hal::gpio::{Input, InputConfig};
use esp_hal::i2c::master::Config;
use esp_hal::interrupt::InterruptHandler;
use esp_hal::peripherals::{
GPIO0, GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2,
UART1,
@@ -17,7 +16,7 @@ use esp_hal::peripherals::{
use esp_hal::rmt::{ConstChannelAccess, Rmt};
use esp_hal::spi::master::{Config as Spi_config, Spi};
use esp_hal::{Blocking, handler};
use esp_hal::Blocking;
use esp_hal::time::Rate;
use esp_hal::timer::timg::TimerGroup;
use esp_hal::{
@@ -28,7 +27,6 @@ use esp_hal::{
timer::systimer::SystemTimer,
uart::Uart,
};
use esp_hal_embassy::InterruptExecutor;
use esp_hal_smartled::{SmartLedsAdapterAsync, buffer_size_async};
use esp_println::dbg;
use esp_println::logger::init_logger;
@@ -80,6 +78,7 @@ pub async fn hardware_init(
I2c<'static, Async>,
SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, LED_BUFFER_SIZE>,
GPIO21<'static>,
GPIO0<'static>,
) {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
@@ -105,17 +104,7 @@ pub async fn hardware_init(
let i2c_device = setup_i2c(peripherals.I2C0, peripherals.GPIO22, peripherals.GPIO23);
let mut io = Io::new(peripherals.IO_MUX);
io.set_interrupt_handler(handler);
let mut sd_det = Input::new(peripherals.GPIO0, InputConfig::default());
critical_section::with(|cs| {
// Here we are listening for a low level to demonstrate
// that you need to stop listening for level interrupts,
// but usually you'd probably use `FallingEdge`.
sd_det.listen(esp_hal::gpio::Event::AnyEdge);
SD_DET.borrow_ref_mut(cs).replace(sd_det);
});
let mut sd_det_gpio = peripherals.GPIO0;
let spi_bus = setup_spi(
peripherals.SPI2,
@@ -140,10 +129,16 @@ pub async fn hardware_init(
debug!("hardware init done");
(uart_device, stack, i2c_device, led, buzzer_gpio)
(
uart_device,
stack,
i2c_device,
led,
buzzer_gpio,
sd_det_gpio,
)
}
fn setup_uart(
uart1: UART1<'static>,
uart_tx: GPIO16<'static>,
@@ -218,34 +213,3 @@ fn setup_led(
led
}
#[handler]
fn handler() {
critical_section::with(|cs| {
let mut sd_det = SD_DET.borrow_ref_mut(cs);
let Some(sd_det) = sd_det.as_mut() else {
// Some other interrupt has occurred
// before the button was set up.
return;
};
if sd_det.is_interrupt_set() {
//card is insert on high
if sd_det.is_high() {
debug!("card insert");
//FEEDBACK_STATE.signal(crate::feedback::FeedbackState::Ack);
//sd_det.unlisten();
//sd_det.listen(esp_hal::gpio::Event::FallingEdge);
}
//card is not insert on low
else {
debug!("card removed");
//FEEDBACK_STATE.signal(crate::feedback::FeedbackState::Nack);
//sd_det.unlisten();
sd_det.listen(esp_hal::gpio::Event::RisingEdge);
}
}
});
}