From a29ffdccad147ccb21c6da117f80d3fa66172f68 Mon Sep 17 00:00:00 2001 From: Gaute Hope Date: Fri, 5 Apr 2024 10:30:20 +0200 Subject: [PATCH] try to use uart --- sfy-buoy/Cargo.lock | 2 ++ sfy-buoy/src/temp.rs | 37 ++++++++++++++--------- sfy-buoy/target-test/Cargo.toml | 1 + sfy-buoy/target-test/tests/temp_sensor.rs | 24 +++++++++++++-- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/sfy-buoy/Cargo.lock b/sfy-buoy/Cargo.lock index b2e1b553..c913f7f8 100644 --- a/sfy-buoy/Cargo.lock +++ b/sfy-buoy/Cargo.lock @@ -957,6 +957,7 @@ dependencies = [ "ambiq-hal", "defmt", "embedded-hal", + "nb 1.1.0", ] [[package]] @@ -1402,6 +1403,7 @@ dependencies = [ "heapless", "ism330dhcx", "micromath", + "one-wire-bus", "panic-probe", "serde", "serde-json-core", diff --git a/sfy-buoy/src/temp.rs b/sfy-buoy/src/temp.rs index c0bcccdb..337d5b11 100644 --- a/sfy-buoy/src/temp.rs +++ b/sfy-buoy/src/temp.rs @@ -4,8 +4,9 @@ use core::marker::PhantomData; use embedded_hal::blocking::delay::{DelayMs, DelayUs}; use embedded_hal::digital::v2::{InputPin, OutputPin}; +use embedded_hal::serial::{Read, Write}; use heapless::Vec; -use one_wire_bus::{Address, OneWire, OneWireError}; +use one_wire_bus::{Address, BaudRate, OneWire, OneWireError}; use crate::waves::wire::ScaledF32; @@ -18,11 +19,9 @@ enum TempsState { Failed(i64), // failure, with timestamp (millis) since failure } -type E = Infallible; - -pub struct Temps { - wire: OneWire, - probes: Vec, +pub struct Temps { + wire: OneWire, + pub probes: Vec, resolution: ds18b20::Resolution, state: TempsState, } @@ -32,13 +31,20 @@ pub struct Probe { pub sensor: ds18b20::Ds18b20, } -impl Temps { +impl Temps +where + U: Read + Write, +{ /// Scan for devices, init and set up logging. /// /// Can be re-run to reset. - pub fn new(w: one_wire_bus::TI, delay: &mut impl DelayUs) -> Result> { + pub fn new( + w: U, + set_baudrate: fn(&mut U, BaudRate) -> (), + delay: &mut impl DelayUs, + ) -> Result, OneWireError> { defmt::info!("setting up temperature sensors.."); - let mut wire = OneWire::new(w)?; + let mut wire = OneWire::new(w, set_baudrate)?; let resolution = ds18b20::Resolution::Bits12; let mut addresses = heapless::Vec::<_, MAX_PROBES>::new(); @@ -77,8 +83,8 @@ impl Temps { for addr in addresses { probes .push(Probe::new(addr, &mut wire, resolution, delay)?) - .map_err(|_| ()) // ds18b20 doesn't impl Debug - .unwrap(); // already checked size + .ok(); + // .unwrap(); // already checked size } Ok(Temps { @@ -155,12 +161,15 @@ impl Temps { } impl Probe { - pub fn new( + pub fn new( address: Address, - wire: &mut OneWire, + wire: &mut OneWire, resolution: ds18b20::Resolution, delay: &mut impl DelayUs, - ) -> Result> { + ) -> Result> + where + U: Read + Write, + { let sensor = ds18b20::Ds18b20::new(address)?; // configure diff --git a/sfy-buoy/target-test/Cargo.toml b/sfy-buoy/target-test/Cargo.toml index 146887a0..775825d4 100644 --- a/sfy-buoy/target-test/Cargo.toml +++ b/sfy-buoy/target-test/Cargo.toml @@ -59,6 +59,7 @@ embedded-hal = "0.2.6" cmsis_dsp = { version = "0.1.0", features = [ "micromath" ] } embedded-sdmmc = { version = "0.6.0", default-features = false, features = ["defmt-log"] } micromath = "2.1.0" +one-wire-bus = "*" [dependencies.ambiq-hal] version = "0.3" diff --git a/sfy-buoy/target-test/tests/temp_sensor.rs b/sfy-buoy/target-test/tests/temp_sensor.rs index 209d82ce..e31e7dc0 100644 --- a/sfy-buoy/target-test/tests/temp_sensor.rs +++ b/sfy-buoy/target-test/tests/temp_sensor.rs @@ -10,12 +10,13 @@ use panic_probe as _; // memory layout + panic handler use embedded_hal::{ blocking::delay::DelayUs, blocking::spi::{write::Default as DefaultWrite, Transfer}, - digital::v2::{OutputPin, InputPin}, + digital::v2::{InputPin, OutputPin}, spi::FullDuplex, }; use embedded_sdmmc::{ Error as GenericSdMmcError, Mode, SdCard, SdCardError, VolumeIdx, VolumeManager, }; +use one_wire_bus::BaudRate; use sfy::storage::{self, Storage}; pub static COUNT: AtomicI32 = AtomicI32::new(0); @@ -123,6 +124,11 @@ mod tests { let mut led = pins.d19.into_push_pull_output(); + let tx = pins.a16; + let rx = pins.a0; + + let uart = hal::uart::new_12_13(dp.UART1, tx, rx, 115200); + // pin D8 (on artemis nano) let mut tp = pins.d8.into_input(); // let mut tp = pins.d10.into_input_output(); @@ -158,7 +164,20 @@ mod tests { defmt::info!("setting up dsb driver"); defmt::flush(); delay.delay_ms(1000_u32); - let temp = sfy::temp::Temps::new(tp, &mut delay).unwrap(); + let temp = sfy::temp::Temps::new( + uart, + |uart, br| match br { + BaudRate::B9600 => { + uart.set_baudrate(9600); + } + BaudRate::B115200 => { + uart.set_baudrate(115200); + } + }, + &mut delay, + ).unwrap(); + + defmt::info!("devices: {}", temp.probes.len()); for _ in 0..1000 { defmt::info!("loop"); @@ -170,4 +189,3 @@ mod tests { } } } -