aboutsummaryrefslogtreecommitdiff
path: root/examples/src/bin/uart.rs
blob: 21e26e3adf912dcf0b1c2d1b5e1e4afc11eab1f6 (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
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

#[path = "../example_common.rs"]
mod example_common;
use example_common::*;

use cortex_m_rt::entry;
use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt};
use embassy_nrf::uarte;
use futures::pin_mut;
use nrf52840_hal::gpio;

#[static_executor::task]
async fn run() {
    let p = embassy_nrf::pac::Peripherals::take().dewrap();

    let port0 = gpio::p0::Parts::new(p.P0);

    let pins = uarte::Pins {
        rxd: port0.p0_08.into_floating_input().degrade(),
        txd: port0
            .p0_06
            .into_push_pull_output(gpio::Level::Low)
            .degrade(),
        cts: None,
        rts: None,
    };

    let u = uarte::Uarte::new(
        p.UARTE0,
        pins,
        uarte::Parity::EXCLUDED,
        uarte::Baudrate::BAUD115200,
    );
    pin_mut!(u);

    info!("uarte initialized!");

    u.write_all(b"Hello!\r\n").await.dewrap();
    info!("wrote hello in uart!");

    // Simple demo, reading 8-char chunks and echoing them back reversed.
    loop {
        info!("reading...");
        let mut buf = [0u8; 8];
        u.read_exact(&mut buf).await.dewrap();
        info!("read done, got {:[u8]}", buf);

        // Reverse buf
        for i in 0..4 {
            let tmp = buf[i];
            buf[i] = buf[7 - i];
            buf[7 - i] = tmp;
        }

        info!("writing...");
        u.write_all(&buf).await.dewrap();
        info!("write done");
    }
}

#[entry]
fn main() -> ! {
    info!("Hello World!");

    unsafe {
        run.spawn().dewrap();
        static_executor::run();
    }
}