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

Bbq uarte #186

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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"examples/ppi-demo",
"examples/gpiote-demo",
"examples/wdt-demo",
"examples/bbq-uarte-demo",
]

[profile.dev]
Expand All @@ -25,3 +26,6 @@ lto = false
debug = true
lto = true
opt-level = "s"

[patch.crates-io]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hack and should be reverted

nrf-hal-common = { path = "./nrf-hal-common" }
37 changes: 37 additions & 0 deletions examples/bbq-uarte-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "bbq-uarte-demo"
version = "0.0.1"
edition = "2018"
authors = [ "James Munns <[email protected]>"]

[dependencies]
cortex-m = "0.6.2"
cortex-m-rtic = "0.5.3"
cortex-m-rt = "0.6.12"
bbqueue = "0.4.6"
embedded-hal = "0.2.3"
rtt-target = {version = "0.2.0", features = ["cortex-m"] }

nrf52810-hal = { version = "0.11", features = ["rt"], optional = true }
nrf52832-hal = { version = "0.11", features = ["rt"], optional = true }
nrf52840-hal = { version = "0.11", features = ["rt"], optional = true }

nrf-hal-common = { version = "0.11", features = ["bbq-uarte"] }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: I need to plumb this feature to the hals

panic-reset = "0.1.0"

[dependencies.panic-persist]
version = "0.2.1"
default-features = false

# Disable documentation to avoid spurious rustdoc warnings
[[bin]]
name = "bbq-uarte-demo"
doc = false
test = false

[features]
52810 = ["nrf52810-hal"]
52832 = ["nrf52832-hal"]
52840 = ["nrf52840-hal"]
default = ["52832"]

45 changes: 45 additions & 0 deletions examples/bbq-uarte-demo/Embed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[default.probe]
# The index of the probe in the connected probe list.
# 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 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"

[default.general]
# The chip name of the chip to be debugged.
chip = "nRF52832_xxAA"
# A list of chip descriptions to be loaded during runtime.
chip_descriptions = []
# The default log level to be used.
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" }
]
# 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

[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
7 changes: 7 additions & 0 deletions examples/bbq-uarte-demo/debug.gdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
target remote :2331
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be deleted

set backtrace limit 32
load
monitor reset
break main
layout split
continue
129 changes: 129 additions & 0 deletions examples/bbq-uarte-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#![no_std]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Document this is for the DWM1001-DEV

#![no_main]

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

use {
bbqueue::{consts::*, BBBuffer, ConstBBBuffer},
core::sync::atomic::AtomicBool,
hal::{
gpio::Level,
pac::{TIMER1, TIMER2},
ppi::{Parts, Ppi0},
Timer,
},
rtt_target::{rprintln, rtt_init_print},
};

use hal::pac::UARTE0;

// Panic provider crate
use panic_reset as _;

#[rtic::app(device = crate::hal::pac, peripherals = true)]
const APP: () = {
struct Resources {
timer: Timer<TIMER1>,

uarte_timer: hal::bbq_uarte::irq::UarteTimer<TIMER2>,
uarte_irq: hal::bbq_uarte::irq::UarteIrq<U1024, U1024, Ppi0, UARTE0>,
uarte_app: hal::bbq_uarte::app::UarteApp<U1024, U1024>,
}

#[init]
fn init(ctx: init::Context) -> init::LateResources {
let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc();

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

let uart = ctx.device.UARTE0;

static UBUF: hal::bbq_uarte::buffer::UarteBuffer<U1024, U1024> =
hal::bbq_uarte::buffer::UarteBuffer {
txd_buf: BBBuffer(ConstBBBuffer::new()),
rxd_buf: BBBuffer(ConstBBBuffer::new()),
timeout_flag: AtomicBool::new(false),
};

rtt_init_print!();

let rxd = p0.p0_11.into_floating_input().degrade();
let txd = p0.p0_05.into_push_pull_output(Level::Low).degrade();

let ppi_channels = Parts::new(ctx.device.PPI);
let channel0 = ppi_channels.ppi0;

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

let ue = UBUF
.try_split(
uarte_pins,
hal::uarte::Parity::EXCLUDED,
hal::uarte::Baudrate::BAUD230400,
ctx.device.TIMER2,
channel0,
uart,
32,
1_000_000,
)
.unwrap();

init::LateResources {
timer: Timer::new(ctx.device.TIMER1),
uarte_timer: ue.timer,
uarte_irq: ue.irq,
uarte_app: ue.app,
}
}

#[idle(resources = [timer, uarte_app])]
fn idle(ctx: idle::Context) -> ! {
let timer = ctx.resources.timer;
let uarte_app = ctx.resources.uarte_app;

use embedded_hal::timer::CountDown;

rprintln!("Start!");

timer.start(5_000_000u32);

loop {
if let Ok(rgr) = uarte_app.read() {
let len = rgr.len();
rprintln!("Brr: {}", len);
if let Ok(mut wgr) = uarte_app.write_grant(len) {
wgr.copy_from_slice(&rgr);
wgr.commit(len);
}
rgr.release(len);
}
if timer.wait().is_ok() {
rprintln!("Hello from idle!");
timer.start(5_000_000u32);
}
}
}

#[task(binds = TIMER2, resources = [uarte_timer])]
fn timer2(ctx: timer2::Context) {
// rprintln!("Hello from timer2!");
ctx.resources.uarte_timer.interrupt();
}

#[task(binds = UARTE0_UART0, resources = [uarte_irq])]
fn uarte0(ctx: uarte0::Context) {
// rprintln!("Hello from uarte0!");
ctx.resources.uarte_irq.interrupt();
}
};
7 changes: 7 additions & 0 deletions nrf-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ version = "0.2.1"
features = ["unproven"]
version = "0.2.4"

[dependencies.bbqueue]
optional = true
version = "0.4.6"

[features]
doc = []
51 = ["nrf51"]
Expand All @@ -69,3 +73,6 @@ doc = []
52833 = ["nrf52833-pac"]
52840 = ["nrf52840-pac"]
9160 = ["nrf9160-pac"]

# Batteries Included options
bbq-uarte = ["bbqueue"]
125 changes: 125 additions & 0 deletions nrf-hal-common/src/bbq_uarte/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use crate::pac::{Interrupt, NVIC};
use bbqueue::{ArrayLength, Consumer, Error, GrantR, GrantW, Producer};
use core::ops::{Deref, DerefMut};

pub struct UarteApp<OutgoingLen, IncomingLen>
where
OutgoingLen: ArrayLength<u8>,
IncomingLen: ArrayLength<u8>,
{
pub(crate) outgoing_prod: Producer<'static, OutgoingLen>,
pub incoming_cons: Consumer<'static, IncomingLen>,
}

impl<OutgoingLen, IncomingLen> UarteApp<OutgoingLen, IncomingLen>
where
OutgoingLen: ArrayLength<u8>,
IncomingLen: ArrayLength<u8>,
{
pub fn read(&mut self) -> Result<UarteGrantR<'static, IncomingLen>, Error> {
self.incoming_cons
.read()
.map(|gr| UarteGrantR { grant_r: gr })
}

pub fn write_grant(
&mut self,
bytes: usize,
) -> Result<UarteGrantW<'static, OutgoingLen>, Error> {
self.outgoing_prod
.grant_exact(bytes)
.map(|gr| UarteGrantW { grant_w: gr })
}
}

/// A write grant for a single Uarte
///
/// NOTE: If the grant is dropped without explicitly commiting
/// the contents, then no Uarte will be comitted for writing.
#[derive(Debug, PartialEq)]
pub struct UarteGrantW<'a, N>
where
N: ArrayLength<u8>,
{
grant_w: GrantW<'a, N>,
}

/// A read grant for a single Uarte
///
/// NOTE: If the grant is dropped without explicitly releasing
/// the contents, then no Uarte will be released.
#[derive(Debug, PartialEq)]
pub struct UarteGrantR<'a, N>
where
N: ArrayLength<u8>,
{
grant_r: GrantR<'a, N>,
}

impl<'a, N> Deref for UarteGrantW<'a, N>
where
N: ArrayLength<u8>,
{
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.grant_w
}
}

impl<'a, N> DerefMut for UarteGrantW<'a, N>
where
N: ArrayLength<u8>,
{
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.grant_w
}
}

impl<'a, N> Deref for UarteGrantR<'a, N>
where
N: ArrayLength<u8>,
{
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.grant_r
}
}

impl<'a, N> DerefMut for UarteGrantR<'a, N>
where
N: ArrayLength<u8>,
{
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.grant_r
}
}

impl<'a, N> UarteGrantW<'a, N>
where
N: ArrayLength<u8>,
{
/// Commit a Uarte to make it available to the Consumer half.
///
/// `used` is the size of the payload, in bytes, not
/// including the Uarte header
pub fn commit(self, used: usize) {
// Commit the header + Uarte
self.grant_w.commit(used);
NVIC::pend(Interrupt::UARTE0_UART0);
}
}

impl<'a, N> UarteGrantR<'a, N>
where
N: ArrayLength<u8>,
{
/// Release a Uarte to make the space available for future writing
///
/// Note: The full Uarte is always released
pub fn release(self, used: usize) {
self.grant_r.release(used);
NVIC::pend(Interrupt::UARTE0_UART0);
}
}
Loading