aboutsummaryrefslogtreecommitdiff
path: root/tests/mspm0/src/bin/uart.rs
blob: 458129d44fcf1bce3028b167da5f7cb9140f37bf (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
#![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::mode::Blocking;
use embassy_mspm0::uart::{ClockSel, Config, Error, Uart};
use {defmt_rtt as _, panic_probe as _};

fn read<const N: usize>(uart: &mut Uart<'_, Blocking>) -> Result<[u8; N], Error> {
    let mut buf = [255; N];
    uart.blocking_read(&mut buf)?;
    Ok(buf)
}

#[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(feature = "mspm0g3507")]
    let (mut tx, mut rx, mut uart) = (p.PA8, p.PA9, p.UART1);

    const MFCLK_BUAD_RATES: &[u32] = &[1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200];

    for &rate in MFCLK_BUAD_RATES {
        info!("{} baud using MFCLK", rate);

        let mut config = Config::default();
        // MSPM0 hardware supports a loopback mode to allow self test.
        config.loop_back_enable = true;
        config.baudrate = rate;

        let mut uart = unwrap!(Uart::new_blocking(
            uart.reborrow(),
            rx.reborrow(),
            tx.reborrow(),
            config
        ));

        // We can't send too many bytes, they have to fit in the FIFO.
        // This is because we aren't sending+receiving at the same time.

        let data = [0xC0, 0xDE];
        unwrap!(uart.blocking_write(&data));
        assert_eq!(unwrap!(read(&mut uart)), data);
    }

    // 9600 is the maximum possible value for 32.768 kHz.
    const LFCLK_BAUD_RATES: &[u32] = &[1200, 2400, 4800, 9600];

    for &rate in LFCLK_BAUD_RATES {
        info!("{} baud using LFCLK", rate);

        let mut config = Config::default();
        // MSPM0 hardware supports a loopback mode to allow self test.
        config.loop_back_enable = true;
        config.baudrate = rate;
        config.clock_source = ClockSel::LfClk;

        let mut uart = expect!(Uart::new_blocking(
            uart.reborrow(),
            rx.reborrow(),
            tx.reborrow(),
            config,
        ));

        // We can't send too many bytes, they have to fit in the FIFO.
        // This is because we aren't sending+receiving at the same time.

        let data = [0xC0, 0xDE];
        unwrap!(uart.blocking_write(&data));
        assert_eq!(unwrap!(read(&mut uart)), data);
    }

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