From 56cb6eb3fd3202fe535c185a503fda16075768fd Mon Sep 17 00:00:00 2001 From: occheung Date: Fri, 4 Sep 2020 13:29:50 +0800 Subject: [PATCH] scpi: redesign sw --- Cargo.toml | 4 ++-- src/lib.rs | 46 +++++++++++++++++++++++++++++++++----- src/nal_tcp_client.rs | 1 + src/scpi.rs | 51 +++++++++++++++++++++---------------------- 4 files changed, 68 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8048ea0..ffdb00f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,8 @@ stm32h7xx-hal = {version = "0.6.0", features = [ "stm32h743v", "rt", "unproven" stm32h7-ethernet = { version = "0.2.0", features = [ "phy_lan8742a", "stm32h743v" ] } smoltcp = { version = "0.6.0", default-features = false, features = [ "ethernet", "proto-ipv4", "proto-ipv6", "socket-raw" ] } nb = "1.0.0" -scpi = { path = "../scpi-fork/scpi", version = "0.3.4" } -# scpi = { git = "https://github.com/occheung/scpi-rs", branch = "issue-4" } +# scpi = { path = "../scpi-fork/scpi", version = "0.3.4" } +scpi = { git = "https://github.com/occheung/scpi-rs", branch = "issue-4" } lexical-core = { version="0.7.1", features=["radix"], default-features=false } libm = "0.2.0" diff --git a/src/lib.rs b/src/lib.rs index fd3219e..a1af640 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,6 +53,7 @@ pub enum Error { DDSError, ConfigRegisterError, DDSCLKError, + ParameterError, } /* @@ -142,7 +143,8 @@ where pub trait UrukulTraits { type Error; - fn switch(&mut self, channel: u32) -> Result<(), Self::Error>; + fn get_channel_switch_status(&mut self, channel: u32) -> Result; + fn set_channel_switch(&mut self, channel: u32, status: bool) -> Result<(), Self::Error>; } impl UrukulTraits for Urukul @@ -151,20 +153,52 @@ where { type Error = Error; - 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)?); + fn get_channel_switch_status(&mut self, channel: u32) -> Result { + if channel < 4 { + match self.config_register.get_status(StatusMask::RF_SW) { + Ok(val) => Ok((val & (1 << channel)) != 0), + Err(_e) => Err(_e), + } + } else { + Err(Error::ParameterError) + } + } + 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)) + } + }; match self.config_register.set_configurations(&mut [ - (CFGMask::RF_SW, switch_set), + (CFGMask::RF_SW, next), ]) { Ok(_) => Ok(()), Err(_e) => Err(_e), } } else { - Err(Error::ConfigRegisterError) + Err(Error::ParameterError) } } + + // fn switch_on(&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::ParameterError) + // } + // } } // /* diff --git a/src/nal_tcp_client.rs b/src/nal_tcp_client.rs index 12de105..6e9bf84 100644 --- a/src/nal_tcp_client.rs +++ b/src/nal_tcp_client.rs @@ -21,6 +21,7 @@ pub enum NetworkError { pub struct NetStorage { pub ip_addrs: [net::wire::IpCidr; 1], pub neighbor_cache: [Option<(net::wire::IpAddress, net::iface::Neighbor)>; 8], + pub routes_cache: [Option<(net::wire::IpCidr, net::iface::Route)>; 8], } pub type NetworkInterface = diff --git a/src/scpi.rs b/src/scpi.rs index c08bcd6..8593ecc 100644 --- a/src/scpi.rs +++ b/src/scpi.rs @@ -31,7 +31,7 @@ use scpi::{ use embedded_hal::blocking::spi::Transfer; use cortex_m_semihosting::hprintln; -use crate::{Urukul, UrukulTraits}; +use crate::{Urukul, UrukulTraits, Error as UrukulError}; // pub struct MyDevice; @@ -49,39 +49,38 @@ impl Command for HelloWorldCommand { } } -pub struct SwitchOnCommand {} -impl Command for SwitchOnCommand { +pub struct Channel1SwitchCommand {} +impl Command for Channel1SwitchCommand { nquery!(); fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()> { - let mut channel :u32 = 0; - let mut channel_index = 0; - while true { - match args.next_data(channel_index != 0) { - Ok(Some(token)) => { - match u32::try_from(token) { - Ok(shift) => { - if shift >= 4 { - return Err(ErrorCode::IllegalParameterValue.into()); - } - channel |= (1 << shift); - }, - Err(_) => return Err(ErrorCode::IllegalParameterValue.into()), - } - }, - Ok(None) => break, - Ok(_) => return Err(ErrorCode::MissingParameter.into()), - Err(_e) => return Err(_e), - }; - channel_index += 1; - } - match context.device.switch(channel) { - Ok(()) => Ok(()), + match context.device.get_channel_switch_status(1) { + Ok(status) => { + let next_state: bool = match args.next_data(true) { + Ok(Some(token)) => token.try_into()?, + Ok(None) => !status, + Err(_) => return Err(ErrorCode::IllegalParameterValue.into()), + }; + match context.device.set_channel_switch(1, next_state) { + Ok(()) => Ok(()), + Err(T) => panic!("Switched an illegal channel!"), + Err(_) => Err(Error::new(ErrorCode::HardwareError)), + } + }, Err(_) => Err(Error::new(ErrorCode::HardwareError)), } } } +// pub struct SwitchCnannelCommand {} +// impl Command for SwitchChannelCommand { +// nquery!(); + +// fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()> { + +// } +// } + /* * Implement "Device" trait from SCPI * TODO: Implement mandatory commands