stm_32/peripherals/pps.rs
1// ******************************************************************************
2// * File : platforms/stm_32/src/peripherals/pps.rs
3// * Date : June 28, 2026
4// ******************************************************************************
5// *
6// * Copyright (c) 2023, AeroVironment, Inc.
7// * All rights reserved.
8// *
9// * Redistribution and use in source and binary forms, with or without
10// * modification, are permitted provided that the following conditions are met:
11// *
12// * 1.Redistributions of source code must retain the above copyright notice, this
13// * list of conditions and the following disclaimer.
14// *
15// * 2.Redistributions in binary form must reproduce the above copyright notice,
16// * this list of conditions and the following disclaimer in the documentation
17// * and/or other materials provided with the distribution.
18// *
19// * 3.Neither the name of the copyright holder nor the names of its
20// * contributors may be used to endorse or promote products derived from
21// * this software without specific prior written permission.
22// *
23// * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24// * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25// * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26// * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27// * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28// * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29// * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30// * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31// * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33// *
34// ******************************************************************************
35
36use embassy_stm32::exti::ExtiInput;
37use embassy_stm32::mode::Async;
38use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
39use embassy_sync::signal::Signal;
40use embassy_time::Instant;
41
42use veloxity_core::packets;
43
44pub static PPS_SIGNAL: Signal<CriticalSectionRawMutex, packets::PpsPacket> =
45 Signal::<CriticalSectionRawMutex, packets::PpsPacket>::new();
46
47pub struct PpsSensor {
48 pub pps: ExtiInput<'static, Async>,
49}
50
51impl PpsSensor {
52 pub async fn run(&mut self) {
53 loop {
54 self.pps.wait_for_rising_edge().await;
55 let timestamp = Instant::now();
56 let status = 1;
57 let header = packets::RosflightPacketHeader {
58 timestamp: timestamp.as_micros(),
59 status,
60 };
61 let pps_packet = packets::PpsPacket { header };
62 PPS_SIGNAL.signal(pps_packet);
63 }
64 }
65}
66
67#[embassy_executor::task]
68pub async fn task(mut pps: PpsSensor) {
69 pps.run().await;
70}