Compare commits

..

No commits in common. "82867178f9f26bef22c0344d497e8022172cdfc1" and "43ccb866971a21f86afb2521a0a6cdf3f99bbe0f" have entirely different histories.

9 changed files with 106 additions and 129 deletions

View File

@ -7,6 +7,7 @@ use core::sync::atomic::{AtomicU32, Ordering};
extern crate log;
use cortex_m;
use cortex_m::asm::nop;
use cortex_m_rt::{
entry,
exception,
@ -14,9 +15,14 @@ use cortex_m_rt::{
extern crate smoltcp;
use stm32h7xx_hal::ethernet;
use stm32h7xx_hal::hal::digital::v2::InputPin;
use stm32h7xx_hal::gpio::Speed;
use stm32h7xx_hal::hal::digital::v2::{
OutputPin,
InputPin,
};
use stm32h7xx_hal::rcc::CoreClocks;
use stm32h7xx_hal::{pac, prelude::*, spi, stm32, stm32::interrupt};
use Speed::*;
use core::{
str,
@ -26,8 +32,8 @@ use core::mem::uninitialized;
// Exception: no phy::wait
//use smoltcp::phy::wait as phy_wait;
use smoltcp::wire::{IpAddress, IpCidr};
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, Routes};
use smoltcp::socket::SocketSet;
use smoltcp::socket::{SocketHandle, TcpSocket, TcpSocketBuffer};
use smoltcp::time::{Duration, Instant};
@ -38,6 +44,16 @@ use embedded_nal::TcpStack;
use firmware;
use firmware::{
attenuator::Attenuator,
config_register::{
ConfigRegister,
CFGMask,
StatusMask,
},
dds::{
DDS,
DDSCFRMask,
},
cpld::{
CPLD,
},
@ -47,10 +63,6 @@ use firmware::{
Channel1SwitchCommand,
Channel2SwitchCommand,
Channel3SwitchCommand,
Channel0AttenuationCommand,
Channel1AttenuationCommand,
Channel2AttenuationCommand,
Channel3AttenuationCommand,
ClockSourceCommand,
ClockDivisionCommand,
},
@ -71,6 +83,9 @@ use scpi::{
ieee488_stb,
ieee488_tst,
ieee488_wai,
nquery,
//Helpers
qonly,
scpi_crate_version,
scpi_status,
scpi_system,
@ -275,20 +290,16 @@ fn main() -> ! {
}
},
"CHANNEL0" => {
"SWitch" => Channel0SwitchCommand,
"Attenuation" => Channel0AttenuationCommand
"SWitch" => Channel0SwitchCommand
},
"CHANNEL1" => {
"SWitch" => Channel1SwitchCommand,
"Attenuation" => Channel1AttenuationCommand
"SWitch" => Channel1SwitchCommand
},
"CHANNEL2" => {
"SWitch" => Channel2SwitchCommand,
"Attenuation" => Channel2AttenuationCommand
"SWitch" => Channel2SwitchCommand
},
"CHANNEL3" => {
"SWitch" => Channel3SwitchCommand,
"Attenuation" => Channel3AttenuationCommand
"SWitch" => Channel3SwitchCommand
},
"CLOCK" => {
"SOURCE" => ClockSourceCommand,
@ -353,7 +364,7 @@ fn main() -> ! {
}
if socket.can_recv() {
let data = socket.recv(|buffer| {
let mut data = socket.recv(|buffer| {
(buffer.len(), buffer)
}).unwrap();
if str::from_utf8(data).unwrap().trim() == "quit" {
@ -383,7 +394,6 @@ use stm32h7xx_hal::gpio::{
/*
* Migrated ethernet setup pins
*/
#[allow(non_camel_case_types)]
pub fn setup_ethernet_pins<REF_CLK, MDIO, MDC, CRS_DV, RXD0, RXD1, TX_EN, TXD0, TXD1>(
pa1: PA1<REF_CLK>, pa2: PA2<MDIO>, pc1: PC1<MDC>, pa7: PA7<CRS_DV>, pc4: PC4<RXD0>,
pc5: PC5<RXD1>, pg11: PG11<TX_EN>, pg13: PG13<TXD0>, pb13: PB13<TXD1>

View File

@ -40,16 +40,20 @@ where
}
let mut clone = self.data.clone();
// Transmit SPI once to set attenuation
self.spi.transfer(&mut clone)
.map(|_| ())
.map_err(|_| Error::AttenuatorError)
match self.spi.transfer(&mut clone).map_err(Error::SPI) {
Err(e) => Err(Error::AttenuatorError),
Ok(_) => Ok(()),
}
}
pub fn set_channel_attenuation(&mut self, channel: u8, attenuation: f32) -> Result<(), Error<E>> {
assert!(channel < 4);
let mut arr: [f32; 4] = self.get_attenuation()?;
arr[channel as usize] = attenuation;
self.set_attenuation(arr).map(|_| ())
match self.set_attenuation(arr) {
Ok(v) => Ok(()),
Err(e) => Err(e)
}
}
pub fn get_channel_attenuation(&mut self, channel: u8) -> Result<f32, Error<E>> {

View File

@ -9,7 +9,6 @@
macro_rules! construct_bitmask {
($collection: ident; $unsigned_type: ty; $($name: ident, $shift: expr, $width: expr),+) => {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum $collection {
$(
$name,

View File

@ -213,7 +213,6 @@ where
}
}
#[allow(non_snake_case)]
fn get_VCO_no(&mut self, f_sys_clk: u64, divider: u8) -> Result<u8, Error<E>> {
// Select a VCO
if divider == 1 {
@ -388,9 +387,10 @@ macro_rules! impl_register_io {
for i in 0..$reg_byte_size {
arr[i+1] = bytes[i];
}
self.spi.transfer(&mut arr)
.map(|_| ())
.map_err(Error::SPI)
match self.spi.transfer(&mut arr).map_err(Error::SPI) {
Ok(v) => Ok(()),
Err(e) => Err(e),
}
},
)*
_ => panic!("Bad address for DDS writing.")

View File

@ -153,7 +153,6 @@ pub trait UrukulTraits {
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_division(&mut self, division: u8) -> Result<(), Self::Error>;
fn set_channel_attenuation(&mut self, channel: u8, attenuation: f32) -> Result<(), Self::Error>;
}
impl<SPI, E> UrukulTraits for Urukul<SPI>
@ -164,7 +163,10 @@ where
fn get_channel_switch_status(&mut self, channel: u32) -> Result<bool, Self::Error> {
if channel < 4 {
self.config_register.get_status(StatusMask::RF_SW).map(|val| (val & (1 << channel)) != 0)
match self.config_register.get_status(StatusMask::RF_SW) {
Ok(val) => Ok((val & (1 << channel)) != 0),
Err(_e) => Err(_e),
}
} else {
Err(Error::ParameterError)
}
@ -180,16 +182,19 @@ where
prev & (!(1 << channel))
}
};
self.config_register.set_configurations(&mut [
match self.config_register.set_configurations(&mut [
(CFGMask::RF_SW, next),
]).map(|_| ())
]) {
Ok(_) => Ok(()),
Err(_e) => Err(_e),
}
} else {
Err(Error::ParameterError)
}
}
fn set_clock_source(&mut self, source: ClockSource) -> Result<(), Self::Error> {
match source {
let result = match source {
ClockSource::OSC => self.config_register.set_configurations(&mut [
(CFGMask::CLK_SEL0, 0),
(CFGMask::CLK_SEL1, 0),
@ -201,11 +206,15 @@ where
ClockSource::SMA => self.config_register.set_configurations(&mut [
(CFGMask::CLK_SEL0, 1),
]),
}.map(|_| ())
};
match result {
Ok(_) => Ok(()),
Err(_e) => Err(_e),
}
}
fn set_clock_division(&mut self, division: u8) -> Result<(), Self::Error> {
match division {
let result = match division {
1 => self.config_register.set_configurations(&mut [
(CFGMask::DIV, 1),
]),
@ -216,10 +225,10 @@ where
(CFGMask::DIV, 3),
]),
_ => Err(Error::ParameterError),
}.map(|_| ())
};
match result {
Ok(_) => Ok(()),
Err(_e) => Err(_e),
}
}
fn set_channel_attenuation(&mut self, channel: u8, attenuation: f32) -> Result<(), Self::Error> {
self.attenuator.set_channel_attenuation(channel, attenuation)
}
}
}

View File

@ -1,10 +1,7 @@
#![no_main]
#![no_std]
#[macro_use]
extern crate log;
use log::{trace, debug, info, warn};
use panic_semihosting as _;
use stm32h7xx_hal::hal::digital::v2::{
InputPin,
@ -14,6 +11,7 @@ use stm32h7xx_hal::{pac, prelude::*, spi};
use cortex_m;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use firmware;
use firmware::{
@ -32,13 +30,10 @@ use firmware::{
}
};
#[path = "../examples/util/logger.rs"]
mod logger;
#[entry]
fn main() -> ! {
let mut cp = cortex_m::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let pwr = dp.PWR.constrain();
@ -46,16 +41,9 @@ fn main() -> ! {
let rcc = dp.RCC.constrain();
let ccdr = rcc
.use_hse(8.mhz())
.sys_ck(400.mhz())
.pll1_q_ck(48.mhz())
.pll1_r_ck(400.mhz())
.freeze(vos, &dp.SYSCFG);
unsafe {
logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM);
}
logger::init();
let mut delay = cp.SYST.delay(ccdr.clocks);
@ -70,10 +58,10 @@ fn main() -> ! {
let fpga_cdone = gpiod.pd15.into_pull_up_input();
match fpga_cdone.is_high() {
Ok(true) => info!("FPGA is ready."),
Ok(_) => info!("FPGA is in reset state."),
Err(_) => info!("Error: Cannot read C_DONE"),
};
Ok(true) => hprintln!("FPGA is ready."),
Ok(_) => hprintln!("FPGA is in reset state."),
Err(_) => hprintln!("Error: Cannot read C_DONE"),
}.unwrap();
/*
* Using SPI1, AF5
@ -162,10 +150,10 @@ fn main() -> ! {
0x04, // Recirculate mode
]).unwrap();
debug!("{:#X?}", dds0.read_register(0x0E, &mut[
hprintln!("{:#X?}", dds0.read_register(0x0E, &mut[
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
]).unwrap());
]).unwrap()).unwrap();
// Choose profile 0
config.set_configurations(&mut [

View File

@ -60,10 +60,15 @@ impl<'a, 'b, 'c, 'n> NetworkStack<'a, 'b, 'c, 'n> {
}
pub fn update(&mut self, time: u32) -> bool {
self.network_interface.poll(
match self.network_interface.poll(
&mut self.sockets.borrow_mut(),
net::time::Instant::from_millis(time as i64),
).map_or(true, |changed| changed == false)
) {
Ok(changed) => changed == false,
Err(e) => {
true
}
}
}
fn get_ephemeral_port(&self) -> u16 {

View File

@ -7,9 +7,26 @@ use scpi::NumericValues;
use core::convert::{TryFrom, TryInto};
use core::str;
use core::str::Utf8Error;
use scpi::ieee488::commands::*;
use scpi::scpi::commands::*;
use scpi::{
ieee488_cls,
ieee488_ese,
ieee488_esr,
ieee488_idn,
ieee488_opc,
ieee488_rst,
ieee488_sre,
ieee488_stb,
ieee488_tst,
ieee488_wai,
nquery,
//Helpers
qonly,
scpi_crate_version,
scpi_status,
scpi_system,
};
use scpi::suffix::{Amplitude, Db};
use uom::si::frequency::{gigahertz, hertz, kilohertz, megahertz, Frequency};
@ -130,10 +147,6 @@ pub struct Channel2SwitchCommand {}
pub struct Channel3SwitchCommand {}
pub struct ClockSourceCommand {}
pub struct ClockDivisionCommand {}
pub struct Channel0AttenuationCommand {}
pub struct Channel1AttenuationCommand {}
pub struct Channel2AttenuationCommand {}
pub struct Channel3AttenuationCommand {}
impl<T: Device + UrukulTraits> Command<T> for Channel0SwitchCommand {
nquery!();
@ -257,68 +270,17 @@ impl<T:Device + UrukulTraits> Command<T> for ClockDivisionCommand {
.map_or(Err(Error::new(ErrorCode::IllegalParameterValue)),
|token| token.try_into())?;
trace!("Received master clock division factor: {}", div);
if div == 1.0 || div == 2.0 || div == 4.0 {
debug!("Set master clock division as {}", div);
context.device.set_clock_division(div as u8)
.map_err(|_| Error::new(ErrorCode::HardwareError))
} else {
Err(Error::new(ErrorCode::IllegalParameterValue))
match div {
1.0 | 2.0 | 4.0 => {
debug!("Set master clock division as {}", div);
context.device.set_clock_division(div as u8)
.map_err(|_| Error::new(ErrorCode::HardwareError))
}
_ => Err(Error::new(ErrorCode::IllegalParameterValue)),
}
}
}
impl<T:Device + UrukulTraits> Command<T> for Channel0AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, args: &mut Tokenizer) -> Result<()> {
let attenuation: f32 = args.next_data(false)?
.map_or(Err(Error::new(ErrorCode::IllegalParameterValue)),
|token| token.try_into())?;
trace!("Received channel 0 attenuation input: {}", attenuation);
context.device.set_channel_attenuation(0, attenuation)
.map_err(|_| Error::new(ErrorCode::HardwareError))
}
}
impl<T:Device + UrukulTraits> Command<T> for Channel1AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, args: &mut Tokenizer) -> Result<()> {
let attenuation: f32 = args.next_data(false)?
.map_or(Err(Error::new(ErrorCode::IllegalParameterValue)),
|token| token.try_into())?;
trace!("Received channel 1 attenuation input: {}", attenuation);
context.device.set_channel_attenuation(1, attenuation)
.map_err(|_| Error::new(ErrorCode::HardwareError))
}
}
impl<T:Device + UrukulTraits> Command<T> for Channel2AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, args: &mut Tokenizer) -> Result<()> {
let attenuation: f32 = args.next_data(false)?
.map_or(Err(Error::new(ErrorCode::IllegalParameterValue)),
|token| token.try_into())?;
trace!("Received channel 2 attenuation input: {}", attenuation);
context.device.set_channel_attenuation(2, attenuation)
.map_err(|_| Error::new(ErrorCode::HardwareError))
}
}
impl<T:Device + UrukulTraits> Command<T> for Channel3AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, args: &mut Tokenizer) -> Result<()> {
let attenuation: f32 = args.next_data(false)?
.map_or(Err(Error::new(ErrorCode::IllegalParameterValue)),
|token| token.try_into())?;
trace!("Received channel 3 attenuation input: {}", attenuation);
context.device.set_channel_attenuation(3, attenuation)
.map_err(|_| Error::new(ErrorCode::HardwareError))
}
}
/*
* Implement "Device" trait from SCPI
* TODO: Implement mandatory commands

View File

@ -51,13 +51,13 @@ where
fn transfer<'w>(&mut self, words: &'w mut[u8]) -> Result<&'w [u8], Self::Error> {
self.0.do_on_get_ref_mut_data(move |mut dev| {
dev.select_chip(self.1).map_err(|_| Error::CSError)?;
let result = dev.spi.transfer(words).map_err(Error::SPI)?;
dev.select_chip(0).map_err(|_| Error::CSError)?;
dev.select_chip(self.1);
let result = dev.spi.transfer(words).map_err(Error::SPI);
dev.select_chip(0);
if self.2 {
dev.issue_io_update().map_err(|_| Error::IOUpdateError)?;
dev.issue_io_update();
}
Ok(result)
result
})
}
}