Skip to content

Commit

Permalink
Merge pull request #974 from quartiq/urukul-crates-split
Browse files Browse the repository at this point in the history
Urukul-crates-split
  • Loading branch information
jordens authored Nov 28, 2024
2 parents 28cbc67 + 7db6291 commit 45cbf4f
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 35 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ features = []
default-target = "thumbv7em-none-eabihf"

[workspace]
members = ["ad9959", "serial-settings"]
members = [ "ad9912", "ad9959", "encoded_pin", "serial-settings", "urukul"]

[dependencies]
panic-persist = { version = "0.3", features = ["utf8", "custom-panic-handler"] }
Expand Down Expand Up @@ -81,6 +81,8 @@ embedded-hal-compat = "0.13.0"
embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
embedded-hal-bus = "0.2.0"
urukul = { version = "0.1", path = "urukul" }
ad9912 = { version = "0.1", path = "ad9912" }

[build-dependencies]
built = { version = "0.7", features = ["git2"], default-features = false }
Expand Down
19 changes: 19 additions & 0 deletions ad9912/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "ad9912"
version = "0.1.0"
edition = "2021"
description = "AD9912 1 GHz DDS SPI driver"
authors = [
"Robert Jördens <[email protected]>",
]
categories = ["embedded", "no-std"]
license = "MIT OR Apache-2.0"
keywords = ["spi"]
repository = "https://github.com/quartiq/stabilizer"

[dependencies]
arbitrary-int = { version = "1.2.7", features = ["serde"] }
thiserror = { version = "2.0.3", default-features = false }
num-traits = { version = "0.2.19", default-features = false }
embedded-hal = { version = "1.0" }
bitbybit = "1.3.2"
4 changes: 3 additions & 1 deletion src/hardware/ad9912.rs → ad9912/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![no_std]

use arbitrary_int::{u10, u14, u48, u5};
use bitbybit::{bitenum, bitfield};
use embedded_hal_1::spi::{self, Operation, SpiDevice};
use embedded_hal::spi::{self, Operation, SpiDevice};
use num_traits::float::FloatCore;

#[bitenum(u13)]
Expand Down
16 changes: 16 additions & 0 deletions encoded_pin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "encoded_pin"
version = "0.1.0"
edition = "2021"
description = "Binary encoded OutputPin, e.g. for encoded SPI chip select"
authors = [
"Robert Jördens <[email protected]>",
]
categories = ["embedded", "no-std"]
license = "MIT OR Apache-2.0"
keywords = ["spi"]
repository = "https://github.com/quartiq/stabilizer"

[dependencies]
arbitrary-int = { version = "1.2.7", features = ["serde"] }
embedded-hal = { version = "1.0" }
18 changes: 10 additions & 8 deletions src/hardware/decoded_cs.rs → encoded_pin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
#![no_std]

use arbitrary_int::UInt;
use core::cell::RefCell;
use embedded_hal_1::digital::{ErrorType, OutputPin};
use embedded_hal::digital::{ErrorType, OutputPin};

pub struct DecodedCs<'a, CS, const N: usize> {
cs: &'a RefCell<[CS; N]>,
pub struct EncodedPin<'a, P, const N: usize> {
cs: &'a RefCell<[P; N]>,
sel: UInt<u8, N>,
}

impl<'a, CS, const N: usize> DecodedCs<'a, CS, N> {
pub fn new(cs: &'a RefCell<[CS; N]>, sel: UInt<u8, N>) -> Self {
impl<'a, P, const N: usize> EncodedPin<'a, P, N> {
pub fn new(cs: &'a RefCell<[P; N]>, sel: UInt<u8, N>) -> Self {
assert!(sel.value() != 0);
Self { cs, sel }
}
}

impl<'a, CS: ErrorType, const N: usize> ErrorType for DecodedCs<'a, CS, N> {
type Error = CS::Error;
impl<'a, P: ErrorType, const N: usize> ErrorType for EncodedPin<'a, P, N> {

Check warning on line 19 in encoded_pin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> encoded_pin/src/lib.rs:19:6 | 19 | impl<'a, P: ErrorType, const N: usize> ErrorType for EncodedPin<'a, P, N> { | ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes = note: `#[warn(clippy::needless_lifetimes)]` on by default help: elide the lifetimes | 19 - impl<'a, P: ErrorType, const N: usize> ErrorType for EncodedPin<'a, P, N> { 19 + impl<P: ErrorType, const N: usize> ErrorType for EncodedPin<'_, P, N> { |
type Error = P::Error;
}

impl<'a, CS: OutputPin, const N: usize> OutputPin for DecodedCs<'a, CS, N> {
impl<'a, P: OutputPin, const N: usize> OutputPin for EncodedPin<'a, P, N> {

Check warning on line 23 in encoded_pin/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> encoded_pin/src/lib.rs:23:6 | 23 | impl<'a, P: OutputPin, const N: usize> OutputPin for EncodedPin<'a, P, N> { | ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes help: elide the lifetimes | 23 - impl<'a, P: OutputPin, const N: usize> OutputPin for EncodedPin<'a, P, N> { 23 + impl<P: OutputPin, const N: usize> OutputPin for EncodedPin<'_, P, N> { |
fn set_low(&mut self) -> Result<(), Self::Error> {
// assert
for (i, cs) in self.cs.borrow_mut().iter_mut().enumerate() {
Expand Down
12 changes: 5 additions & 7 deletions src/bin/urukul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use rtic_monotonics::Monotonic;

use stabilizer::{
hardware::{
self, ad9912, hal,
urukul::{self, ClkSel, DivSel},
SerialTerminal, SystemTimer, Systick, Urukul, UsbDevice,
self, hal, SerialTerminal, SystemTimer, Systick, Urukul, UsbDevice,
},
net::{NetworkState, NetworkUsers},
settings::NetSettings,
Expand Down Expand Up @@ -85,8 +83,8 @@ impl Default for Channel {
#[derive(Clone, Debug, Tree)]
pub struct App {
refclk: Leaf<f64>,
clk_sel: Leaf<ClkSel>,
div_sel: Leaf<DivSel>,
clk_sel: Leaf<urukul::ClkSel>,
div_sel: Leaf<urukul::DivSel>,
update: Leaf<bool>,
ch: [Channel; 4],
}
Expand All @@ -95,8 +93,8 @@ impl Default for App {
fn default() -> Self {
let ch = Channel::default();
Self {
clk_sel: ClkSel::Osc.into(),
div_sel: DivSel::One.into(),
clk_sel: urukul::ClkSel::Osc.into(),
div_sel: urukul::DivSel::One.into(),
update: true.into(),
refclk: 100.0e6.into(),
ch: [ch.clone(), ch.clone(), ch.clone(), ch.clone()],
Expand Down
3 changes: 0 additions & 3 deletions src/hardware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
pub use embedded_hal_02;
pub use stm32h7xx_hal as hal;

pub mod ad9912;
pub mod adc;
pub mod afe;
pub mod cpu_temp_sensor;
pub mod dac;
pub mod decoded_cs;
pub mod delay;
pub mod design_parameters;
pub mod eem;
Expand All @@ -22,7 +20,6 @@ pub mod setup;
pub mod shared_adc;
pub mod signal_generator;
pub mod timers;
pub mod urukul;

// Type alias for the analog front-end (AFE) for ADC0.
pub type AFE0 = afe::ProgrammableGainAmplifier<
Expand Down
2 changes: 1 addition & 1 deletion src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{
adc, afe, cpu_temp_sensor::CpuTempSensor, dac, delay, design_parameters,
eeprom, input_stamper::InputStamper, metadata::ApplicationMetadata,
platform, pounder, pounder::dds_output::DdsOutput, shared_adc::SharedAdc,
timers, urukul, DigitalInput0, DigitalInput1, Eem, EthernetPhy, Gpio,
timers, DigitalInput0, DigitalInput1, Eem, EthernetPhy, Gpio,
HardwareVersion, NetworkStack, SerialTerminal, SystemTimer, Systick,
UsbDevice, AFE0, AFE1,
};
Expand Down
24 changes: 24 additions & 0 deletions urukul/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "urukul"
version = "0.1.0"
edition = "2021"
description = "Sinara Urukul quad 1 GHz DDS driver"
authors = [
"Robert Jördens <[email protected]>",
]
categories = ["embedded", "no-std"]
license = "MIT OR Apache-2.0"
keywords = ["spi"]
repository = "https://github.com/quartiq/stabilizer"

[dependencies]
encoded_pin = { version = "0.1", path = "../encoded_pin" }
arbitrary-int = { version = "1.2.7", features = ["serde"] }
thiserror = { version = "2.0.3", default-features = false }
num-traits = { version = "0.2.19", default-features = false }
embedded-hal = { version = "1.0" }
embedded-hal-bus = "0.2.0"
bitbybit = "1.3.2"
ad9912 = { version = "0.1", path = "../ad9912" }
log = "0.4"
serde = { version = "1.0", features = ["derive"], default-features = false }
28 changes: 14 additions & 14 deletions src/hardware/urukul.rs → urukul/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::hardware::ad9912;
#![no_std]

use arbitrary_int::{u2, u24, u3, u4, u7};
use bitbybit::{bitenum, bitfield};
use embedded_hal_1::digital::OutputPin;
use embedded_hal_1::spi::{self, SpiBus, SpiDevice};
use embedded_hal::digital::OutputPin;
use embedded_hal::spi::{self, SpiBus, SpiDevice};
use embedded_hal_bus::spi::{DeviceError, NoDelay, RefCellDevice};
use log;
use num_traits::Float;
use num_traits::float::FloatCore;
use serde::{Deserialize, Serialize};

use super::ad9912::Ad9912;
use super::decoded_cs::DecodedCs;
use ad9912::Ad9912;
use encoded_pin::EncodedPin;

#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum Error {
Expand All @@ -18,7 +18,7 @@ pub enum Error {
#[error("SPI Error {0}")]
Spi(spi::ErrorKind),
#[error("DDS")]
Dds(#[from] ad9912::Error),
Dds(#[source] ad9912::Error),
}

impl<E: spi::Error> From<E> for Error {
Expand Down Expand Up @@ -101,13 +101,13 @@ pub fn att_to_mu(att: f32) -> u8 {
}

pub struct Urukul<'a, B, P> {
att_spi: RefCellDevice<'a, B, DecodedCs<'a, P, 3>, NoDelay>,
cfg_spi: RefCellDevice<'a, B, DecodedCs<'a, P, 3>, NoDelay>,
att_spi: RefCellDevice<'a, B, EncodedPin<'a, P, 3>, NoDelay>,
cfg_spi: RefCellDevice<'a, B, EncodedPin<'a, P, 3>, NoDelay>,
io_update: P,
_sync: P,
cfg: Cfg,
att: [u8; 4],
dds: [Ad9912<RefCellDevice<'a, B, DecodedCs<'a, P, 3>, NoDelay>>; 4],
dds: [Ad9912<RefCellDevice<'a, B, EncodedPin<'a, P, 3>, NoDelay>>; 4],
}

impl<'a, B: SpiBus<u8>, P: OutputPin> Urukul<'a, B, P> {
Expand All @@ -118,7 +118,7 @@ impl<'a, B: SpiBus<u8>, P: OutputPin> Urukul<'a, B, P> {
sync: P,
) -> Result<Self, Error> {
let sel = |sel| {
RefCellDevice::new(spi, DecodedCs::new(cs, u3::new(sel)), NoDelay)
RefCellDevice::new(spi, EncodedPin::new(cs, u3::new(sel)), NoDelay)
.unwrap()
};
let cfg_spi = sel(1);
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<'a, B: SpiBus<u8>, P: OutputPin> Urukul<'a, B, P> {
self.att_spi.write(&self.att)?;

for dds in self.dds.iter_mut() {
dds.init()?;
dds.init().map_err(Error::Dds)?;
}

log::info!("Urukul initialized");
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<'a, B: SpiBus<u8>, P: OutputPin> Urukul<'a, B, P> {
pub fn dds(
&mut self,
ch: u2,
) -> &mut Ad9912<RefCellDevice<'a, B, DecodedCs<'a, P, 3>, NoDelay>> {
) -> &mut Ad9912<RefCellDevice<'a, B, EncodedPin<'a, P, 3>, NoDelay>> {
&mut self.dds[ch.value() as usize]
}
}

0 comments on commit 45cbf4f

Please sign in to comment.