Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Use `write` instead of `modify` to clear flags [#829]
- Bump `stm32f4-staging` to 0.18, update other dependencies [#831]
- `serial` mod refactor [#833] [#839]
- Add "capture" support for timers [#830]
- FMPI2c APB timings [#770]
- Fefactor FMPI2c `embedded-hal` implementations [#784]
- remove `NoPin`, use `Option` instead [#813]
Expand All @@ -23,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#784]: https://github.com/stm32-rs/stm32f4xx-hal/pull/784
[#813]: https://github.com/stm32-rs/stm32f4xx-hal/pull/813
[#829]: https://github.com/stm32-rs/stm32f4xx-hal/pull/829
[#830]: https://github.com/stm32-rs/stm32f4xx-hal/pull/830
[#831]: https://github.com/stm32-rs/stm32f4xx-hal/pull/831
[#832]: https://github.com/stm32-rs/stm32f4xx-hal/pull/832
[#833]: https://github.com/stm32-rs/stm32f4xx-hal/pull/833
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,7 @@ required-features = []
[[example]]
name = "fmc-sdram"
required-features = ["stm32f469", "stm32-fmc"]

[[example]]
name = "rtic2-timer-input-capture"
required-features = ["rtic2"]
78 changes: 78 additions & 0 deletions examples/rtic2-timer-input-capture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#![no_main]
#![no_std]

use defmt_rtt as _;
use panic_probe as _;
use stm32f4xx_hal::{
pac,
pac::{TIM2, TIM5},
prelude::*,
rcc,
timer::{CaptureChannel, CaptureHzManager, CapturePolarity, Event, Flag, PwmChannel, Timer},
};

use rtic::app;

#[app(device = pac, dispatchers = [USART1], peripherals = true)]
mod app {
use super::*;

#[shared]
struct Shared {}

#[local]
struct Local {
tim5: CaptureHzManager<TIM5>,
ch1: CaptureChannel<TIM5, 0>,
}

#[init]
fn init(ctx: init::Context) -> (Shared, Local) {
let dp = ctx.device;
let mut rcc = dp.RCC.freeze(rcc::Config::hsi().sysclk(48.MHz()));
let gpioa = dp.GPIOA.split(&mut rcc);

// Configuration of TIM2 in PWM mode
let timer = Timer::new(dp.TIM2, &mut rcc);
let (_, (ch1, ..)) = timer.pwm_hz(893.Hz());
let mut tim_2: PwmChannel<TIM2, 0> = ch1.with(gpioa.pa5);
tim_2.set_duty(50);
tim_2.enable();

// It is necessary to connect pins PA0 and PA5 through a resistor of 1 kΩ - 10 kΩ

// Configuration of TIM5 in input capture mode
let (mut tim5, (ch1, ..)) = Timer::new(dp.TIM5, &mut rcc).capture_hz(48.MHz());
let mut ch1 = ch1.with(gpioa.pa0);
tim5.listen(Event::C1);

ch1.set_polarity(CapturePolarity::ActiveHigh);
ch1.enable();

defmt::info!("Start");

(Shared {}, Local { tim5, ch1 })
}

#[task(binds = TIM5, local = [tim5, ch1, prev_capture: u32 = 0], priority = 3)]
fn tim5_interrupt(cx: tim5_interrupt::Context) {
if cx.local.tim5.flags().contains(Flag::C1) {
let timer_clock = cx.local.tim5.get_timer_clock();
let max_auto_reload = cx.local.tim5.get_max_auto_reload();
let current_capture = cx.local.ch1.get_capture();

let delta = if current_capture >= *cx.local.prev_capture {
current_capture - *cx.local.prev_capture
} else {
(max_auto_reload - *cx.local.prev_capture) + current_capture
};

let freq = timer_clock as f32 / delta as f32;

defmt::info!("Freq: {} Hz", freq); // Output = Freq: 893.00665 Hz

*cx.local.prev_capture = current_capture;
cx.local.tim5.clear_flags(Flag::C1);
}
}
}
2 changes: 1 addition & 1 deletion src/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait FlashExt {
}
/// Unlock flash for erasing/programming until this method's
/// result is dropped
fn unlocked(&mut self) -> UnlockedFlash;
fn unlocked(&mut self) -> UnlockedFlash<'_>;
// Returns true if flash is in dual bank organization
fn dual_bank(&self) -> bool;
/// Returns flash memory sector of a given offset. Returns none if offset is out of range.
Expand Down
2 changes: 1 addition & 1 deletion src/pacext/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod reg {
fn psc(r: &R<Self>) -> usart1::gtpr::PSC_R;
}
pub trait GtprW: RegisterSpec<Ux = u16> + Writable + Resettable + Sized {
fn psc(w: &mut W<Self>) -> usart1::gtpr::PSC_W<Self>;
fn psc(w: &mut W<Self>) -> usart1::gtpr::PSC_W<'_, Self>;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use core::fmt;
use core::marker::PhantomData;
use enumflags2::BitFlags;

#[allow(unused)]
use crate::pacext::uart::UartRB;
mod hal_02;
mod hal_1;
Expand Down
9 changes: 6 additions & 3 deletions src/serial/hal_02.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod nb {

use super::super::{Error, Instance, RegisterBlockImpl, Rx, Serial, Tx};
#[allow(unused)]
use super::super::RegisterBlockImpl;
use super::super::{Error, Instance, Rx, Serial, Tx};
use embedded_hal_02::serial::{Read, Write};

impl<USART: Instance, WORD> Read<WORD> for Serial<USART, WORD>
Expand Down Expand Up @@ -82,7 +83,9 @@ mod nb {
mod blocking {
use core::ops::Deref;

use super::super::{Error, Instance, RegisterBlockImpl, Serial, Tx};
#[allow(unused)]
use super::super::RegisterBlockImpl;
use super::super::{Error, Instance, Serial, Tx};
use embedded_hal_02::blocking::serial::Write;

impl<USART: Instance> Write<u8> for Tx<USART, u8> {
Expand Down
8 changes: 6 additions & 2 deletions src/serial/hal_1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod nb {
use super::super::{Error, Instance, RegisterBlockImpl, Rx, Serial, Tx};
#[allow(unused)]
use super::super::RegisterBlockImpl;
use super::super::{Error, Instance, Rx, Serial, Tx};
use embedded_hal_nb::serial::{ErrorKind, Read, Write};

impl embedded_hal_nb::serial::Error for Error {
Expand Down Expand Up @@ -89,7 +91,9 @@ mod nb {
}

mod io {
use super::super::{Error, Instance, RegisterBlockImpl, Rx, Serial, Tx};
#[allow(unused)]
use super::super::RegisterBlockImpl;
use super::super::{Error, Instance, Rx, Serial, Tx};
use embedded_io::Write;

impl embedded_io::Error for Error {
Expand Down
Loading
Loading