Skip to content

Commit

Permalink
Adds a simple UART example
Browse files Browse the repository at this point in the history
Provides a simple UART example so that we can test out UART on the nRF52840 and nRF9160
  • Loading branch information
huntc committed May 15, 2021
1 parent a8dddf1 commit fc11ace
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "hello-world"
version = "0.1.0"
authors = ["Christopher Hunt"]
edition = "2018"
publish = false

# 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"

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]

[dependencies.nrf9160-hal]
features = ["rt"]
path = "../../nrf9160-hal"
optional = true

[dependencies.nrf52840-hal]
features = ["rt"]
path = "../../nrf52840-hal"
optional = true

[features]
9160 = ["nrf9160-hal"]
52840 = ["nrf52840-hal"]
63 changes: 63 additions & 0 deletions examples/hello-world/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_xxAA"
# 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 = false
# 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 = true
# 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
36 changes: 36 additions & 0 deletions examples/hello-world/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
fn main() {
// We only need the memory.x file for the nRF9160 as our program
// must then reside in a location where unsecure programs need to be.
#[cfg(feature = "9160")]
{
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());

// By default, Cargo will re-run a build script whenever
// any file in the project changes. By specifying `memory.x`
// here, we ensure the build script is only re-run when
// `memory.x` is changed.
println!("cargo:rerun-if-changed=memory.x");
}
}
32 changes: 32 additions & 0 deletions examples/hello-world/memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
MEMORY
{
/* NOTE 1 K = 1 KiBi = 1024 bytes */
FLASH : ORIGIN = 0x00050000, LENGTH = 768K
RAM : ORIGIN = 0x20020000, LENGTH = 128K
}

/* This is where the call stack will be allocated. */
/* The stack is of the full descending type. */
/* You may want to use this variable to locate the call stack and static
variables in different memory regions. Below is shown the default value */
/* _stack_start = ORIGIN(RAM) + LENGTH(RAM); */

/* You can use this symbol to customize the location of the .text section */
/* If omitted the .text section will be placed right after the .vector_table
section */
/* This is required only on microcontrollers that store some configuration right
after the vector table */
/* _stext = ORIGIN(FLASH) + 0x400; */

/* Example of putting non-initialized variables into custom RAM locations. */
/* This assumes you have defined a region RAM2 above, and in the Rust
sources added the attribute `#[link_section = ".ram2bss"]` to the data
you want to place there. */
/* Note that the section will not be zero-initialized by the runtime! */
/* SECTIONS {
.ram2bss (NOLOAD) : ALIGN(4) {
*(.ram2bss);
. = ALIGN(4);
} > RAM2
} INSERT AFTER .bss;
*/
64 changes: 64 additions & 0 deletions examples/hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![no_std]
#![no_main]

// Simple UART example

#[cfg(feature = "52840")]
use nrf52840_hal as hal;
#[cfg(feature = "9160")]
use nrf9160_hal as hal;

use core::fmt::Write;
use hal::{gpio, uarte, uarte::Uarte};

#[cortex_m_rt::entry]
fn main() -> ! {
let p = hal::pac::Peripherals::take().unwrap();

#[cfg(feature = "52840")]
let (uart0, cdc_pins) = {
let p0 = gpio::p0::Parts::new(p.P0);
(
p.UARTE0,
uarte::Pins {
txd: p0.p0_06.into_push_pull_output(gpio::Level::High).degrade(),
rxd: p0.p0_08.into_floating_input().degrade(),
cts: Some(p0.p0_07.into_floating_input().degrade()),
rts: Some(p0.p0_05.into_push_pull_output(gpio::Level::High).degrade()),
},
)
};
#[cfg(feature = "9160")]
let (uart0, cdc_pins) = {
let p0 = gpio::p0::Parts::new(p.P0_NS);
(
p.UARTE0_NS,
uarte::Pins {
txd: p0.p0_29.into_push_pull_output(gpio::Level::High).degrade(),
rxd: p0.p0_28.into_floating_input().degrade(),
cts: Some(p0.p0_26.into_floating_input().degrade()),
rts: Some(p0.p0_27.into_push_pull_output(gpio::Level::High).degrade()),
},
)
};

let mut uarte = Uarte::new(
uart0,
cdc_pins,
uarte::Parity::EXCLUDED,
uarte::Baudrate::BAUD115200,
);

write!(uarte, "Hello, World!\r\n").unwrap();

loop {
cortex_m::asm::wfi();
}
}

#[panic_handler] // panicking behavior
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {
cortex_m::asm::bkpt();
}
}
1 change: 1 addition & 0 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub static EXAMPLES: &[(&str, &[&str])] = &[
"ecb-demo",
&["51", "52810", "52811", "52832", "52833", "52840"],
),
("hello-world", &["52840"]),
("gpiote-demo", &[]),
("i2s-controller-demo", &[]),
("i2s-peripheral-demo", &[]),
Expand Down

0 comments on commit fc11ace

Please sign in to comment.