stm_32/peripherals/telem.rs
1// ******************************************************************************
2// * File : platforms/stm_32/src/peripherals/telem.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::mode::Async;
37use embassy_stm32::usart::UartRx;
38use embassy_stm32::usart::UartTx;
39use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
40use embassy_sync::pipe::Pipe;
41use embassy_time::Timer;
42
43use veloxity_core::comm::interface::EmbeddedComInterface;
44use veloxity_core::errors;
45
46pub static TX_BUFF_SIZE: usize = 4 * 2048;
47pub static RX_BUFF_SIZE: usize = 4 * 2048;
48
49pub static TELEM_TX: Pipe<CriticalSectionRawMutex, TX_BUFF_SIZE> = Pipe::new();
50pub static TELEM_RX: Pipe<CriticalSectionRawMutex, RX_BUFF_SIZE> = Pipe::new();
51
52pub struct BasicProcessor;
53
54impl EmbeddedComInterface for BasicProcessor {
55 async fn process_bytes(&mut self, buf: &[u8], num_bytes: usize) {
56 TELEM_RX.write_all(&buf[0..num_bytes]).await;
57 }
58}
59
60pub struct TelemTx {
61 pub uart_tx: UartTx<'static, Async>,
62}
63
64pub struct TelemRx<ECI: EmbeddedComInterface> {
65 pub uart_rx: UartRx<'static, Async>,
66 pub byte_processor: ECI,
67}
68
69impl TelemTx {
70 pub async fn run(&mut self) {
71 loop {
72 let mut buf = [0u8; TX_BUFF_SIZE]; // read up to the whole buffer
73 let n = TELEM_TX.read(&mut buf).await;
74 if n > 0 && n <= TX_BUFF_SIZE {
75 match self.uart_tx.write(&mut buf[0..n]).await {
76 Ok(()) => {}
77 Err(_) => {
78 let _ = errors::TelemError::GenericTelemError("TelemTx Failed!");
79 }
80 }
81 }
82 }
83 }
84}
85
86// is this impl how this is supposed to work?
87impl<ECI: EmbeddedComInterface> TelemRx<ECI> {
88 pub async fn run(&mut self) {
89 let mut buf = [0u8; RX_BUFF_SIZE]; // Read as much as we can.
90 loop {
91 let result = self.uart_rx.read_until_idle(&mut buf).await;
92 match result {
93 Err(_) => {
94 Timer::after_millis(1).await;
95 }
96 Ok(n) => {
97 if n > 0 && n <= RX_BUFF_SIZE {
98 // added the above if statement to match phil's code
99 self.byte_processor.process_bytes(&buf, n).await;
100 }
101 }
102 }
103 }
104 }
105}
106
107#[embassy_executor::task]
108pub async fn task_rx(mut telem_rx: TelemRx<BasicProcessor>) {
109 telem_rx.run().await;
110}
111
112#[embassy_executor::task]
113pub async fn task_tx(mut telem_tx: TelemTx) {
114 telem_tx.run().await;
115}