aboutsummaryrefslogtreecommitdiff
path: root/tests/mspm0/src/bin/uart_buffered.rs
blob: 135ac12871b7895c8295e0cfc37535d7ce9e1371 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#![no_std]
#![no_main]

#[cfg(feature = "mspm0g3507")]
teleprobe_meta::target!(b"lp-mspm0g3507");

use defmt::{assert_eq, unwrap, *};
use embassy_executor::Spawner;
use embassy_mspm0::uart::{BufferedInterruptHandler, BufferedUart, Config};
use embassy_mspm0::{bind_interrupts, peripherals};
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
    UART1 => BufferedInterruptHandler<peripherals::UART1>;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_mspm0::init(Default::default());
    info!("Hello World!");

    // TODO: Allow creating a looped-back UART (so pins are not needed).
    // Do not select default UART since the virtual COM port is attached to UART0.
    #[cfg(any(feature = "mspm0g3507"))]
    let (mut tx, mut rx, mut uart) = (p.PA8, p.PA9, p.UART1);

    {
        use embedded_io_async::{Read, Write};

        let mut config = Config::default();
        config.loop_back_enable = true;
        config.fifo_enable = false;

        let tx_buf = &mut [0u8; 16];
        let rx_buf = &mut [0u8; 16];
        let mut uart = unwrap!(BufferedUart::new(
            uart.reborrow(),
            tx.reborrow(),
            rx.reborrow(),
            Irqs,
            tx_buf,
            rx_buf,
            config
        ));

        let mut buf = [0; 16];
        for (j, b) in buf.iter_mut().enumerate() {
            *b = j as u8;
        }

        unwrap!(uart.write_all(&buf).await);
        unwrap!(uart.flush().await);

        unwrap!(uart.read_exact(&mut buf).await);
        for (j, b) in buf.iter().enumerate() {
            assert_eq!(*b, j as u8);
        }

        // Buffer is unclogged, should be able to write again.
        unwrap!(uart.write_all(&buf).await);
        unwrap!(uart.flush().await);

        unwrap!(uart.read_exact(&mut buf).await);
        for (j, b) in buf.iter().enumerate() {
            assert_eq!(*b, j as u8);
        }
    }

    info!("Blocking buffered");
    {
        use embedded_io::{Read, Write};

        let mut config = Config::default();
        config.loop_back_enable = true;
        config.fifo_enable = false;

        let tx_buf = &mut [0u8; 16];
        let rx_buf = &mut [0u8; 16];
        let mut uart = unwrap!(BufferedUart::new(
            uart.reborrow(),
            tx.reborrow(),
            rx.reborrow(),
            Irqs,
            tx_buf,
            rx_buf,
            config
        ));

        let mut buf = [0; 16];

        for (j, b) in buf.iter_mut().enumerate() {
            *b = j as u8;
        }

        unwrap!(uart.write_all(&buf));
        unwrap!(uart.blocking_flush());
        unwrap!(uart.read_exact(&mut buf));

        for (j, b) in buf.iter().enumerate() {
            assert_eq!(*b, j as u8);
        }

        // Buffer is unclogged, should be able to write again.
        unwrap!(uart.write_all(&buf));
        unwrap!(uart.blocking_flush());
        unwrap!(uart.read_exact(&mut buf));

        for (j, b) in buf.iter().enumerate() {
            assert_eq!(*b, j as u8, "at {}", j);
        }
    }

    info!("Test OK");
    cortex_m::asm::bkpt();
}