Skip to content

Commit

Permalink
Cleaning up embeedded-io impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Nov 16, 2023
1 parent b811633 commit e9ff9c3
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 130 deletions.
59 changes: 42 additions & 17 deletions Cargo.lock

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

13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ members = ["ad9959", "serial-settings"]
[dependencies]
embedded-io = "0.5"
embedded-storage = "0.3"
menu = "0.3"
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
cortex-m-rt = { version = "0.7", features = ["device"] }
log = { version = "0.4", features = ["max_level_trace", "release_max_level_info"] }
Expand Down Expand Up @@ -65,8 +64,8 @@ rand_xorshift = "0.3.0"
rand_core = "0.6.4"
minimq = "0.8.0"
# patch with https://github.com/rust-embedded-community/usb-device/pull/129
usb-device = "0.2.9"
usbd-serial = "0.1.1"
usb-device = "0.3.0"
usbd-serial = "0.2"
# Keep this synced with the miniconf version in py/setup.py
miniconf = "0.9.0"
smoltcp-nal = { version = "0.4.1", features = ["shared-stack"]}
Expand All @@ -77,6 +76,14 @@ postcard = "1"
version = "0.15.1"
features = ["stm32h743v", "rt", "ethernet", "xspi", "usb_hs"]

[patch.crates-io.usbd-serial]
git = "https://github.com/rust-embedded-community/usbd-serial"
branch = "feature/embedded-io"

[patch.crates-io.synopsys-usb-otg]
git = "https://github.com/quartiq/synopsys-usb-otg"
branch = "feature/usb-device-bump"

[features]
nightly = [ ]
pounder_v1_0 = [ ]
Expand Down
6 changes: 3 additions & 3 deletions serial-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ edition = "2021"
[dependencies]
embedded-storage = "0.3"
miniconf = "0.9"
menu = {path = "../../../rust-embedded-community/menu"}
menu = "0.4"
cortex-m = "0.7"
heapless = "0.8"
postcard = "1"
embedded-io = "0.5"
serde = {version = "1", default-features=false}
embedded-io = "0.6"
serde = { version = "1", default-features=false }
21 changes: 17 additions & 4 deletions serial-settings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![no_std]
#![cfg_attr(not(test), no_std)]

use core::fmt::Write;
use embedded_storage::nor_flash::NorFlash;
Expand All @@ -16,7 +16,18 @@ pub trait Settings:
}

pub trait Interface:
embedded_io::Read + embedded_io::ReadReady + embedded_io::Write
embedded_io::Read
+ embedded_io::ReadReady
+ embedded_io::Write
+ embedded_io::WriteReady
{
}

impl<T> Interface for T where
T: embedded_io::Read
+ embedded_io::ReadReady
+ embedded_io::Write
+ embedded_io::WriteReady
{
}

Expand All @@ -43,7 +54,7 @@ impl<'a, I: Interface, S: Settings, Flash: NorFlash + 'static>
Context<'a, I, S, Flash>
{
fn save(&mut self) -> Result<(), Error<Flash::Error>> {
let serialized = postcard::to_slice(&self.settings, &mut self.buffer)?;
let serialized = postcard::to_slice(&self.settings, self.buffer)?;
self.flash
.erase(0, serialized.len() as u32)
.map_err(Error::Flash)?;
Expand Down Expand Up @@ -123,7 +134,9 @@ impl<'a, I: Interface, S: Settings, Flash: NorFlash + 'static> core::fmt::Write
/// # Note
/// The terminal uses an internal buffer. Overflows of the output buffer are silently ignored.
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.interface.write_all(s.as_bytes()).ok();
if let Ok(true) = self.interface.write_ready() {
self.interface.write_all(s.as_bytes()).ok();
}
Ok(())
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/bin/dual-iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use stabilizer::{
hal,
signal_generator::{self, SignalGenerator},
timers::SamplingTimer,
usb::UsbDevice,
DigitalInput0, DigitalInput1, SerialTerminal, SystemTimer, Systick,
AFE0, AFE1,
},
Expand Down Expand Up @@ -183,7 +184,7 @@ mod app {

#[shared]
struct Shared {
usb_terminal: SerialTerminal,
usb: UsbDevice,
network: NetworkUsers<Settings, Telemetry, 3>,

settings: Settings,
Expand All @@ -193,6 +194,7 @@ mod app {

#[local]
struct Local {
usb_terminal: SerialTerminal,
sampling_timer: SamplingTimer,
digital_inputs: (DigitalInput0, DigitalInput1),
afes: (AFE0, AFE1),
Expand Down Expand Up @@ -231,7 +233,7 @@ mod app {
let settings = Settings::default();

let shared = Shared {
usb_terminal: stabilizer.usb_serial,
usb: stabilizer.usb_device,
network,
settings,
telemetry: TelemetryBuffer::default(),
Expand All @@ -250,6 +252,7 @@ mod app {
};

let mut local = Local {
usb_terminal: stabilizer.usb_serial,
sampling_timer: stabilizer.adc_dac_timer,
digital_inputs: stabilizer.digital_inputs,
afes: stabilizer.afes,
Expand Down Expand Up @@ -393,7 +396,7 @@ mod app {
);
}

#[idle(shared=[network, usb_terminal])]
#[idle(shared=[network, usb])]
fn idle(mut c: idle::Context) -> ! {
loop {
match c.shared.network.lock(|net| net.update()) {
Expand All @@ -403,9 +406,7 @@ mod app {
NetworkState::Updated => {}
NetworkState::NoChange => {
// We can't sleep if USB is not in suspend.
if c.shared.usb_terminal.lock(|terminal| {
terminal.interface().usb_is_suspended()
}) {
if c.shared.usb.lock(|usb| usb.usb_is_suspended()) {
cortex_m::asm::wfi();
}
}
Expand Down Expand Up @@ -464,12 +465,11 @@ mod app {
.unwrap();
}

#[task(priority = 1, shared=[usb_terminal])]
#[task(priority = 1, shared=[usb], local=[usb_terminal])]
fn usb(mut c: usb::Context) {
// Handle the USB serial terminal.
c.shared.usb_terminal.lock(|usb| {
usb.interface_mut().process();
usb.process().ok();
c.shared.usb.lock(|usb| {
usb.process(c.local.usb_terminal);
});

// Schedule to run this task every 10 milliseconds.
Expand Down
20 changes: 10 additions & 10 deletions src/bin/lockin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use stabilizer::{
input_stamper::InputStamper,
signal_generator,
timers::SamplingTimer,
usb::UsbDevice,
DigitalInput0, DigitalInput1, SerialTerminal, SystemTimer, Systick,
AFE0, AFE1,
},
Expand Down Expand Up @@ -222,14 +223,15 @@ mod app {

#[shared]
struct Shared {
usb_terminal: SerialTerminal,
usb: UsbDevice,
network: NetworkUsers<Settings, Telemetry, 2>,
settings: Settings,
telemetry: TelemetryBuffer,
}

#[local]
struct Local {
usb_terminal: SerialTerminal,
sampling_timer: SamplingTimer,
digital_inputs: (DigitalInput0, DigitalInput1),
timestamper: InputStamper,
Expand Down Expand Up @@ -270,7 +272,7 @@ mod app {

let shared = Shared {
network,
usb_terminal: stabilizer.usb_serial,
usb: stabilizer.usb_device,
telemetry: TelemetryBuffer::default(),
settings: Settings::default(),
};
Expand All @@ -285,6 +287,7 @@ mod app {
};

let mut local = Local {
usb_terminal: stabilizer.usb_serial,
sampling_timer: stabilizer.adc_dac_timer,
digital_inputs: stabilizer.digital_inputs,
afes: stabilizer.afes,
Expand Down Expand Up @@ -455,7 +458,7 @@ mod app {
});
}

#[idle(shared=[network, usb_terminal])]
#[idle(shared=[network, usb])]
fn idle(mut c: idle::Context) -> ! {
loop {
match c.shared.network.lock(|net| net.update()) {
Expand All @@ -465,9 +468,7 @@ mod app {
NetworkState::Updated => {}
NetworkState::NoChange => {
// We can't sleep if USB is not in suspend.
if c.shared.usb_terminal.lock(|terminal| {
terminal.interface().usb_is_suspended()
}) {
if c.shared.usb.lock(|usb| usb.usb_is_suspended()) {
cortex_m::asm::wfi();
}
}
Expand Down Expand Up @@ -515,12 +516,11 @@ mod app {
.unwrap();
}

#[task(priority = 1, shared=[usb_terminal])]
#[task(priority = 1, shared=[usb], local=[usb_terminal])]
fn usb(mut c: usb::Context) {
// Handle the USB serial terminal.
c.shared.usb_terminal.lock(|usb| {
usb.interface_mut().process();
usb.process().ok();
c.shared.usb.lock(|usb| {
usb.process(c.local.usb_terminal);
});

// Schedule to run this task every 10 milliseconds.
Expand Down
4 changes: 2 additions & 2 deletions src/hardware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub mod design_parameters;
pub mod flash;
pub mod input_stamper;
pub mod pounder;
pub mod serial_terminal;
pub mod setup;
pub mod shared_adc;
pub mod signal_generator;
pub mod timers;
pub mod usb;

mod eeprom;

Expand Down Expand Up @@ -83,7 +83,7 @@ pub type I2c1Proxy =

pub type SerialTerminal = serial_settings::SerialSettings<
'static,
serial_terminal::SerialInterface,
usbd_serial::SerialPort<'static, UsbBus>,
flash::Settings,
flash::Flash,
>;
Expand Down
Loading

0 comments on commit e9ff9c3

Please sign in to comment.