Skip to content

Commit b2e5b97

Browse files
bors[bot]huntc
andauthored
Merge #317
317: Uart example r=thejpster a=huntc Provides the simplest example of using UART for both the 52840 and 9160. Tested successfully on: * nrf52840-dk * nrf9160-dk * Thingy:91 (having modified the GPIO pins locally) Co-authored-by: huntc <[email protected]>
2 parents a8dddf1 + 961a4d0 commit b2e5b97

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

examples/hello-world/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "hello-world"
3+
version = "0.1.0"
4+
authors = ["Christopher Hunt"]
5+
edition = "2018"
6+
publish = false
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
cortex-m = "0.6.2"
12+
cortex-m-rt = "0.6.12"
13+
14+
[dependencies.embedded-hal]
15+
version = "0.2.3"
16+
features = ["unproven"]
17+
18+
[dependencies.nrf9160-hal]
19+
features = ["rt"]
20+
path = "../../nrf9160-hal"
21+
optional = true
22+
23+
[dependencies.nrf52840-hal]
24+
features = ["rt"]
25+
path = "../../nrf52840-hal"
26+
optional = true
27+
28+
[features]
29+
9160 = ["nrf9160-hal"]
30+
52840 = ["nrf52840-hal"]

examples/hello-world/Embed.toml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[default.probe]
2+
# USB vendor ID
3+
# usb_vid = "1337"
4+
# USB product ID
5+
# usb_pid = "1337"
6+
# Serial number
7+
# serial = "12345678"
8+
# The protocol to be used for communicating with the target.
9+
protocol = "Swd"
10+
# The speed in kHz of the data link to the target.
11+
# speed = 1337
12+
13+
[default.flashing]
14+
# Whether or not the target should be flashed.
15+
enabled = true
16+
# Whether or not the target should be halted after reset.
17+
# DEPRECATED, moved to reset section
18+
halt_afterwards = false
19+
# Whether or not bytes erased but not rewritten with data from the ELF
20+
# should be restored with their contents before erasing.
21+
restore_unwritten_bytes = false
22+
# The path where an SVG of the assembled flash layout should be written to.
23+
# flash_layout_output_path = "out.svg"
24+
25+
[default.reset]
26+
# Whether or not the target should be reset.
27+
# When flashing is enabled as well, the target will be reset after flashing.
28+
enabled = true
29+
# Whether or not the target should be halted after reset.
30+
halt_afterwards = false
31+
32+
[default.general]
33+
# The chip name of the chip to be debugged.
34+
chip = "nRF52840_xxAA"
35+
# A list of chip descriptions to be loaded during runtime.
36+
chip_descriptions = []
37+
# The default log level to be used. Possible values are one of:
38+
# "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"
39+
log_level = "WARN"
40+
41+
[default.rtt]
42+
# Whether or not an RTTUI should be opened after flashing.
43+
# This is exclusive and cannot be used with GDB at the moment.
44+
enabled = false
45+
# A list of channel associations to be displayed. If left empty, all channels are displayed.
46+
channels = [
47+
# { up = 0, down = 0, name = "name", format = "String" }
48+
]
49+
# The duration in ms for which the logger should retry to attach to RTT.
50+
timeout = 3000
51+
# Whether timestamps in the RTTUI are enabled
52+
show_timestamps = true
53+
# Whether to save rtt history buffer on exit.
54+
log_enabled = false
55+
# Where to save rtt history buffer relative to manifest path.
56+
log_path = "./logs"
57+
58+
[default.gdb]
59+
# Whether or not a GDB server should be opened after flashing.
60+
# This is exclusive and cannot be used with RTT at the moment.
61+
enabled = false
62+
# The connection string in host:port format wher the GDB server will open a socket.
63+
# gdb_connection_string

examples/hello-world/src/main.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
// Simple UART example
5+
6+
#[cfg(feature = "52840")]
7+
use nrf52840_hal as hal;
8+
#[cfg(feature = "9160")]
9+
use nrf9160_hal as hal;
10+
11+
use core::fmt::Write;
12+
use hal::{gpio, uarte, uarte::Uarte};
13+
14+
#[cortex_m_rt::entry]
15+
fn main() -> ! {
16+
let p = hal::pac::Peripherals::take().unwrap();
17+
18+
#[cfg(feature = "52840")]
19+
let (uart0, cdc_pins) = {
20+
let p0 = gpio::p0::Parts::new(p.P0);
21+
(
22+
p.UARTE0,
23+
uarte::Pins {
24+
txd: p0.p0_06.into_push_pull_output(gpio::Level::High).degrade(),
25+
rxd: p0.p0_08.into_floating_input().degrade(),
26+
cts: Some(p0.p0_07.into_floating_input().degrade()),
27+
rts: Some(p0.p0_05.into_push_pull_output(gpio::Level::High).degrade()),
28+
},
29+
)
30+
};
31+
#[cfg(feature = "9160")]
32+
let (uart0, cdc_pins) = {
33+
let p0 = gpio::p0::Parts::new(p.P0_NS);
34+
(
35+
p.UARTE0_NS,
36+
uarte::Pins {
37+
txd: p0.p0_29.into_push_pull_output(gpio::Level::High).degrade(),
38+
rxd: p0.p0_28.into_floating_input().degrade(),
39+
cts: Some(p0.p0_26.into_floating_input().degrade()),
40+
rts: Some(p0.p0_27.into_push_pull_output(gpio::Level::High).degrade()),
41+
},
42+
)
43+
};
44+
45+
let mut uarte = Uarte::new(
46+
uart0,
47+
cdc_pins,
48+
uarte::Parity::EXCLUDED,
49+
uarte::Baudrate::BAUD115200,
50+
);
51+
52+
write!(uarte, "Hello, World!\r\n").unwrap();
53+
54+
loop {
55+
cortex_m::asm::wfi();
56+
}
57+
}
58+
59+
#[panic_handler] // panicking behavior
60+
fn panic(_: &core::panic::PanicInfo) -> ! {
61+
loop {
62+
cortex_m::asm::bkpt();
63+
}
64+
}

xtask/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub static EXAMPLES: &[(&str, &[&str])] = &[
1818
"ecb-demo",
1919
&["51", "52810", "52811", "52832", "52833", "52840"],
2020
),
21+
("hello-world", &["52840", "9160"]),
2122
("gpiote-demo", &[]),
2223
("i2s-controller-demo", &[]),
2324
("i2s-peripheral-demo", &[]),
@@ -44,6 +45,7 @@ pub fn feature_to_target(feat: &str) -> &str {
4445
match feat {
4546
"51" => "thumbv6m-none-eabi",
4647
"52810" | "52811" => "thumbv7em-none-eabi",
48+
"9160" => "thumbv8m.main-none-eabihf",
4749
_ if feat.starts_with("52") => "thumbv7em-none-eabihf",
4850
_ => panic!("unknown Cargo feature `{}`", feat),
4951
}

0 commit comments

Comments
 (0)