Skip to content

Commit

Permalink
[examples] Added Sx1276 LoRa example for Nucleo64
Browse files Browse the repository at this point in the history
  • Loading branch information
nesos authored and salkinium committed Jun 13, 2020
1 parent 5c381ab commit 7d1f7cc
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
90 changes: 90 additions & 0 deletions examples/nucleo_f411re/sx1276_rx/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2020 Benjamin Carrick
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/radio/sx1276.hpp>

using namespace Board;
using namespace modm;

int
main()
{
Board::initialize();

//RxTx selection on the mbed shield
using RxTx = Board::A4;

using SpiMosi = Board::D11;
using SpiMiso = Board::D12;
using SpiSck = Board::D13;
using SpiCs = Board::D10;

//Set the RF switch to Receive
RxTx::setOutput();
RxTx::reset();

//Initialize the SPI
SpiCs::setOutput();
SpiCs::set();

SpiMaster1::connect<SpiSck::Sck, SpiMosi::Mosi, SpiMiso::Miso>();
SpiMaster1::initialize<Board::SystemClock, 1500_kHz>();

MODM_LOG_DEBUG << "Starting the modem" << modm::endl;

// Create an instance of the driver
Sx1276<SpiMaster1,SpiCs> loraModem;

// Initialize the modem
RF_CALL_BLOCKING(loraModem.initialize());

// Set Modulation Parameters
RF_CALL_BLOCKING(loraModem.setModemParams(sx1276::Bandwidth::BW_7,
sx1276::SpreadingFactor::SF_8,
sx1276::CodingRate::CR_4_8,
false,
false));
// Set Carrier Frequency
RF_CALL_BLOCKING(loraModem.setCarrierFrequency(433.920_MHz));

// Enable the continous listening mode
RF_CALL_BLOCKING(loraModem.enableListening());

//the variable to hold the counter transmitted by the tx example
uint32_t rxCounter(0);

while (true)
{
//check the modem if new data is available and read it
uint8_t bytesReceived = RF_CALL_BLOCKING(loraModem.readPacket(reinterpret_cast<uint8_t*>(&rxCounter),4));
if(bytesReceived > 0)
{
int8_t snr = RF_CALL_BLOCKING(loraModem.getPacketSnr());

//dirty fixed point numbers
uint8_t snr_dec = (snr & 0x03) * 25;
snr = snr >> 2;

int16_t rssi = RF_CALL_BLOCKING(loraModem.getPacketRssi());

MODM_LOG_DEBUG << "Received Message" << modm::endl;
MODM_LOG_DEBUG << "Counter Value: " << rxCounter << modm::endl;
MODM_LOG_DEBUG << "SNR: "<< snr <<"."<< snr_dec << " dB" << modm::endl;
MODM_LOG_DEBUG << "RSSI: "<< rssi << " dBm" << modm::endl;
MODM_LOG_DEBUG << modm::endl;
}
//check for new data every 100ms
modm::delay(100ms);
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_f411re/sx1276_rx/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-f411re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f411re/sx1276_rx</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:spi:1</module>
<module>modm:driver:sx1276</module>
</modules>
</library>
69 changes: 69 additions & 0 deletions examples/nucleo_f411re/sx1276_tx/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020 Benjamin Carrick
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/radio/sx1276.hpp>

using namespace Board;
using namespace modm;

int
main()
{
Board::initialize();

//RxTx selection on the mbed shield
using RxTx = Board::A4;

using SpiMosi = Board::D11;
using SpiMiso = Board::D12;
using SpiSck = Board::D13;
using SpiCs = Board::D10;

//Set the RF switch to Transmit
RxTx::setOutput();
RxTx::set();

//Initialize the SPI
SpiCs::setOutput();
SpiCs::set();

SpiMaster1::connect<SpiSck::Sck, SpiMosi::Mosi, SpiMiso::Miso>();
SpiMaster1::initialize<Board::SystemClock, 1500_kHz>();

// Create an instance of the driver
Sx1276<SpiMaster1,SpiCs> loraModem;

// Initialize the modem
RF_CALL_BLOCKING(loraModem.initialize());

// Set Modulation Parameters
RF_CALL_BLOCKING(loraModem.setModemParams(sx1276::Bandwidth::BW_7,
sx1276::SpreadingFactor::SF_8,
sx1276::CodingRate::CR_4_8,
false,
false));
// Set Carrier Frequency
RF_CALL_BLOCKING(loraModem.setCarrierFrequency(433.920_MHz));

//The message to be sent
uint32_t counter(0);

while (true)
{
MODM_LOG_INFO << "Trasmitting Message " << counter << modm::endl;
RF_CALL_BLOCKING(loraModem.transmit(reinterpret_cast<uint8_t*>(&counter),4));
counter++;
modm::delay(1000ms);
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_f411re/sx1276_tx/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-f411re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f411re/sx1276_tx</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:spi:1</module>
<module>modm:driver:sx1276</module>
</modules>
</library>

0 comments on commit 7d1f7cc

Please sign in to comment.