Created basic lib for AD5940.

This commit is contained in:
2025-07-16 09:55:10 +02:00
parent 73b96c2756
commit c6b250a4a8
2 changed files with 91 additions and 6 deletions

51
src/ad5940.rs Normal file
View File

@@ -0,0 +1,51 @@
use embassy_stm32::{gpio::Output, mode::Blocking, spi::Spi};
pub struct AD5940 {
spi: Spi<'static, Blocking>,
cs: Output<'static>,
}
impl AD5940 {
pub fn new(spi: Spi<'static, Blocking>, cs: Output<'static>) -> Self {
AD5940 { spi, cs }
}
fn read_reg_16(&mut self, address: u16) -> Result<u16, embassy_stm32::spi::Error> {
let addr_bytes = address.to_be_bytes();
// Write address command
self.cs.set_low();
self.spi.blocking_write(&[Command::SPICMD_SETADDR as u8])?;
self.spi.blocking_write(&addr_bytes)?;
self.cs.set_high();
// Read command
self.cs.set_low();
self.spi.blocking_write(&[Command::SPICMD_READREG as u8])?;
let mut data = [0u8; 3]; // First byte dummy, then two data bytes
self.spi.blocking_read(&mut data)?;
self.cs.set_high();
Ok(u16::from_be_bytes([data[1], data[2]]))
}
pub fn get_chipid(&mut self) -> Result<u16, embassy_stm32::spi::Error> {
self.read_reg_16(Register::CHIPID as u16)
}
}
#[allow(dead_code)]
#[allow(non_camel_case_types)]
enum Command {
SPICMD_SETADDR = 0x20,
SPICMD_READREG = 0x6D,
SPICMD_WRITEREG = 0x2D,
SPICMD_READFIFO = 0x5F,
}
#[allow(non_camel_case_types)]
#[repr(u16)]
enum Register {
CHIPID = 0x0000_0404, // Changed from u32 to u16, as expected by SPI write
}

View File

@@ -4,23 +4,57 @@
use defmt::*; use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::{spi, Config};
use embassy_time::Timer; use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
mod ad5940;
use ad5940::AD5940;
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
let mut config = Config::default();
{
use embassy_stm32::rcc::*;
config.rcc.pll1 = Some(Pll {
source: PllSource::HSI,
prediv: PllPreDiv::DIV4,
mul: PllMul::MUL8,
divp: Some(PllDiv::DIV2),
divq: Some(PllDiv::DIV2),
divr: Some(PllDiv::DIV2),
})
}
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);
let cs = Output::new(p.PC9, Level::High, Speed::Low);
let spi = spi::Spi::new_blocking(
p.SPI1,
p.PA5, // SCK
p.PA7, // MOSI
p.PA6, // MISO
spi::Config::default()
);
let mut ad5940 = AD5940::new(spi, cs);
loop { loop {
info!("high"); // Read chip id
led.set_high(); let chip_id = ad5940.get_chipid().unwrap();
info!("Chip ID: 0x{:04X}", chip_id);
// info!("high");
// led.set_high();
Timer::after_millis(500).await; Timer::after_millis(500).await;
info!("low"); // info!("low");
led.set_low(); // led.set_low();
Timer::after_millis(500).await; Timer::after_millis(500).await;
} }
} }