aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/lpuart_buffered.rs
blob: 420589d00fa555a3a8ac51b63f96492cf054204f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_mcxa::clocks::config::Div8;
use embassy_mcxa::lpuart::buffered::BufferedLpuart;
use embassy_mcxa::lpuart::Config;
use embassy_mcxa::{bind_interrupts, lpuart};
use embedded_io_async::Write;
use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};

// Bind OS_EVENT for timers plus LPUART2 IRQ for the buffered driver
bind_interrupts!(struct Irqs {
    LPUART2 => lpuart::buffered::BufferedInterruptHandler::<hal::peripherals::LPUART2>;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let mut cfg = hal::config::Config::default();
    cfg.clock_cfg.sirc.fro_12m_enabled = true;
    cfg.clock_cfg.sirc.fro_lf_div = Some(Div8::no_div());
    let p = hal::init(cfg);

    // Configure NVIC for LPUART2
    hal::interrupt::LPUART2.configure_for_uart(hal::interrupt::Priority::P3);

    // UART configuration (enable both TX and RX)
    let config = Config {
        baudrate_bps: 115_200,
        rx_fifo_watermark: 0,
        tx_fifo_watermark: 0,
        ..Default::default()
    };

    let mut tx_buf = [0u8; 256];
    let mut rx_buf = [0u8; 256];

    // Create a buffered LPUART2 instance with both TX and RX
    let mut uart = BufferedLpuart::new(
        p.LPUART2,
        p.P2_2, // TX pin
        p.P2_3, // RX pin
        Irqs,
        &mut tx_buf,
        &mut rx_buf,
        config,
    )
    .unwrap();

    // Split into TX and RX parts
    let (tx, rx) = uart.split_ref();

    tx.write(b"Hello buffered LPUART.\r\n").await.unwrap();
    tx.write(b"Type characters to echo them back.\r\n").await.unwrap();

    // Echo loop
    let mut buf = [0u8; 4];
    loop {
        let used = rx.read(&mut buf).await.unwrap();
        tx.write_all(&buf[..used]).await.unwrap();
    }
}