Skip to content

Commit

Permalink
Merge pull request #975 from quartiq/urukul-ad9912-limits
Browse files Browse the repository at this point in the history
Urukul-ad9912-limits
  • Loading branch information
jordens authored Nov 30, 2024
2 parents 45cbf4f + 42e7734 commit 70c9f82
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 30 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions ad9912/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

use arbitrary_int::{u10, u14, u48, u5};
use arbitrary_int::{u10, u14, u48, u5, Number};
use bitbybit::{bitenum, bitfield};
use embedded_hal::spi::{self, Operation, SpiDevice};
use num_traits::float::FloatCore;
Expand Down Expand Up @@ -172,17 +172,23 @@ pub struct Ad9912<B> {

pub fn frequency_to_ftw(frequency: f64, sysclk: f64) -> u48 {
let lsb = sysclk.recip() * (1u64 << 48) as f64;
u48::new((frequency * lsb).round() as _)
// Alias into Nyquist
u48::new(((frequency * lsb).round() as i64 as u64) & u48::MASK)
}

pub fn phase_to_pow(phase: f32) -> u14 {
u14::new((phase * (1u32 << 14) as f32).round() as _)
// Alias into Nyquist
u14::new(((phase * (1u32 << 14) as f32).round() as i32 as u16) & u14::MASK)
}

pub fn dac_fs_to_fsc(dac_fs: f32, r_dac_ref: f32) -> u10 {
let lsb = r_dac_ref * (1024.0 / 192.0 / 1.2);
let fsc = dac_fs * lsb - (1024.0 / 192.0 * 72.0);
u10::new(fsc.round() as _)
// Clamp
u10::new(
fsc.round()
.clamp(u10::MIN.value() as _, u10::MAX.value() as _) as _,
)
}

impl<B: SpiDevice<u8>> Ad9912<B> {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/dual-iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ const SAMPLE_PERIOD: f32 =

#[derive(Clone, Debug, Tree)]
pub struct Settings {
pub dual_iir: DualIir,
pub net: NetSettings,
dual_iir: DualIir,
net: NetSettings,
}

impl stabilizer::settings::AppSettings for Settings {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/lockin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ const SAMPLE_TICKS: u32 = 1 << SAMPLE_TICKS_LOG2;

#[derive(Clone, Debug, Tree)]
pub struct Settings {
pub lockin: Lockin,
pub net: NetSettings,
lockin: Lockin,
net: NetSettings,
}

impl stabilizer::settings::AppSettings for Settings {
Expand Down
11 changes: 3 additions & 8 deletions src/bin/urukul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use stabilizer::{

#[derive(Clone, Debug, Tree)]
pub struct Settings {
pub urukul: App,
pub net: NetSettings,
urukul: App,
net: NetSettings,
}

impl stabilizer::settings::AppSettings for Settings {
Expand Down Expand Up @@ -53,13 +53,9 @@ impl serial_settings::Settings for Settings {
pub struct Channel {
pll_n: Leaf<Option<u5>>,
pll_doubler: Leaf<bool>,
// TODO: range check
frequency: Leaf<f64>,
// TODO: range check
phase: Leaf<f32>,
// TODO: range check
full_scale_current: Leaf<f32>,
// TODO: range check
attenuation: Leaf<f32>,
enable: Leaf<bool>,
update: Leaf<bool>,
Expand Down Expand Up @@ -91,13 +87,12 @@ pub struct App {

impl Default for App {
fn default() -> Self {
let ch = Channel::default();
Self {
clk_sel: urukul::ClkSel::Osc.into(),
div_sel: urukul::DivSel::One.into(),
update: true.into(),
refclk: 100.0e6.into(),
ch: [ch.clone(), ch.clone(), ch.clone(), ch.clone()],
ch: Default::default(),
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions urukul/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ impl<'a, B: SpiBus<u8>, P: OutputPin> Urukul<'a, B, P> {
sta.proto_rev().value() as _,
));
}
if sta.rf_sw() != u4::new(0) {
if sta.rf_sw().value() != 0 {
return Err(Error::Initialization(
"RF_SW driven",
sta.rf_sw().value() as _,
));
}
if sta.ifc_mode() != u4::new(0) {
if sta.ifc_mode().value() != 0 {
return Err(Error::Initialization(
"invalid IFC_MODE",
sta.ifc_mode().value() as _,
Expand Down Expand Up @@ -228,10 +228,10 @@ impl<'a, B: SpiBus<u8>, P: OutputPin> Urukul<'a, B, P> {
ch: u2,
state: bool,
) -> Result<(), DeviceError<B::Error, P::Error>> {
let mut v = self.cfg.rf_sw();
v &= !u4::new(1u8 << ch.value());
v |= u4::new((state as u8) << ch.value());
self.set_cfg(self.cfg.with_rf_sw(v))?;
let mut v = self.cfg.rf_sw().value();
v &= !(1 << ch.value());
v |= (state as u8) << ch.value();
self.set_cfg(self.cfg.with_rf_sw(u4::new(v)))?;
Ok(())
}

Expand All @@ -240,10 +240,10 @@ impl<'a, B: SpiBus<u8>, P: OutputPin> Urukul<'a, B, P> {
ch: u2,
state: bool,
) -> Result<(), DeviceError<B::Error, P::Error>> {
let mut v = self.cfg.led();
v &= !u4::new(1u8 << ch.value());
v |= u4::new((state as u8) << ch.value());
self.set_cfg(self.cfg.with_led(v))?;
let mut v = self.cfg.led().value();
v &= !(1 << ch.value());
v |= (state as u8) << ch.value();
self.set_cfg(self.cfg.with_led(u4::new(v)))?;
Ok(())
}

Expand Down

0 comments on commit 70c9f82

Please sign in to comment.