mirror of
https://github.com/hubaldv/bioz-firmware-rs.git
synced 2025-12-06 05:01:18 +00:00
Started writing nice library.
This commit is contained in:
74
src/main.rs
74
src/main.rs
@@ -10,10 +10,13 @@ use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embassy_stm32::{i2c, spi, Config};
|
||||
use embassy_stm32::time::Hertz;
|
||||
|
||||
use crate::ad5940_registers::{ADCCON, AFECON, SWCON};
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
use crate::ad5940::*;
|
||||
use crate::ad5940_registers::*;
|
||||
|
||||
use bioz_icd_rs::Impedance;
|
||||
|
||||
mod ad5940;
|
||||
use ad5940::AD5940;
|
||||
|
||||
@@ -119,24 +122,38 @@ async fn main(spawner: Spawner) {
|
||||
// Sequencer test
|
||||
ad5940.sequencer_enable(true).await;
|
||||
|
||||
ad5940.wgfcw(100).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
|
||||
ad5940.swcon(SWCON::DMUXCON_DR0 | SWCON::PMUXCON_PR0 | SWCON::NMUXCON_NR1 | SWCON::TMUXCON_TR1 | SWCON::T9CON).await; // RCAL0 -->
|
||||
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 * 750_000).await; // 0.75 second
|
||||
ad5940.sequencer_wait(16 * 102_400).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
|
||||
ad5940.swcon(SWCON::DMUXCON_D5 | SWCON::PMUXCON_P11 | SWCON::NMUXCON_N2 | SWCON::TMUXCON_T2 | SWCON::T9CON).await;
|
||||
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 * 750_000).await; // 0.75 second
|
||||
ad5940.sequencer_wait(16 * 102_400).await; // 0.75 second
|
||||
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
|
||||
@@ -161,23 +178,6 @@ 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;
|
||||
@@ -196,11 +196,12 @@ async fn main(spawner: Spawner) {
|
||||
|
||||
// info!("Mainloop still running!");
|
||||
|
||||
Timer::after_millis(2500).await;
|
||||
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();
|
||||
|
||||
@@ -220,18 +221,25 @@ async fn main(spawner: Spawner) {
|
||||
|
||||
// info!("Counter: {}", counter);
|
||||
|
||||
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();
|
||||
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);
|
||||
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);
|
||||
// You’ll 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user