|
| 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 | +} |
0 commit comments