Implemented fifo readout.

This commit is contained in:
2025-08-15 22:23:27 +02:00
parent 8d96817029
commit b3843b529e
3 changed files with 92 additions and 59 deletions

View File

@@ -75,10 +75,6 @@ async fn main(spawner: Spawner) {
let cs = Output::new(p.PC9, Level::High, Speed::Low);
let rst = Output::new(p.PB3, Level::High, Speed::Low);
// Set up interrupt at GPIO for AD5940
let ad5940_gpio_0 = ExtiInput::new(p.PC8, p.EXTI8, embassy_stm32::gpio::Pull::Up);
spawner.must_spawn(ad5940_readout_task(ad5940_gpio_0));
let spi = spi::Spi::new_blocking(
p.SPI1,
p.PA5, // SCK
@@ -158,7 +154,7 @@ async fn main(spawner: Spawner) {
// Toggle leds
ad5940.write_reg(ad5940::Register::SYNCEXTDEVICE, 0b010).await.unwrap();
ad5940.sequencer_wait(16 * 100_000).await; // 0.1 second
ad5940.sequencer_wait(16 * 25_000).await; // 0.025 second
ad5940.write_reg(ad5940::Register::SYNCEXTDEVICE, 0b111).await.unwrap();
ad5940.sequencer_enable(false).await;
@@ -178,6 +174,13 @@ async fn main(spawner: Spawner) {
// Green led task
// spawner.must_spawn(green_led(led));
// Trigger the sequencer
ad5940.sequencer_trigger(0).await;
// Set up interrupt at GPIO for AD5940
let ad5940_gpio_0 = ExtiInput::new(p.PC8, p.EXTI8, embassy_stm32::gpio::Pull::Up);
spawner.must_spawn(ad5940_readout_task(ad5940_gpio_0, ad5940));
loop {
// Read chip id
// let chip_id = ad5940.get_chipid().await;
@@ -188,58 +191,9 @@ async fn main(spawner: Spawner) {
// info!("Temperature: {}°C", temp);
// let result = electrodes.get_all();
// info!("Electrodes states: {:?}", result);
// info!("Electrodes states: {:?}", result);
// info!("high");
ad5940.sequencer_trigger(0).await;
// info!("Mainloop still running!");
Timer::after_micros((102_400.0 * 3.0) as u64).await;
let count = ad5940.get_fifo_count().await.unwrap();
info!("FIFOCNTSTA: {}", count);
// for _ in 0..count {
// let mut data = ad5940.read_reg(ad5940::Register::DATAFIFORD).await.unwrap();
// data &= 0xFFFF; // Mask to 16 bits
// use libm::powf;
// let value = 1.82 * (data as i32 - 0x8000) as f32 / powf(2f32, 15f32);
// // let value = (data as i32 - 0x8000) as f32;
// match IMPEDANCE_CHANNEL.try_send(value) {
// Ok(_) => {counter += 1;},
// Err(e) => {
// info!("Failed to send impedance data: {}", e);
// }
// }
// }
// info!("Counter: {}", counter);
if count >= 4 {
let mut data: [u32; 4] = [0; 4];
data[0] = ad5940.read_reg(ad5940::Register::DATAFIFORD).await.unwrap();
data[1] = ad5940.read_reg(ad5940::Register::DATAFIFORD).await.unwrap();
data[2] = ad5940.read_reg(ad5940::Register::DATAFIFORD).await.unwrap();
data[3] = ad5940.read_reg(ad5940::Register::DATAFIFORD).await.unwrap();
let result = calculate_impedance(data);
// Youll need to implement your own logging or send this over serial in embedded
info!("Impedance: Magnitude = {} Ω, Phase = {} rad", result.magnitude, result.phase);
let data = Impedance {
magnitude: result.magnitude,
phase: result.phase,
};
IMPEDANCE_CHANNEL.try_send(data).ok();
}
Timer::after_secs(1).await;
}
}
@@ -267,9 +221,35 @@ async fn green_led(mut led: Output<'static>) {
}
#[embassy_executor::task]
async fn ad5940_readout_task(mut pin: ExtiInput<'static>) {
async fn ad5940_readout_task(mut pin: ExtiInput<'static>, mut ad5940: AD5940) {
loop {
pin.wait_for_falling_edge().await;
// Wait untill sequence is done
pin.wait_for_rising_edge().await;
// Trigger the sequencer agina
ad5940.sequencer_trigger(0).await;
// Read the FIFO count
let count = ad5940.get_fifo_count().await.unwrap();
// info!("FIFOCNTSTA: {}", count);
if count >= 4 {
let mut data: [u32; 4] = [0; 4];
ad5940.read_fifo(data.as_mut_slice()).await.unwrap();
let result = calculate_impedance(data);
// Youll need to implement your own logging or send this over serial in embedded
info!("Impedance: Magnitude = {} Ω, Phase = {} rad", result.magnitude, result.phase);
let data = Impedance {
magnitude: result.magnitude,
phase: result.phase,
};
IMPEDANCE_CHANNEL.try_send(data).ok();
}
}
}