Skip to content

Commit b254e42

Browse files
kalkyljamesmunns
authored andcommitted
Add SPIS module
1 parent cbe0eef commit b254e42

File tree

5 files changed

+770
-0
lines changed

5 files changed

+770
-0
lines changed

examples/spis-demo/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "spis-demo"
3+
version = "0.1.0"
4+
authors = ["Henrik Alsér"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
cortex-m = "0.6.2"
11+
cortex-m-rtic = "0.5.3"
12+
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
13+
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }
14+
15+
[dependencies.embedded-hal]
16+
version = "0.2.3"
17+
features = ["unproven"]

examples/spis-demo/Embed.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[default.probe]
2+
protocol = "Swd"
3+
4+
[default.flashing]
5+
enabled = true
6+
halt_afterwards = false
7+
restore_unwritten_bytes = false
8+
9+
[default.general]
10+
chip = "nRF52840"
11+
chip_descriptions = []
12+
log_level = "Warn"
13+
14+
[default.rtt]
15+
enabled = true
16+
channels = []
17+
timeout = 3000
18+
show_timestamps = true
19+
20+
[default.gdb]
21+
enabled = false

examples/spis-demo/src/main.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use {
5+
core::{
6+
panic::PanicInfo,
7+
sync::atomic::{compiler_fence, Ordering},
8+
},
9+
hal::{gpiote::Gpiote, pac::SPIS0, spis::*},
10+
nrf52840_hal as hal,
11+
rtt_target::{rprintln, rtt_init_print},
12+
};
13+
14+
#[rtic::app(device = crate::hal::pac, peripherals = true)]
15+
const APP: () = {
16+
struct Resources {
17+
gpiote: Gpiote,
18+
transfer: Option<Transfer<SPIS0, &'static mut [u8; 8]>>,
19+
}
20+
21+
#[init]
22+
fn init(ctx: init::Context) -> init::LateResources {
23+
static mut BUF: [u8; 8] = [0; 8];
24+
25+
let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc();
26+
rtt_init_print!();
27+
rprintln!("Send me [u8; 8] over SPI");
28+
rprintln!("Press button to reset buffer");
29+
30+
let p0 = hal::gpio::p0::Parts::new(ctx.device.P0);
31+
let cs_pin = p0.p0_25.into_floating_input().degrade();
32+
let sck_pin = p0.p0_24.into_floating_input().degrade();
33+
let copi_pin = p0.p0_16.into_floating_input().degrade();
34+
let cipo_pin = p0.p0_14.into_floating_input().degrade();
35+
36+
let spis = Spis::new(
37+
ctx.device.SPIS0,
38+
&sck_pin,
39+
&cs_pin,
40+
Some(&copi_pin),
41+
Some(&cipo_pin),
42+
);
43+
spis.enable_interrupt(SpisEvent::End);
44+
45+
let btn = p0.p0_29.into_pullup_input().degrade();
46+
let gpiote = Gpiote::new(ctx.device.GPIOTE);
47+
gpiote.port().input_pin(&btn).low();
48+
gpiote.port().enable_interrupt();
49+
50+
init::LateResources {
51+
gpiote,
52+
transfer: spis.transfer(BUF).ok(),
53+
}
54+
}
55+
56+
#[task(binds = GPIOTE, resources = [gpiote, transfer])]
57+
fn on_gpiote(ctx: on_gpiote::Context) {
58+
ctx.resources.gpiote.reset_events();
59+
rprintln!("Reset buffer");
60+
let (buf, spis) = ctx.resources.transfer.take().unwrap().wait();
61+
spis.acquire(); // Acquire the SPIS semaphore to be able to safely update `buf`
62+
buf.copy_from_slice(&[0; 8][..]);
63+
rprintln!("{:?}", buf);
64+
*ctx.resources.transfer = spis.transfer(buf).ok();
65+
}
66+
67+
#[task(binds = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0, resources = [transfer])]
68+
fn on_spis(ctx: on_spis::Context) {
69+
let (buf, spis) = ctx.resources.transfer.take().unwrap().wait();
70+
spis.reset_event(SpisEvent::End);
71+
rprintln!("Received: {:?}", buf);
72+
*ctx.resources.transfer = spis.transfer(buf).ok();
73+
}
74+
};
75+
76+
#[inline(never)]
77+
#[panic_handler]
78+
fn panic(info: &PanicInfo) -> ! {
79+
cortex_m::interrupt::disable();
80+
rprintln!("{}", info);
81+
loop {
82+
compiler_fence(Ordering::SeqCst);
83+
}
84+
}

nrf-hal-common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub mod saadc;
5757
pub mod spi;
5858
#[cfg(not(feature = "51"))]
5959
pub mod spim;
60+
#[cfg(not(feature = "51"))]
61+
pub mod spis;
6062
#[cfg(not(feature = "9160"))]
6163
pub mod temp;
6264
pub mod time;

0 commit comments

Comments
 (0)