Basic working impedance.

This commit is contained in:
2025-08-14 17:31:46 +02:00
parent 03a04bf789
commit bf3f00cfb0
4 changed files with 70 additions and 16 deletions

View File

@@ -1,7 +1,7 @@
#![no_std]
#![no_main]
use defmt::info;
use defmt::{info, error};
use embassy_executor::Spawner;
use embassy_stm32::exti::ExtiInput;
use embassy_time::{Timer, Duration};
@@ -10,7 +10,7 @@ use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::{i2c, spi, Config};
use embassy_stm32::time::Hertz;
use crate::ad5940_registers::{AFECON, SWCON};
use crate::ad5940_registers::{ADCCON, AFECON, SWCON};
use {defmt_rtt as _, panic_probe as _};
@@ -33,6 +33,8 @@ use communication::{init_communication, LED_FREQUENCY_SIGNAL};
mod impedance_test;
use impedance_test::IMPEDANCE_CHANNEL;
bind_interrupts!(struct Irqs {
USB_DRD_FS => usb::InterruptHandler<peripherals::USB>;
});
@@ -117,7 +119,7 @@ async fn main(spawner: Spawner) {
// Sequencer test
ad5940.sequencer_enable(true).await;
ad5940.wgfcw(50000).await;
ad5940.wgfcw(100).await;
let wg_amplitude = 2047; // 2047 is the maximum amplitude for a 12-bit DAC --> 1.62V peak-to-peak
ad5940.write_reg(ad5940::Register::WGAMPLITUDE, wg_amplitude).await.unwrap();
@@ -128,7 +130,6 @@ async fn main(spawner: Spawner) {
ad5940.afecon(AFECON::ADCCONVEN | AFECON::DFTEN, true).await;
ad5940.sequencer_wait(16 * 750_000).await; // 0.75 second
ad5940.afecon(AFECON::WAVEGENEN | AFECON:: ADCEN | AFECON::ADCCONVEN | AFECON::DFTEN, false).await;
ad5940.write_reg(ad5940::Register::AFECON, 0x0000_FFFF).await.unwrap();
// Rz
ad5940.swcon(SWCON::DMUXCON_D5 | SWCON::PMUXCON_P11 | SWCON::NMUXCON_N2 | SWCON::TMUXCON_T2 | SWCON::T9CON).await;
@@ -160,6 +161,23 @@ async fn main(spawner: Spawner) {
// Green led task
// spawner.must_spawn(green_led(led));
// ad5940.sequencer_trigger(0).await;
// Set inputs
// ad5940.write_reg(ad5940::Register::ADCCON, 0b00101 << 8 | 0b011001).await.unwrap();
// ad5940.write_reg(ad5940::Register::ADCCON, ADCCON::MUXSELN_TIAN.bits() | ADCCON::MUXSELP_TIAP.bits()).await.unwrap();
// ad5940.wgfcw(10).await;
// let wg_amplitude = 2047; // 2047 is the maximum amplitude for a 12-bit DAC --> 1.62V peak-to-peak
// ad5940.write_reg(ad5940::Register::WGAMPLITUDE, wg_amplitude).await.unwrap();
// ad5940.swcon(SWCON::DMUXCON_D5 | SWCON::PMUXCON_P11 | SWCON::NMUXCON_N2 | SWCON::TMUXCON_T2 | SWCON::T9CON).await;
// ad5940.afecon(AFECON::WAVEGENEN | AFECON::ADCEN | AFECON::ADCCONVEN, true).await;
ad5940.write_reg(ad5940::Register::ADCFILTERCON, 0x00000301 | 1 << 4).await.unwrap();
ad5940.write_reg(ad5940::Register::HSRTIACON, 0b010000 << 5 | 0b0010).await.unwrap();
let mut counter = 0;
loop {
// Read chip id
// let chip_id = ad5940.get_chipid().await;
@@ -178,10 +196,29 @@ async fn main(spawner: Spawner) {
// info!("Mainloop still running!");
Timer::after_millis(1750).await;
Timer::after_millis(2500).await;
// let test = ad5940.read_reg_raw(0x2200).await.unwrap();
// info!("FIFOCNTSTA: {}", (test>>16) & 0b111_1111_1111);
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);
let mut data: [u32; 4] = [0; 4];
data[0] = ad5940.read_reg(ad5940::Register::DATAFIFORD).await.unwrap();
@@ -194,8 +231,7 @@ async fn main(spawner: Spawner) {
// Youll need to implement your own logging or send this over serial in embedded
info!("Impedance: Magnitude = {} Ω, Phase = {} rad", result.magnitude, result.phase);
// let test = ad5940.read_reg_raw(0x2200).await.unwrap();
// info!("FIFOCNTSTA: {}", (test>>16) & 0b111_1111_1111);
}
}