diff options
Diffstat (limited to 'examples/nrf/src/bin/spim.rs')
| -rw-r--r-- | examples/nrf/src/bin/spim.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/spim.rs b/examples/nrf/src/bin/spim.rs new file mode 100644 index 000000000..c42cc6015 --- /dev/null +++ b/examples/nrf/src/bin/spim.rs | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(min_type_alias_impl_trait)] | ||
| 4 | #![feature(impl_trait_in_bindings)] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | #![allow(incomplete_features)] | ||
| 7 | |||
| 8 | #[path = "../example_common.rs"] | ||
| 9 | mod example_common; | ||
| 10 | |||
| 11 | use defmt::panic; | ||
| 12 | use embassy::executor::Spawner; | ||
| 13 | use embassy_nrf::gpio::{Level, Output, OutputDrive}; | ||
| 14 | use embassy_nrf::Peripherals; | ||
| 15 | use embassy_nrf::{interrupt, spim}; | ||
| 16 | use embassy_traits::spi::FullDuplex; | ||
| 17 | use embedded_hal::digital::v2::*; | ||
| 18 | use example_common::*; | ||
| 19 | |||
| 20 | #[embassy::main] | ||
| 21 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 22 | info!("running!"); | ||
| 23 | |||
| 24 | let mut config = spim::Config::default(); | ||
| 25 | config.frequency = spim::Frequency::M16; | ||
| 26 | |||
| 27 | let irq = interrupt::take!(SPIM3); | ||
| 28 | let mut spim = spim::Spim::new(p.SPI3, irq, p.P0_29, p.P0_28, p.P0_30, config); | ||
| 29 | |||
| 30 | let mut ncs = Output::new(p.P0_31, Level::High, OutputDrive::Standard); | ||
| 31 | |||
| 32 | // Example on how to talk to an ENC28J60 chip | ||
| 33 | |||
| 34 | // softreset | ||
| 35 | cortex_m::asm::delay(10); | ||
| 36 | ncs.set_low().unwrap(); | ||
| 37 | cortex_m::asm::delay(5); | ||
| 38 | let tx = [0xFF]; | ||
| 39 | unwrap!(spim.read_write(&mut [], &tx).await); | ||
| 40 | cortex_m::asm::delay(10); | ||
| 41 | ncs.set_high().unwrap(); | ||
| 42 | |||
| 43 | cortex_m::asm::delay(100000); | ||
| 44 | |||
| 45 | let mut rx = [0; 2]; | ||
| 46 | |||
| 47 | // read ESTAT | ||
| 48 | cortex_m::asm::delay(5000); | ||
| 49 | ncs.set_low().unwrap(); | ||
| 50 | cortex_m::asm::delay(5000); | ||
| 51 | let tx = [0b000_11101, 0]; | ||
| 52 | unwrap!(spim.read_write(&mut rx, &tx).await); | ||
| 53 | cortex_m::asm::delay(5000); | ||
| 54 | ncs.set_high().unwrap(); | ||
| 55 | info!("estat: {=[?]}", rx); | ||
| 56 | |||
| 57 | // Switch to bank 3 | ||
| 58 | cortex_m::asm::delay(10); | ||
| 59 | ncs.set_low().unwrap(); | ||
| 60 | cortex_m::asm::delay(5); | ||
| 61 | let tx = [0b100_11111, 0b11]; | ||
| 62 | unwrap!(spim.read_write(&mut rx, &tx).await); | ||
| 63 | cortex_m::asm::delay(10); | ||
| 64 | ncs.set_high().unwrap(); | ||
| 65 | |||
| 66 | // read EREVID | ||
| 67 | cortex_m::asm::delay(10); | ||
| 68 | ncs.set_low().unwrap(); | ||
| 69 | cortex_m::asm::delay(5); | ||
| 70 | let tx = [0b000_10010, 0]; | ||
| 71 | unwrap!(spim.read_write(&mut rx, &tx).await); | ||
| 72 | cortex_m::asm::delay(10); | ||
| 73 | ncs.set_high().unwrap(); | ||
| 74 | |||
| 75 | info!("erevid: {=[?]}", rx); | ||
| 76 | } | ||
