compiled hw and sw uart in semestralka
This commit is contained in:
2
semestralka_1/Cargo.lock
generated
2
semestralka_1/Cargo.lock
generated
@@ -275,6 +275,8 @@ dependencies = [
|
|||||||
"embassy-usb",
|
"embassy-usb",
|
||||||
"embedded-graphics",
|
"embedded-graphics",
|
||||||
"embedded-hal 1.0.0",
|
"embedded-hal 1.0.0",
|
||||||
|
"embedded-io",
|
||||||
|
"embedded-io-async",
|
||||||
"heapless 0.9.1",
|
"heapless 0.9.1",
|
||||||
"micromath",
|
"micromath",
|
||||||
"panic-halt",
|
"panic-halt",
|
||||||
|
|||||||
@@ -27,3 +27,5 @@ panic-probe = { version = "1.0.0", features = ["defmt"] }
|
|||||||
defmt-rtt = "1.1.0"
|
defmt-rtt = "1.1.0"
|
||||||
defmt = "1.0.1"
|
defmt = "1.0.1"
|
||||||
static_cell = "2.1.1"
|
static_cell = "2.1.1"
|
||||||
|
embedded-io = "0.6.1"
|
||||||
|
embedded-io-async = "0.6.1"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_time::Instant;
|
||||||
use embassy_stm32::dma::Request;
|
use embassy_stm32::dma::Request;
|
||||||
use embassy_stm32::gpio::{Input, Output, Level, Pull, Speed};
|
use embassy_stm32::gpio::{Input, Output, Level, Pull, Speed};
|
||||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, pipe::Pipe};
|
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, pipe::Pipe};
|
||||||
@@ -17,9 +18,11 @@ use dma_gpio::config::{TX_RING_BYTES, RX_RING_BYTES, PIPE_RX_SIZE};
|
|||||||
use dma_gpio::software_uart::gpio_dma_uart_tx::tx_dma_task;
|
use dma_gpio::software_uart::gpio_dma_uart_tx::tx_dma_task;
|
||||||
use static_cell::StaticCell;
|
use static_cell::StaticCell;
|
||||||
use embassy_futures::yield_now;
|
use embassy_futures::yield_now;
|
||||||
use hw_uart_pc::usart1;
|
use dma_gpio::hw_uart_pc::usart1;
|
||||||
|
use dma_gpio::hw_uart_pc::driver::uart_task;
|
||||||
use embassy_stm32::usart::{BufferedUart, Config, BufferedInterruptHandler};
|
use embassy_stm32::usart::{BufferedUart, Config, BufferedInterruptHandler};
|
||||||
use embassy_stm32::peripherals;
|
use embassy_stm32::peripherals;
|
||||||
|
use embassy_stm32::bind_interrupts;
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
bind_interrupts!(struct Irqs {
|
bind_interrupts!(struct Irqs {
|
||||||
@@ -53,7 +56,8 @@ async fn main(spawner: Spawner) {
|
|||||||
Irqs,
|
Irqs,
|
||||||
cfg,
|
cfg,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
let handle = usart1::setup_and_spawn(&spawner, uart, cfg.baudrate);
|
let (handle, yield_period) = usart1::setup_and_spawn(BAUD);
|
||||||
|
spawner.spawn(uart_task(uart, handle.tx, handle.rx).unwrap());
|
||||||
// END OF HARDWARE UART to the PC
|
// END OF HARDWARE UART to the PC
|
||||||
|
|
||||||
// SOFTWARE UART
|
// SOFTWARE UART
|
||||||
@@ -74,11 +78,17 @@ async fn main(spawner: Spawner) {
|
|||||||
spawner.spawn(tx_dma_task(p.GPDMA1_CH0, odr_ptr, tx_ring_mem, &PIPE_RX).unwrap());
|
spawner.spawn(tx_dma_task(p.GPDMA1_CH0, odr_ptr, tx_ring_mem, &PIPE_RX).unwrap());
|
||||||
// EDN OF SOFTWARE UART
|
// EDN OF SOFTWARE UART
|
||||||
|
|
||||||
|
let mut last_yield = Instant::now();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
info!("tick start");
|
info!("tick start");
|
||||||
// Timer::after(Duration::from_millis(100)).await;
|
// Timer::after(Duration::from_millis(100)).await;
|
||||||
// info!("tick end");
|
// info!("tick end");
|
||||||
|
|
||||||
yield_now().await;
|
if Instant::now().duration_since(last_yield) >= handle.yield_period {
|
||||||
|
embassy_futures::yield_now().await;
|
||||||
|
last_yield = Instant::now();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// src/uart/driver.rs
|
// src/uart/driver.rs
|
||||||
use defmt::unwrap;
|
use defmt::unwrap;
|
||||||
use embassy_executor::Spawner;
|
|
||||||
use embassy_futures::select::{select, Either};
|
use embassy_futures::select::{select, Either};
|
||||||
use embassy_stm32::usart::BufferedUart;
|
use embassy_stm32::usart::BufferedUart;
|
||||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||||
@@ -8,7 +7,7 @@ use embassy_sync::pipe::Pipe;
|
|||||||
use embassy_time::Duration;
|
use embassy_time::Duration;
|
||||||
use embedded_io_async::{Read, Write};
|
use embedded_io_async::{Read, Write};
|
||||||
|
|
||||||
use crate::uart::safety::{RX_PIPE_CAP, TX_PIPE_CAP};
|
use crate::hw_uart_pc::safety::{RX_PIPE_CAP, TX_PIPE_CAP};
|
||||||
|
|
||||||
pub struct UartHandle {
|
pub struct UartHandle {
|
||||||
pub tx: &'static Pipe<CriticalSectionRawMutex, TX_PIPE_CAP>,
|
pub tx: &'static Pipe<CriticalSectionRawMutex, TX_PIPE_CAP>,
|
||||||
@@ -36,9 +35,7 @@ pub async fn uart_task(
|
|||||||
// Incoming data from UART hardware
|
// Incoming data from UART hardware
|
||||||
Either::First(res) => {
|
Either::First(res) => {
|
||||||
if let Ok(_) = res {
|
if let Ok(_) = res {
|
||||||
// Forward to RX pipe and echo to TX pipe (same behavior as before)
|
|
||||||
let _ = rx_pipe.write(&rx_byte).await;
|
let _ = rx_pipe.write(&rx_byte).await;
|
||||||
let _ = tx_pipe.try_write(&rx_byte);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Outgoing data waiting in TX pipe
|
// Outgoing data waiting in TX pipe
|
||||||
@@ -48,18 +45,3 @@ pub async fn uart_task(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawn_for(
|
|
||||||
spawner: &Spawner,
|
|
||||||
uart: BufferedUart<'static>,
|
|
||||||
tx_pipe: &'static Pipe<CriticalSectionRawMutex, TX_PIPE_CAP>,
|
|
||||||
rx_pipe: &'static Pipe<CriticalSectionRawMutex, RX_PIPE_CAP>,
|
|
||||||
yield_period: Duration,
|
|
||||||
) -> UartHandle {
|
|
||||||
spawner.spawn(uart_task(uart, tx_pipe, rx_pipe)).unwrap();
|
|
||||||
UartHandle {
|
|
||||||
tx: tx_pipe,
|
|
||||||
rx: rx_pipe,
|
|
||||||
yield_period,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
// src/uart/usart1.rs
|
// src/uart/usart1.rs
|
||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy_executor::Spawner;
|
|
||||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||||
use embassy_sync::pipe::Pipe;
|
use embassy_sync::pipe::Pipe;
|
||||||
use embassy_time::Duration;
|
use embassy_time::Duration;
|
||||||
|
|
||||||
use crate::uart::safety::{preflight_and_suggest_yield_period, RX_PIPE_CAP, TX_PIPE_CAP};
|
use crate::hw_uart_pc::safety::{preflight_and_suggest_yield_period, RX_PIPE_CAP, TX_PIPE_CAP};
|
||||||
use crate::uart::driver::{spawn_for, UartHandle};
|
use crate::hw_uart_pc::driver::UartHandle;
|
||||||
|
|
||||||
// Static pipes and buffers
|
// Static pipes and buffers
|
||||||
static UART1_TX_PIPE: Pipe<CriticalSectionRawMutex, TX_PIPE_CAP> = Pipe::new();
|
static UART1_TX_PIPE: Pipe<CriticalSectionRawMutex, TX_PIPE_CAP> = Pipe::new();
|
||||||
static UART1_RX_PIPE: Pipe<CriticalSectionRawMutex, RX_PIPE_CAP> = Pipe::new();
|
static UART1_RX_PIPE: Pipe<CriticalSectionRawMutex, RX_PIPE_CAP> = Pipe::new();
|
||||||
|
|
||||||
pub fn setup_and_spawn(
|
pub fn setup_and_spawn(baudrate: u32,) -> (UartHandle, Duration) {
|
||||||
spawner: &Spawner,
|
|
||||||
uart: embassy_stm32::usart::BufferedUart<'static>,
|
|
||||||
baudrate: u32,
|
|
||||||
) -> UartHandle {
|
|
||||||
let yield_period: Duration = preflight_and_suggest_yield_period(baudrate);
|
let yield_period: Duration = preflight_and_suggest_yield_period(baudrate);
|
||||||
info!("USART1 initialized");
|
info!("HW USART1 safe");
|
||||||
spawn_for(spawner, uart, &UART1_TX_PIPE, &UART1_RX_PIPE, yield_period)
|
let handle = UartHandle {
|
||||||
|
tx: &UART1_TX_PIPE,
|
||||||
|
rx: &UART1_RX_PIPE,
|
||||||
|
yield_period,
|
||||||
|
};
|
||||||
|
|
||||||
|
(handle, yield_period)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user