summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: dda8d229e71baa25e5cc5e90af355a8e54b073cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
        }
    }
}