-
Notifications
You must be signed in to change notification settings - Fork 140
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
base: master
Are you sure you want to change the base?
Bbq uarte #186
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||
|
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
target remote :2331 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#![no_std] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use bbqueue::{ArrayLength, Consumer, Producer, Error, GrantR, GrantW}; | ||
use core::ops::{Deref, DerefMut}; | ||
use crate::pac::{NVIC, Interrupt}; | ||
|
||
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); | ||
} | ||
} |
There was a problem hiding this comment.
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