410: tighten signal_generator symmetry bounds r=jordens a=jordens

close #408 

Co-authored-by: Robert Jördens <rj@quartiq.de>
master
bors[bot] 2021-07-21 11:29:56 +00:00 committed by GitHub
commit 93667091e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 21 deletions

View File

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

View File

@ -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 core::convert::{TryFrom, TryInto};
use miniconf::Miniconf; use miniconf::Miniconf;
use serde::Deserialize; use serde::Deserialize;
@ -52,27 +55,43 @@ impl Default for BasicConfig {
pub enum Error { pub enum Error {
/// The provided amplitude is out-of-range. /// The provided amplitude is out-of-range.
InvalidAmplitude, InvalidAmplitude,
/// The provided symmetry is out of range.
InvalidSymmetry,
/// The provided frequency is out of range.
InvalidFrequency,
} }
impl TryFrom<BasicConfig> for Config { impl TryFrom<BasicConfig> for Config {
type Error = Error; type Error = Error;
fn try_from(config: BasicConfig) -> Result<Config, Error> { fn try_from(config: BasicConfig) -> Result<Config, Error> {
// Calculate the frequency tuning words // Validate symmetry
let frequency_tuning_word: [u32; 2] = { if config.symmetry < 0.0 || config.symmetry > 1.0 {
const LSB_PER_HERTZ: f32 = return Err(Error::InvalidSymmetry);
(1u64 << (31 + ADC_SAMPLE_TICKS_LOG2)) as f32 / 100.0e6; }
let ftw = config.frequency * LSB_PER_HERTZ;
if config.symmetry <= 0.0 { const LSB_PER_HERTZ: f32 = (1u64 << (31 + ADC_SAMPLE_TICKS_LOG2))
[1u32 << 31, ftw as u32] as f32
} else if config.symmetry >= 1.0 { / (TIMER_FREQUENCY.0 * 1_000_000) as f32;
[ftw as u32, 1u32 << 31] 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 { } else {
[ [ftws[0] as u32, ftws[1] as u32]
(ftw / config.symmetry) as u32,
(ftw / (1.0 - config.symmetry)) as u32,
]
} }
}; };

View File

@ -74,12 +74,10 @@ impl NetworkProcessor {
// Service the network stack to process any inbound and outbound traffic. // Service the network stack to process any inbound and outbound traffic.
let now = self.clock.current_ms(); 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(true) => UpdateState::Updated,
Ok(false) => UpdateState::NoChange, Ok(false) => UpdateState::NoChange,
Err(_) => UpdateState::Updated, Err(_) => UpdateState::Updated,
}; }
result
} }
} }