Working sequencer.

This commit is contained in:
2025-08-12 20:53:24 +02:00
parent 6b15a77cb5
commit 04b6083460
2 changed files with 93 additions and 84 deletions

View File

@@ -45,8 +45,7 @@ impl AD5940 {
self.cs.set_high(); self.cs.set_high();
// Wait after cs is high // Wait after cs is high
// Timer::after_nanos(80).await; Timer::after_nanos(80).await;
Timer::after_millis(10).await;
// Read command // Read command
self.cs.set_low(); self.cs.set_low();
@@ -66,7 +65,6 @@ impl AD5940 {
// let mut data = [0u8; 5]; // First byte dummy, then four data bytes // let mut data = [0u8; 5]; // First byte dummy, then four data bytes
// self.spi.blocking_read(&mut data)?; // self.spi.blocking_read(&mut data)?;
self.cs.set_high(); self.cs.set_high();
Timer::after_millis(1).await;
Ok(result) Ok(result)
} }
@@ -87,8 +85,7 @@ impl AD5940 {
self.cs.set_high(); self.cs.set_high();
// Wait after cs is high // Wait after cs is high
// Timer::after_nanos(80).await; Timer::after_nanos(80).await;
Timer::after_millis(10).await;
// Write value command // Write value command
self.cs.set_low(); self.cs.set_low();
@@ -99,7 +96,6 @@ impl AD5940 {
self.spi.blocking_write(&[value as u16])?; self.spi.blocking_write(&[value as u16])?;
} }
self.cs.set_high(); self.cs.set_high();
Timer::after_millis(1).await;
Ok(()) Ok(())
@@ -137,7 +133,7 @@ impl AD5940 {
if address as u32 > 0x21FF { if address as u32 > 0x21FF {
error!("Sequencer write address out of range: 0x{:04X}", address as u32); error!("Sequencer write address out of range: 0x{:04X}", address as u32);
} }
let cmd = (((address as u32) >> 2) & 0x7F) << 24 | (value & 0xFF_FFFF); let cmd = 0b1 << 31 | (((address as u32) >> 2) & 0x7F) << 24 | (value & 0xFF_FFFF);
self.sequencer_insert(cmd).await; self.sequencer_insert(cmd).await;
return Ok(()) return Ok(())
@@ -148,7 +144,7 @@ impl AD5940 {
error!("Sequencer wait cycles out of range: {}", cycles); error!("Sequencer wait cycles out of range: {}", cycles);
return; return;
} }
let cmd = 0b01 << 30 | 0x0000_0000 | cycles & 0x3FFF_FFFF; let cmd = 0b00 << 30 | 0x0000_0000 | cycles & 0x3FFF_FFFF;
self.sequencer_insert(cmd).await; self.sequencer_insert(cmd).await;
} }
@@ -174,12 +170,8 @@ impl AD5940 {
start_address &= 0x7FF; // Ensure start address is within bounds start_address &= 0x7FF; // Ensure start address is within bounds
while counter < self.seq_len { while counter < self.seq_len {
self.write_reg(Register::CMDFIFOWADDR, start_address + (counter) as u32).await.unwrap(); self.write_reg(Register::CMDFIFOWADDR, start_address + counter as u32).await.unwrap();
Timer::after_millis(1).await;
self.write_reg(Register::CMDFIFOWRITE, self.seq_buffer[counter as usize]).await.unwrap(); self.write_reg(Register::CMDFIFOWRITE, self.seq_buffer[counter as usize]).await.unwrap();
// self.write_reg(Register::CMDFIFOWRITE, 0).await.unwrap();
Timer::after_millis(1).await;
// info!("{:032b}", self.seq_buffer[counter as usize]);
counter += 1; counter += 1;
} }
} }
@@ -198,40 +190,23 @@ impl AD5940 {
} }
} }
pub async fn sequencer_trigger(&mut self) { pub async fn sequencer_trigger(&mut self, seq: u8) {
// Trigger the sequencer // Trigger the sequencer
self.write_reg(Register::TRIGSEQ, 1 << 0).await.unwrap(); match seq {
// self.write_reg(Register::TRIGSEQ, 1 << 1).await.unwrap(); 0 => self.write_reg(Register::TRIGSEQ, 1 << 0).await.unwrap(),
// self.write_reg(Register::TRIGSEQ, 1 << 2).await.unwrap(); 1 => self.write_reg(Register::TRIGSEQ, 1 << 1).await.unwrap(),
// self.write_reg(Register::TRIGSEQ, 1 << 3).await.unwrap(); 2 => self.write_reg(Register::TRIGSEQ, 1 << 2).await.unwrap(),
3 => self.write_reg(Register::TRIGSEQ, 1 << 3).await.unwrap(),
_ => {
error!("Sequencers from 0 till 3 are allows, now: seq={}", seq);
},
}
// wait // for _ in 0..100 {
// Timer::after_millis(10).await; // let test = self.read_reg_raw(0x206C).await.unwrap();
// info!("DATAFIFORD: 0x{:08X}", test);
let test = self.read_reg(Register::SEQCNT).await.unwrap();
info!("Sequencer triggered, SEQCNT: {}", test);
let test = self.read_reg(Register::INTCFLAG0).await.unwrap();
info!("INTCFLAG0: 0x{:08X}", test);
let test = self.read_reg(Register::INTCFLAG1).await.unwrap();
info!("INTCFLAG1: 0x{:08X}", test);
self.write_reg_raw(0x3008, 0xFFFF_FFFF).await.unwrap();
self.write_reg_raw(0x300C, 0xFFFF_FFFF).await.unwrap();
let test = self.read_reg(Register::SEQTIMEOUT).await.unwrap();
info!("SEQTIMEOUT: {}", test);
let test = self.read_reg(Register::CMDFIFOWADDR).await.unwrap();
info!("CMDFIFOWADDR: {}", test);
// for i in 0..200 {
let test= self.read_reg_raw(0x206C).await.unwrap();
info!("DATAFIFORD: 0x{:08X}", test);
// } // }
let test = self.read_reg_raw(0x2200).await.unwrap(); let test = self.read_reg_raw(0x2200).await.unwrap();
info!("FIFOCNTSTA: {}", (test>>16) & 0b111_1111_1111); info!("FIFOCNTSTA: {}", (test>>16) & 0b111_1111_1111);
} }
@@ -314,12 +289,12 @@ impl AD5940 {
self.write_reg(Register::SEQCON, reg).await.unwrap(); self.write_reg(Register::SEQCON, reg).await.unwrap();
// Reset SEQCON // Reset SEQCON
self.write_reg(Register::SEQCNT, 0x0000_0002).await.unwrap(); self.write_reg(Register::SEQCNT, 0x0000_0001).await.unwrap();
// Disable fifo // Disable fifo
self.write_reg(Register::FIFOCON, 0b010 << 13).await.unwrap(); self.write_reg(Register::FIFOCON, 0b010 << 13).await.unwrap();
let cmd = 0b10 << 9 | 0b001 << 6 | 1 << 3 | 1; let cmd = 0b101 << 9 | 0b001 << 6 | 0b01 << 3 | 0b01;
self.write_reg(Register::CMDDATACON, cmd).await.unwrap(); self.write_reg(Register::CMDDATACON, cmd).await.unwrap();
// Enable FIFO // Enable FIFO
@@ -397,6 +372,37 @@ impl AD5940 {
Ok(()) Ok(())
} }
pub async fn init_impedance(&mut self) -> Result<(), Error> {
// AFECON:
self.afecon(
AFECON::DACBUFEN
| AFECON::DACREFEN
| AFECON::SINC2EN
| AFECON::DFTEN
| AFECON::WAVEGENEN
| AFECON::TIAEN
| AFECON::INAMPEN
| AFECON::EXBUFEN
| AFECON::ADCCONVEN
| AFECON::ADCEN
| AFECON::DACEN,
true,
)
.await;
// SWCON - set up switch matrix for impedance measurement
self.swcon(
SWCON::NMUXCON_N2
| SWCON::PMUXCON_P11
| SWCON::DMUXCON_D5).await;
//
self.write_reg_raw(0x0000_20D0, 0b1000 << 4).await.unwrap();
// SINC3 = 5 --> 160000 Hz
// SINC2 = 178 --> 898,8764044944Hz
// ... (DFTNUM = 2048)
Ok(())
}
} }
#[allow(dead_code)] #[allow(dead_code)]

View File

@@ -13,8 +13,8 @@ use crate::ad5940_registers::{AFECON, AFEGENINTSTA};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
// mod ad5940; mod ad5940;
// use ad5940::AD5940; use ad5940::AD5940;
// mod adg2128; // mod adg2128;
// use adg2128::State; // use adg2128::State;
@@ -63,27 +63,28 @@ async fn main(spawner: Spawner) {
let p = embassy_stm32::init(config); let p = embassy_stm32::init(config);
info!("Hello World!"); info!("Hello World!");
let mut led = Output::new(p.PA5, Level::High, Speed::Low); // let mut led = Output::new(p.PA5, Level::High, Speed::Low);
// // Set up SPI for AD5940 // Set up SPI for AD5940
// let cs = Output::new(p.PC9, Level::High, Speed::Low); let cs = Output::new(p.PC9, Level::High, Speed::Low);
// let rst = Output::new(p.PB3, Level::High, Speed::Low); let rst = Output::new(p.PB3, Level::High, Speed::Low);
// let spi = spi::Spi::new_blocking( let spi = spi::Spi::new_blocking(
// p.SPI1, p.SPI1,
// p.PA5, // SCK p.PA5, // SCK
// p.PA7, // MOSI p.PA7, // MOSI
// p.PA6, // MISO p.PA6, // MISO
// spi::Config::default() spi::Config::default()
// ); );
// let mut ad5940 = AD5940::new(spi, cs, rst); let mut ad5940 = AD5940::new(spi, cs, rst);
// ad5940.reset().await.unwrap(); ad5940.reset().await.unwrap();
// Timer::after_millis(1).await; Timer::after_millis(1).await;
// ad5940.system_init().await.unwrap(); ad5940.system_init().await.unwrap();
// ad5940.init_temperature().await.unwrap(); // ad5940.init_temperature().await.unwrap();
// ad5940.init_waveform().await.unwrap(); ad5940.init_waveform().await.unwrap();
ad5940.init_impedance().await.unwrap();
// // Set up I2C for ADG2128 // // Set up I2C for ADG2128
// let i2c = i2c::I2c::new_blocking( // let i2c = i2c::I2c::new_blocking(
@@ -101,37 +102,39 @@ async fn main(spawner: Spawner) {
// electrodes.set(Electrode::E3, AD5940Pin::AIN1, State::ENABLED); // electrodes.set(Electrode::E3, AD5940Pin::AIN1, State::ENABLED);
// // electrodes.set(Electrode::E12, AD5940Pin::RE0, State::ENABLED); // // electrodes.set(Electrode::E12, AD5940Pin::RE0, State::ENABLED);
// Turn on the green LED
ad5940.write_reg_raw(0x0000_0004, 1 << 1).await.unwrap();
ad5940.write_reg_raw(0x0000_001C, 1 << 1).await.unwrap();
// // Sequencer test // // Sequencer test
// ad5940.sequencer_enable(true).await; ad5940.sequencer_enable(true).await;
// ad5940.afecon(AFECON::WAVEGENEN, true).await; ad5940.afecon(AFECON::WAVEGENEN, true).await;
// ad5940.wgfcw(1000).await; ad5940.wgfcw(1000).await;
// ad5940.sequencer_wait(600_000).await; ad5940.sequencer_wait(300_000).await;
// ad5940.wgfcw(2000).await; ad5940.wgfcw(2000).await;
ad5940.sequencer_wait(160_000).await;
// ad5940.sequencer_trigger_interrupt(AFEGENINTSTA::CUSTOMINT0).await; // ad5940.sequencer_trigger_interrupt(AFEGENINTSTA::CUSTOMINT0).await;
// ad5940.sequencer_trigger_interrupt(AFEGENINTSTA::CUSTOMINT1).await; ad5940.afecon(AFECON::WAVEGENEN, false).await;
// ad5940.sequencer_wait(160_000).await; ad5940.sequencer_enable(false).await;
// ad5940.afecon(AFECON::WAVEGENEN, false).await;
// ad5940.sequencer_enable(false).await;
// // Configure the sequencer cmd data sram // // Configure the sequencer cmd data sram
// ad5940.cmddatacon().await; ad5940.cmddatacon().await;
// let start_address = 100; // 20 works let start_address = 0;
// ad5940.sequencer_cmd_write(start_address).await; ad5940.sequencer_cmd_write(start_address).await;
// ad5940.sequencer_info_configure(0, ad5940.seq_len, start_address).await; ad5940.sequencer_info_configure(0, ad5940.seq_len, start_address).await;
// info!("{}", ad5940.seq_len);
// Create USB driver and start postcard-rpc server // Create USB driver and start postcard-rpc server
let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11); let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11);
init_communication(driver, spawner); init_communication(driver, spawner);
// Green led task // Green led task
spawner.must_spawn(green_led(led)); // spawner.must_spawn(green_led(led));
loop { loop {
// Read chip id // Read chip id
// let chip_id = ad5940.get_chipid().await.unwrap(); // let chip_id = ad5940.get_chipid().await;
// info!("Chip ID: 0x{:04X}", chip_id); // info!("Chip ID: 0x{:04X}", chip_id);
// Read temperature // Read temperature
@@ -143,15 +146,15 @@ async fn main(spawner: Spawner) {
// info!("high"); // info!("high");
ad5940.sequencer_trigger(0).await;
// info!("Mainloop still running!");
info!("Mainloop still running!"); Timer::after_millis(250).await;
Timer::after_millis(5000).await;
// ad5940.sequencer_trigger().await;
} }
} }