diff options
| author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-10-16 19:33:17 +0100 | 
|---|---|---|
| committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-10-16 19:33:17 +0100 | 
| commit | cdf2a5026e45ac2b65001ed41cc67d86590a681a (patch) | |
| tree | 56fda9ee1cc0159e725952802547e5e6d327428e /src/main.rs | |
| download | scale-drv-example-master.tar.gz scale-drv-example-master.tar.xz scale-drv-example-master.zip | |
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 128 | 
1 files changed, 128 insertions, 0 deletions
| diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..dda8d22 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,128 @@ +// SPDX-FileCopyrightText: 2025 Tomasz Kramkowski <tomasz@kramkow.ski> +// SPDX-License-Identifier: GPL-3.0-or-later + +#![no_main] +#![no_std] + +mod dual_hx711; +mod pulse; + +use embassy_stm32::{bind_interrupts, peripherals, timer}; +use {defmt_rtt as _, panic_probe as _}; + +use rtic_monotonics::systick::prelude::*; + +systick_monotonic!(Mono); + +bind_interrupts!(struct Irqs { +    TIM3 => timer::UpdateInterruptHandler<peripherals::TIM3>; +}); + +#[rtic::app(device = embassy_stm32, peripherals = true, dispatchers = [SDIO, USART6])] +mod app { +    use crate::pulse::CountingMode; + +    use super::*; +    use embassy_stm32::{ +        exti::ExtiInput, +        gpio::{Level, OutputOpenDrain, OutputType, Pull, Speed}, +        time::*, +        timer::simple_pwm::PwmPin, +        Config, +    }; +    use pulse::Pulse; +    #[shared] +    struct Shared {} +    #[local] +    struct Local {} + +    #[init] +    fn init(cx: init::Context) -> (Shared, Local) { +        defmt::trace!("init"); + +        embassy_stm32::pac::FLASH.acr().modify(|w| { +            w.set_prften(true); +            w.set_icen(true); +            w.set_dcen(true) +        }); + +        let mut config = Config::default(); +        { +            use embassy_stm32::rcc::*; +            config.rcc.hsi = false; +            config.rcc.hse = Some(Hse { +                freq: mhz(25), +                mode: HseMode::Oscillator, +            }); +            config.rcc.pll_src = PllSource::HSE; +            config.rcc.pll = Some(Pll { +                prediv: PllPreDiv::DIV13, +                mul: PllMul::MUL104, +                divp: Some(PllPDiv::DIV2), +                divq: None, +                divr: None, +            }); +            config.rcc.ahb_pre = AHBPrescaler::DIV1; +            config.rcc.apb1_pre = APBPrescaler::DIV2; +            config.rcc.apb2_pre = APBPrescaler::DIV1; +            config.rcc.sys = Sysclk::PLL1_P; +        } + +        let p = embassy_stm32::init(config); + +        defmt::trace!("embassy_stm32::init done"); + +        Mono::start(cx.core.SYST, 100_000_000); + +        let led = OutputOpenDrain::new(p.PC13, Level::High, Speed::Medium); + +        blink::spawn(led).ok(); + +        // Create the clock pin +        let tim3_ch3_pin = PwmPin::new(p.PB0, OutputType::OpenDrain); +        let tim3_pulse = Pulse::new( +            p.TIM3, +            None, +            None, +            Some(tim3_ch3_pin), +            None, +            khz(500), +            CountingMode::EdgeAlignedUp, +            Irqs, +        ); + +        // Data pins +        let left_lc = ExtiInput::new(p.PB8, p.EXTI8, Pull::None); +        let right_lc = ExtiInput::new(p.PB9, p.EXTI9, Pull::None); + +        // Spawn test task +        test::spawn(tim3_pulse, left_lc, right_lc).ok(); + +        (Shared {}, Local {}) +    } + +    #[task()] +    async fn test( +        _cx: test::Context, +        clk: Pulse<'static, peripherals::TIM3>, +        a: ExtiInput<'static>, +        b: ExtiInput<'static>, +    ) { +        // Create driver from peripherals +        let mut scales = dual_hx711::DualHx711::new(clk, a, b); + +        // Keep reading forever +        loop { +            let (a, b) = scales.read().await.unwrap(); +            defmt::info!("scales read: {}, {}", a, b); +        } +    } + +    #[task()] +    async fn blink(_cx: blink::Context, mut led: OutputOpenDrain<'static>) { +        loop { +            led.toggle(); +            Mono::delay(1.secs()).await; +        } +    } +} | 
