summaryrefslogtreecommitdiff
path: root/examples/nrf/src/bin/spim.rs
blob: 9afd17ef9e579fc258c4d26f5331517eb204b5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

#[path = "../example_common.rs"]
mod example_common;

use defmt::panic;
use embassy::executor::Spawner;
use embassy_nrf::gpio::{Level, Output, OutputDrive};
use embassy_nrf::Peripherals;
use embassy_nrf::{interrupt, spim};
use embassy_traits::spi::FullDuplex;
use embedded_hal::digital::v2::*;
use example_common::*;

#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
    info!("running!");

    let mut config = spim::Config::default();
    config.frequency = spim::Frequency::M16;

    let irq = interrupt::take!(SPIM3);
    let mut spim = spim::Spim::new(p.SPI3, irq, p.P0_29, p.P0_28, p.P0_30, config);

    let mut ncs = Output::new(p.P0_31, Level::High, OutputDrive::Standard);

    // Example on how to talk to an ENC28J60 chip

    // softreset
    cortex_m::asm::delay(10);
    unwrap!(ncs.set_low());
    cortex_m::asm::delay(5);
    let tx = [0xFF];
    unwrap!(spim.read_write(&mut [], &tx).await);
    cortex_m::asm::delay(10);
    unwrap!(ncs.set_high());

    cortex_m::asm::delay(100000);

    let mut rx = [0; 2];

    // read ESTAT
    cortex_m::asm::delay(5000);
    unwrap!(ncs.set_low());
    cortex_m::asm::delay(5000);
    let tx = [0b000_11101, 0];
    unwrap!(spim.read_write(&mut rx, &tx).await);
    cortex_m::asm::delay(5000);
    unwrap!(ncs.set_high());
    info!("estat: {=[?]}", rx);

    // Switch to bank 3
    cortex_m::asm::delay(10);
    unwrap!(ncs.set_low());
    cortex_m::asm::delay(5);
    let tx = [0b100_11111, 0b11];
    unwrap!(spim.read_write(&mut rx, &tx).await);
    cortex_m::asm::delay(10);
    unwrap!(ncs.set_high());

    // read EREVID
    cortex_m::asm::delay(10);
    unwrap!(ncs.set_low());
    cortex_m::asm::delay(5);
    let tx = [0b000_10010, 0];
    unwrap!(spim.read_write(&mut rx, &tx).await);
    cortex_m::asm::delay(10);
    unwrap!(ncs.set_high());

    info!("erevid: {=[?]}", rx);
}