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

Adds PPI support and example #162

Merged
merged 3 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"examples/twi-ssd1306",
"examples/ecb-demo",
"examples/ccm-demo",
"examples/ppi-demo",
]

[profile.dev]
Expand Down
28 changes: 28 additions & 0 deletions examples/ppi-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "ppi-demo"
version = "0.0.1"
edition = "2018"
authors = [ "Thales Fragoso <[email protected]>"]

[dependencies]
cortex-m = "0.6.2"
cortex-m-rt = "0.6.12"
rtt-target = {version = "0.2.0", features = ["cortex-m"] }

nrf52810-hal = { path = "../../nrf52810-hal", features = ["rt"], optional = true }
nrf52832-hal = { path = "../../nrf52832-hal", features = ["rt"], optional = true }
nrf52840-hal = { path = "../../nrf52840-hal", features = ["rt"], optional = true }
nrf52833-hal = { path = "../../nrf52833-hal", features = ["rt"], optional = true }
nrf51-hal = { path = "../../nrf51-hal", features = ["rt"], optional = true}

[[bin]]
name = "ppi-demo"
doc = false
test = false

[features]
51 = ["nrf51-hal"]
52810 = ["nrf52810-hal"]
52832 = ["nrf52832-hal"]
52840 = ["nrf52840-hal"]
52833 = ["nrf52833-hal"]
46 changes: 46 additions & 0 deletions examples/ppi-demo/Embed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[probe]
# The index of the probe in the connected probe list.
# probe_index = 0
# 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

[flashing]
# Whether or not the target should be flashed.
enabled = true
# Whether or not the target should be halted after flashing.
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"

[general]
# The chip name of the chip to be debugged.
# chip = "nRF52832"
# A list of chip descriptions to be loaded during runtime.
chip_descriptions = []
# The default log level to be used.
log_level = "Warn"

[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" }
]
# 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

[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
19 changes: 19 additions & 0 deletions examples/ppi-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Programmable peripheral interconnect demo

Choose the microcontroller with one of the following features:
- 51
- 52810
- 52832
- 52840

Also, if using `cargo-embed`, change the `chip` and `protocol` fields in [Embed.toml](Embed.toml).

This demo uses the [rtt-target](https://crates.io/crates/rtt-target) crate for communication.

If using `cargo-embed`, just run

```console
$ cargo embed --release --features=52832 --target=thumbv7em-none-eabihf
```

Replace `52832` and `thumbv7em-none-eabihf` with the correct feature and target for your microcontroller.
106 changes: 106 additions & 0 deletions examples/ppi-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#![no_std]
#![no_main]

#[cfg(not(any(
feature = "51",
feature = "52810",
feature = "52832",
feature = "52833",
feature = "52840"
)))]
compile_error!(
"This example requires one of the following device features enabled:
51
52810
52832
52833
52840"
);

// Import the right HAL/PAC crate, depending on the target chip
#[cfg(feature = "51")]
pub use nrf51_hal as hal;
#[cfg(feature = "52810")]
pub use nrf52810_hal as hal;
#[cfg(feature = "52832")]
pub use nrf52832_hal as hal;
#[cfg(feature = "52833")]
pub use nrf52833_hal as hal;
#[cfg(feature = "52840")]
pub use nrf52840_hal as hal;

use {
core::{
cell::RefCell,
panic::PanicInfo,
sync::atomic::{compiler_fence, Ordering},
},
cortex_m::interrupt::Mutex,
cortex_m_rt::entry,
hal::{
pac::{interrupt, Interrupt, RADIO},
ppi,
prelude::*,
timer::Timer,
Clocks,
},
rtt_target::{rprintln, rtt_init_print},
};

static RADIO_REGS: Mutex<RefCell<Option<RADIO>>> = Mutex::new(RefCell::new(None));

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

let _clocks = Clocks::new(p.CLOCK).enable_ext_hfosc();
rtt_init_print!();

let ppi_channels = ppi::Parts::new(p.PPI);
let mut channel0 = ppi_channels.ppi0;

channel0.set_task_endpoint(&p.RADIO.tasks_disable as *const _ as u32);
channel0.set_event_endpoint(&p.TIMER0.events_compare[0] as *const _ as u32);
channel0.enable();

let radio = p.RADIO;
radio.intenset.write(|w| w.disabled().set());
cortex_m::interrupt::free(|cs| RADIO_REGS.borrow(cs).replace(Some(radio)));
// NOTE(unsafe) There isn't any abstraction depending on this interrupt being masked
unsafe {
cortex_m::peripheral::NVIC::unmask(Interrupt::RADIO);
}

let mut timer = Timer::one_shot(p.TIMER0);
timer.start(0xFFFFu32);

loop {
// Prevent empty loop optimizations
compiler_fence(Ordering::SeqCst);
}
}

#[interrupt]
fn RADIO() {
cortex_m::interrupt::free(|cs| {
if let Some(regs) = RADIO_REGS.borrow(cs).borrow_mut().as_mut() {
if regs.events_disabled.read().bits() == 1 {
rprintln!("We hit the RADIO disabled interrupt");

// Clear the disabled flag
// NOTE(unsafe) 0 is a valid value to write to this register
regs.events_disabled.write(|w| unsafe { w.bits(0) });
}
}
});
}

#[inline(never)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
cortex_m::interrupt::disable();
rprintln!("{}", info);
loop {
compiler_fence(Ordering::SeqCst);
}
}
4 changes: 4 additions & 0 deletions nrf-hal-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub mod delay;
pub mod ecb;
pub mod gpio;
#[cfg(not(feature = "9160"))]
pub mod ppi;
#[cfg(not(feature = "9160"))]
pub mod rng;
pub mod rtc;
#[cfg(not(feature = "51"))]
Expand Down Expand Up @@ -58,6 +60,8 @@ pub mod prelude {
pub use crate::hal::digital::v2::*;
pub use crate::hal::prelude::*;

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

Expand Down
Loading