2020-08-10 17:04:40 +08:00
|
|
|
#![no_std]
|
2020-09-01 14:50:49 +08:00
|
|
|
#![feature(generic_associated_types)]
|
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;
|
|
|
|
use cortex_m_semihosting::hprintln;
|
|
|
|
|
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;
|
2020-08-31 12:32:39 +08:00
|
|
|
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;
|
2020-08-31 12:32:39 +08:00
|
|
|
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;
|
|
|
|
|
2020-08-28 15:48:13 +08:00
|
|
|
pub mod scpi;
|
2020-08-13 16:51:08 +08:00
|
|
|
|
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-08-10 18:06:15 +08:00
|
|
|
}
|
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],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<SPI, E> Urukul<SPI>
|
|
|
|
where
|
|
|
|
SPI: Transfer<u8, Error = E>,
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Master constructor for the entire Urukul device
|
2020-08-31 12:32:39 +08:00
|
|
|
* Supply 7 SPI channels to Urukul and 4 reference clock frequencies
|
2020-08-31 11:36:05 +08:00
|
|
|
*/
|
|
|
|
pub fn new(spi1: SPI, spi2: SPI, spi3: SPI, spi4: SPI, spi5: SPI, spi6: SPI, spi7: SPI, f_ref_clks: [u64; 4]) -> Self {
|
|
|
|
// Construct Urukul
|
|
|
|
Urukul {
|
|
|
|
config_register: ConfigRegister::new(spi1),
|
|
|
|
attenuator: Attenuator::new(spi2),
|
|
|
|
dds: [
|
|
|
|
DDS::new(spi4, f_ref_clks[1]),
|
|
|
|
DDS::new(spi5, f_ref_clks[1]),
|
|
|
|
DDS::new(spi6, f_ref_clks[2]),
|
|
|
|
DDS::new(spi7, f_ref_clks[3]),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
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()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clock tree reset. CPLD divides clock frequency by 4 by default.
|
|
|
|
for chip_no in 0..4 {
|
2020-08-31 17:43:15 +08:00
|
|
|
self.dds[chip_no].set_ref_clk_frequency(25_000_000)?;
|
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-08-31 12:32:39 +08:00
|
|
|
}
|
|
|
|
|
2020-09-03 17:41:27 +08:00
|
|
|
pub trait UrukulTraits {
|
|
|
|
type Error;
|
|
|
|
fn switch(&mut self, channel: u32) -> Result<(), Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<SPI, E> UrukulTraits for Urukul<SPI>
|
|
|
|
where
|
|
|
|
SPI: Transfer<u8, Error = E>
|
|
|
|
{
|
|
|
|
type Error = Error<E>;
|
|
|
|
|
|
|
|
fn switch(&mut self, channel: u32) -> Result<(), Self::Error>{
|
|
|
|
if channel < 16 {
|
|
|
|
let switch_set = channel | u32::from(self.config_register.get_status(StatusMask::RF_SW)?);
|
|
|
|
|
|
|
|
match self.config_register.set_configurations(&mut [
|
|
|
|
(CFGMask::RF_SW, switch_set),
|
|
|
|
]) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_e) => Err(_e),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::ConfigRegisterError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 12:32:39 +08:00
|
|
|
// /*
|
|
|
|
// * Struct for a better Urukul master device
|
|
|
|
// */
|
|
|
|
// pub struct BetterUrukul<'a, SPI, CS0, CS1, CS2, GPIO> {
|
|
|
|
// cpld: CPLD<SPI, CS0, CS1, CS2, GPIO>,
|
|
|
|
// parts: Option<Parts<'a, CPLD<SPI, CS0, CS1, CS2, GPIO>, SPI, CS0, CS1, CS2, GPIO>>,
|
|
|
|
// config_register: Option<ConfigRegister<SPISlave<'a, CPLD<SPI, CS0, CS1, CS2, GPIO>, SPI, CS0, CS1, CS2, GPIO>>>,
|
|
|
|
// attenuator: Option<Attenuator<SPISlave<'a, CPLD<SPI, CS0, CS1, CS2, GPIO>, SPI, CS0, CS1, CS2, GPIO>>>,
|
|
|
|
// dds: [Option<DDS<SPISlave<'a, CPLD<SPI, CS0, CS1, CS2, GPIO>, SPI, CS0, CS1, CS2, GPIO>>>; 4],
|
|
|
|
// }
|
|
|
|
|
|
|
|
// impl<'a, SPI, CS0, CS1, CS2, GPIO> BetterUrukul<'a, SPI, CS0, CS1, CS2, GPIO>
|
|
|
|
// where
|
|
|
|
// SPI: Transfer<u8>,
|
|
|
|
// CS0: OutputPin,
|
|
|
|
// CS1: OutputPin,
|
|
|
|
// CS2: OutputPin,
|
|
|
|
// GPIO: OutputPin,
|
|
|
|
// {
|
|
|
|
// pub fn new(spi: SPI, chip_select: (CS0, CS1, CS2), io_update: GPIO) -> Self {
|
|
|
|
// // let switch = CPLD::new(spi, chip_select, io_update);
|
|
|
|
// // let parts = switch.split();
|
|
|
|
|
|
|
|
// // Construct Urukul
|
|
|
|
// BetterUrukul {
|
|
|
|
// cpld: CPLD::new(spi, chip_select, io_update),
|
|
|
|
// // parts: CPLD::new(spi, chip_select, io_update).split(),
|
|
|
|
// // config_register: ConfigRegister::new(self.parts.spi1),
|
|
|
|
// // attenuator: Attenuator::new(self.parts.spi2),
|
|
|
|
// // dds: [
|
|
|
|
// // DDS::new(self.parts.spi4, f_ref_clks[1]),
|
|
|
|
// // DDS::new(self.parts.spi5, f_ref_clks[1]),
|
|
|
|
// // DDS::new(self.parts.spi6, f_ref_clks[2]),
|
|
|
|
// // DDS::new(self.parts.spi7, f_ref_clks[3]),
|
|
|
|
// // ],
|
|
|
|
// parts: None,
|
|
|
|
// config_register: None,
|
|
|
|
// attenuator: None,
|
|
|
|
// dds: [None, None, None, None],
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// pub fn init(&'a mut self, f_ref_clks:[u64; 4]) {
|
|
|
|
// self.parts = Some(self.cpld.split());
|
|
|
|
// self.config_register = Some(ConfigRegister::new(self.parts.unwrap().spi1));
|
|
|
|
// self.attenuator = Some(Attenuator::new(self.parts.unwrap().spi2));
|
|
|
|
// self.dds[0] = Some(DDS::new(self.parts.unwrap().spi4, f_ref_clks[0]));
|
|
|
|
// self.dds[1] = Some(DDS::new(self.parts.unwrap().spi5, f_ref_clks[1]));
|
|
|
|
// self.dds[2] = Some(DDS::new(self.parts.unwrap().spi6, f_ref_clks[2]));
|
|
|
|
// self.dds[3] = Some(DDS::new(self.parts.unwrap().spi7, f_ref_clks[3]));
|
|
|
|
// }
|
|
|
|
// }
|