Skip to content

Commit 75bf56f

Browse files
thalesfragosojamesmunns
authored andcommitted
Adds PPI support and example
1 parent bc4f549 commit 75bf56f

File tree

8 files changed

+398
-0
lines changed

8 files changed

+398
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"examples/twi-ssd1306",
1616
"examples/ecb-demo",
1717
"examples/ccm-demo",
18+
"examples/ppi-demo",
1819
]
1920

2021
[profile.dev]

examples/ppi-demo/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "ppi-demo"
3+
version = "0.0.1"
4+
edition = "2018"
5+
authors = [ "Thales Fragoso <[email protected]>"]
6+
7+
[dependencies]
8+
cortex-m = "0.6.2"
9+
cortex-m-rt = "0.6.12"
10+
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
11+
12+
nrf52810-hal = { path = "../../nrf52810-hal", features = ["rt"], optional = true }
13+
nrf52832-hal = { path = "../../nrf52832-hal", features = ["rt"], optional = true }
14+
nrf52840-hal = { path = "../../nrf52840-hal", features = ["rt"], optional = true }
15+
nrf52833-hal = { path = "../../nrf52833-hal", features = ["rt"], optional = true }
16+
nrf51-hal = { path = "../../nrf51-hal", features = ["rt"], optional = true}
17+
18+
[[bin]]
19+
name = "ppi-demo"
20+
doc = false
21+
test = false
22+
23+
[features]
24+
51 = ["nrf51-hal"]
25+
52810 = ["nrf52810-hal"]
26+
52832 = ["nrf52832-hal"]
27+
52840 = ["nrf52840-hal"]
28+
52833 = ["nrf52833-hal"]

examples/ppi-demo/Embed.toml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[probe]
2+
# The index of the probe in the connected probe list.
3+
# probe_index = 0
4+
# The protocol to be used for communicating with the target.
5+
protocol = "Swd"
6+
# The speed in kHz of the data link to the target.
7+
# speed = 1337
8+
9+
[flashing]
10+
# Whether or not the target should be flashed.
11+
enabled = true
12+
# Whether or not the target should be halted after flashing.
13+
halt_afterwards = false
14+
# Whether or not bytes erased but not rewritten with data from the ELF
15+
# should be restored with their contents before erasing.
16+
restore_unwritten_bytes = false
17+
# The path where an SVG of the assembled flash layout should be written to.
18+
# flash_layout_output_path = "out.svg"
19+
20+
[general]
21+
# The chip name of the chip to be debugged.
22+
# chip = "nRF52832"
23+
# A list of chip descriptions to be loaded during runtime.
24+
chip_descriptions = []
25+
# The default log level to be used.
26+
log_level = "Warn"
27+
28+
[rtt]
29+
# Whether or not an RTTUI should be opened after flashing.
30+
# This is exclusive and cannot be used with GDB at the moment.
31+
enabled = true
32+
# A list of channel associations to be displayed. If left empty, all channels are displayed.
33+
channels = [
34+
# { up = 0, down = 0, name = "name" }
35+
]
36+
# The duration in ms for which the logger should retry to attach to RTT.
37+
timeout = 3000
38+
# Whether timestamps in the RTTUI are enabled
39+
show_timestamps = true
40+
41+
[gdb]
42+
# Whether or not a GDB server should be opened after flashing.
43+
# This is exclusive and cannot be used with RTT at the moment.
44+
enabled = false
45+
# The connection string in host:port format wher the GDB server will open a socket.
46+
# gdb_connection_string

examples/ppi-demo/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Programmable peripheral interconnect demo
2+
3+
Choose the microcontroller with one of the following features:
4+
- 51
5+
- 52810
6+
- 52832
7+
- 52840
8+
9+
Also, if using `cargo-embed`, change the `chip` and `protocol` fields in [Embed.toml](Embed.toml).
10+
11+
This demo uses the [rtt-target](https://crates.io/crates/rtt-target) crate for communication.
12+
13+
If using `cargo-embed`, just run
14+
15+
```console
16+
$ cargo embed --release --features=52832 --target=thumbv7em-none-eabihf
17+
```
18+
19+
Replace `52832` and `thumbv7em-none-eabihf` with the correct feature and target for your microcontroller.

examples/ppi-demo/src/main.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
#[cfg(not(any(
5+
feature = "51",
6+
feature = "52810",
7+
feature = "52832",
8+
feature = "52833",
9+
feature = "52840"
10+
)))]
11+
compile_error!(
12+
"This example requires one of the following device features enabled:
13+
51
14+
52810
15+
52832
16+
52833
17+
52840"
18+
);
19+
20+
// Import the right HAL/PAC crate, depending on the target chip
21+
#[cfg(feature = "51")]
22+
pub use nrf51_hal as hal;
23+
#[cfg(feature = "52810")]
24+
pub use nrf52810_hal as hal;
25+
#[cfg(feature = "52832")]
26+
pub use nrf52832_hal as hal;
27+
#[cfg(feature = "52833")]
28+
pub use nrf52833_hal as hal;
29+
#[cfg(feature = "52840")]
30+
pub use nrf52840_hal as hal;
31+
32+
use {
33+
core::{
34+
cell::RefCell,
35+
panic::PanicInfo,
36+
sync::atomic::{compiler_fence, Ordering},
37+
},
38+
cortex_m::interrupt::Mutex,
39+
cortex_m_rt::entry,
40+
hal::{
41+
pac::{interrupt, Interrupt, RADIO},
42+
ppi,
43+
prelude::*,
44+
timer::Timer,
45+
Clocks,
46+
},
47+
rtt_target::{rprintln, rtt_init_print},
48+
};
49+
50+
static RADIO_REGS: Mutex<RefCell<Option<RADIO>>> = Mutex::new(RefCell::new(None));
51+
52+
#[entry]
53+
fn main() -> ! {
54+
let p = hal::pac::Peripherals::take().unwrap();
55+
56+
let _clocks = Clocks::new(p.CLOCK).enable_ext_hfosc();
57+
rtt_init_print!();
58+
59+
let ppi_channels = ppi::Parts::new(p.PPI);
60+
let mut channel0 = ppi_channels.ppi0;
61+
62+
channel0.set_task_endpoint(&p.RADIO.tasks_disable as *const _ as u32);
63+
channel0.set_event_endpoint(&p.TIMER0.events_compare[0] as *const _ as u32);
64+
channel0.enable();
65+
66+
let radio = p.RADIO;
67+
radio.intenset.write(|w| w.disabled().set());
68+
cortex_m::interrupt::free(|cs| RADIO_REGS.borrow(cs).replace(Some(radio)));
69+
// NOTE(unsafe) There isn't any abstraction depending on this interrupt being masked
70+
unsafe {
71+
cortex_m::peripheral::NVIC::unmask(Interrupt::RADIO);
72+
}
73+
74+
let mut timer = Timer::one_shot(p.TIMER0);
75+
timer.start(0xFFFFu32);
76+
77+
loop {
78+
// Prevent empty loop optimizations
79+
compiler_fence(Ordering::SeqCst);
80+
}
81+
}
82+
83+
#[interrupt]
84+
fn RADIO() {
85+
cortex_m::interrupt::free(|cs| {
86+
if let Some(regs) = RADIO_REGS.borrow(cs).borrow_mut().as_mut() {
87+
if regs.events_disabled.read().bits() == 1 {
88+
rprintln!("We hit the RADIO disabled interrupt");
89+
90+
// Clear the disabled flag
91+
// NOTE(unsafe) 0 is a valid value to write to this register
92+
regs.events_disabled.write(|w| unsafe { w.bits(0) });
93+
}
94+
}
95+
});
96+
}
97+
98+
#[inline(never)]
99+
#[panic_handler]
100+
fn panic(info: &PanicInfo) -> ! {
101+
cortex_m::interrupt::disable();
102+
rprintln!("{}", info);
103+
loop {
104+
compiler_fence(Ordering::SeqCst);
105+
}
106+
}

nrf-hal-common/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub mod delay;
3131
pub mod ecb;
3232
pub mod gpio;
3333
#[cfg(not(feature = "9160"))]
34+
pub mod ppi;
35+
#[cfg(not(feature = "9160"))]
3436
pub mod rng;
3537
pub mod rtc;
3638
#[cfg(not(feature = "51"))]
@@ -58,6 +60,8 @@ pub mod prelude {
5860
pub use crate::hal::digital::v2::*;
5961
pub use crate::hal::prelude::*;
6062

63+
#[cfg(not(feature = "9160"))]
64+
pub use crate::ppi::{ConfigurablePpi, Ppi};
6165
pub use crate::time::U32Ext;
6266
}
6367

0 commit comments

Comments
 (0)