nrf52dk/tests/
uart.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5use kernel::hil::uart::Transmit;
6use kernel::static_init;
7use nrf52832::uart::Uarte;
8
9const BUFFER_SIZE_2048: usize = 2048;
10
11/// To run the tests add the following `main.rs::main` somewhere after that the UART
12/// peripheral has been initilized:
13///
14/// ```rustc
15///     tests::uart::run(base_peripherals.uarte0);
16/// ```
17///
18/// Make sure you don't are running any user-space processes and remove all `debug!` prints
19/// in `main.rs::main()` otherwise race-conditions in the UART will occur.
20/// Then enable the test you want run in `run()`
21///
22pub unsafe fn run(uart: &'static Uarte) {
23    // Note: you can only one of these tests at the time because
24    //  1. It will generate race-conditions in the UART because we don't have any checks against that
25    //  2. `buf` can only be `borrowed` once and avoid allocate four different buffers
26
27    let buf = static_init!([u8; BUFFER_SIZE_2048], [0; BUFFER_SIZE_2048]);
28
29    // create an iterator of printable ascii characters and write to the uart buffer
30    for (ascii_char, b) in (33..126).cycle().zip(buf.iter_mut()) {
31        *b = ascii_char;
32    }
33
34    transmit_entire_buffer(uart, buf);
35    // transmit_512(uart, buf);
36    // should_not_transmit(uart, buf);
37    // transmit_254(uart, buf);
38}
39
40#[allow(unused)]
41unsafe fn transmit_entire_buffer(uart: &'static Uarte, buf: &'static mut [u8]) {
42    uart.transmit_buffer(buf, BUFFER_SIZE_2048);
43}
44
45#[allow(unused)]
46unsafe fn should_not_transmit(uart: &'static Uarte, buf: &'static mut [u8]) {
47    uart.transmit_buffer(buf, 0);
48}
49
50#[allow(unused)]
51unsafe fn transmit_512(uart: &'static Uarte, buf: &'static mut [u8]) {
52    uart.transmit_buffer(buf, 512);
53}
54
55#[allow(unused)]
56unsafe fn transmit_254(uart: &'static Uarte, buf: &'static mut [u8]) {
57    uart.transmit_buffer(buf, 254);
58}