Skip to content

Commit

Permalink
Merge #410
Browse files Browse the repository at this point in the history
410: tighten signal_generator symmetry bounds r=jordens a=jordens

close #408 

Co-authored-by: Robert Jördens <rj@quartiq.de>
  • Loading branch information
bors[bot] and jordens authored Jul 21, 2021
2 parents 37fa1a0 + 4f04bf1 commit 9366709
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ impl TcpSocketStorage {
}
}

impl NetStorage {
pub fn new() -> Self {
impl Default for NetStorage {
fn default() -> Self {
NetStorage {
// Placeholder for the real IP address, which is initialized at runtime.
ip_addrs: [smoltcp::wire::IpCidr::Ipv6(
Expand Down Expand Up @@ -671,7 +671,7 @@ pub fn setup(
// Note(unwrap): The hardware configuration function is only allowed to be called once.
// Unwrapping is intended to panic if called again to prevent re-use of global memory.
let store =
cortex_m::singleton!(: NetStorage = NetStorage::new()).unwrap();
cortex_m::singleton!(: NetStorage = NetStorage::default()).unwrap();

store.ip_addrs[0] = smoltcp::wire::IpCidr::new(
smoltcp::wire::IpAddress::Ipv4(
Expand Down
49 changes: 34 additions & 15 deletions src/hardware/signal_generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{configuration::ADC_SAMPLE_TICKS_LOG2, hardware::dac::DacCode};
use crate::{
configuration::ADC_SAMPLE_TICKS_LOG2, hardware::dac::DacCode,
hardware::design_parameters::TIMER_FREQUENCY,
};
use core::convert::{TryFrom, TryInto};
use miniconf::Miniconf;
use serde::Deserialize;
Expand Down Expand Up @@ -52,27 +55,43 @@ impl Default for BasicConfig {
pub enum Error {
/// The provided amplitude is out-of-range.
InvalidAmplitude,
/// The provided symmetry is out of range.
InvalidSymmetry,
/// The provided frequency is out of range.
InvalidFrequency,
}

impl TryFrom<BasicConfig> for Config {
type Error = Error;

fn try_from(config: BasicConfig) -> Result<Config, Error> {
// Calculate the frequency tuning words
let frequency_tuning_word: [u32; 2] = {
const LSB_PER_HERTZ: f32 =
(1u64 << (31 + ADC_SAMPLE_TICKS_LOG2)) as f32 / 100.0e6;
let ftw = config.frequency * LSB_PER_HERTZ;

if config.symmetry <= 0.0 {
[1u32 << 31, ftw as u32]
} else if config.symmetry >= 1.0 {
[ftw as u32, 1u32 << 31]
// Validate symmetry
if config.symmetry < 0.0 || config.symmetry > 1.0 {
return Err(Error::InvalidSymmetry);
}

const LSB_PER_HERTZ: f32 = (1u64 << (31 + ADC_SAMPLE_TICKS_LOG2))
as f32
/ (TIMER_FREQUENCY.0 * 1_000_000) as f32;
let ftw = config.frequency * LSB_PER_HERTZ;

// Validate base frequency tuning word to be below Nyquist.
const NYQUIST: f32 = (1u32 << 31) as _;
if ftw < 0.0 || 2.0 * ftw > NYQUIST {
return Err(Error::InvalidFrequency);
}

// Calculate the frequency tuning words.
let frequency_tuning_word = {
let ftws = [ftw / config.symmetry, ftw / (1.0 - config.symmetry)];

// Clip both frequency tuning words to within Nyquist before rounding.
if ftws[0] > NYQUIST {
[1u32 << 31, ftws[1] as u32]
} else if ftws[1] > NYQUIST {
[ftws[0] as u32, 1u32 << 31]
} else {
[
(ftw / config.symmetry) as u32,
(ftw / (1.0 - config.symmetry)) as u32,
]
[ftws[0] as u32, ftws[1] as u32]
}
};

Expand Down
6 changes: 2 additions & 4 deletions src/net/network_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ impl NetworkProcessor {
// Service the network stack to process any inbound and outbound traffic.
let now = self.clock.current_ms();

let result = match self.stack.lock(|stack| stack.poll(now)) {
match self.stack.lock(|stack| stack.poll(now)) {
Ok(true) => UpdateState::Updated,
Ok(false) => UpdateState::NoChange,
Err(_) => UpdateState::Updated,
};

result
}
}
}

0 comments on commit 9366709

Please sign in to comment.