aboutsummaryrefslogtreecommitdiff
path: root/tests/rp/src/bin/uart_dma.rs
blob: cd4af1ef2aa518dc2b93cd1caa14a3f5a28f2c05 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
teleprobe_meta::target!(b"rpi-pico");

use defmt::{assert_eq, *};
use embassy_executor::Spawner;
use embassy_rp::bind_interrupts;
use embassy_rp::gpio::{Level, Output};
use embassy_rp::peripherals::UART0;
use embassy_rp::uart::{Async, Config, Error, Instance, InterruptHandler, Parity, Uart, UartRx};
use embassy_time::{Duration, Timer};
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(struct Irqs {
    UART0_IRQ => InterruptHandler<UART0>;
});

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

async fn read1<const N: usize>(uart: &mut UartRx<'_, impl Instance, Async>) -> Result<[u8; N], Error> {
    let mut buf = [255; N];
    uart.read(&mut buf).await?;
    Ok(buf)
}

async fn send(pin: &mut Output<'_, impl embassy_rp::gpio::Pin>, v: u8, parity: Option<bool>) {
    pin.set_low();
    Timer::after(Duration::from_millis(1)).await;
    for i in 0..8 {
        if v & (1 << i) == 0 {
            pin.set_low();
        } else {
            pin.set_high();
        }
        Timer::after(Duration::from_millis(1)).await;
    }
    if let Some(b) = parity {
        if b {
            pin.set_high();
        } else {
            pin.set_low();
        }
        Timer::after(Duration::from_millis(1)).await;
    }
    pin.set_high();
    Timer::after(Duration::from_millis(1)).await;
}

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

    let (mut tx, mut rx, mut uart) = (p.PIN_0, p.PIN_1, p.UART0);

    // 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 config = Config::default();
        let mut uart = Uart::new(
            &mut uart,
            &mut tx,
            &mut rx,
            Irqs,
            &mut p.DMA_CH0,
            &mut p.DMA_CH1,
            config,
        );

        let data = [0xC0, 0xDE];
        uart.write(&data).await.unwrap();

        let mut buf = [0; 2];
        uart.read(&mut buf).await.unwrap();
        assert_eq!(buf, data);
    }

    info!("test overflow detection");
    {
        let config = Config::default();
        let mut uart = Uart::new(
            &mut uart,
            &mut tx,
            &mut rx,
            Irqs,
            &mut p.DMA_CH0,
            &mut p.DMA_CH1,
            config,
        );

        uart.blocking_write(&[42; 32]).unwrap();
        uart.blocking_write(&[1, 2, 3]).unwrap();
        uart.blocking_flush().unwrap();

        // can receive regular fifo contents
        assert_eq!(read(&mut uart).await, Ok([42; 16]));
        assert_eq!(read(&mut uart).await, Ok([42; 16]));
        // receiving the rest fails with overrun
        assert_eq!(read::<16>(&mut uart).await, Err(Error::Overrun));
        // new data is accepted, latest overrunning byte first
        assert_eq!(read(&mut uart).await, Ok([3]));
        uart.blocking_write(&[8, 9]).unwrap();
        Timer::after(Duration::from_millis(1)).await;
        assert_eq!(read(&mut uart).await, Ok([8, 9]));
    }

    info!("test break detection");
    {
        let config = Config::default();
        let (mut tx, mut rx) = Uart::new(
            &mut uart,
            &mut tx,
            &mut rx,
            Irqs,
            &mut p.DMA_CH0,
            &mut p.DMA_CH1,
            config,
        )
        .split();

        // break before read
        tx.send_break(20).await;
        tx.write(&[64]).await.unwrap();
        assert_eq!(read1::<1>(&mut rx).await.unwrap_err(), Error::Break);
        assert_eq!(read1(&mut rx).await.unwrap(), [64]);

        // break during read
        {
            let r = read1::<2>(&mut rx);
            tx.write(&[2]).await.unwrap();
            tx.send_break(20).await;
            tx.write(&[3]).await.unwrap();
            assert_eq!(r.await.unwrap_err(), Error::Break);
            assert_eq!(read1(&mut rx).await.unwrap(), [3]);
        }

        // break after read
        {
            let r = read1(&mut rx);
            tx.write(&[2]).await.unwrap();
            tx.send_break(20).await;
            tx.write(&[3]).await.unwrap();
            assert_eq!(r.await.unwrap(), [2]);
            assert_eq!(read1::<1>(&mut rx).await.unwrap_err(), Error::Break);
            assert_eq!(read1(&mut rx).await.unwrap(), [3]);
        }
    }

    // parity detection. here we bitbang to not require two uarts.
    info!("test parity error detection");
    {
        let mut pin = Output::new(&mut tx, Level::High);
        // choose a very slow baud rate to make tests reliable even with O0
        let mut config = Config::default();
        config.baudrate = 1000;
        config.parity = Parity::ParityEven;
        let mut uart = UartRx::new(&mut uart, &mut rx, Irqs, &mut p.DMA_CH0, config);

        async fn chr(pin: &mut Output<'_, impl embassy_rp::gpio::Pin>, v: u8, parity: u32) {
            send(pin, v, Some(parity != 0)).await;
        }

        // first check that we can send correctly
        chr(&mut pin, 32, 1).await;
        assert_eq!(read1(&mut uart).await.unwrap(), [32]);

        // parity error before read
        chr(&mut pin, 32, 0).await;
        chr(&mut pin, 31, 1).await;
        assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Parity);
        assert_eq!(read1(&mut uart).await.unwrap(), [31]);

        // parity error during read
        {
            let r = read1::<2>(&mut uart);
            chr(&mut pin, 2, 1).await;
            chr(&mut pin, 32, 0).await;
            chr(&mut pin, 3, 0).await;
            assert_eq!(r.await.unwrap_err(), Error::Parity);
            assert_eq!(read1(&mut uart).await.unwrap(), [3]);
        }

        // parity error after read
        {
            let r = read1(&mut uart);
            chr(&mut pin, 2, 1).await;
            chr(&mut pin, 32, 0).await;
            chr(&mut pin, 3, 0).await;
            assert_eq!(r.await.unwrap(), [2]);
            assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Parity);
            assert_eq!(read1(&mut uart).await.unwrap(), [3]);
        }
    }

    // framing error detection. here we bitbang because there's no other way.
    info!("test framing error detection");
    {
        let mut pin = Output::new(&mut tx, Level::High);
        // choose a very slow baud rate to make tests reliable even with O0
        let mut config = Config::default();
        config.baudrate = 1000;
        let mut uart = UartRx::new(&mut uart, &mut rx, Irqs, &mut p.DMA_CH0, config);

        async fn chr(pin: &mut Output<'_, impl embassy_rp::gpio::Pin>, v: u8, good: bool) {
            if good {
                send(pin, v, None).await;
            } else {
                send(pin, v, Some(false)).await;
            }
        }

        // first check that we can send correctly
        chr(&mut pin, 32, true).await;
        assert_eq!(read1(&mut uart).await.unwrap(), [32]);

        // parity error before read
        chr(&mut pin, 32, false).await;
        chr(&mut pin, 31, true).await;
        assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Framing);
        assert_eq!(read1(&mut uart).await.unwrap(), [31]);

        // parity error during read
        {
            let r = read1::<2>(&mut uart);
            chr(&mut pin, 2, true).await;
            chr(&mut pin, 32, false).await;
            chr(&mut pin, 3, true).await;
            assert_eq!(r.await.unwrap_err(), Error::Framing);
            assert_eq!(read1(&mut uart).await.unwrap(), [3]);
        }

        // parity error after read
        {
            let r = read1(&mut uart);
            chr(&mut pin, 2, true).await;
            chr(&mut pin, 32, false).await;
            chr(&mut pin, 3, true).await;
            assert_eq!(r.await.unwrap(), [2]);
            assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Framing);
            assert_eq!(read1(&mut uart).await.unwrap(), [3]);
        }
    }

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