Skip to content

Interrupted uart example missing from #102 #106

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

Closed
wants to merge 8 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"nrf52832-hal",
"nrf52840-hal",
"examples/rtfm-demo",
"examples/rtfm-uarte-interrupts",
"examples/spi-demo",
"examples/twi-ssd1306",
]
Expand Down
2 changes: 1 addition & 1 deletion examples/rtfm-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["James Munns <[email protected]>"]
edition = "2018"

[dependencies]
cortex-m-rtfm = "0.4.3"
cortex-m-rtfm = { git = "https://github.com/japaric/cortex-m-rtfm.git"}
panic-semihosting = "0.5.1"
cortex-m-semihosting = "0.3.3"

Expand Down
5 changes: 5 additions & 0 deletions examples/rtfm-uarte-interrupts/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.thumbv7em-none-eabihf]
runner = "arm-none-eabi-gdb -tui"

[build]
target = "thumbv7em-none-eabihf"
10 changes: 10 additions & 0 deletions examples/rtfm-uarte-interrupts/.gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set remotetimeout 60000
target remote :2331
set arm force-mode thumb

# Uncomment to enable semihosting, when necessary
monitor semihosting enable

layout split
monitor reset
load
33 changes: 33 additions & 0 deletions examples/rtfm-uarte-interrupts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "rtfm-uarte-interrupts"
version = "0.1.0"
authors = ["James Munns <[email protected]>"]
edition = "2018"

[dependencies]
cortex-m-rtfm = { git = "https://github.com/japaric/cortex-m-rtfm.git"}
panic-semihosting = "0.5.1"
cortex-m-semihosting = "0.3.3"
heapless = ">= 0.5.0"

[dependencies.nrf52810-hal]
version = "0.8.0"
path = "../../nrf52810-hal"
optional = true

[dependencies.nrf52832-hal]
version = "0.8.0"
path = "../../nrf52832-hal"
optional = true
features = ["rt", "xxAB-package", "POOL"]

[dependencies.nrf52840-hal]
version = "0.8.0"
path = "../../nrf52840-hal"
optional = true

[features]
52810 = ["nrf52810-hal"]
52832 = ["nrf52832-hal"]
52840 = ["nrf52840-hal"]
default = ["52832"]
113 changes: 113 additions & 0 deletions examples/rtfm-uarte-interrupts/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#![no_main]
#![no_std]

// panic handler
extern crate panic_semihosting;

use cortex_m_semihosting::hprintln;

use nrf52832_hal as hal;

use hal::gpio::{p0, Level};
use hal::target::{interrupt, UARTE0};
use hal::{uarte, Uarte};
use hal::{DMAPool, RXError, UarteRX, UarteTX, DMA_SIZE};

use heapless::{
consts::U3,
pool::singleton::Box,
pool::singleton::Pool,
spsc::{Producer, Queue},
};

use rtfm::app;

const NR_PACKAGES: usize = 10;
const DMA_MEM: usize = DMA_SIZE * NR_PACKAGES + 16;

type TXQSize = U3;

#[app(device = crate::hal::target)]
const APP: () = {
static mut RX: UarteRX<UARTE0> = ();
static mut TX: UarteTX<UARTE0, TXQSize> = ();
static mut PRODUCER: Producer<'static, Box<DMAPool>, TXQSize> = ();

#[init(spawn = [])]
fn init(c: init::Context) -> init::LateResources {
// for the actual DMA buffers
static mut MEMORY: [u8; DMA_MEM] = [0; DMA_MEM];
// for the producer/consumer of TX
static mut TX_RB: Option<Queue<Box<DMAPool>, TXQSize>> = None;

hprintln!("init").unwrap();
// move MEMORY to P (the DMA buffer allocator)
DMAPool::grow(MEMORY);

let port0 = p0::Parts::new(c.device.P0);

//adafruit nrf52 le
let uarte0 = Uarte::new(
c.device.UARTE0,
uarte::Pins {
txd: port0.p0_06.into_push_pull_output(Level::High).degrade(),
rxd: port0.p0_08.into_floating_input().degrade(),
cts: None,
rts: None,
},
uarte::Parity::EXCLUDED,
uarte::Baudrate::BAUD115200,
);

*TX_RB = Some(Queue::new());
let (txp, txc) = TX_RB.as_mut().unwrap().split();
let (rx, tx) = uarte0.split(Queue::new(), txc);

init::LateResources {
RX: rx,
TX: tx,
PRODUCER: txp,
}
}

// // we can get Box<P> us being now the owner
#[task(capacity = 2, resources = [PRODUCER])]
fn printer(c: printer::Context, data: Box<DMAPool>) {
// enqueue a test message
// let mut b = DMAPool::alloc().unwrap().freeze();
// b.copy_from_slice(&[0, 1, 2, 3]);

// hprintln!("{:?}", &data).unwrap();
// just do the buffer dance without copying
c.resources.PRODUCER.enqueue(data).unwrap();
rtfm::pend(interrupt::UARTE0_UART0);
}

#[task]
fn rx_error(_: rx_error::Context, err: RXError) {
hprintln!("rx_error {:?}", err).unwrap();
}

#[interrupt(priority = 2, resources = [RX, TX], spawn = [printer, rx_error])]
fn UARTE0_UART0(c: UARTE0_UART0::Context) {
// probe RX
match c.resources.RX.process_interrupt() {
Ok(Some(b)) => {
// delegate data to printer
match c.spawn.printer(b) {
Err(_) => c.spawn.rx_error(RXError::OOM).unwrap(),
_ => (),
};
}
Ok(None) => (), // no
Err(err) => c.spawn.rx_error(err).unwrap(),
}

c.resources.TX.process_interrupt();
}

extern "C" {
fn SWI1_EGU1();
fn SWI2_EGU2();
}
};
11 changes: 11 additions & 0 deletions nrf52-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ nb = "0.1.1"
fpa = "0.1.0"
rand_core = "0.4.0"

[dependencies.heapless]
version = "0.5.0"

[dependencies.void]
default-features = false
version = "1.0.2"
Expand Down Expand Up @@ -52,3 +55,11 @@ default = ["52832"]
52810 = ["nrf52810-pac"]
52832 = ["nrf52832-pac"]
52840 = ["nrf52840-pac"]
POOL = []
DMA_SIZE_4 = []
DMA_SIZE_8 = []
DMA_SIZE_16 = []
DMA_SIZE_32 = []
DMA_SIZE_64 = []
DMA_SIZE_128 = []
DMA_SIZE_256 = []
11 changes: 4 additions & 7 deletions nrf52-hal-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ pub mod target_constants {
/// Does this slice reside entirely within RAM?
pub(crate) fn slice_in_ram(slice: &[u8]) -> bool {
let ptr = slice.as_ptr() as usize;
ptr >= target_constants::SRAM_LOWER &&
(ptr + slice.len()) < target_constants::SRAM_UPPER
ptr >= target_constants::SRAM_LOWER
&& (ptr + slice.len()) < target_constants::SRAM_UPPER
}

/// A handy structure for converting rust slices into ptr and len pairs
Expand All @@ -67,10 +67,7 @@ pub(crate) struct DmaSlice {

impl DmaSlice {
pub fn null() -> Self {
Self {
ptr: 0,
len: 0,
}
Self { ptr: 0, len: 0 }
}

pub fn from_slice(slice: &[u8]) -> Self {
Expand All @@ -89,4 +86,4 @@ pub use crate::saadc::Saadc;
pub use crate::spim::Spim;
pub use crate::timer::Timer;
pub use crate::twim::Twim;
pub use crate::uarte::Uarte;
pub use crate::uarte::{DMAPool, RXError, Uarte, UarteRX, UarteTX, DMA_SIZE};
Loading