aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32h7/src/bin/low_level_timer_api.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-07-23 13:15:17 +0000
committerGitHub <[email protected]>2022-07-23 13:15:17 +0000
commite61e36a8158073ec92df1a751cfdd7fce5004cf8 (patch)
tree4a54aee47c0d3881b9e0bc809e075728cee8eeae /examples/stm32h7/src/bin/low_level_timer_api.rs
parent96f67671677d8bf7175f98e87c98954dc728286c (diff)
parent709df0dc1dfff577fb79bbc2f67ea84670072456 (diff)
Merge #842
842: WIP: Make unborrow safe to use r=Dirbaio a=GrantM11235 The basic idea is that `Unborrow::unborrow` is now safe to call and returns an `Unborrowed<'a, T>` which impls `Deref` and `DerefMut` ```rust /// This is essentially a `&mut T`, but it is the size of `T` not the size /// of a pointer. This is useful if T is a zero sized type. pub struct Unborrowed<'a, T> { inner: T, _lifetime: PhantomData<&'a mut T>, } ``` ## Todo - [x] Update other crates - [x] embassy-cortex-m - [x] embassy-hal-common - [x] embassy-lora - [x] embassy-nrf - [x] embassy-rp - [x] embassy-stm32 - [x] Remove usage of the unsafe `into_inner` method if possible - [x] Review and amend docs for `Unborrow` and `Unborrowed` Co-authored-by: Grant Miller <[email protected]> Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'examples/stm32h7/src/bin/low_level_timer_api.rs')
-rw-r--r--examples/stm32h7/src/bin/low_level_timer_api.rs24
1 files changed, 9 insertions, 15 deletions
diff --git a/examples/stm32h7/src/bin/low_level_timer_api.rs b/examples/stm32h7/src/bin/low_level_timer_api.rs
index fc19d84e4..d7c6da5bd 100644
--- a/examples/stm32h7/src/bin/low_level_timer_api.rs
+++ b/examples/stm32h7/src/bin/low_level_timer_api.rs
@@ -2,8 +2,6 @@
2#![no_main] 2#![no_main]
3#![feature(type_alias_impl_trait)] 3#![feature(type_alias_impl_trait)]
4 4
5use core::marker::PhantomData;
6
7use defmt::*; 5use defmt::*;
8use embassy::executor::Spawner; 6use embassy::executor::Spawner;
9use embassy::time::{Duration, Timer}; 7use embassy::time::{Duration, Timer};
@@ -11,7 +9,7 @@ use embassy_stm32::gpio::low_level::AFType;
11use embassy_stm32::gpio::Speed; 9use embassy_stm32::gpio::Speed;
12use embassy_stm32::pwm::*; 10use embassy_stm32::pwm::*;
13use embassy_stm32::time::{khz, mhz, Hertz}; 11use embassy_stm32::time::{khz, mhz, Hertz};
14use embassy_stm32::{unborrow, Config, Peripherals, Unborrow}; 12use embassy_stm32::{into_ref, Config, Peripheral, PeripheralRef, Peripherals};
15use {defmt_rtt as _, panic_probe as _}; 13use {defmt_rtt as _, panic_probe as _};
16 14
17pub fn config() -> Config { 15pub fn config() -> Config {
@@ -49,20 +47,19 @@ async fn main(_spawner: Spawner, p: Peripherals) {
49 } 47 }
50} 48}
51pub struct SimplePwm32<'d, T: CaptureCompare32bitInstance> { 49pub struct SimplePwm32<'d, T: CaptureCompare32bitInstance> {
52 phantom: PhantomData<&'d mut T>, 50 inner: PeripheralRef<'d, T>,
53 inner: T,
54} 51}
55 52
56impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> { 53impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> {
57 pub fn new( 54 pub fn new(
58 tim: impl Unborrow<Target = T> + 'd, 55 tim: impl Peripheral<P = T> + 'd,
59 ch1: impl Unborrow<Target = impl Channel1Pin<T>> + 'd, 56 ch1: impl Peripheral<P = impl Channel1Pin<T>> + 'd,
60 ch2: impl Unborrow<Target = impl Channel2Pin<T>> + 'd, 57 ch2: impl Peripheral<P = impl Channel2Pin<T>> + 'd,
61 ch3: impl Unborrow<Target = impl Channel3Pin<T>> + 'd, 58 ch3: impl Peripheral<P = impl Channel3Pin<T>> + 'd,
62 ch4: impl Unborrow<Target = impl Channel4Pin<T>> + 'd, 59 ch4: impl Peripheral<P = impl Channel4Pin<T>> + 'd,
63 freq: Hertz, 60 freq: Hertz,
64 ) -> Self { 61 ) -> Self {
65 unborrow!(tim, ch1, ch2, ch3, ch4); 62 into_ref!(tim, ch1, ch2, ch3, ch4);
66 63
67 T::enable(); 64 T::enable();
68 <T as embassy_stm32::rcc::low_level::RccPeripheral>::reset(); 65 <T as embassy_stm32::rcc::low_level::RccPeripheral>::reset();
@@ -78,10 +75,7 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> {
78 ch4.set_as_af(ch1.af_num(), AFType::OutputPushPull); 75 ch4.set_as_af(ch1.af_num(), AFType::OutputPushPull);
79 } 76 }
80 77
81 let mut this = Self { 78 let mut this = Self { inner: tim };
82 inner: tim,
83 phantom: PhantomData,
84 };
85 79
86 this.set_freq(freq); 80 this.set_freq(freq);
87 this.inner.start(); 81 this.inner.start();