Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UART EasyDMA example #272

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/uarte-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "uarte-demo"
version = "0.1.0"
authors = ["Jesse C. Laning"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m = "0.6.2"
cortex-m-rt = "0.6.12"
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]
63 changes: 63 additions & 0 deletions examples/uarte-demo/Embed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[default.probe]
# USB vendor ID
# usb_vid = "1337"
# USB product ID
# usb_pid = "1337"
# Serial number
# serial = "12345678"
# The protocol to be used for communicating with the target.
protocol = "Swd"
# The speed in kHz of the data link to the target.
# speed = 1337

[default.flashing]
# Whether or not the target should be flashed.
enabled = true
# Whether or not the target should be halted after reset.
# DEPRECATED, moved to reset section
halt_afterwards = false
# Whether or not bytes erased but not rewritten with data from the ELF
# should be restored with their contents before erasing.
restore_unwritten_bytes = false
# The path where an SVG of the assembled flash layout should be written to.
# flash_layout_output_path = "out.svg"

[default.reset]
# Whether or not the target should be reset.
# When flashing is enabled as well, the target will be reset after flashing.
enabled = true
# Whether or not the target should be halted after reset.
halt_afterwards = false

[default.general]
# The chip name of the chip to be debugged.
chip = "nRF52840"
# A list of chip descriptions to be loaded during runtime.
chip_descriptions = []
# The default log level to be used. Possible values are one of:
# "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"
log_level = "WARN"

[default.rtt]
# Whether or not an RTTUI should be opened after flashing.
# This is exclusive and cannot be used with GDB at the moment.
enabled = true
# A list of channel associations to be displayed. If left empty, all channels are displayed.
channels = [
# { up = 0, down = 0, name = "name", format = "String" }
]
# The duration in ms for which the logger should retry to attach to RTT.
timeout = 3000
# Whether timestamps in the RTTUI are enabled
show_timestamps = false
# Whether to save rtt history buffer on exit.
log_enabled = false
# Where to save rtt history buffer relative to manifest path.
log_path = "./logs"

[default.gdb]
# Whether or not a GDB server should be opened after flashing.
# This is exclusive and cannot be used with RTT at the moment.
enabled = false
# The connection string in host:port format wher the GDB server will open a socket.
# gdb_connection_string
26 changes: 26 additions & 0 deletions examples/uarte-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# UART EasyDMA Demo

This example echos commands, denoted by a new line, sent to the UARTE0 interface and over RTT (Real-Time Transfer) I/O protocol for debug purposes.

This was designed for the nRF52840-DK (PCA10056):
https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-DK

## HW connections
Pin Connecton
P0.06 TX
P0.08 RX

## Set up with `cargo-embed`

Install `cargo-embed` if you don't have it already:

```console
$ cargo install cargo-embed
```

Then just `cd` to the example folder and run

```console
$ cargo embed --target thumbv7em-none-eabihf
```

81 changes: 81 additions & 0 deletions examples/uarte-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#![no_std]
#![no_main]

use {
core::{
str,
fmt::Write,
panic::PanicInfo,
sync::atomic::{compiler_fence, Ordering},
},
cortex_m_rt::entry,
hal::gpio::Level,
nrf52840_hal as hal,
rtt_target::{rprintln, rprint, rtt_init_print},
};

#[inline(never)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
cortex_m::interrupt::disable();
rprintln!("{}", info);
loop {
compiler_fence(Ordering::SeqCst);
}
}

#[entry]
fn main() -> ! {
rtt_init_print!();
let peripherals = hal::pac::Peripherals::take().unwrap();
hal::clocks::Clocks::new(peripherals.CLOCK).enable_ext_hfosc();

let p0 = hal::gpio::p0::Parts::new(peripherals.P0);

// configure UARTE0
let uarte0 = peripherals.UARTE0;
let txd = p0.p0_06.into_push_pull_output(Level::Low).degrade();
let rxd = p0.p0_08.into_floating_input().degrade();

let pins = hal::uarte::Pins {
rxd,
txd,
cts: None,
rts: None,
};

let mut serial = hal::uarte::Uarte::new(
uarte0,
pins,
hal::uarte::Parity::EXCLUDED,
hal::uarte::Baudrate::BAUD115200,
);

// duplicating messages over rtt to compare results
writeln!(serial, "Hello, World!").unwrap();
rprintln!("Hello, World!");

let mut rx_buffer = [0u8; 255];
let mut index = 0;

// basic serial echoing
loop {
// read one byte
serial.read(&mut rx_buffer[index .. index + 1]).unwrap();
index += 1;

// check if we've filled our buffer or a new line byte was sent
if index == rx_buffer.len() || rx_buffer[index - 1] == '\n' as u8 {
// write buffer back
serial.write(&rx_buffer).unwrap();

// duplicating messages over rtt to compare results
rprint!(str::from_utf8(&rx_buffer).unwrap());

// reset the buffer so we don't have stale data
rx_buffer.iter_mut().for_each(|m| *m = 0);

index = 0;
}
}
}
1 change: 1 addition & 0 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub static EXAMPLES: &[(&str, &[&str])] = &[
("twim-demo", &[]),
("twis-demo", &[]),
("twis-dma-demo", &[]),
("uarte-demo", &[]),
("wdt-demo", &[]),
];

Expand Down