humpback-dds/src/lib.rs

289 lines
8.4 KiB
Rust
Raw Normal View History

2020-08-10 17:04:40 +08:00
#![no_std]
2020-09-01 14:50:49 +08:00
#![feature(generic_associated_types)]
#![feature(str_strip)]
2020-08-11 00:07:07 +08:00
extern crate embedded_hal;
2020-08-31 11:36:05 +08:00
use embedded_hal::{
digital::v2::OutputPin,
blocking::spi::Transfer,
};
use core::{
cell,
marker::PhantomData,
};
2020-08-10 18:06:15 +08:00
use cortex_m;
2020-08-13 16:51:08 +08:00
#[macro_use]
pub mod bitmask_macro;
2020-08-10 18:06:15 +08:00
pub mod spi_slave;
use crate::spi_slave::{
Parts,
SPISlave,
};
2020-08-11 00:07:07 +08:00
2020-08-31 09:31:56 +08:00
pub mod cpld;
2020-08-31 11:36:05 +08:00
use crate::cpld::CPLD;
use crate::cpld::DoOnGetRefMutData;
2020-08-31 09:31:56 +08:00
2020-08-11 16:51:17 +08:00
pub mod config_register;
2020-08-31 11:36:05 +08:00
use crate::config_register::ConfigRegister;
2020-08-31 16:48:21 +08:00
use crate::config_register::CFGMask;
2020-08-31 17:43:15 +08:00
use crate::config_register::StatusMask;
2020-08-31 11:36:05 +08:00
2020-08-11 00:07:07 +08:00
pub mod attenuator;
2020-08-31 11:36:05 +08:00
use crate::attenuator::Attenuator;
2020-08-17 12:15:11 +08:00
pub mod dds;
2020-08-31 11:36:05 +08:00
use crate::dds::DDS;
pub mod scpi;
2020-08-13 16:51:08 +08:00
2020-09-15 12:17:42 +08:00
pub mod translation;
2020-09-01 14:50:49 +08:00
pub mod nal_tcp_client;
2020-08-10 18:06:15 +08:00
/*
* Enum for structuring error
*/
#[derive(Debug)]
pub enum Error<E> {
SPI(E),
CSError,
GetRefMutDataError,
2020-08-11 11:29:47 +08:00
AttenuatorError,
2020-08-24 17:03:44 +08:00
IOUpdateError,
2020-08-26 13:18:50 +08:00
DDSError,
2020-08-31 16:48:21 +08:00
ConfigRegisterError,
DDSCLKError,
2020-09-04 13:29:50 +08:00
ParameterError,
2020-08-10 18:06:15 +08:00
}
2020-08-31 11:36:05 +08:00
#[derive(Debug)]
2020-09-07 14:07:39 +08:00
pub enum ClockSource {
OSC,
SMA,
MMCX,
}
2020-08-31 11:36:05 +08:00
/*
* Struct for Urukul master device
*/
pub struct Urukul<SPI> {
config_register: ConfigRegister<SPI>,
attenuator: Attenuator<SPI>,
dds: [DDS<SPI>; 4],
2020-09-16 12:05:45 +08:00
f_master_clk: f64,
2020-08-31 11:36:05 +08:00
}
impl<SPI, E> Urukul<SPI>
where
SPI: Transfer<u8, Error = E>,
{
/*
* Master constructor for the entire Urukul device
* Supply 7 SPI channels to Urukul and 4 reference clock frequencies
2020-08-31 11:36:05 +08:00
*/
2020-09-16 12:05:45 +08:00
pub fn new(spi1: SPI, spi2: SPI, spi3: SPI, spi4: SPI, spi5: SPI, spi6: SPI, spi7: SPI) -> Self {
2020-08-31 11:36:05 +08:00
// Construct Urukul
Urukul {
config_register: ConfigRegister::new(spi1),
attenuator: Attenuator::new(spi2),
2020-09-16 12:05:45 +08:00
// Create 4 DDS instances with fixed 25MHz clock
// Counter-intuitive to assign urukul clock before having a urukul
2020-08-31 11:36:05 +08:00
dds: [
2020-09-16 12:05:45 +08:00
DDS::new(spi4, 25_000_000.0),
DDS::new(spi5, 25_000_000.0),
DDS::new(spi6, 25_000_000.0),
DDS::new(spi7, 25_000_000.0),
2020-08-31 11:36:05 +08:00
],
2020-09-16 12:05:45 +08:00
// Default clock selection: OSC, fixed 100MHz speed
f_master_clk: 100_000_000.0,
2020-08-31 11:36:05 +08:00
}
}
2020-08-31 16:48:21 +08:00
/*
* Reset method. To be invoked by initialization and manual reset.
* Only Urukul struct provides reset method.
* DDS reset is controlled by Urukul (RST).
* Attenuators only have shift register reset, which does not affect its data
* CPLD only has a "all-zero" default state.
*/
pub fn reset(&mut self) -> Result<(), Error<E>> {
// Reset DDS and attenuators
self.config_register.set_configurations(&mut [
(CFGMask::RST, 1),
(CFGMask::IO_RST, 1),
(CFGMask::IO_UPDATE, 0)
])?;
// Set 0 to all fields on configuration register.
self.config_register.set_configurations(&mut [
(CFGMask::RF_SW, 0),
(CFGMask::LED, 0),
(CFGMask::PROFILE, 0),
(CFGMask::IO_UPDATE, 0),
(CFGMask::MASK_NU, 0),
(CFGMask::CLK_SEL0, 0),
(CFGMask::SYNC_SEL, 0),
(CFGMask::RST, 0),
(CFGMask::IO_RST, 0),
(CFGMask::CLK_SEL1, 0),
(CFGMask::DIV, 0),
])?;
// Init all DDS chips. Configure SDIO as input only.
for chip_no in 0..4 {
self.dds[chip_no].init()?;
}
2020-09-16 12:05:45 +08:00
// Clock tree reset. OSC clock source by default
self.f_master_clk = 100_000_000.0;
// CPLD divides clock frequency by 4 by default.
2020-08-31 16:48:21 +08:00
for chip_no in 0..4 {
2020-09-16 12:05:45 +08:00
self.dds[chip_no].set_ref_clk_frequency(self.f_master_clk / 4.0)?;
2020-08-31 16:48:21 +08:00
}
Ok(())
}
2020-08-31 17:43:15 +08:00
/*
* Test method fo Urukul.
* Return the number of test failed.
*/
pub fn test(&mut self) -> Result<u32, Error<E>> {
let mut count = self.config_register.test()?;
count += self.attenuator.test()?;
for chip_no in 0..4 {
count += self.dds[chip_no].test()?;
}
Ok(count)
}
}
2020-09-03 17:41:27 +08:00
pub trait UrukulTraits {
type Error;
2020-09-04 13:29:50 +08:00
fn get_channel_switch_status(&mut self, channel: u32) -> Result<bool, Self::Error>;
fn set_channel_switch(&mut self, channel: u32, status: bool) -> Result<(), Self::Error>;
2020-09-15 14:03:59 +08:00
fn set_clock_source(&mut self, source: ClockSource, frequency: f64) -> Result<(), Self::Error>;
2020-09-07 14:07:39 +08:00
fn set_clock_division(&mut self, division: u8) -> Result<(), Self::Error>;
2020-09-14 17:33:50 +08:00
fn set_channel_attenuation(&mut self, channel: u8, attenuation: f32) -> Result<(), Self::Error>;
fn set_profile(&mut self, profile: u8) -> Result<(), Self::Error>;
2020-09-16 12:05:45 +08:00
fn set_channel_single_tone_profile(&mut self, channel: u8, profile: u8, frequency: f64, phase: f64, amplitude: f64) -> Result<(), Self::Error>;
2020-09-16 14:23:01 +08:00
fn set_channel_single_tone_profile_frequency(&mut self, channel: u8, profile: u8, frequency: f64)-> Result<(), Self::Error>;
fn set_channel_single_tone_profile_phase(&mut self, channel: u8, profile: u8, phase: f64)-> Result<(), Self::Error>;
fn set_channel_single_tone_profile_amplitude(&mut self, channel: u8, profile: u8, amplitude: f64)-> Result<(), Self::Error>;
2020-09-16 12:05:45 +08:00
fn set_channel_sys_clk(&mut self, channel: u8, sys_clk: f64) -> Result<(), Self::Error>;
2020-09-03 17:41:27 +08:00
}
impl<SPI, E> UrukulTraits for Urukul<SPI>
where
SPI: Transfer<u8, Error = E>
{
type Error = Error<E>;
2020-09-04 13:29:50 +08:00
fn get_channel_switch_status(&mut self, channel: u32) -> Result<bool, Self::Error> {
if channel < 4 {
2020-09-14 17:33:50 +08:00
self.config_register.get_status(StatusMask::RF_SW).map(|val| (val & (1 << channel)) != 0)
2020-09-04 13:29:50 +08:00
} else {
Err(Error::ParameterError)
}
}
2020-09-03 17:41:27 +08:00
2020-09-04 13:29:50 +08:00
fn set_channel_switch(&mut self, channel: u32, status: bool) -> Result<(), Self::Error> {
if channel < 4 {
let prev = u32::from(self.config_register.get_status(StatusMask::RF_SW)?);
let next = {
if status {
prev | (1 << channel)
} else {
prev & (!(1 << channel))
}
};
2020-09-14 17:33:50 +08:00
self.config_register.set_configurations(&mut [
2020-09-04 13:29:50 +08:00
(CFGMask::RF_SW, next),
2020-09-14 17:33:50 +08:00
]).map(|_| ())
2020-09-03 17:41:27 +08:00
} else {
2020-09-04 13:29:50 +08:00
Err(Error::ParameterError)
2020-09-03 17:41:27 +08:00
}
}
2020-09-04 13:29:50 +08:00
2020-09-15 14:03:59 +08:00
fn set_clock_source(&mut self, source: ClockSource, frequency: f64) -> Result<(), Self::Error> {
// Change clock source through configuration register
2020-09-14 17:33:50 +08:00
match source {
2020-09-07 14:07:39 +08:00
ClockSource::OSC => self.config_register.set_configurations(&mut [
(CFGMask::CLK_SEL0, 0),
(CFGMask::CLK_SEL1, 0),
]),
ClockSource::MMCX => self.config_register.set_configurations(&mut [
(CFGMask::CLK_SEL0, 0),
(CFGMask::CLK_SEL1, 1),
]),
ClockSource::SMA => self.config_register.set_configurations(&mut [
(CFGMask::CLK_SEL0, 1),
]),
2020-09-15 14:03:59 +08:00
}?;
2020-09-16 12:05:45 +08:00
// Save the new master clock frequency
self.f_master_clk = frequency;
2020-09-15 14:03:59 +08:00
// Calculate reference clock frequency after clock division from configuration register
2020-09-16 12:05:45 +08:00
let f_ref_clk = self.f_master_clk / (self.config_register.get_configuration(CFGMask::DIV) as f64);
2020-09-15 14:03:59 +08:00
// Update all DDS chips on reference clock frequency
for dds_channel in 0..4 {
2020-09-16 12:05:45 +08:00
self.dds[dds_channel].set_ref_clk_frequency(f_ref_clk)?;
2020-09-15 14:03:59 +08:00
}
Ok(())
2020-09-07 14:07:39 +08:00
}
2020-09-03 17:41:27 +08:00
2020-09-07 14:07:39 +08:00
fn set_clock_division(&mut self, division: u8) -> Result<(), Self::Error> {
2020-09-14 17:33:50 +08:00
match division {
2020-09-07 14:07:39 +08:00
1 => self.config_register.set_configurations(&mut [
(CFGMask::DIV, 1),
]),
2 => self.config_register.set_configurations(&mut [
(CFGMask::DIV, 2),
]),
4 => self.config_register.set_configurations(&mut [
(CFGMask::DIV, 3),
]),
_ => Err(Error::ParameterError),
2020-09-16 12:05:45 +08:00
}?;
// Calculate reference clock frequency after clock division from configuration register
let f_ref_clk = self.f_master_clk / (division as f64);
// Update all DDS chips on reference clock frequency
for dds_channel in 0..4 {
self.dds[dds_channel].set_ref_clk_frequency(f_ref_clk)?;
}
Ok(())
2020-09-07 14:07:39 +08:00
}
2020-09-14 17:33:50 +08:00
fn set_channel_attenuation(&mut self, channel: u8, attenuation: f32) -> Result<(), Self::Error> {
self.attenuator.set_channel_attenuation(channel, attenuation)
}
fn set_profile(&mut self, profile: u8) -> Result<(), Self::Error> {
self.config_register.set_configurations(&mut [
(CFGMask::PROFILE, profile.into())
]).map(|_| ())
}
2020-09-16 12:05:45 +08:00
fn set_channel_single_tone_profile(&mut self, channel: u8, profile: u8, frequency: f64, phase: f64, amplitude: f64) -> Result<(), Self::Error> {
self.dds[usize::from(channel)].set_single_tone_profile(profile, frequency, phase, amplitude)
}
2020-09-16 14:23:01 +08:00
fn set_channel_single_tone_profile_frequency(&mut self, channel: u8, profile: u8, frequency: f64)-> Result<(), Self::Error> {
self.dds[usize::from(channel)].set_single_tone_profile_frequency(profile, frequency)
}
fn set_channel_single_tone_profile_phase(&mut self, channel: u8, profile: u8, phase: f64)-> Result<(), Self::Error> {
self.dds[usize::from(channel)].set_single_tone_profile_phase(profile, phase)
}
fn set_channel_single_tone_profile_amplitude(&mut self, channel: u8, profile: u8, amplitude: f64)-> Result<(), Self::Error> {
self.dds[usize::from(channel)].set_single_tone_profile_amplitude(profile, amplitude)
}
2020-09-16 12:05:45 +08:00
fn set_channel_sys_clk(&mut self, channel: u8, f_sys_clk: f64) -> Result<(), Self::Error> {
self.dds[usize::from(channel)].set_sys_clk_frequency(f_sys_clk)
}
2020-09-14 17:33:50 +08:00
}