diff --git a/.cargo/config.toml b/.cargo/config.toml index 4f6377c..f330c4e 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -5,7 +5,7 @@ runner = 'probe-rs run --chip STM32F405RGTx' target = "thumbv7em-none-eabi" [env] -DEFMT_LOG = "trace" +DEFMT_LOG = "info" [unstable] build-std-features = ["panic_immediate_abort"] diff --git a/.github/workflows/rust-ci.yml b/.github/workflows/rust-ci.yml index 66d2e18..6bfd98e 100644 --- a/.github/workflows/rust-ci.yml +++ b/.github/workflows/rust-ci.yml @@ -1,29 +1,31 @@ name: Rust CI on: - push: - branches: - - main - - develop - pull_request: - branches: - - main - - develop + push: + branches: + - main + - develop + pull_request: + branches: + - main + - develop jobs: - build: - runs-on: ubuntu-latest + build: + runs-on: ubuntu-latest - steps: - - name: Setup Rust - uses: actions/checkout@v3 - with: - submodules: recursive - - name: Build - run: cargo build --verbose --workspace --all --all-features - # - name: Test - # run: cargo test --verbose - - name: Clippy - run: cargo clippy --all-features --all --workspace - - name: Format - run: cargo fmt --verbose --all --check + steps: + - name: Clone repo + uses: actions/checkout@v6 + with: + submodules: recursive + - name: Setup Rust + uses: actions-rust-lang/setup-rust-toolchain@v1 + - name: Build + run: cargo build --verbose --workspace --all --all-features + # - name: Test + # run: cargo test --verbose + - name: Clippy + run: cargo clippy --all-features --all --workspace + - name: Format + run: cargo fmt --verbose --all --check diff --git a/Cargo.toml b/Cargo.toml index 65c0a62..ede142e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,41 +1,30 @@ [workspace] members = ["msb-fw-rs","crates/*"] -resolver = "2" +resolver = "3" [workspace.dependencies] -embassy-stm32 = { version = "0.1.0", features = ["defmt", "stm32f405rg", "unstable-pac", "memory-x", "time", "time-driver-any", "exti"] } -embassy-sync = { version = "0.6.1", features = ["defmt"] } -embassy-executor = { version = "0.7.0", features = ["nightly", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } -embassy-time = { version = "0.4.0", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } -embassy-embedded-hal = { version = "0.2.0", features = ["defmt"] } -embassy-futures = { version = "0.1.1", features = ["defmt"] } - -defmt = "0.3.10" -defmt-rtt = "0.4.1" - -cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] } -cortex-m-rt = "0.7.5" -embedded-hal = { version = "1.0.0", features = ["defmt-03"] } -embedded-hal-bus = { version = "0.2", features = ["async", "defmt-03"] } -embedded-io = { version = "0.6.1", features = ["defmt-03"] } -embedded-io-async = { version = "0.6.1", features = ["defmt-03"] } -panic-probe = { version = "0.3.2", features = ["print-defmt", "defmt-error"] } -heapless = { version = "0.8.0", default-features = false } -static_cell = "2.1.0" +embassy-stm32 = { version = "0.6.0", features = ["defmt", "stm32f405rg", "unstable-pac", "memory-x", "time", "time-driver-any", "exti"] } +embassy-sync = { version = "0.8.0", features = ["defmt"] } +embassy-executor = { version = "0.10.0", features = ["executor-thread", "executor-interrupt", "defmt", "embassy-time-driver", "platform-cortex-m"] } +embassy-time = { version = "0.5.1", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } +embassy-embedded-hal = { version = "0.6.0", features = ["defmt", "time"] } +embassy-futures = { version = "0.1.2", features = ["defmt"] } + +defmt = "1" +defmt-rtt = "1" + +cortex-m = { version = "0.7", features = ["inline-asm", "critical-section-single-core"] } +cortex-m-rt = "0.7" +embedded-hal = { version = "1", features = ["defmt-03"] } +embedded-hal-bus = { version = "0.3", features = ["async", "defmt-03"] } +embedded-io = { version = "0.7", features = ["defmt"] } +embedded-io-async = { version = "0.7", features = ["defmt"] } +panic-probe = { version = "1", features = ["print-defmt", "defmt-error"] } +heapless = { version = "0.9", default-features = false } +static_cell = "2.1" #chrono = { version = "^0.4", default-features = false} -embedded-hal-async = {version = "1.0.0", features = ["defmt-03"] } -bitfield = "^0.17.0" - - -[patch.crates-io] -embassy-stm32 = {git = "https://github.com/embassy-rs/embassy", rev = "b127ca9844a7e4e9079800be81d173e2725066c6"} -embassy-sync = {git = "https://github.com/embassy-rs/embassy", rev = "b127ca9844a7e4e9079800be81d173e2725066c6"} -embassy-executor = {git = "https://github.com/embassy-rs/embassy", rev = "b127ca9844a7e4e9079800be81d173e2725066c6"} -embassy-time = {git = "https://github.com/embassy-rs/embassy", rev = "b127ca9844a7e4e9079800be81d173e2725066c6"} -embassy-embedded-hal = {git = "https://github.com/embassy-rs/embassy", rev = "b127ca9844a7e4e9079800be81d173e2725066c6"} -embassy-futures = {git = "https://github.com/embassy-rs/embassy", rev = "b127ca9844a7e4e9079800be81d173e2725066c6"} - - +embedded-hal-async = {version = "1", features = ["defmt-03"] } +bitfield = "0.19" [profile.release] opt-level = "s" diff --git a/README.md b/README.md index f9c114f..eb8c703 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,13 @@ To lint and check stuff, run `cargo clippy`. ### On car stuff To run a RTT terminal dedicated: -`cargo embed --release --config rtt` +`cargo embed --release rtt` To run a GDB terminal dedicated: -`cargo embed --release --config gdb` +`cargo embed --release gdb` To flash and leave code: -`cargo embed --release --config ` +`cargo embed --release` @@ -56,3 +56,14 @@ This structure has multiple benefits, including: - Shared `target` folder meaning a shared build cache for quicker and space-saving builds - Other quirks, such as vscode `settings.json` and `config.toml`, are shared between projects + +### Versioning + +Updating versions is as follows + +1. Update the rust-toolchain to the version found in embassy repo +2. Update all embassy versions to the versions found in the embassy repo, use x.y.z, in main Cargo.toml +3. Update all features, especially ones that say the version in them (ex. "defmt-03") +3. Update major package versions of other projects, use x.y, in main Cargo.toml +4. Update package versions of other projects, in individual Cargo.toml +4. Fix any build issues diff --git a/crates/lsm6dso-ner/Cargo.toml b/crates/lsm6dso-ner/Cargo.toml index efd0c83..721d381 100644 --- a/crates/lsm6dso-ner/Cargo.toml +++ b/crates/lsm6dso-ner/Cargo.toml @@ -1,10 +1,8 @@ [package] name = "lsm6dso-ner" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] arrayref = "0.3.8" embedded-hal-async.workspace = true - - diff --git a/crates/pca9539-ner/Cargo.toml b/crates/pca9539-ner/Cargo.toml index 8664ac8..e5d4d52 100644 --- a/crates/pca9539-ner/Cargo.toml +++ b/crates/pca9539-ner/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "pca9539-ner" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] embedded-hal-async.workspace = true -embedded-hal.workspace = true \ No newline at end of file +embedded-hal.workspace = true diff --git a/crates/sht3x-ner/Cargo.toml b/crates/sht3x-ner/Cargo.toml index f59703d..a778a63 100644 --- a/crates/sht3x-ner/Cargo.toml +++ b/crates/sht3x-ner/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sht3x-ner" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] defmt.workspace = true diff --git a/crates/vl6180x-ner/Cargo.toml b/crates/vl6180x-ner/Cargo.toml index f58b446..27564bb 100644 --- a/crates/vl6180x-ner/Cargo.toml +++ b/crates/vl6180x-ner/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "vl6180x-ner" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] embedded-hal-async.workspace = true embedded-hal.workspace = true int-enum = {version = "1.1.2", default-features = false} -#int-enum-impl = "0.5.0" \ No newline at end of file +#int-enum-impl = "0.5.0" diff --git a/crates/vl6180x-ner/src/config.rs b/crates/vl6180x-ner/src/config.rs index 06fee7d..d5418e5 100644 --- a/crates/vl6180x-ner/src/config.rs +++ b/crates/vl6180x-ner/src/config.rs @@ -139,7 +139,7 @@ impl Config { pub fn set_range_inter_measurement_period(&mut self, time_ms: u16) -> Result<(), Error<()>> { let min_eq_val = ((self.range_max_convergence_time + 5) as f32 / 0.9) as u16; let min = if 10 < min_eq_val { min_eq_val } else { 10 }; - if time_ms % 10 != 0 || time_ms < min || time_ms > 2550 { + if !time_ms.is_multiple_of(10) || time_ms < min || time_ms > 2550 { return Err(Error::InvalidConfigurationValue(time_ms)); } self.range_inter_measurement_period = time_ms; @@ -257,7 +257,7 @@ impl Config { pub fn set_ambient_inter_measurement_period(&mut self, time_ms: u16) -> Result<(), Error<()>> { let min_eq_val = ((self.ambient_integration_period as f32 * 1.1) / 0.9) as u16; let min = if 10 < min_eq_val { min_eq_val } else { 10 }; - if time_ms % 10 != 0 || time_ms < min || time_ms > 2560 { + if !time_ms.is_multiple_of(10) || time_ms < min || time_ms > 2560 { return Err(Error::InvalidConfigurationValue(time_ms)); } self.ambient_inter_measurement_period = time_ms; diff --git a/crates/vl6180x-ner/src/device_status.rs b/crates/vl6180x-ner/src/device_status.rs index 20caf48..4bbbe4e 100644 --- a/crates/vl6180x-ner/src/device_status.rs +++ b/crates/vl6180x-ner/src/device_status.rs @@ -95,10 +95,10 @@ where async fn wait_device_booted(&mut self) -> Result<(), E> { loop { - if let Ok(result) = self.read_named_register(SYSTEM__FRESH_OUT_OF_RESET).await { - if result == 0x01 { - break; - } + if let Ok(result) = self.read_named_register(SYSTEM__FRESH_OUT_OF_RESET).await + && result == 0x01 + { + break; } } Ok(()) diff --git a/crates/vl6180x-ner/src/i2c_interface.rs b/crates/vl6180x-ner/src/i2c_interface.rs index 42982a3..917c3f9 100644 --- a/crates/vl6180x-ner/src/i2c_interface.rs +++ b/crates/vl6180x-ner/src/i2c_interface.rs @@ -1,7 +1,7 @@ use embedded_hal_async::i2c::I2c; use super::*; -use crate::register::{Register16Bit, Register8Bit}; +use crate::register::{Register8Bit, Register16Bit}; impl VL6180X where diff --git a/crates/vl6180x-ner/src/init.rs b/crates/vl6180x-ner/src/init.rs index c0b56ef..0029056 100644 --- a/crates/vl6180x-ner/src/init.rs +++ b/crates/vl6180x-ner/src/init.rs @@ -2,8 +2,8 @@ use embedded_hal_async::i2c::I2c; use super::VL6180X; use crate::register::{ - Register16Bit::*, Register8Bit::*, SysModeGpio1Polarity, SysModeGpio1Select, - AMBIENT_ANALOGUE_GAIN_CODE, RANGE_SCALAR_CODE, + AMBIENT_ANALOGUE_GAIN_CODE, RANGE_SCALAR_CODE, Register8Bit::*, Register16Bit::*, + SysModeGpio1Polarity, SysModeGpio1Select, }; impl VL6180X diff --git a/crates/vl6180x-ner/src/mode.rs b/crates/vl6180x-ner/src/mode.rs index 0331910..4eefb47 100644 --- a/crates/vl6180x-ner/src/mode.rs +++ b/crates/vl6180x-ner/src/mode.rs @@ -10,8 +10,8 @@ use embedded_hal_async::i2c::I2c; pub use powered_off::*; pub use ready::*; -use crate::error::Error; use crate::VL6180X; +use crate::error::Error; impl VL6180X where @@ -93,12 +93,12 @@ where /// /// Does not return the result. To get the measured value the host has the following options: /// 1. Check regularly to see if the result is ready with [`read_ambient_lux`](VL6180X::read_ambient_lux) - /// or [`read_ambient`](VL6180X::read_ambient) + /// or [`read_ambient`](VL6180X::read_ambient) /// 2. Call [`read_ambient_lux_blocking`](VL6180X::read_ambient_lux_blocking) or - /// [`read_ambient_blocking`](VL6180X::read_ambient_blocking) to have the driver - /// perform the regular checks in a blocking way. + /// [`read_ambient_blocking`](VL6180X::read_ambient_blocking) to have the driver + /// perform the regular checks in a blocking way. /// 3. Wait for the ambient interrupt to be triggered, indicating that the - /// new sample is ready, then call the methods listed in option 1. + /// new sample is ready, then call the methods listed in option 1. pub async fn start_ambient_single(&mut self) -> Result<(), Error> { self.start_ambient_single_direct().await?; Ok(()) @@ -115,9 +115,9 @@ where /// Does not return the result. To get the measured value the host has the following options: /// 1. Check regularly to see if the result is ready with [`read_range_mm()`](VL6180X::read_range_mm) /// 2. Call [`read_range_mm_blocking()`](VL6180X::read_range_mm_blocking) to have the driver - /// perform the regular checks in a blocking way. + /// perform the regular checks in a blocking way. /// 3. Wait for the range interrupt to be triggered, indicating that the - /// new sample is ready, then call [`read_range_mm()`](VL6180X::read_range_mm). + /// new sample is ready, then call [`read_range_mm()`](VL6180X::read_range_mm). pub async fn start_range_single(&mut self) -> Result<(), Error> { self.start_range_single_direct().await?; Ok(()) diff --git a/crates/vl6180x-ner/src/mode/continuous.rs b/crates/vl6180x-ner/src/mode/continuous.rs index 7394b7b..2a55bc4 100644 --- a/crates/vl6180x-ner/src/mode/continuous.rs +++ b/crates/vl6180x-ner/src/mode/continuous.rs @@ -1,6 +1,6 @@ use embedded_hal_async::i2c::I2c; -use crate::{error::Error, AllowCommunication, VL6180X}; +use crate::{AllowCommunication, VL6180X, error::Error}; use super::{AllowReadMeasurement, AllowStartAmbientSingle, AllowStartRangeSingle, ReadyMode}; diff --git a/crates/vl6180x-ner/src/mode/dynamic.rs b/crates/vl6180x-ner/src/mode/dynamic.rs index d08f311..8f09a4f 100644 --- a/crates/vl6180x-ner/src/mode/dynamic.rs +++ b/crates/vl6180x-ner/src/mode/dynamic.rs @@ -1,8 +1,8 @@ -use crate::error::{Error, Error2}; use crate::VL6180X; +use crate::error::{Error, Error2}; +use OperatingMode::*; use embedded_hal::digital::OutputPin; use embedded_hal_async::i2c::I2c; -use OperatingMode::*; /// A mode where the state is kept track of at runtime, instead of being /// encoded into the type. Thus allowing you to change the mode often, diff --git a/crates/vl6180x-ner/src/mode/powered_off.rs b/crates/vl6180x-ner/src/mode/powered_off.rs index 32a6da5..6965628 100644 --- a/crates/vl6180x-ner/src/mode/powered_off.rs +++ b/crates/vl6180x-ner/src/mode/powered_off.rs @@ -1,7 +1,7 @@ use embedded_hal::digital::OutputPin; use embedded_hal_async::i2c::I2c; -use crate::{error::Error2, VL6180X}; +use crate::{VL6180X, error::Error2}; use super::ReadyMode; diff --git a/crates/vl6180x-ner/src/mode/ready.rs b/crates/vl6180x-ner/src/mode/ready.rs index d5c095e..576c8cb 100644 --- a/crates/vl6180x-ner/src/mode/ready.rs +++ b/crates/vl6180x-ner/src/mode/ready.rs @@ -1,7 +1,7 @@ use embedded_hal_async::i2c::I2c; -use crate::{error::Error, Config}; use crate::{AllowCommunication, VL6180X}; +use crate::{Config, error::Error}; use super::{ AllowReadMeasurement, AllowStartAmbientSingle, AllowStartRangeSingle, AmbientContinuousMode, diff --git a/crates/vl6180x-ner/src/read_measurements.rs b/crates/vl6180x-ner/src/read_measurements.rs index 41f1a5b..a187dcc 100644 --- a/crates/vl6180x-ner/src/read_measurements.rs +++ b/crates/vl6180x-ner/src/read_measurements.rs @@ -3,12 +3,12 @@ use core::convert::TryFrom; use embedded_hal_async::i2c::I2c; use crate::{ + VL6180X, error::Error, register::{ - self, AmbientStatusErrorCode, RangeStatusErrorCode, Register16Bit, Register8Bit, + self, AmbientStatusErrorCode, RangeStatusErrorCode, Register8Bit, Register16Bit, ResultInterruptStatusGpioCode, }, - VL6180X, }; impl VL6180X diff --git a/crates/vl6180x-ner/src/start_stop_measurements.rs b/crates/vl6180x-ner/src/start_stop_measurements.rs index 9da0b93..c1e8c96 100644 --- a/crates/vl6180x-ner/src/start_stop_measurements.rs +++ b/crates/vl6180x-ner/src/start_stop_measurements.rs @@ -1,9 +1,9 @@ use embedded_hal_async::i2c::I2c; use crate::{ + VL6180X, error::Error, register::{InterleavedModeEnableCode, Register8Bit, SysAmbientStartCode, SysRangeStartCode}, - VL6180X, }; impl VL6180X diff --git a/msb-fw-rs/Cargo.toml b/msb-fw-rs/Cargo.toml index ec95cf3..e4b16aa 100644 --- a/msb-fw-rs/Cargo.toml +++ b/msb-fw-rs/Cargo.toml @@ -1,5 +1,5 @@ [package] -edition = "2021" +edition = "2024" name = "msb-fw-rs" version = "0.1.0" @@ -24,4 +24,4 @@ vl6180x-ner = { version = "0.1.0", path = "../crates/vl6180x-ner", optional = tr default = ["temp-sensor", "imu-sensor"] tof-sensor = ["dep:vl6180x-ner"] temp-sensor = ["dep:sht3x-ner"] -imu-sensor = ["dep:lsm6dso-ner"] \ No newline at end of file +imu-sensor = ["dep:lsm6dso-ner"] diff --git a/msb-fw-rs/src/controllers.rs b/msb-fw-rs/src/controllers.rs index 0af3044..f0eaddb 100644 --- a/msb-fw-rs/src/controllers.rs +++ b/msb-fw-rs/src/controllers.rs @@ -13,7 +13,7 @@ pub async fn control_leds( ) { let mut i = 0u8; loop { - if i % 8 == 0 { + if i.is_multiple_of(8) { match device_loc { DeviceLocation::FrontLeft => { led1.set_low(); diff --git a/msb-fw-rs/src/lib.rs b/msb-fw-rs/src/lib.rs index 4260f4d..8e31fd8 100644 --- a/msb-fw-rs/src/lib.rs +++ b/msb-fw-rs/src/lib.rs @@ -1,5 +1,4 @@ #![no_std] -#![feature(impl_trait_in_assoc_type)] // declare all files in this project except main pub mod can_handler; @@ -12,7 +11,7 @@ pub mod readers; // dont import anything in a lib.rs file, instead use fully resolved definitions pub type SharedI2c3 = embassy_sync::mutex::Mutex< embassy_sync::blocking_mutex::raw::NoopRawMutex, - embassy_stm32::i2c::I2c<'static, embassy_stm32::mode::Async>, + embassy_stm32::i2c::I2c<'static, embassy_stm32::mode::Async, embassy_stm32::i2c::Master>, >; #[derive(Clone, defmt::Format)] pub enum DeviceLocation { diff --git a/msb-fw-rs/src/main.rs b/msb-fw-rs/src/main.rs index 4f58149..6e0161f 100644 --- a/msb-fw-rs/src/main.rs +++ b/msb-fw-rs/src/main.rs @@ -1,33 +1,33 @@ #![no_std] #![no_main] -#![feature(impl_trait_in_assoc_type)] use core::fmt::Write; use cortex_m::{peripheral::SCB, singleton}; -use cortex_m_rt::{exception, ExceptionFrame}; -use defmt::{debug, info, unwrap}; +use cortex_m_rt::{ExceptionFrame, exception}; +use defmt::{debug, info, unwrap, warn}; use embassy_executor::Spawner; use embassy_stm32::{ - adc::{Adc, SampleTime, Sequence}, - bind_interrupts, - can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}, - i2c::{self, I2c}, - peripherals::CAN1, - time::Hertz, -}; -use embassy_stm32::{ + Config, + adc::{AdcChannel, CONTINUOUS}, can::Frame, + dma, gpio::{Input, Level, Output, Pull, Speed}, peripherals, usart::{self, Uart}, wdg::IndependentWatchdog, - Config, +}; +use embassy_stm32::{ + adc::{Adc, SampleTime}, + bind_interrupts, + can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}, + i2c::{self, I2c}, + peripherals::CAN1, }; use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, channel::Channel, mutex::Mutex}; use embassy_time::Timer; use heapless::String; -use msb_fw_rs::{can_handler, controllers, readers, DeviceLocation, SharedI2c3}; +use msb_fw_rs::{DeviceLocation, SharedI2c3, can_handler, controllers, readers}; use static_cell::StaticCell; use {defmt_rtt as _, panic_probe as _}; @@ -41,11 +41,19 @@ bind_interrupts!(struct IrqsCAN { bind_interrupts!(struct IrqsUsart { USART2 => usart::InterruptHandler; + DMA1_STREAM6 => dma::InterruptHandler; + DMA1_STREAM5 => dma::InterruptHandler; }); bind_interrupts!(struct IrqsI2c { I2C3_EV => i2c::EventInterruptHandler; I2C3_ER => i2c::ErrorInterruptHandler; + DMA1_STREAM4 => dma::InterruptHandler; + DMA1_STREAM2 => dma::InterruptHandler; +}); + +bind_interrupts!(struct IrqsAdc1 { + DMA2_STREAM0 => dma::InterruptHandler; }); // channels are like RTOS queues, with a limit. They are MPMC easy to pass around in threads. @@ -59,7 +67,8 @@ static CAN_CHANNEL: Channel = Channel::new(); async fn main(spawner: Spawner) -> ! { info!("Initializing MSB-FW..."); // initialize the project, ensure we can debug during sleep - let mut p = embassy_stm32::init(Config::default()); + let p = embassy_stm32::init(Default::default()); + warn!("TRACE {}", Config::default().enable_debug_during_sleep); // create some GPIO on input mode and read from them let pin0 = Input::new(p.PC10, Pull::None); @@ -78,17 +87,20 @@ async fn main(spawner: Spawner) -> ! { // create a thread to hold some LEDs and blink them or whatever let led1 = Output::new(p.PC4, Level::High, Speed::Low); let led2 = Output::new(p.PC5, Level::High, Speed::Low); - spawner.must_spawn(controllers::control_leds( - // note that most types have an internal generic holding the pin or bus itself, this can be removed by degrade - // this makes types more generic and should be done for all pins, but is not necessary for multi-bus i2c or whatnot - led1, - led2, - loc.clone(), - )); + spawner.spawn( + controllers::control_leds( + // note that most types have an internal generic holding the pin or bus itself, this can be removed by degrade + // this makes types more generic and should be done for all pins, but is not necessary for multi-bus i2c or whatnot + led1, + led2, + loc.clone(), + ) + .unwrap(), + ); // embassy enforces pin mappings to their correct functions for the most at compile time let can = Can::new(p.CAN1, p.PA11, p.PA12, IrqsCAN); // pass in a can channel consumer to get the frames from any producer - spawner.must_spawn(can_handler::can_handler(can, CAN_CHANNEL.receiver(), loc)); + spawner.spawn(can_handler::can_handler(can, CAN_CHANNEL.receiver(), loc).unwrap()); // checkout this fuckery, the official way to have two things use one i2c bus // see here: https://github.com/embassy-rs/embassy/blob/main/examples/rp/src/bin/shared_bus.rs @@ -98,41 +110,50 @@ async fn main(spawner: Spawner) -> ! { p.I2C3, p.PA8, p.PC9, - IrqsI2c, p.DMA1_CH4, // for must things embassy is DMA by default, allowing for bet use of the async executer. NoDma can be passed to disable that p.DMA1_CH2, - Hertz(100_000), + IrqsI2c, i2c::Config::default(), ); let i2c_bus = I2C_BUS.init(Mutex::new(i2c)); #[cfg(feature = "temp-sensor")] - spawner.must_spawn(readers::temperature_reader(i2c_bus, CAN_CHANNEL.sender())); + spawner.spawn(readers::temperature_reader(i2c_bus, CAN_CHANNEL.sender()).unwrap()); #[cfg(feature = "tof-sensor")] - spawner.must_spawn(readers::tof_reader(i2c_bus, CAN_CHANNEL.sender())); + spawner.spawn(readers::tof_reader(i2c_bus, CAN_CHANNEL.sender()).unwrap()); #[cfg(feature = "imu-sensor")] - spawner.must_spawn(readers::imu_reader(i2c_bus, CAN_CHANNEL.sender())); + spawner.spawn(readers::imu_reader(i2c_bus, CAN_CHANNEL.sender()).unwrap()); // this pretty much straight from docs, adc dma is very new in embassy stm32 hal const ADC_BUF_SIZE: usize = 1024; let adc1 = Adc::new(p.ADC1); let adc_data = singleton!(ADCDAT : [u16; ADC_BUF_SIZE] = [0u16; ADC_BUF_SIZE]) .expect("Could not init adc buffer"); - let mut adc1 = adc1.into_ring_buffered(p.DMA2_CH0, adc_data); - adc1.set_sample_sequence(Sequence::One, &mut p.PA0, SampleTime::CYCLES112); // SHOCKPOT - adc1.set_sample_sequence(Sequence::Two, &mut p.PA5, SampleTime::CYCLES112); // STRAIN 1 - adc1.set_sample_sequence(Sequence::Three, &mut p.PA6, SampleTime::CYCLES112); // STRAIN 2 - spawner.must_spawn(readers::adc1_reader(adc1, CAN_CHANNEL.sender())); + let adc1 = adc1.into_ring_buffered( + p.DMA2_CH0, + adc_data, + IrqsAdc1, + [ + (p.PA0.degrade_adc(), SampleTime::CYCLES112), // SHOCKPOT + (p.PA5.degrade_adc(), SampleTime::CYCLES112), // STRAIN 1 + (p.PA6.degrade_adc(), SampleTime::CYCLES112), + ] + .into_iter(), + CONTINUOUS, + embassy_stm32::adc::Exten::DISABLED, + ); + + spawner.spawn(readers::adc1_reader(adc1, CAN_CHANNEL.sender()).unwrap()); let mut usart = Uart::new( p.USART2, p.PA3, p.PA2, - IrqsUsart, p.DMA1_CH6, p.DMA1_CH5, + IrqsUsart, usart::Config::default(), ) .unwrap(); diff --git a/msb-fw-rs/src/readers.rs b/msb-fw-rs/src/readers.rs index 9122c25..026f106 100644 --- a/msb-fw-rs/src/readers.rs +++ b/msb-fw-rs/src/readers.rs @@ -22,7 +22,7 @@ pub async fn temperature_reader( const TEMPERATURE_SEND_MSG_ID: StandardId = StandardId::new(0x602).expect("Could not parse ID"); let i2c_dev = I2cDevice::new(i2c); - let mut sht30 = Sht3x::new(i2c_dev, sht3x_ner::Address::High); + let mut sht30 = Sht3x::new(i2c_dev, sht3x_ner::Address::Low); loop { Timer::after(TEMPERATURE_REFRESH_TIME).await; @@ -41,8 +41,7 @@ pub async fn temperature_reader( trace!( "Sending temp: {}, humidity {}", - res.temperature, - res.humidity + res.temperature, res.humidity ); let frame = Frame::new_data(TEMPERATURE_SEND_MSG_ID, &bits).expect("Could not create frame"); @@ -136,7 +135,7 @@ pub async fn tof_reader( } } -const ADC_REFRESH_TIME: Duration = Duration::from_millis(250); +const ADC_REFRESH_TIME: Duration = Duration::from_millis(1000); const STRAIN_SEND_MSG_ID: StandardId = StandardId::new(0x606).expect("Could not parse ID"); const SHOCKPOT_SEND_MSG_ID: StandardId = StandardId::new(0x605).expect("Could not parse ID"); @@ -149,28 +148,21 @@ pub async fn adc1_reader( let mut strain_bits: [u8; 4] = [0; 4]; loop { - match adc1.read(&mut measurements).await { - Ok(_) => { - adc1.teardown_adc(); - trace!("Sending strain + shockpot: {}", measurements); - // TODO transform measurements - strain_bits[0..2].copy_from_slice(&measurements[1].to_be_bytes()); - strain_bits[2..4].copy_from_slice(&measurements[2].to_be_bytes()); - can_send - .send(unwrap!(Frame::new_data( - STRAIN_SEND_MSG_ID, - &measurements[0].to_be_bytes() - ))) - .await; - can_send - .send(unwrap!(Frame::new_data(SHOCKPOT_SEND_MSG_ID, &strain_bits))) - .await; - } - Err(_) => { - warn!("DMA overrun"); - continue; - } - } + adc1.read_latest(&mut measurements); + trace!("Sending strain + shockpot: {}", measurements); + // TODO transform measurements + strain_bits[0..2].copy_from_slice(&measurements[1].to_be_bytes()); + strain_bits[2..4].copy_from_slice(&measurements[2].to_be_bytes()); + can_send + .send(unwrap!(Frame::new_data( + STRAIN_SEND_MSG_ID, + &measurements[0].to_be_bytes() + ))) + .await; + can_send + .send(unwrap!(Frame::new_data(SHOCKPOT_SEND_MSG_ID, &strain_bits))) + .await; + Timer::after(ADC_REFRESH_TIME).await; } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 412f855..87a0892 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,5 +1,5 @@ -# The channel key should match the nightly rust-toolchain.toml in the embassy repo +# The channel key should match the stable rust-toolchain.toml in the embassy repo [toolchain] -channel = "nightly-2024-12-10" -components = [ "rust-src", "rustfmt", "llvm-tools", "miri" ] +channel = "1.92" +components = [ "rust-src", "rustfmt", "llvm-tools", "clippy" ] targets = [ "thumbv7em-none-eabi" ]