Updated library.

This commit is contained in:
2025-08-18 14:49:58 +02:00
parent eaeff4f527
commit d17ef6c7ae
5 changed files with 346 additions and 116 deletions

View File

@@ -3,6 +3,9 @@ use defmt::{info, error};
use embassy_stm32::spi::Error;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::channel::Channel;
use embassy_sync::mutex::Mutex;
use static_cell::StaticCell;
use crate::ad5940::*;
use crate::ad5940_registers::*;
@@ -11,13 +14,17 @@ use bioz_icd_rs::ImpedanceOutput;
pub static IMPEDANCE_CHANNEL: Channel<ThreadModeRawMutex, ImpedanceOutput, 2000> = Channel::new();
pub type ImpedanceSetupType = Mutex<ThreadModeRawMutex, ImpedanceSetup>;
pub static IMPEDANCE_SETUP: StaticCell<ImpedanceSetupType> = StaticCell::new();
pub struct ImpedanceSetup {
ad5940: AD5940,
dsp_config: Option<DspConfig>,
}
impl ImpedanceSetup {
pub fn new(ad5940: AD5940) -> Self {
ImpedanceSetup { ad5940 }
ImpedanceSetup { ad5940, dsp_config: None }
}
pub async fn init(&mut self) -> Result<(), Error> {
@@ -48,10 +55,16 @@ impl ImpedanceSetup {
.hanning(true);
self.ad5940.apply_dsp_config(&dsp_config).await.unwrap();
self.dsp_config = Some(dsp_config);
let test = self.ad5940.sequencer_calculate_wait_time(&dsp_config).await.unwrap();
info!("Wait time: {:?}", test);
// Set SRAM configuration (cmd and data sram)
let sram_config = SramConfig::default()
.datafifosrcsel(DATAFIFOSRCSEL::DFT)
.datafifoen(DATAFIFOEN::Normal)
.data_size(DATA_MEM_SEL::Size2kB)
.cmd_mode(CMDMEMMDE::MemoryMode)
.cmd_size(CMD_MEM_SEL::Size2kB);
self.ad5940.apply_sram_config(sram_config).await.unwrap();
// WGCON: set sinus output
let config_wgcon = WGCON::TYPESEL_SIN.bits();
@@ -60,15 +73,24 @@ impl ImpedanceSetup {
Ok(())
}
pub async fn init_measurement(&mut self) {
pub async fn init_single_frequency_measurement(&mut self, frequency: u32) {
// Configure GPIOs
self.ad5940.write_reg(Register::GP0CON, 0b10 << 4 | 0b10 << 2 | 0b10).await.unwrap();
self.ad5940.write_reg(Register::SYNCEXTDEVICE, 0b111).await.unwrap();
// Calculate wait time between measurement start and stop
let mut wait_time = 0;
if let Some(dsp_config) = &self.dsp_config {
wait_time = self.ad5940.sequencer_calculate_wait_time(dsp_config).await.unwrap();
info!("Sinus periods per DFT: {}", wait_time as f32 / 16e6 * frequency as f32);
} else {
error!("DSP configuration not set, cannot calculate wait time");
}
// Configure sequencer
self.ad5940.sequencer_enable(true).await;
self.ad5940.wgfcw(50000).await;
self.ad5940.wgfcw(frequency).await;
let wg_amplitude = 557; // 2047 is the maximum amplitude for a 12-bit DAC --> 1.62V peak-to-peak
self.ad5940.write_reg(Register::WGAMPLITUDE, wg_amplitude).await.unwrap();
@@ -83,9 +105,8 @@ impl ImpedanceSetup {
self.ad5940.afecon(AFECON::WAVEGENEN | AFECON::ADCEN, true).await;
self.ad5940.sequencer_wait(16*10).await; // 10 us
self.ad5940.afecon(AFECON::ADCCONVEN | AFECON::DFTEN, true).await;
// self.ad5940.sequencer_wait(16 * 102_400).await; // 0.75 second // 0,0512
self.ad5940.sequencer_wait(16 * 12_800).await; // 0.75 second // 0,0512
self.ad5940.sequencer_wait(16*20).await; // 0.75 second // 0,0512
self.ad5940.sequencer_wait(wait_time).await; // Determined above
self.ad5940.sequencer_wait(16*20).await; // 10 us
self.ad5940.afecon(AFECON::WAVEGENEN | AFECON:: ADCEN | AFECON::ADCCONVEN | AFECON::DFTEN, false).await;
// Rz
@@ -99,9 +120,8 @@ impl ImpedanceSetup {
self.ad5940.afecon(AFECON::WAVEGENEN | AFECON::ADCEN, true).await;
self.ad5940.sequencer_wait(16*10).await; // 10 us
self.ad5940.afecon(AFECON::ADCCONVEN | AFECON::DFTEN, true).await;
// self.ad5940.sequencer_wait(16 * 102_400).await; // 0.75 second
self.ad5940.sequencer_wait(16 * 12_800).await; // 0.75 second // 0,0512
self.ad5940.sequencer_wait(16*20).await; // 0.75 second // 0,0512
self.ad5940.sequencer_wait(wait_time).await; // Determined above
self.ad5940.sequencer_wait(16*20).await; // 10 us
self.ad5940.afecon(AFECON::WAVEGENEN | AFECON:: ADCEN | AFECON::ADCCONVEN | AFECON::DFTEN, false).await;
// Toggle leds
@@ -111,13 +131,13 @@ impl ImpedanceSetup {
self.ad5940.sequencer_enable(false).await;
// // Configure the sequencer cmd data sram
self.ad5940.cmddatacon().await;
// Write sequence to SRAM
let start_address = 0;
self.ad5940.sequencer_cmd_write(start_address).await;
self.ad5940.sequencer_info_configure(0, self.ad5940.seq_len, start_address).await;
self.start_measurement().await;
}
pub async fn start_measurement(&mut self) {