mirror of
https://github.com/hubaldv/bioz-firmware-rs.git
synced 2025-12-06 05:01:18 +00:00
Created new impedance file for impedance_setup.
This commit is contained in:
95
src/main.rs
95
src/main.rs
@@ -1,7 +1,7 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::{info, error};
|
||||
use defmt::info;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_time::{Timer, Duration};
|
||||
@@ -12,10 +12,10 @@ use embassy_stm32::time::Hertz;
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
use crate::ad5940::*;
|
||||
use crate::ad5940_registers::*;
|
||||
// use crate::ad5940::*;
|
||||
// use crate::ad5940_registers::*;
|
||||
|
||||
use bioz_icd_rs::Impedance;
|
||||
use bioz_icd_rs::ImpedanceOutput;
|
||||
|
||||
mod ad5940;
|
||||
use ad5940::AD5940;
|
||||
@@ -34,9 +34,10 @@ use embassy_stm32::{bind_interrupts, peripherals, usb};
|
||||
mod communication;
|
||||
use communication::{init_communication, LED_FREQUENCY_SIGNAL};
|
||||
|
||||
mod impedance_test;
|
||||
use impedance::IMPEDANCE_CHANNEL;
|
||||
|
||||
use impedance_test::IMPEDANCE_CHANNEL;
|
||||
mod impedance;
|
||||
use impedance::ImpedanceSetup;
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
USB_DRD_FS => usb::InterruptHandler<peripherals::USB>;
|
||||
@@ -90,7 +91,9 @@ async fn main(spawner: Spawner) {
|
||||
|
||||
// ad5940.init_temperature().await.unwrap();
|
||||
// ad5940.init_waveform().await.unwrap();
|
||||
ad5940.init_impedance().await.unwrap();
|
||||
let mut impedance_setup = ImpedanceSetup::new(ad5940);
|
||||
impedance_setup.init().await.unwrap();
|
||||
impedance_setup.init_measurement().await;
|
||||
|
||||
// // Set up I2C for ADG2128
|
||||
// let i2c = i2c::I2c::new_blocking(
|
||||
@@ -112,63 +115,6 @@ async fn main(spawner: Spawner) {
|
||||
// ad5940.write_reg_raw(0x0000_0004, 1 << 1).await.unwrap();
|
||||
// ad5940.write_reg_raw(0x0000_001C, 1 << 1).await.unwrap();
|
||||
|
||||
ad5940.write_reg(ad5940::Register::GP0CON, 0b10 << 4 | 0b10 << 2 | 0b10).await.unwrap();
|
||||
ad5940.write_reg(ad5940::Register::SYNCEXTDEVICE, 0b111).await.unwrap();
|
||||
|
||||
// Sequencer test
|
||||
ad5940.sequencer_enable(true).await;
|
||||
|
||||
ad5940.wgfcw(50000).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();
|
||||
|
||||
// Rcal
|
||||
let switch_config = SwitchConfig::default()
|
||||
.t9con(T9CON::T9Closed)
|
||||
.tmuxcon(TMUXCON::TR1Closed)
|
||||
.nmuxcon(NMUXCON::NR1Closed)
|
||||
.pmuxcon(PMUXCON::PR0Closed)
|
||||
.dmuxcon(DMUXCON::DR0Closed);
|
||||
ad5940.apply_switch_config(switch_config).await.unwrap();
|
||||
ad5940.afecon(AFECON::WAVEGENEN | AFECON::ADCEN, true).await;
|
||||
ad5940.sequencer_wait(16*10).await; // 10 us
|
||||
ad5940.afecon(AFECON::ADCCONVEN | AFECON::DFTEN, true).await;
|
||||
// ad5940.sequencer_wait(16 * 102_400).await; // 0.75 second // 0,0512
|
||||
ad5940.sequencer_wait(16 * 12_800).await; // 0.75 second // 0,0512
|
||||
ad5940.sequencer_wait(16*20).await; // 0.75 second // 0,0512
|
||||
ad5940.afecon(AFECON::WAVEGENEN | AFECON:: ADCEN | AFECON::ADCCONVEN | AFECON::DFTEN, false).await;
|
||||
|
||||
// Rz
|
||||
let switch_config = SwitchConfig::default()
|
||||
.t9con(T9CON::T9Closed)
|
||||
.tmuxcon(TMUXCON::T2Closed)
|
||||
.nmuxcon(NMUXCON::N2Closed)
|
||||
.pmuxcon(PMUXCON::P11Closed)
|
||||
.dmuxcon(DMUXCON::D5Closed);
|
||||
ad5940.apply_switch_config(switch_config).await.unwrap();
|
||||
ad5940.afecon(AFECON::WAVEGENEN | AFECON::ADCEN, true).await;
|
||||
ad5940.sequencer_wait(16*10).await; // 10 us
|
||||
ad5940.afecon(AFECON::ADCCONVEN | AFECON::DFTEN, true).await;
|
||||
// ad5940.sequencer_wait(16 * 102_400).await; // 0.75 second
|
||||
ad5940.sequencer_wait(16 * 12_800).await; // 0.75 second // 0,0512
|
||||
ad5940.sequencer_wait(16*20).await; // 0.75 second // 0,0512
|
||||
ad5940.afecon(AFECON::WAVEGENEN | AFECON:: ADCEN | AFECON::ADCCONVEN | AFECON::DFTEN, false).await;
|
||||
|
||||
// Toggle leds
|
||||
ad5940.write_reg(ad5940::Register::SYNCEXTDEVICE, 0b010).await.unwrap();
|
||||
ad5940.sequencer_wait(16 * 1_000).await; // 0.025 second
|
||||
ad5940.write_reg(ad5940::Register::SYNCEXTDEVICE, 0b111).await.unwrap();
|
||||
|
||||
ad5940.sequencer_enable(false).await;
|
||||
|
||||
// // Configure the sequencer cmd data sram
|
||||
ad5940.cmddatacon().await;
|
||||
|
||||
let start_address = 0;
|
||||
ad5940.sequencer_cmd_write(start_address).await;
|
||||
|
||||
ad5940.sequencer_info_configure(0, ad5940.seq_len, start_address).await;
|
||||
|
||||
// Create USB driver and start postcard-rpc server
|
||||
let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11);
|
||||
init_communication(driver, spawner);
|
||||
@@ -177,11 +123,11 @@ async fn main(spawner: Spawner) {
|
||||
// spawner.must_spawn(green_led(led));
|
||||
|
||||
// Trigger the sequencer
|
||||
ad5940.sequencer_trigger(0).await;
|
||||
impedance_setup.start_measurement().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));
|
||||
spawner.must_spawn(impedance_setup_readout_task(ad5940_gpio_0, impedance_setup));
|
||||
|
||||
loop {
|
||||
// Read chip id
|
||||
@@ -223,29 +169,30 @@ async fn green_led(mut led: Output<'static>) {
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn ad5940_readout_task(mut pin: ExtiInput<'static>, mut ad5940: AD5940) {
|
||||
async fn impedance_setup_readout_task(mut pin: ExtiInput<'static>, mut impedance_setup: ImpedanceSetup) {
|
||||
loop {
|
||||
// Wait untill sequence is done
|
||||
pin.wait_for_rising_edge().await;
|
||||
|
||||
// Trigger the sequencer agina
|
||||
ad5940.sequencer_trigger(0).await;
|
||||
// Trigger the sequencer again
|
||||
impedance_setup.start_measurement().await;
|
||||
|
||||
// Read the FIFO count
|
||||
let count = ad5940.get_fifo_count().await.unwrap();
|
||||
let count = impedance_setup.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();
|
||||
impedance_setup.read_fifo(data.as_mut_slice()).await.unwrap();
|
||||
|
||||
let result = calculate_impedance(data);
|
||||
|
||||
// You’ll need to implement your own logging or send this over serial in embedded
|
||||
info!("Impedance: Magnitude = {} Ω, Phase = {} rad", result.magnitude, result.phase);
|
||||
|
||||
// Log
|
||||
// info!("Impedance: Magnitude = {} Ω, Phase = {} rad", result.magnitude, result.phase);
|
||||
|
||||
let data = Impedance {
|
||||
let data = ImpedanceOutput {
|
||||
magnitude: result.magnitude,
|
||||
phase: result.phase,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user