summaryrefslogtreecommitdiff
path: root/examples/nrf/src/bin/lora_p2p_report.rs
blob: d512b83f6e9f53e6903c844b9eaafd99ac527652 (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
74
75
76
77
78
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
//! Other nrf/sx126x combinations may work with appropriate pin modifications.
//! It demonstates LORA P2P functionality in conjunction with example lora_p2p_sense.rs.
#![no_std]
#![no_main]
#![macro_use]
#![allow(dead_code)]
#![feature(type_alias_impl_trait)]

use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::sx126x::*;
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
use embassy_nrf::{interrupt, spim};
use embassy_time::{Duration, Timer};
use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor};
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_nrf::init(Default::default());
    let mut spi_config = spim::Config::default();
    spi_config.frequency = spim::Frequency::M16;

    let mut radio = {
        let irq = interrupt::take!(SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1);
        let spim = spim::Spim::new(p.TWISPI1, irq, p.P1_11, p.P1_13, p.P1_12, spi_config);

        let cs = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
        let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
        let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
        let busy = Input::new(p.P1_14.degrade(), Pull::Down);
        let antenna_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
        let antenna_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);

        match Sx126xRadio::new(spim, cs, reset, antenna_rx, antenna_tx, dio1, busy, false).await {
            Ok(r) => r,
            Err(err) => {
                info!("Sx126xRadio error = {}", err);
                return;
            }
        }
    };

    let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
    let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);

    start_indicator.set_high();
    Timer::after(Duration::from_secs(5)).await;
    start_indicator.set_low();

    loop {
        let rf_config = RfConfig {
            frequency: 903900000, // channel in Hz
            bandwidth: Bandwidth::_250KHz,
            spreading_factor: SpreadingFactor::_10,
            coding_rate: CodingRate::_4_8,
        };

        let mut buffer = [00u8; 100];

        // P2P receive
        match radio.rx(rf_config, &mut buffer).await {
            Ok((buffer_len, rx_quality)) => info!(
                "RX received = {:?} with length = {} rssi = {} snr = {}",
                &buffer[0..buffer_len],
                buffer_len,
                rx_quality.rssi(),
                rx_quality.snr()
            ),
            Err(err) => info!("RX error = {}", err),
        }

        debug_indicator.set_high();
        Timer::after(Duration::from_secs(2)).await;
        debug_indicator.set_low();
    }
}