nrf52dk/tests/
uart.rs

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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

use kernel::hil::uart::Transmit;
use kernel::static_init;
use nrf52832::uart::Uarte;

const BUFFER_SIZE_2048: usize = 2048;

/// To run the tests add the following `main.rs::main` somewhere after that the UART
/// peripheral has been initilized:
///
/// ```rustc
///     tests::uart::run(base_peripherals.uarte0);
/// ```
///
/// Make sure you don't are running any user-space processes and remove all `debug!` prints
/// in `main.rs::main()` otherwise race-conditions in the UART will occur.
/// Then enable the test you want run in `run()`
///
pub unsafe fn run(uart: &'static Uarte) {
    // Note: you can only one of these tests at the time because
    //  1. It will generate race-conditions in the UART because we don't have any checks against that
    //  2. `buf` can only be `borrowed` once and avoid allocate four different buffers

    let buf = static_init!([u8; BUFFER_SIZE_2048], [0; BUFFER_SIZE_2048]);

    // create an iterator of printable ascii characters and write to the uart buffer
    for (ascii_char, b) in (33..126).cycle().zip(buf.iter_mut()) {
        *b = ascii_char;
    }

    transmit_entire_buffer(uart, buf);
    // transmit_512(uart, buf);
    // should_not_transmit(uart, buf);
    // transmit_254(uart, buf);
}

#[allow(unused)]
unsafe fn transmit_entire_buffer(uart: &'static Uarte, buf: &'static mut [u8]) {
    uart.transmit_buffer(buf, BUFFER_SIZE_2048);
}

#[allow(unused)]
unsafe fn should_not_transmit(uart: &'static Uarte, buf: &'static mut [u8]) {
    uart.transmit_buffer(buf, 0);
}

#[allow(unused)]
unsafe fn transmit_512(uart: &'static Uarte, buf: &'static mut [u8]) {
    uart.transmit_buffer(buf, 512);
}

#[allow(unused)]
unsafe fn transmit_254(uart: &'static Uarte, buf: &'static mut [u8]) {
    uart.transmit_buffer(buf, 254);
}