Compare commits

...

3 Commits

Author SHA1 Message Date
occheung 331d1ff86f mqtt_to_scpi: strip away expected topic 2020-09-15 17:50:35 +08:00
occheung b502b42c92 scpi: fix frequency control 2020-09-15 14:03:59 +08:00
occheung 5438a81722 mqtt: fix ignoring args 2020-09-15 13:03:54 +08:00
8 changed files with 147 additions and 64 deletions

View File

@ -13,6 +13,7 @@ embedded-hal = "0.2.4"
stm32h7xx-hal = {version = "0.7.1", features = [ "stm32h743v", "rt", "unproven", "ethernet", "phy_lan8742a" ] }
smoltcp = { version = "0.6.0", default-features = false, features = [ "ethernet", "proto-ipv4", "proto-ipv6", "socket-tcp", "log" ] }
nb = "1.0.0"
libm = "0.2.0"
embedded-nal = "0.1.0"
minimq = { git = "https://github.com/quartiq/minimq.git", branch = "master" }
@ -32,6 +33,12 @@ branch = "issue-4"
default-features = false
features = [ "build-info", "unit-frequency" ]
# Use below SCPI dependency when need to modify SCPI fork offline
# [dependencies.scpi]
# path = "../scpi-fork/scpi"
# default-features = false
# features = [ "build-info", "unit-frequency" ]
[[example]]
name = "ethernet"

View File

@ -53,6 +53,7 @@ use firmware::{
Channel3AttenuationCommand,
ClockSourceCommand,
ClockDivisionCommand,
ProfileCommand
},
Urukul, scpi_root, recursive_scpi_tree, scpi_tree
};
@ -208,7 +209,7 @@ fn main() -> ! {
let parts = switch.split();
let mut urukul = Urukul::new(
parts.spi1, parts.spi2, parts.spi3, parts.spi4, parts.spi5, parts.spi6, parts.spi7,
[25_000_000, 25_000_000, 25_000_000, 25_000_000]
[25_000_000.0, 25_000_000.0, 25_000_000.0, 25_000_000.0]
);
// Setup ethernet pins

View File

@ -43,6 +43,7 @@ use firmware::{
Channel3AttenuationCommand,
ClockSourceCommand,
ClockDivisionCommand,
ProfileCommand,
},
Urukul, scpi_root, recursive_scpi_tree, scpi_tree
};
@ -209,7 +210,7 @@ fn main() -> ! {
let mut urukul = Urukul::new(
parts.spi1, parts.spi2, parts.spi3, parts.spi4, parts.spi5, parts.spi6, parts.spi7,
[25_000_000, 25_000_000, 25_000_000, 25_000_000]
[25_000_000.0, 25_000_000.0, 25_000_000.0, 25_000_000.0]
);
cp.SCB.invalidate_icache();
@ -272,7 +273,11 @@ fn main() -> ! {
.poll(|_client, topic, message, _properties| match topic {
topic => {
info!("On '{:?}', received: {:?}", topic, message);
context.run_with_mqtt(topic.as_bytes(), &mut buf);
// Why is topic a string while message is a slice?
context.run_with_mqtt(topic,
core::str::from_utf8(message).unwrap(),
&mut buf)
.unwrap();
},
}).is_ok();

View File

@ -75,6 +75,7 @@ where
/*
* Return selected configuration field
* TODO: Return result type instead for error checking
*/
pub fn get_configuration(&mut self, config_type: CFGMask) -> u8 {
config_type.get_filtered_content(self.data) as u8

View File

@ -1,6 +1,7 @@
use embedded_hal::blocking::spi::Transfer;
use crate::Error;
use core::mem::size_of;
use libm::round;
/*
* Bitmask for all configurations (Order: CFR3, CFR2, CFR1)
@ -64,15 +65,15 @@ const READ_MASK :u8 = 0x80;
pub struct DDS<SPI> {
spi: SPI,
f_ref_clk: u64,
f_sys_clk: u64,
f_ref_clk: f64,
f_sys_clk: f64,
}
impl<SPI, E> DDS<SPI>
where
SPI: Transfer<u8, Error = E>
{
pub fn new(spi: SPI, f_ref_clk: u64) -> Self {
pub fn new(spi: SPI, f_ref_clk: f64) -> Self {
DDS {
spi,
f_ref_clk,
@ -121,7 +122,7 @@ where
// Ensure divider is not reset
(DDSCFRMask::REFCLK_IN_DIV_RESETB, 1),
])?;
self.f_sys_clk = self.f_ref_clk / 2;
self.f_sys_clk = self.f_ref_clk / 2.0;
Ok(())
}
@ -138,9 +139,9 @@ where
Ok(())
}
pub fn enable_pll(&mut self, f_sys_clk: u64) -> Result<(), Error<E>> {
pub fn enable_pll(&mut self, f_sys_clk: f64) -> Result<(), Error<E>> {
// Get a divider
let divider = f_sys_clk / self.f_ref_clk;
let divider = (f_sys_clk / self.f_ref_clk) as u64;
// Reject extreme divider values. However, accept no frequency division
if ((divider > 127 || divider < 12) && divider != 1) {
// panic!("Invalid divider value for PLL!");
@ -159,14 +160,16 @@ where
self.set_configurations(&mut [
(DDSCFRMask::PFD_RESET, 0),
])?;
self.f_sys_clk = self.f_ref_clk * divider;
self.f_sys_clk = self.f_ref_clk * (divider as f64);
Ok(())
}
// Change external clock source (ref_clk)
pub fn set_ref_clk_frequency(&mut self, f_ref_clk: u64) -> Result<(), Error<E>> {
pub fn set_ref_clk_frequency(&mut self, f_ref_clk: f64) -> Result<(), Error<E>> {
// Override old reference clock frequency (ref_clk)
self.f_ref_clk = f_ref_clk;
// TODO: Examine clock tree and update f_sys_clk
// Calculate the new system clock frequency, examine the clock tree
let mut configuration_queries = [
// Acquire PLL status
(DDSCFRMask::PLL_ENABLE, 0),
@ -179,7 +182,7 @@ where
self.get_configurations(&mut configuration_queries)?;
if configuration_queries[0].1 == 1 {
// Recalculate sys_clk
let divider :u64 = configuration_queries[1].1.into();
let divider :f64 = configuration_queries[1].1.into();
let f_sys_clk = self.f_ref_clk * divider;
// Adjust VCO
match self.get_VCO_no(f_sys_clk, divider as u8) {
@ -204,7 +207,7 @@ where
}
}
else if configuration_queries[2].1 == 0 {
self.f_sys_clk = self.f_ref_clk / 2;
self.f_sys_clk = self.f_ref_clk / 2.0;
Ok(())
}
else {
@ -214,23 +217,23 @@ where
}
#[allow(non_snake_case)]
fn get_VCO_no(&mut self, f_sys_clk: u64, divider: u8) -> Result<u8, Error<E>> {
fn get_VCO_no(&mut self, f_sys_clk: f64, divider: u8) -> Result<u8, Error<E>> {
// Select a VCO
if divider == 1 {
Ok(6) // Bypass PLL if no frequency division needed
} else if f_sys_clk > 1_150_000_000 {
} else if f_sys_clk > 1_150_000_000.0 {
Err(Error::DDSCLKError)
} else if f_sys_clk > 820_000_000 {
} else if f_sys_clk > 820_000_000.0 {
Ok(5)
} else if f_sys_clk > 700_000_000 {
} else if f_sys_clk > 700_000_000.0 {
Ok(4)
} else if f_sys_clk > 600_000_000 {
} else if f_sys_clk > 600_000_000.0 {
Ok(3)
} else if f_sys_clk > 500_000_000 {
} else if f_sys_clk > 500_000_000.0 {
Ok(2)
} else if f_sys_clk > 420_000_000 {
} else if f_sys_clk > 420_000_000.0 {
Ok(1)
} else if f_sys_clk > 370_000_000 {
} else if f_sys_clk > 370_000_000.0 {
Ok(0)
} else {
Ok(7) // Bypass PLL if f_sys_clk is too low
@ -311,14 +314,14 @@ where
* Frequency: Must be integer
* Amplitude: In a scale from 0 to 1, taking float
*/
pub fn set_single_tone_profile(&mut self, profile: u8, f_out: u64, phase_offset: f64, amp_scale_factor: f64) -> Result<(), Error<E>> {
pub fn set_single_tone_profile(&mut self, profile: u8, f_out: f64, phase_offset: f64, amp_scale_factor: f64) -> Result<(), Error<E>> {
assert!(profile < 8);
assert!(phase_offset >= 0.0 && phase_offset < 360.0);
assert!(amp_scale_factor >=0.0 && amp_scale_factor <= 1.0);
let resolutions :[u64; 3] = [1 << 32, 1 << 16, 1 << 14];
let ftw = (resolutions[0] * f_out / self.f_sys_clk) as u32;
let ftw = ((resolutions[0] as f64) * f_out / self.f_sys_clk) as u32;
let pow = ((resolutions[1] as f64) * phase_offset / 360.0) as u16;
let asf :u16 = if amp_scale_factor == 1.0 {
0x3FFF

View File

@ -1,5 +1,6 @@
#![no_std]
#![feature(generic_associated_types)]
#![feature(str_strip)]
extern crate embedded_hal;
use embedded_hal::{
digital::v2::OutputPin,
@ -81,7 +82,7 @@ where
* Master constructor for the entire Urukul device
* Supply 7 SPI channels to Urukul and 4 reference clock frequencies
*/
pub fn new(spi1: SPI, spi2: SPI, spi3: SPI, spi4: SPI, spi5: SPI, spi6: SPI, spi7: SPI, f_ref_clks: [u64; 4]) -> Self {
pub fn new(spi1: SPI, spi2: SPI, spi3: SPI, spi4: SPI, spi5: SPI, spi6: SPI, spi7: SPI, f_ref_clks: [f64; 4]) -> Self {
// Construct Urukul
Urukul {
config_register: ConfigRegister::new(spi1),
@ -130,7 +131,7 @@ where
// Clock tree reset. CPLD divides clock frequency by 4 by default.
for chip_no in 0..4 {
self.dds[chip_no].set_ref_clk_frequency(25_000_000)?;
self.dds[chip_no].set_ref_clk_frequency(25_000_000.0)?;
}
Ok(())
}
@ -153,9 +154,10 @@ pub trait UrukulTraits {
type Error;
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>;
fn set_clock_source(&mut self, source: ClockSource) -> Result<(), Self::Error>;
fn set_clock_source(&mut self, source: ClockSource, frequency: f64) -> Result<(), Self::Error>;
fn set_clock_division(&mut self, division: u8) -> Result<(), Self::Error>;
fn set_channel_attenuation(&mut self, channel: u8, attenuation: f32) -> Result<(), Self::Error>;
fn set_profile(&mut self, profile: u8) -> Result<(), Self::Error>;
}
impl<SPI, E> UrukulTraits for Urukul<SPI>
@ -190,7 +192,8 @@ where
}
}
fn set_clock_source(&mut self, source: ClockSource) -> Result<(), Self::Error> {
fn set_clock_source(&mut self, source: ClockSource, frequency: f64) -> Result<(), Self::Error> {
// Change clock source through configuration register
match source {
ClockSource::OSC => self.config_register.set_configurations(&mut [
(CFGMask::CLK_SEL0, 0),
@ -203,7 +206,16 @@ where
ClockSource::SMA => self.config_register.set_configurations(&mut [
(CFGMask::CLK_SEL0, 1),
]),
}.map(|_| ())
}?;
// Calculate reference clock frequency after clock division from configuration register
let frequency = frequency / (self.config_register.get_configuration(CFGMask::DIV) as f64);
// Update all DDS chips on reference clock frequency
for dds_channel in 0..4 {
self.dds[dds_channel].set_ref_clk_frequency(frequency)?;
}
Ok(())
}
fn set_clock_division(&mut self, division: u8) -> Result<(), Self::Error> {
@ -224,4 +236,10 @@ where
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(|_| ())
}
}

View File

@ -114,8 +114,6 @@ macro_rules! scpi_root {
macro_rules! scpi_tree {
() => {
scpi_root!(
["Control"] => {
["Urukul"] => {
"CHANNEL0" => {
"SWitch" => Channel0SwitchCommand,
"Attenuation" => Channel0AttenuationCommand
@ -135,9 +133,8 @@ macro_rules! scpi_tree {
"CLOCK" => {
"SOURCE" => ClockSourceCommand,
"DIVision" => ClockDivisionCommand
}
}
},
"PROFILE" => ProfileCommand,
["EXAMple"] => {
"HELLO" => {
"WORLD" => HelloWorldCommand
@ -171,6 +168,7 @@ pub struct Channel0AttenuationCommand {}
pub struct Channel1AttenuationCommand {}
pub struct Channel2AttenuationCommand {}
pub struct Channel3AttenuationCommand {}
pub struct ProfileCommand {}
impl<T: Device + UrukulTraits> Command<T> for Channel0SwitchCommand {
nquery!();
@ -281,7 +279,7 @@ impl<T:Device + UrukulTraits> Command<T> for ClockSourceCommand {
};
trace!("Changing clock source to {:?} at {:?}", clock_source, frequency);
context.device.set_clock_source(clock_source)
context.device.set_clock_source(clock_source, frequency.get::<hertz>())
.map_err(|_| Error::new(ErrorCode::HardwareError))
}
}
@ -356,6 +354,28 @@ impl<T:Device + UrukulTraits> Command<T> for Channel3AttenuationCommand {
}
}
impl<T:Device + UrukulTraits> Command<T> for ProfileCommand {
nquery!();
fn event(&self, context: &mut Context<T>, args: &mut Tokenizer) -> Result<()> {
let profile :f32 = args.next_data(false)?
.map_or(Err(Error::new(ErrorCode::IllegalParameterValue)),
|token| token.try_into())?;
if ((profile as u8) as f32) != profile {
return Err(Error::new(ErrorCode::IllegalParameterValue));
}
trace!("Selected Profile :{}", profile);
let profile = profile as u8;
if profile >= 8 {
Err(Error::new(ErrorCode::IllegalParameterValue))
} else {
context.device.set_profile(profile)
.map_err(|_| Error::new(ErrorCode::HardwareError))
}
}
}
/*
* Implement "Device" trait from SCPI
* TODO: Implement mandatory commands

View File

@ -1,28 +1,56 @@
use scpi::prelude::*;
use scpi::Context;
use scpi::error::Result;
use log::{trace, info};
use arrayvec::{ArrayVec};
pub trait MqttScpiTranslator {
fn run_with_mqtt<FMT: Formatter>(&mut self, s: &[u8], response: &mut FMT) -> Result<()>;
// Convert an MQTT publish message into SCPI compatible command
// The argument part/ MQTT message must follow SCPI standard for parameter formatting
fn run_with_mqtt<FMT: Formatter>(&mut self, topic: &str, args: &str, response: &mut FMT) -> Result<()>;
}
impl<'a, T: Device> MqttScpiTranslator for Context<'a, T> {
fn run_with_mqtt<FMT>(&mut self, s: &[u8], response: &mut FMT) -> Result<()>
fn run_with_mqtt<FMT>(&mut self, topic: &str, args: &str, response: &mut FMT) -> Result<()>
where
FMT: Formatter,
{
let mut array_vec = ArrayVec::<[u8; 1024]>::new();
for i in s.into_iter() {
if *i == b'/' {
array_vec.try_push(b'/')
if !topic.starts_with("Urukul/Control") {
info!("Received a publish, but not for control! Topic: {}", topic);
return Ok(());
}
let command_topic = topic.strip_prefix("Urukul/Control/")
.unwrap_or("");
// Create a fixed-size buffer to handle slice operation
let mut buffer = ArrayVec::<[u8; 1024]>::new();
// Copy MQTT topic, convert it into SCPI header format
for i in command_topic.chars() {
if i == '/' {
// The topic separator is colon(':') in SCPI, and slash('/') in MQTT
buffer.try_push(b':')
.map_err(|_| ErrorCode::OutOfMemory)?;
} else {
array_vec.try_push(*i)
buffer.try_push(i as u8)
.map_err(|_| ErrorCode::OutOfMemory)?;
}
}
self.run(array_vec.as_slice(), response)
// Place a space bar between header and parameter
buffer.try_push(b' ')
.map_err(|_| ErrorCode::OutOfMemory)?;
// Copy the arguments into the buffer
for i in args.chars() {
buffer.try_push(i as u8)
.map_err(|_| ErrorCode::OutOfMemory)?;
}
// Pass the message to SCPI processing unit
trace!("Translated MQTT message into SCPI. Translated command: {}",
core::str::from_utf8(buffer.as_slice()).unwrap());
self.run(buffer.as_slice(), response)
}
}